networkx 2.8.2 Pypi GitHub Homepage
Other Docs
NotesParametersReturnsBackRef
copy(self, as_view=False)

The copy method by default returns an independent shallow copy of the graph and attributes. That is, if an attribute is a container, that container is shared by the original an the copy. Use Python's copy.deepcopy for new containers.

If :None:None:`as_view` is True then a view is returned instead of a copy.

Notes

All copies reproduce the graph structure, but data attributes may be handled in different ways. There are four types of copies of a graph that people might want.

Deepcopy -- A "deepcopy" copies the graph structure as well as all data attributes and any objects they might contain. The entire graph object is new so that changes in the copy do not affect the original object. (see Python's copy.deepcopy)

Data Reference (Shallow) -- For a shallow copy the graph structure is copied but the edge, node and graph attribute dicts are references to those in the original graph. This saves time and memory but could cause confusion if you change an attribute in one graph and it changes the attribute in the other. NetworkX does not provide this level of shallow copy.

Independent Shallow -- This copy creates new independent attribute dicts and then does a shallow copy of the attributes. That is, any attributes that are containers are shared between the new graph and the original. This is exactly what :None:None:`dict.copy()` provides. You can obtain this style copy using:

>>> G = nx.path_graph(5)
>>> H = G.copy()
>>> H = G.copy(as_view=False)
>>> H = nx.Graph(G)
>>> H = G.__class__(G)

Fresh Data -- For fresh data, the graph structure is copied while new empty data attribute dicts are created. The resulting graph is independent of the original and it has no edge, node or graph attributes. Fresh copies are not enabled. Instead use:

>>> H = G.__class__()
>>> H.add_nodes_from(G)
>>> H.add_edges_from(G.edges)

View -- Inspired by dict-views, graph-views act like read-only versions of the original graph, providing a copy of the original structure without requiring any memory for copying the information.

See the Python copy module for more information on shallow and deep copies, https://docs.python.org/3/library/copy.html.

Parameters

as_view : bool, optional (default=False)

If True, the returned graph-view provides a read-only view of the original graph without actually copying any data.

Returns

G : Graph

A copy of the graph.

Returns a copy of the graph.

See Also

to_directed

return a directed copy of the graph.

Examples

>>> G = nx.path_graph(4)  # or DiGraph, MultiGraph, MultiDiGraph, etc
... H = G.copy()
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

networkx.classes.graph.Graph.copy networkx.classes.graph.Graph.to_undirected networkx.classes.multigraph.MultiGraph.copy networkx.algorithms.shortest_paths.generic.average_shortest_path_length networkx.algorithms.components.connected.connected_components networkx.algorithms.cycles.simple_cycles

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


GitHub : /networkx/classes/graph.py#1459
type: <class 'function'>
Commit: