networkx/networkx — pull request #1164 by ysitu (2014-05-15 16:24:28+00:00)
Title: Add structure-only/shallow graph copy/to_directed/to_undirected/reverse

This addresses #1152.

To summarize the changes, first, I unified all implementations of `copy`/`to_directed`/`to_undirected` into `Graph` and those of `reverse` into `DiGraph`. So one implementation of each function will handle directed and undirected, simple and multigraphs.

Second, graphs can be copied in three ways as indicated by a `data` argument. It can be structure-only (`data=False`), shallow (`data='shallow'`) or deep (`data=True`). A structure-only copy shallow copies only nodes and edges (and keys if copying a multigraph). A shallow copy also includes graph, node and edge attributes but ignores any custom data if the user subclasses the built-in graph types. Both structure-only and shallow copies create new internal dictionaries while sharing the same keys and values with the original graph. A deep copy simply calls `deepcopy` to duplicate everything uniquely.

Third, `to_directed`/`to_undirected`/`reverse` also support the same copy semantics via `data` arguments but also ignore custom data during deep copy. Deep copy is now done via a single call to `deepcopy`. This ensure that a deep copy is referentially isomorphic to a shallow copy. In fact, this is achieved by first creating a shallow copy followed by calling `deepcopy` on it. Calling `to_directed`/`to_undirected` when the graph is already directed/undirected is the same as calling `copy`. An in-place reverse does not copy any data.

PEP8 fixes done with `autopep8` are also included in this PR.

The existing tests still pass. I do not have time to write new tests to cover every corner shortly.