Returns a copy of the graph. The copy method by default returns an independent deep copy of the graph and attributes. If `as_view` is True then a view is returned instead of a copy. Notes ----- All copies reproduce the graph structure, but data attr
(self, as_view=False)
| 1596 | |
| 1597 | @clear_mutation_cache |
| 1598 | def copy(self, as_view=False): |
| 1599 | """Returns a copy of the graph. |
| 1600 | |
| 1601 | The copy method by default returns an independent deep copy |
| 1602 | of the graph and attributes. |
| 1603 | |
| 1604 | If `as_view` is True then a view is returned instead of a copy. |
| 1605 | |
| 1606 | Notes |
| 1607 | ----- |
| 1608 | All copies reproduce the graph structure, but data attributes |
| 1609 | may be handled in different ways. There are three types of copies |
| 1610 | of a graph that people might want. |
| 1611 | |
| 1612 | Deepcopy -- A "deepcopy" copies the graph structure as well as |
| 1613 | all data attributes and any objects they might contain in Engine backend. |
| 1614 | The entire graph object is new so that changes in the copy |
| 1615 | do not affect the original object. |
| 1616 | |
| 1617 | Fresh Data -- For fresh data, the graph structure is copied while |
| 1618 | new empty data attribute dicts are created. The resulting graph |
| 1619 | is independent of the original and it has no edge, node or graph |
| 1620 | attributes. Fresh copies are not enabled. Instead use: |
| 1621 | |
| 1622 | >>> H = G.__class__() |
| 1623 | >>> H.add_nodes_from(G) |
| 1624 | >>> H.add_edges_from(G.edges) |
| 1625 | |
| 1626 | View -- Inspired by dict-views, graph-views act like read-only |
| 1627 | versions of the original graph, providing a copy of the original |
| 1628 | structure without requiring any memory for copying the information. |
| 1629 | |
| 1630 | Parameters |
| 1631 | ---------- |
| 1632 | as_view : bool, optional (default=False) |
| 1633 | If True, the returned graph-view provides a read-only view |
| 1634 | of the original graph without actually copying any data. |
| 1635 | |
| 1636 | Returns |
| 1637 | ------- |
| 1638 | G : Graph |
| 1639 | A copy of the graph. |
| 1640 | |
| 1641 | See Also |
| 1642 | -------- |
| 1643 | to_directed: return a directed copy of the graph. |
| 1644 | |
| 1645 | Examples |
| 1646 | -------- |
| 1647 | >>> G = nx.path_graph(4) # or DiGraph |
| 1648 | >>> H = G.copy() |
| 1649 | |
| 1650 | """ |
| 1651 | if as_view: |
| 1652 | g = generic_graph_view(self) |
| 1653 | g._is_client_view = True |
| 1654 | g._op = self._op |
| 1655 | g.cache = self.cache |