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

Method to_directed

easygraph/classes/multigraph.py:620–671  ·  view source on GitHub ↗

Returns a directed representation of the graph. Returns ------- G : MultiDiGraph 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

618 return G
619
620 def to_directed(self):
621 """Returns a directed representation of the graph.
622
623 Returns
624 -------
625 G : MultiDiGraph
626 A directed graph with the same name, same nodes, and with
627 each edge (u, v, data) replaced by two directed edges
628 (u, v, data) and (v, u, data).
629
630 Notes
631 -----
632 This returns a "deepcopy" of the edge, node, and
633 graph attributes which attempts to completely copy
634 all of the data and references.
635
636 This is in contrast to the similar D=DiGraph(G) which returns a
637 shallow copy of the data.
638
639 See the Python copy module for more information on shallow
640 and deep copies, https://docs.python.org/3/library/copy.html.
641
642 Warning: If you have subclassed MultiGraph to use dict-like objects
643 in the data structure, those changes do not transfer to the
644 MultiDiGraph created by this method.
645
646 Examples
647 --------
648 >>> G = eg.Graph() # or MultiGraph, etc
649 >>> G.add_edge(0, 1)
650 >>> H = G.to_directed()
651 >>> list(H.edges)
652 [(0, 1), (1, 0)]
653
654 If already directed, return a (deep) copy
655
656 >>> G = eg.DiGraph() # or MultiDiGraph, etc
657 >>> G.add_edge(0, 1)
658 >>> H = G.to_directed()
659 >>> list(H.edges)
660 [(0, 1)]
661 """
662 G = eg.MultiDiGraph()
663 G.graph.update(deepcopy(self.graph))
664 G.add_nodes_from((n, deepcopy(d)) for n, d in self._node.items())
665 G.add_edges_from(
666 (u, v, key, deepcopy(datadict))
667 for u, nbrs in self.adj.items()
668 for v, keydict in nbrs.items()
669 for key, datadict in keydict.items()
670 )
671 return G
672
673 def number_of_edges(self, u=None, v=None):
674 """Returns the number of edges between two nodes.

Callers

nothing calls this directly

Calls 3

updateMethod · 0.80
add_nodes_fromMethod · 0.45
add_edges_fromMethod · 0.45

Tested by

no test coverage detected