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

Method to_undirected

easygraph/classes/directed_multigraph.py:326–393  ·  view source on GitHub ↗

Returns an undirected representation of the multidigraph. Parameters ---------- reciprocal : bool (optional) If True only keep edges that appear in both directions in the original digraph. Returns ------- G : MultiGraph

(self, reciprocal=False)

Source from the content-addressed store, hash-verified

324 return True
325
326 def to_undirected(self, reciprocal=False):
327 """Returns an undirected representation of the multidigraph.
328
329 Parameters
330 ----------
331 reciprocal : bool (optional)
332 If True only keep edges that appear in both directions
333 in the original digraph.
334
335 Returns
336 -------
337 G : MultiGraph
338 An undirected graph with the same name and nodes and
339 with edge (u, v, data) if either (u, v, data) or (v, u, data)
340 is in the digraph. If both edges exist in digraph and
341 their edge data is different, only one edge is created
342 with an arbitrary choice of which edge data to use.
343 You must check and correct for this manually if desired.
344
345 See Also
346 --------
347 MultiGraph, add_edge, add_edges_from
348
349 Notes
350 -----
351 This returns a "deepcopy" of the edge, node, and
352 graph attributes which attempts to completely copy
353 all of the data and references.
354
355 This is in contrast to the similar D=MultiDiGraph(G) which
356 returns a shallow copy of the data.
357
358 See the Python copy module for more information on shallow
359 and deep copies, https://docs.python.org/3/library/copy.html.
360
361 Warning: If you have subclassed MultiDiGraph to use dict-like
362 objects in the data structure, those changes do not transfer
363 to the MultiGraph created by this method.
364
365 Examples
366 --------
367 >>> G = eg.path_graph(2) # or MultiGraph, etc
368 >>> H = G.to_directed()
369 >>> list(H.edges)
370 [(0, 1), (1, 0)]
371 >>> G2 = H.to_undirected()
372 >>> list(G2.edges)
373 [(0, 1)]
374 """
375 G = eg.MultiGraph()
376 G.graph.update(deepcopy(self.graph))
377 G.add_nodes_from((n, deepcopy(d)) for n, d in self._node.items())
378 if reciprocal is True:
379 G.add_edges_from(
380 (u, v, key, deepcopy(data))
381 for u, nbrs in self._adj.items()
382 for v, keydict in nbrs.items()
383 for key, data in keydict.items()

Callers

nothing calls this directly

Calls 3

add_edges_fromMethod · 0.95
updateMethod · 0.80
add_nodes_fromMethod · 0.45

Tested by

no test coverage detected