Esempio n. 1
0
 def run(self):
     """Executable pseudocode."""
     size = self.graph.v()
     # Create flow network.
     network = self.graph.__class__(size + 2, directed=True)
     self.source = size
     self.sink = size + 1
     network.add_node(self.source)
     network.add_node(self.sink)
     for node in self.graph.iternodes():  # O(V) time
         network.add_node(node)
     for node in self.v1:  # edges from source to V1
         network.add_edge(Edge(self.source, node))
     for edge in self.graph.iteredges():  # edges from V1 to V2
         if edge.source in self.v1:  # weights are 1
             network.add_edge(Edge(edge.source, edge.target))
         else:
             network.add_edge(Edge(edge.target, edge.source))
     for node in self.v2:  # edges from V2 to sink
         network.add_edge(Edge(node, self.sink))
     algorithm = FordFulkersonSparse(network)
     algorithm.run(self.source, self.sink)  # O(V*E) time
     for source in self.v1:
         for target in self.v2:
             if algorithm.flow[source].get(target, 0) == 1:
                 self.mate[source] = target
                 self.mate[target] = source
     self.cardinality = algorithm.max_flow
Esempio n. 2
0
 def test_ford_fulkerson_sparse(self):
     algorithm = FordFulkersonSparse(self.G)
     algorithm.run(0, 3)
     expected_max_flow = 20
     expected_flow = {
         0: {
             1: 10,
             2: 10
         },
         1: {
             0: -10,
             2: 0,
             3: 10
         },
         2: {
             0: -10,
             1: 0,
             3: 10
         },
         3: {
             1: -10,
             2: -10
         }
     }
     self.assertEqual(algorithm.max_flow, expected_max_flow)
     self.assertEqual(algorithm.flow, expected_flow)
Esempio n. 3
0
 def run(self):
     """Executable pseudocode."""
     size = self.graph.v()
     # Create flow network.
     network = self.graph.__class__(size + 2, directed=True)
     self.source = size
     self.sink = size + 1
     network.add_node(self.source)
     network.add_node(self.sink)
     for node in self.graph.iternodes():   # O(V) time
         network.add_node(node)
     for node in self.v1:   # edges from source to V1
         network.add_edge(Edge(self.source, node))
     for edge in self.graph.iteredges():   # edges from V1 to V2
         if edge.source in self.v1:   # weights are 1
             network.add_edge(Edge(edge.source, edge.target))
         else:
             network.add_edge(Edge(edge.target, edge.source))
     for node in self.v2:   # edges from V2 to sink
         network.add_edge(Edge(node, self.sink))
     algorithm = FordFulkersonSparse(network)
     algorithm.run(self.source, self.sink)   # O(V*E) time
     for source in self.v1:
         for target in self.v2:
             if algorithm.flow[source].get(target, 0) == 1:
                 self.mate[source] = target
                 self.mate[target] = source
     self.cardinality = algorithm.max_flow
Esempio n. 4
0
 def test_ford_fulkerson_sparse(self):
     algorithm = FordFulkersonSparse(self.G)
     algorithm.run(0, 3)
     expected_max_flow = 20
     expected_flow = {
         0: {1: 10, 2: 10},
         1: {0: -10, 2: 0, 3: 10},
         2: {0: -10, 1: 0, 3: 10},
         3: {1: -10, 2: -10}}
     self.assertEqual(algorithm.max_flow, expected_max_flow)
     self.assertEqual(algorithm.flow, expected_flow)
Esempio n. 5
0
 def test_ford_fulkerson_sparse(self):
     algorithm = FordFulkersonSparse(self.G)
     algorithm.run(0, 5)
     expected_max_flow = 19
     expected_flow = {
         0: {1: 10, 2: 9},
         1: {0: -10, 2: 0, 3: 4, 4: 6},
         2: {0: -9, 1: 0, 4: 9},
         3: {1: -4, 4: -5, 5: 9},
         4: {1: -6, 2: -9, 3: 5, 5: 10},
         5: {3: -9, 4: -10}}
     self.assertEqual(algorithm.max_flow, expected_max_flow)
     self.assertEqual(algorithm.flow, expected_flow)
Esempio n. 6
0
 def test_ford_fulkerson_sparse(self):
     algorithm = FordFulkersonSparse(self.G)
     algorithm.run(0, 6)
     expected_max_flow = 5
     expected_flow = {
         0: {1: 2, 2: 0, 3: 3},
         1: {0: -2, 2: 2, 4: 0},
         2: {0: 0, 1: -2, 3: 1, 4: 1},
         3: {0: -3, 2: -1, 4: 0, 5: 4},
         4: {1: 0, 2: -1, 3: 0, 6: 1},
         5: {3: -4, 6: 4},
         6: {4: -1, 5: -4}}
     self.assertEqual(algorithm.max_flow, expected_max_flow)
     self.assertEqual(algorithm.flow, expected_flow)
Esempio n. 7
0
 def test_ford_fulkerson_sparse(self):
     algorithm = FordFulkersonSparse(self.G)
     algorithm.run(0, 6)
     expected_max_flow = 5
     expected_flow = {
         0: {
             1: 2,
             2: 0,
             3: 3
         },
         1: {
             0: -2,
             2: 2,
             4: 0
         },
         2: {
             0: 0,
             1: -2,
             3: 1,
             4: 1
         },
         3: {
             0: -3,
             2: -1,
             4: 0,
             5: 4
         },
         4: {
             1: 0,
             2: -1,
             3: 0,
             6: 1
         },
         5: {
             3: -4,
             6: 4
         },
         6: {
             4: -1,
             5: -4
         }
     }
     self.assertEqual(algorithm.max_flow, expected_max_flow)
     self.assertEqual(algorithm.flow, expected_flow)
Esempio n. 8
0
 def test_ford_fulkerson_sparse(self):
     algorithm = FordFulkersonSparse(self.G)
     algorithm.run(0, 5)
     expected_max_flow = 19
     expected_flow = {
         0: {
             1: 10,
             2: 9
         },
         1: {
             0: -10,
             2: 0,
             3: 4,
             4: 6
         },
         2: {
             0: -9,
             1: 0,
             4: 9
         },
         3: {
             1: -4,
             4: -5,
             5: 9
         },
         4: {
             1: -6,
             2: -9,
             3: 5,
             5: 10
         },
         5: {
             3: -9,
             4: -10
         }
     }
     self.assertEqual(algorithm.max_flow, expected_max_flow)
     self.assertEqual(algorithm.flow, expected_flow)
Esempio n. 9
0
 def test_exceptions(self):
     self.assertRaises(ValueError, FordFulkerson, Graph())
     self.assertRaises(ValueError, FordFulkersonSparse, Graph())
     self.assertRaises(ValueError, FordFulkersonWithEdges, Graph())
     self.assertRaises(ValueError, FordFulkersonRecursive, Graph())
     self.assertRaises(ValueError, lambda: FordFulkerson(self.G).run(0, 0))
     self.assertRaises(ValueError,
                       lambda: FordFulkersonSparse(self.G).run(0, 0))
     self.assertRaises(ValueError,
                       lambda: FordFulkersonWithEdges(self.G).run(0, 0))
     self.assertRaises(ValueError,
                       lambda: FordFulkersonRecursive(self.G).run(0, 0))
     self.assertRaises(
         ValueError,
         lambda: FordFulkersonRecursiveWithEdges(self.G).run(0, 0))
Esempio n. 10
0
graph_factory = GraphFactory(Graph)
G = graph_factory.make_flow_network(V)
E = G.e()
#G.show()

print "Calculate parameters ..."
print "Nodes:", G.v(), V
print "Edges:", G.e(), E
print "Directed:", G.is_directed()

print "Testing FordFulkerson ..."
t1 = timeit.Timer(lambda: FordFulkerson(G).run(0, V - 1))
print V, E, t1.timeit(1)  # single run

print "Testing FordFulkersonSparse ..."
t1 = timeit.Timer(lambda: FordFulkersonSparse(G).run(0, V - 1))
print V, E, t1.timeit(1)  # single run

print "Testing FordFulkersonWithEdges ..."
t1 = timeit.Timer(lambda: FordFulkersonWithEdges(G).run(0, V - 1))
print V, E, t1.timeit(1)  # single run

print "Testing FordFulkersonRecursive ..."
t1 = timeit.Timer(lambda: FordFulkersonRecursive(G).run(0, V - 1))
print V, E, t1.timeit(1)  # single run

print "Testing EdmondsKarp ..."
t1 = timeit.Timer(lambda: EdmondsKarp(G).run(0, V - 1))
print V, E, t1.timeit(1)  # single run

print "Testing EdmondsKarpSparse ..."