def test_default_flow_function_karate_club_graph(self): G = nx.karate_club_graph() nx.set_edge_attributes(G, 1, "capacity") T = nx.gomory_hu_tree(G) assert nx.is_tree(T) for u, v in combinations(G, 2): cut_value, edge = self.minimum_edge_weight(T, u, v) assert nx.minimum_cut_value(G, u, v) == cut_value
def test_karate_club_graph_cutset(self): G = nx.karate_club_graph() nx.set_edge_attributes(G, 1, 'capacity') T = nx.gomory_hu_tree(G) assert_true(nx.is_tree(T)) u, v = 0, 33 cut_value, edge = self.minimum_edge_weight(T, u, v) cutset = self.compute_cutset(G, T, edge) assert_equal(cut_value, len(cutset))
def test_davis_southern_women_graph(self): G = nx.davis_southern_women_graph() nx.set_edge_attributes(G, 1, 'capacity') for flow_func in flow_funcs: T = nx.gomory_hu_tree(G, flow_func=flow_func) assert_true(nx.is_tree(T)) for u, v in combinations(G, 2): cut_value, edge = self.minimum_edge_weight(T, u, v) assert_equal(nx.minimum_cut_value(G, u, v), cut_value)
def test_florentine_families_graph(self): G = nx.florentine_families_graph() nx.set_edge_attributes(G, 1, 'capacity') for flow_func in flow_funcs: T = nx.gomory_hu_tree(G, flow_func=flow_func) assert nx.is_tree(T) for u, v in combinations(G, 2): cut_value, edge = self.minimum_edge_weight(T, u, v) assert (nx.minimum_cut_value(G, u, v) == cut_value)
def test_les_miserables_graph_cutset(self): G = nx.les_miserables_graph() nx.set_edge_attributes(G, 1, "capacity") for flow_func in flow_funcs: T = nx.gomory_hu_tree(G, flow_func=flow_func) assert nx.is_tree(T) for u, v in combinations(G, 2): cut_value, edge = self.minimum_edge_weight(T, u, v) assert nx.minimum_cut_value(G, u, v) == cut_value
def test_karate_club_graph_cutset(self): G = nx.karate_club_graph() nx.set_edge_attributes(G, 'capacity', 1) T = nx.gomory_hu_tree(G) assert_true(nx.is_tree(T)) u, v = 0, 33 cut_value, edge = self.minimum_edge_weight(T, u, v) cutset = self.compute_cutset(G, T, edge) assert_equal(cut_value, len(cutset))
def test_default_flow_function_karate_club_graph(self): G = nx.karate_club_graph() nx.set_edge_attributes(G, 1, 'capacity') T = nx.gomory_hu_tree(G) assert_true(nx.is_tree(T)) for u, v in combinations(G, 2): cut_value, edge = self.minimum_edge_weight(T, u, v) assert_equal(nx.minimum_cut_value(G, u, v), cut_value)
def test_karate_club_graph_cutset(self): G = nx.karate_club_graph() nx.set_edge_attributes(G, 1, "capacity") T = nx.gomory_hu_tree(G) assert nx.is_tree(T) u, v = 0, 33 cut_value, edge = self.minimum_edge_weight(T, u, v) cutset = self.compute_cutset(G, T, edge) assert cut_value == len(cutset)
def test_florentine_families_graph(self): G = nx.florentine_families_graph() nx.set_edge_attributes(G, 1, 'capacity') for flow_func in flow_funcs: T = nx.gomory_hu_tree(G, flow_func=flow_func) assert_true(nx.is_tree(T)) for u, v in combinations(G, 2): cut_value, edge = self.minimum_edge_weight(T, u, v) assert_equal(nx.minimum_cut_value(G, u, v), cut_value)
def test_davis_southern_women_graph(self): G = nx.davis_southern_women_graph() nx.set_edge_attributes(G, 'capacity', 1) for flow_func in flow_funcs: T = nx.gomory_hu_tree(G, flow_func=flow_func) assert_true(nx.is_tree(T)) for u, v in combinations(G, 2): cut_value, edge = self.minimum_edge_weight(T, u, v) assert_equal(nx.minimum_cut_value(G, u, v), cut_value)
def test_karate_club_graph(self): G = nx.karate_club_graph() nx.set_edge_attributes(G, 'capacity', 1) for flow_func in flow_funcs: T = nx.gomory_hu_tree(G, flow_func=flow_func) assert_true(nx.is_tree(T)) for u, v in combinations(G, 2): cut_value, edge = self.minimum_edge_weight(T, u, v) assert_equal(nx.minimum_cut_value(G, u, v), cut_value)
def test_wikipedia_example(self): # Example from https://en.wikipedia.org/wiki/Gomory%E2%80%93Hu_tree G = nx.Graph() G.add_weighted_edges_from(( (0, 1, 1), (0, 2, 7), (1, 2, 1), (1, 3, 3), (1, 4, 2), (2, 4, 4), (3, 4, 1), (3, 5, 6), (4, 5, 2), )) for flow_func in flow_funcs: T = nx.gomory_hu_tree(G, capacity='weight', flow_func=flow_func) assert_true(nx.is_tree(T)) for u, v in combinations(G, 2): cut_value, edge = self.minimum_edge_weight(T, u, v) assert_equal(nx.minimum_cut_value(G, u, v, capacity='weight'), cut_value)
def test_directed_raises(self): G = nx.DiGraph() T = nx.gomory_hu_tree(G)
def test_empty_raises(self): with pytest.raises(nx.NetworkXError): G = nx.empty_graph() T = nx.gomory_hu_tree(G)
def test_directed_raises(self): with pytest.raises(nx.NetworkXNotImplemented): G = nx.DiGraph() T = nx.gomory_hu_tree(G)
import sys import matplotlib.pyplot as plt import networkx as nx from networkx.algorithms import flow from networkx.algorithms.flow import shortest_augmenting_path G = nx.Graph(); G.add_nodes_from([0,1,2,3,4,5]) G.add_weighted_edges_from([(0,1,1), (0,2,7), (1,2,1), (1,3,3), (1,4,2), (2,4,4), (3,4,1), (3,5,6), (4,5,2)]) # nx.set_edge_attributes(G, 10, 'capacity') T = nx.gomory_hu_tree(G, capacity='weight', flow_func=shortest_augmenting_path) #weight = nx.get_edge_attributes(G,'weight') #nx.draw_circular(G, with_labels=True, font_weight='bold') #layout = nx.circular_layout(G) #nx.draw_networkx_edge_labels(G,pos=layout,edge_labels=weight) weight = nx.get_edge_attributes(T,'weight') nx.draw_circular(T, with_labels=True, font_weight='bold') layout = nx.circular_layout(T) nx.draw_networkx_edge_labels(T,pos=layout,edge_labels=weight) plt.show()
def test_empty_raises(self): G = nx.empty_graph() T = nx.gomory_hu_tree(G)