MCPcopy Create free account
hub / github.com/easy-graph/Easy-Graph / to_directed

Method to_directed

easygraph/classes/graph.py:1439–1491  ·  view source on GitHub ↗

Returns a directed representation of the graph. Returns ------- G : DiGraph A directed graph with the same name, same nodes, and with each edge (u, v, data) replaced by two directed edges (u, v, data) and (v, u, data). Notes

(self)

Source from the content-addressed store, hash-verified

1437 return eg.DiGraph
1438
1439 def to_directed(self):
1440 """Returns a directed representation of the graph.
1441
1442 Returns
1443 -------
1444 G : DiGraph
1445 A directed graph with the same name, same nodes, and with
1446 each edge (u, v, data) replaced by two directed edges
1447 (u, v, data) and (v, u, data).
1448
1449 Notes
1450 -----
1451 This returns a "deepcopy" of the edge, node, and
1452 graph attributes which attempts to completely copy
1453 all of the data and references.
1454
1455 This is in contrast to the similar D=DiGraph(G) which returns a
1456 shallow copy of the data.
1457
1458 See the Python copy module for more information on shallow
1459 and deep copies, https://docs.python.org/3/library/copy.html.
1460
1461 Warning: If you have subclassed Graph to use dict-like objects
1462 in the data structure, those changes do not transfer to the
1463 DiGraph created by this method.
1464
1465 Examples
1466 --------
1467 >>> G = eg.Graph() # or MultiGraph, etc
1468 >>> G.add_edge(0, 1)
1469 >>> H = G.to_directed()
1470 >>> list(H.edges)
1471 [(0, 1), (1, 0)]
1472
1473 If already directed, return a (deep) copy
1474
1475 >>> G = eg.DiGraph() # or MultiDiGraph, etc
1476 >>> G.add_edge(0, 1)
1477 >>> H = G.to_directed()
1478 >>> list(H.edges)
1479 [(0, 1)]
1480 """
1481 graph_class = self.to_directed_class()
1482
1483 G = graph_class()
1484 G.graph.update(deepcopy(self.graph))
1485 G.add_nodes_from((n, deepcopy(d)) for n, d in self._node.items())
1486 G.add_edges_from(
1487 (u, v, deepcopy(data))
1488 for u, nbrs in self._adj.items()
1489 for v, data in nbrs.items()
1490 )
1491 return G
1492
1493 def _clear_cache(self):
1494 r"""Clear the cache."""

Callers 1

node_link_graphFunction · 0.95

Calls 4

to_directed_classMethod · 0.95
updateMethod · 0.80
add_nodes_fromMethod · 0.45
add_edges_fromMethod · 0.45

Tested by

no test coverage detected