Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.9/dist-packages/networkx/algorithms/connectivity/utils.py: 19%

32 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-10-20 07:00 +0000

1""" 

2Utilities for connectivity package 

3""" 

4import networkx as nx 

5 

6__all__ = ["build_auxiliary_node_connectivity", "build_auxiliary_edge_connectivity"] 

7 

8 

9@nx._dispatch 

10def build_auxiliary_node_connectivity(G): 

11 r"""Creates a directed graph D from an undirected graph G to compute flow 

12 based node connectivity. 

13 

14 For an undirected graph G having `n` nodes and `m` edges we derive a 

15 directed graph D with `2n` nodes and `2m+n` arcs by replacing each 

16 original node `v` with two nodes `vA`, `vB` linked by an (internal) 

17 arc in D. Then for each edge (`u`, `v`) in G we add two arcs (`uB`, `vA`) 

18 and (`vB`, `uA`) in D. Finally we set the attribute capacity = 1 for each 

19 arc in D [1]_. 

20 

21 For a directed graph having `n` nodes and `m` arcs we derive a 

22 directed graph D with `2n` nodes and `m+n` arcs by replacing each 

23 original node `v` with two nodes `vA`, `vB` linked by an (internal) 

24 arc (`vA`, `vB`) in D. Then for each arc (`u`, `v`) in G we add one 

25 arc (`uB`, `vA`) in D. Finally we set the attribute capacity = 1 for 

26 each arc in D. 

27 

28 A dictionary with a mapping between nodes in the original graph and the 

29 auxiliary digraph is stored as a graph attribute: D.graph['mapping']. 

30 

31 References 

32 ---------- 

33 .. [1] Kammer, Frank and Hanjo Taubig. Graph Connectivity. in Brandes and 

34 Erlebach, 'Network Analysis: Methodological Foundations', Lecture 

35 Notes in Computer Science, Volume 3418, Springer-Verlag, 2005. 

36 https://doi.org/10.1007/978-3-540-31955-9_7 

37 

38 """ 

39 directed = G.is_directed() 

40 

41 mapping = {} 

42 H = nx.DiGraph() 

43 

44 for i, node in enumerate(G): 

45 mapping[node] = i 

46 H.add_node(f"{i}A", id=node) 

47 H.add_node(f"{i}B", id=node) 

48 H.add_edge(f"{i}A", f"{i}B", capacity=1) 

49 

50 edges = [] 

51 for source, target in G.edges(): 

52 edges.append((f"{mapping[source]}B", f"{mapping[target]}A")) 

53 if not directed: 

54 edges.append((f"{mapping[target]}B", f"{mapping[source]}A")) 

55 H.add_edges_from(edges, capacity=1) 

56 

57 # Store mapping as graph attribute 

58 H.graph["mapping"] = mapping 

59 return H 

60 

61 

62@nx._dispatch 

63def build_auxiliary_edge_connectivity(G): 

64 """Auxiliary digraph for computing flow based edge connectivity 

65 

66 If the input graph is undirected, we replace each edge (`u`,`v`) with 

67 two reciprocal arcs (`u`, `v`) and (`v`, `u`) and then we set the attribute 

68 'capacity' for each arc to 1. If the input graph is directed we simply 

69 add the 'capacity' attribute. Part of algorithm 1 in [1]_ . 

70 

71 References 

72 ---------- 

73 .. [1] Abdol-Hossein Esfahanian. Connectivity Algorithms. (this is a 

74 chapter, look for the reference of the book). 

75 http://www.cse.msu.edu/~cse835/Papers/Graph_connectivity_revised.pdf 

76 """ 

77 if G.is_directed(): 

78 H = nx.DiGraph() 

79 H.add_nodes_from(G.nodes()) 

80 H.add_edges_from(G.edges(), capacity=1) 

81 return H 

82 else: 

83 H = nx.DiGraph() 

84 H.add_nodes_from(G.nodes()) 

85 for source, target in G.edges(): 

86 H.add_edges_from([(source, target), (target, source)], capacity=1) 

87 return H