Returns a directed representation of the graph. Parameters ---------- as_view : bool, optional (default=False) If True return a view of the original directed graph. Returns ------- G : DiGraph A directed graph with the same na
(self, as_view=False)
| 1722 | |
| 1723 | @clear_mutation_cache |
| 1724 | def to_directed(self, as_view=False): |
| 1725 | """Returns a directed representation of the graph. |
| 1726 | |
| 1727 | Parameters |
| 1728 | ---------- |
| 1729 | as_view : bool, optional (default=False) |
| 1730 | If True return a view of the original directed graph. |
| 1731 | |
| 1732 | Returns |
| 1733 | ------- |
| 1734 | G : DiGraph |
| 1735 | A directed graph with the same name, same nodes, and with |
| 1736 | each edge (u, v, data) replaced by two directed edges |
| 1737 | (u, v, data) and (v, u, data). |
| 1738 | |
| 1739 | Notes |
| 1740 | ----- |
| 1741 | This by default returns a "deepcopy" of the edge, node, and |
| 1742 | graph attributes which attempts to completely copy |
| 1743 | all of the data and references. |
| 1744 | |
| 1745 | Examples |
| 1746 | -------- |
| 1747 | >>> G = nx.Graph() |
| 1748 | >>> G.add_edge(0, 1) |
| 1749 | >>> H = G.to_directed() |
| 1750 | >>> list(H.edges) |
| 1751 | [(0, 1), (1, 0)] |
| 1752 | |
| 1753 | If already directed, return a (deep) copy |
| 1754 | |
| 1755 | >>> G = nx.DiGraph() |
| 1756 | >>> G.add_edge(0, 1) |
| 1757 | >>> H = G.to_directed() |
| 1758 | >>> list(H.edges) |
| 1759 | [(0, 1)] |
| 1760 | """ |
| 1761 | self._convert_arrow_to_dynamic() |
| 1762 | |
| 1763 | if self.is_directed(): |
| 1764 | return self.copy(as_view=as_view) |
| 1765 | graph_class = self.to_directed_class() |
| 1766 | if as_view: |
| 1767 | g = generic_graph_view(self, graph_class) |
| 1768 | g._op = self._op |
| 1769 | g._key = self._key |
| 1770 | g._session = self._session |
| 1771 | g._is_client_view = True |
| 1772 | return g |
| 1773 | g = graph_class(create_empty_in_engine=False) |
| 1774 | g.graph = copy.deepcopy(self.graph) |
| 1775 | op = dag_utils.to_directed(self) |
| 1776 | graph_def = op.eval(leaf=False) |
| 1777 | g._key = graph_def.key |
| 1778 | g._session = self._session |
| 1779 | g._op = op |
| 1780 | g.cache.warmup() |
| 1781 | return g |