utils.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. """
  2. Utilities for connectivity package
  3. """
  4. import networkx as nx
  5. __all__ = ["build_auxiliary_node_connectivity", "build_auxiliary_edge_connectivity"]
  6. def build_auxiliary_node_connectivity(G):
  7. r"""Creates a directed graph D from an undirected graph G to compute flow
  8. based node connectivity.
  9. For an undirected graph G having `n` nodes and `m` edges we derive a
  10. directed graph D with `2n` nodes and `2m+n` arcs by replacing each
  11. original node `v` with two nodes `vA`, `vB` linked by an (internal)
  12. arc in D. Then for each edge (`u`, `v`) in G we add two arcs (`uB`, `vA`)
  13. and (`vB`, `uA`) in D. Finally we set the attribute capacity = 1 for each
  14. arc in D [1]_.
  15. For a directed graph having `n` nodes and `m` arcs we derive a
  16. directed graph D with `2n` nodes and `m+n` arcs by replacing each
  17. original node `v` with two nodes `vA`, `vB` linked by an (internal)
  18. arc (`vA`, `vB`) in D. Then for each arc (`u`, `v`) in G we add one
  19. arc (`uB`, `vA`) in D. Finally we set the attribute capacity = 1 for
  20. each arc in D.
  21. A dictionary with a mapping between nodes in the original graph and the
  22. auxiliary digraph is stored as a graph attribute: H.graph['mapping'].
  23. References
  24. ----------
  25. .. [1] Kammer, Frank and Hanjo Taubig. Graph Connectivity. in Brandes and
  26. Erlebach, 'Network Analysis: Methodological Foundations', Lecture
  27. Notes in Computer Science, Volume 3418, Springer-Verlag, 2005.
  28. https://doi.org/10.1007/978-3-540-31955-9_7
  29. """
  30. directed = G.is_directed()
  31. mapping = {}
  32. H = nx.DiGraph()
  33. for i, node in enumerate(G):
  34. mapping[node] = i
  35. H.add_node(f"{i}A", id=node)
  36. H.add_node(f"{i}B", id=node)
  37. H.add_edge(f"{i}A", f"{i}B", capacity=1)
  38. edges = []
  39. for source, target in G.edges():
  40. edges.append((f"{mapping[source]}B", f"{mapping[target]}A"))
  41. if not directed:
  42. edges.append((f"{mapping[target]}B", f"{mapping[source]}A"))
  43. H.add_edges_from(edges, capacity=1)
  44. # Store mapping as graph attribute
  45. H.graph["mapping"] = mapping
  46. return H
  47. def build_auxiliary_edge_connectivity(G):
  48. """Auxiliary digraph for computing flow based edge connectivity
  49. If the input graph is undirected, we replace each edge (`u`,`v`) with
  50. two reciprocal arcs (`u`, `v`) and (`v`, `u`) and then we set the attribute
  51. 'capacity' for each arc to 1. If the input graph is directed we simply
  52. add the 'capacity' attribute. Part of algorithm 1 in [1]_ .
  53. References
  54. ----------
  55. .. [1] Abdol-Hossein Esfahanian. Connectivity Algorithms. (this is a
  56. chapter, look for the reference of the book).
  57. http://www.cse.msu.edu/~cse835/Papers/Graph_connectivity_revised.pdf
  58. """
  59. if G.is_directed():
  60. H = nx.DiGraph()
  61. H.add_nodes_from(G.nodes())
  62. H.add_edges_from(G.edges(), capacity=1)
  63. return H
  64. else:
  65. H = nx.DiGraph()
  66. H.add_nodes_from(G.nodes())
  67. for source, target in G.edges():
  68. H.add_edges_from([(source, target), (target, source)], capacity=1)
  69. return H