comparison env/lib/python3.9/site-packages/networkx/algorithms/flow/tests/test_gomory_hu.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 from itertools import combinations
2 import pytest
3
4 import networkx as nx
5 from networkx.algorithms.flow import boykov_kolmogorov
6 from networkx.algorithms.flow import edmonds_karp
7 from networkx.algorithms.flow import preflow_push
8 from networkx.algorithms.flow import shortest_augmenting_path
9 from networkx.algorithms.flow import dinitz
10
11 flow_funcs = [
12 boykov_kolmogorov,
13 dinitz,
14 edmonds_karp,
15 preflow_push,
16 shortest_augmenting_path,
17 ]
18
19
20 class TestGomoryHuTree:
21 def minimum_edge_weight(self, T, u, v):
22 path = nx.shortest_path(T, u, v, weight="weight")
23 return min((T[u][v]["weight"], (u, v)) for (u, v) in zip(path, path[1:]))
24
25 def compute_cutset(self, G, T_orig, edge):
26 T = T_orig.copy()
27 T.remove_edge(*edge)
28 U, V = list(nx.connected_components(T))
29 cutset = set()
30 for x, nbrs in ((n, G[n]) for n in U):
31 cutset.update((x, y) for y in nbrs if y in V)
32 return cutset
33
34 def test_default_flow_function_karate_club_graph(self):
35 G = nx.karate_club_graph()
36 nx.set_edge_attributes(G, 1, "capacity")
37 T = nx.gomory_hu_tree(G)
38 assert nx.is_tree(T)
39 for u, v in combinations(G, 2):
40 cut_value, edge = self.minimum_edge_weight(T, u, v)
41 assert nx.minimum_cut_value(G, u, v) == cut_value
42
43 def test_karate_club_graph(self):
44 G = nx.karate_club_graph()
45 nx.set_edge_attributes(G, 1, "capacity")
46 for flow_func in flow_funcs:
47 T = nx.gomory_hu_tree(G, flow_func=flow_func)
48 assert nx.is_tree(T)
49 for u, v in combinations(G, 2):
50 cut_value, edge = self.minimum_edge_weight(T, u, v)
51 assert nx.minimum_cut_value(G, u, v) == cut_value
52
53 def test_davis_southern_women_graph(self):
54 G = nx.davis_southern_women_graph()
55 nx.set_edge_attributes(G, 1, "capacity")
56 for flow_func in flow_funcs:
57 T = nx.gomory_hu_tree(G, flow_func=flow_func)
58 assert nx.is_tree(T)
59 for u, v in combinations(G, 2):
60 cut_value, edge = self.minimum_edge_weight(T, u, v)
61 assert nx.minimum_cut_value(G, u, v) == cut_value
62
63 def test_florentine_families_graph(self):
64 G = nx.florentine_families_graph()
65 nx.set_edge_attributes(G, 1, "capacity")
66 for flow_func in flow_funcs:
67 T = nx.gomory_hu_tree(G, flow_func=flow_func)
68 assert nx.is_tree(T)
69 for u, v in combinations(G, 2):
70 cut_value, edge = self.minimum_edge_weight(T, u, v)
71 assert nx.minimum_cut_value(G, u, v) == cut_value
72
73 @pytest.mark.slow
74 def test_les_miserables_graph_cutset(self):
75 G = nx.les_miserables_graph()
76 nx.set_edge_attributes(G, 1, "capacity")
77 for flow_func in flow_funcs:
78 T = nx.gomory_hu_tree(G, flow_func=flow_func)
79 assert nx.is_tree(T)
80 for u, v in combinations(G, 2):
81 cut_value, edge = self.minimum_edge_weight(T, u, v)
82 assert nx.minimum_cut_value(G, u, v) == cut_value
83
84 def test_karate_club_graph_cutset(self):
85 G = nx.karate_club_graph()
86 nx.set_edge_attributes(G, 1, "capacity")
87 T = nx.gomory_hu_tree(G)
88 assert nx.is_tree(T)
89 u, v = 0, 33
90 cut_value, edge = self.minimum_edge_weight(T, u, v)
91 cutset = self.compute_cutset(G, T, edge)
92 assert cut_value == len(cutset)
93
94 def test_wikipedia_example(self):
95 # Example from https://en.wikipedia.org/wiki/Gomory%E2%80%93Hu_tree
96 G = nx.Graph()
97 G.add_weighted_edges_from(
98 (
99 (0, 1, 1),
100 (0, 2, 7),
101 (1, 2, 1),
102 (1, 3, 3),
103 (1, 4, 2),
104 (2, 4, 4),
105 (3, 4, 1),
106 (3, 5, 6),
107 (4, 5, 2),
108 )
109 )
110 for flow_func in flow_funcs:
111 T = nx.gomory_hu_tree(G, capacity="weight", flow_func=flow_func)
112 assert nx.is_tree(T)
113 for u, v in combinations(G, 2):
114 cut_value, edge = self.minimum_edge_weight(T, u, v)
115 assert nx.minimum_cut_value(G, u, v, capacity="weight") == cut_value
116
117 def test_directed_raises(self):
118 with pytest.raises(nx.NetworkXNotImplemented):
119 G = nx.DiGraph()
120 T = nx.gomory_hu_tree(G)
121
122 def test_empty_raises(self):
123 with pytest.raises(nx.NetworkXError):
124 G = nx.empty_graph()
125 T = nx.gomory_hu_tree(G)