Exemplo n.º 1
0
 def test_ford_fulkerson_with_edges(self):
     algorithm = FordFulkersonWithEdges(self.G)
     algorithm.run(0, 5)
     expected_max_flow = 19
     expected_flow = {
         Edge(0, 1, 10): 10,
         Edge(0, 2, 10): 9,
         Edge(1, 0, 0): -10,
         Edge(1, 2, 2): 0,
         Edge(1, 3, 4): 4,
         Edge(1, 4, 8): 6,
         Edge(2, 0, 0): -9,
         Edge(2, 1, 0): 0,
         Edge(2, 4, 9): 9,
         Edge(3, 1, 0): -4,
         Edge(3, 4, 0): -5,
         Edge(3, 5, 10): 9,
         Edge(4, 1, 0): -6,
         Edge(4, 2, 0): -9,
         Edge(4, 3, 6): 5,
         Edge(4, 5, 10): 10,
         Edge(5, 3, 0): -9,
         Edge(5, 4, 0): -10
     }
     self.assertEqual(algorithm.max_flow, expected_max_flow)
     self.assertEqual(algorithm.flow, expected_flow)
Exemplo n.º 2
0
 def test_ford_fulkerson_with_edges(self):
     algorithm = FordFulkersonWithEdges(self.G)
     algorithm.run(0, 6)
     expected_max_flow = 5
     expected_flow = {
         Edge(0, 1, 3): 2,
         Edge(0, 2, 0): 0,
         Edge(0, 3, 3): 3,
         Edge(1, 0, 0): -2,
         Edge(1, 2, 4): 2,
         Edge(1, 4, 0): 0,
         Edge(2, 0, 3): 0,
         Edge(2, 1, 0): -2,
         Edge(2, 3): 1,
         Edge(2, 4, 2): 1,
         Edge(3, 0, 0): -3,
         Edge(3, 2, 0): -1,
         Edge(3, 4, 2): 0,
         Edge(3, 5, 6): 4,
         Edge(4, 1): 0,
         Edge(4, 2, 0): -1,
         Edge(4, 3, 0): 0,
         Edge(4, 6): 1,
         Edge(5, 3, 0): -4,
         Edge(5, 6, 9): 4,
         Edge(6, 4, 0): -1,
         Edge(6, 5, 0): -4
     }
     self.assertEqual(algorithm.max_flow, expected_max_flow)
     self.assertEqual(algorithm.flow, expected_flow)
Exemplo n.º 3
0
 def test_ford_fulkerson_with_edges(self):
     algorithm = FordFulkersonWithEdges(self.G)
     algorithm.run(0, 3)
     expected_max_flow = 20
     expected_flow = {
         Edge(0, 1, 10): 10, Edge(0, 2, 10): 10,
         Edge(1, 0, 0): -10, Edge(1, 2): 0, Edge(1, 3, 10): 10,
         Edge(2, 0, 0): -10, Edge(2, 1, 0): 0, Edge(2, 3, 10): 10,
         Edge(3, 1, 0): -10, Edge(3, 2, 0): -10}
     self.assertEqual(algorithm.max_flow, expected_max_flow)
     self.assertEqual(algorithm.flow, expected_flow)
Exemplo n.º 4
0
 def test_ford_fulkerson_with_edges(self):
     algorithm = FordFulkersonWithEdges(self.G)
     algorithm.run(0, 5)
     expected_max_flow = 19
     expected_flow = {
         Edge(0, 1, 10): 10, Edge(0, 2, 10): 9,
         Edge(1, 0, 0): -10, Edge(1, 2, 2): 0,
         Edge(1, 3, 4): 4, Edge(1, 4, 8): 6,
         Edge(2, 0, 0): -9, Edge(2, 1, 0): 0, Edge(2, 4, 9): 9,
         Edge(3, 1, 0): -4, Edge(3, 4, 0): -5, Edge(3, 5, 10): 9,
         Edge(4, 1, 0): -6, Edge(4, 2, 0): -9,
         Edge(4, 3, 6): 5, Edge(4, 5, 10): 10,
         Edge(5, 3, 0): -9, Edge(5, 4, 0): -10}
     self.assertEqual(algorithm.max_flow, expected_max_flow)
     self.assertEqual(algorithm.flow, expected_flow)
Exemplo n.º 5
0
 def test_ford_fulkerson_with_edges(self):
     algorithm = FordFulkersonWithEdges(self.G)
     algorithm.run(0, 6)
     expected_max_flow = 5
     expected_flow = {
         Edge(0, 1, 3): 2, Edge(0, 2, 0): 0, Edge(0, 3, 3): 3,
         Edge(1, 0, 0): -2, Edge(1, 2, 4): 2, Edge(1, 4, 0): 0,
         Edge(2, 0, 3): 0, Edge(2, 1, 0): -2, 
         Edge(2, 3): 1, Edge(2, 4, 2): 1,
         Edge(3, 0, 0): -3, Edge(3, 2, 0): -1,
         Edge(3, 4, 2): 0, Edge(3, 5, 6): 4,
         Edge(4, 1): 0, Edge(4, 2, 0): -1,
         Edge(4, 3, 0): 0, Edge(4, 6): 1,
         Edge(5, 3, 0): -4, Edge(5, 6, 9): 4,
         Edge(6, 4, 0): -1, Edge(6, 5, 0): -4}
     self.assertEqual(algorithm.max_flow, expected_max_flow)
     self.assertEqual(algorithm.flow, expected_flow)
Exemplo n.º 6
0
 def test_ford_fulkerson_with_edges(self):
     algorithm = FordFulkersonWithEdges(self.G)
     algorithm.run(0, 3)
     expected_max_flow = 20
     expected_flow = {
         Edge(0, 1, 10): 10,
         Edge(0, 2, 10): 10,
         Edge(1, 0, 0): -10,
         Edge(1, 2): 0,
         Edge(1, 3, 10): 10,
         Edge(2, 0, 0): -10,
         Edge(2, 1, 0): 0,
         Edge(2, 3, 10): 10,
         Edge(3, 1, 0): -10,
         Edge(3, 2, 0): -10
     }
     self.assertEqual(algorithm.max_flow, expected_max_flow)
     self.assertEqual(algorithm.flow, expected_flow)
Exemplo n.º 7
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))
Exemplo n.º 8
0
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 ..."
t1 = timeit.Timer(lambda: EdmondsKarpSparse(G).run(0, V - 1))
print V, E, t1.timeit(1)  # single run

# EOF