Ejemplo n.º 1
0
 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
Ejemplo n.º 2
0
 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))
Ejemplo n.º 3
0
 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)
Ejemplo n.º 4
0
 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)
Ejemplo n.º 5
0
 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
Ejemplo n.º 6
0
 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))
Ejemplo n.º 7
0
 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)
Ejemplo n.º 8
0
 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)
Ejemplo n.º 9
0
 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)
Ejemplo n.º 10
0
 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)
Ejemplo n.º 11
0
 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)
Ejemplo n.º 12
0
 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)
Ejemplo n.º 13
0
 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)
Ejemplo n.º 14
0
 def test_directed_raises(self):
     G = nx.DiGraph()
     T = nx.gomory_hu_tree(G)
Ejemplo n.º 15
0
 def test_empty_raises(self):
     with pytest.raises(nx.NetworkXError):
         G = nx.empty_graph()
         T = nx.gomory_hu_tree(G)
Ejemplo n.º 16
0
 def test_directed_raises(self):
     with pytest.raises(nx.NetworkXNotImplemented):
         G = nx.DiGraph()
         T = nx.gomory_hu_tree(G)
Ejemplo n.º 17
0
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()
Ejemplo n.º 18
0
 def test_empty_raises(self):
     G = nx.empty_graph()
     T = nx.gomory_hu_tree(G)
Ejemplo n.º 19
0
 def test_directed_raises(self):
     G = nx.DiGraph()
     T = nx.gomory_hu_tree(G)
Ejemplo n.º 20
0
 def test_empty_raises(self):
     G = nx.empty_graph()
     T = nx.gomory_hu_tree(G)