def test_fordfulkerson_recursive(self):
     algorithm = FordFulkersonRecursive(self.G)
     algorithm.run(0, 6)
     expected_max_flow = 5
     expected_flow = {
         Edge(0, 1, 3): 3,
         Edge(0, 2, 0): -1,
         Edge(0, 3, 3): 3,
         Edge(1, 0, 0): -3,
         Edge(1, 2, 4): 3,
         Edge(1, 4, 0): 0,
         Edge(2, 0, 3): 1,
         Edge(2, 1, 0): -3,
         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)
Esempio n. 2
0
 def test_ford_fulkerson_recursive(self):
     algorithm = FordFulkersonRecursive(self.G)
     algorithm.run(0, 3)
     expected_max_flow = 20
     expected_flow = {
         0: {
             0: 0,
             2: 10,
             1: 10,
             3: 0
         },
         1: {
             0: -10,
             2: 0,
             1: 0,
             3: 10
         },
         2: {
             0: -10,
             2: 0,
             1: 0,
             3: 10
         },
         3: {
             0: 0,
             2: -10,
             1: -10,
             3: 0
         }
     }
     self.assertEqual(algorithm.max_flow, expected_max_flow)
     self.assertEqual(algorithm.flow, expected_flow)
Esempio n. 3
0
 def test_ford_fulkerson_recursive(self):
     algorithm = FordFulkersonRecursive(self.G)
     algorithm.run(0, 5)
     expected_max_flow = 19
     expected_flow = {
         0: {
             0: 0,
             1: 10,
             2: 9,
             3: 0,
             4: 0,
             5: 0
         },
         1: {
             0: -10,
             1: 0,
             2: 0,
             3: 4,
             4: 6,
             5: 0
         },
         2: {
             0: -9,
             1: 0,
             2: 0,
             3: 0,
             4: 9,
             5: 0
         },
         3: {
             0: 0,
             1: -4,
             2: 0,
             3: 0,
             4: -6,
             5: 10
         },
         4: {
             0: 0,
             1: -6,
             2: -9,
             3: 6,
             4: 0,
             5: 9
         },
         5: {
             0: 0,
             1: 0,
             2: 0,
             3: -10,
             4: -9,
             5: 0
         }
     }
     self.assertEqual(algorithm.max_flow, expected_max_flow)
     self.assertEqual(algorithm.flow, expected_flow)
Esempio n. 4
0
 def test_ford_fulkerson_recursive(self):
     algorithm = FordFulkersonRecursive(self.G)
     algorithm.run(0, 3)
     expected_max_flow = 20
     expected_flow = {
         0: {0: 0, 2: 10, 1: 10, 3: 0}, 
         1: {0: -10, 2: 0, 1: 0, 3: 10}, 
         2: {0: -10, 2: 0, 1: 0, 3: 10}, 
         3: {0: 0, 2: -10, 1: -10, 3: 0}}
     self.assertEqual(algorithm.max_flow, expected_max_flow)
     self.assertEqual(algorithm.flow, expected_flow)
Esempio n. 5
0
 def test_ford_fulkerson_recursive(self):
     algorithm = FordFulkersonRecursive(self.G)
     algorithm.run(0, 5)
     expected_max_flow = 19
     expected_flow = {
         0: {0: 0, 1: 10, 2: 9, 3: 0, 4: 0, 5: 0},
         1: {0: -10, 1: 0, 2: 0, 3: 4, 4: 6, 5: 0},
         2: {0: -9, 1: 0, 2: 0, 3: 0, 4: 9, 5: 0},
         3: {0: 0, 1: -4, 2: 0, 3: 0, 4: -6, 5: 10},
         4: {0: 0, 1: -6, 2: -9, 3: 6, 4: 0, 5: 9},
         5: {0: 0, 1: 0, 2: 0, 3: -10, 4: -9, 5: 0}}
     self.assertEqual(algorithm.max_flow, expected_max_flow)
     self.assertEqual(algorithm.flow, expected_flow)
Esempio n. 6
0
 def test_ford_fulkerson_recursive(self):
     algorithm = FordFulkersonRecursive(self.G)
     algorithm.run(0, 6)
     expected_max_flow = 5
     expected_flow = {
         0: {0: 0, 1: 2, 2: 0, 3: 3, 4: 0, 5: 0, 6: 0},
         1: {0: -2, 1: 0, 2: 2, 3: 0, 4: 0, 5: 0, 6: 0},
         2: {0: 0, 1: -2, 2: 0, 3: 1, 4: 1, 5: 0, 6: 0},
         3: {0: -3, 1: 0, 2: -1, 3: 0, 4: 0, 5: 4, 6: 0},
         4: {0: 0, 1: 0, 2: -1, 3: 0, 4: 0, 5: 0, 6: 1},
         5: {0: 0, 1: 0, 2: 0, 3: -4, 4: 0, 5: 0, 6: 4},
         6: {0: 0, 1: 0, 2: 0, 3: 0, 4: -1, 5: -4, 6: 0}}
     self.assertEqual(algorithm.max_flow, expected_max_flow)
     self.assertEqual(algorithm.flow, expected_flow)
 def test_fordfulkerson_recursive(self):
     algorithm = FordFulkersonRecursive(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)
Esempio n. 8
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. 9
0
 def test_ford_fulkerson_recursive(self):
     algorithm = FordFulkersonRecursive(self.G)
     algorithm.run(0, 6)
     expected_max_flow = 5
     expected_flow = {
         0: {
             0: 0,
             1: 2,
             2: 0,
             3: 3,
             4: 0,
             5: 0,
             6: 0
         },
         1: {
             0: -2,
             1: 0,
             2: 2,
             3: 0,
             4: 0,
             5: 0,
             6: 0
         },
         2: {
             0: 0,
             1: -2,
             2: 0,
             3: 1,
             4: 1,
             5: 0,
             6: 0
         },
         3: {
             0: -3,
             1: 0,
             2: -1,
             3: 0,
             4: 0,
             5: 4,
             6: 0
         },
         4: {
             0: 0,
             1: 0,
             2: -1,
             3: 0,
             4: 0,
             5: 0,
             6: 1
         },
         5: {
             0: 0,
             1: 0,
             2: 0,
             3: -4,
             4: 0,
             5: 0,
             6: 4
         },
         6: {
             0: 0,
             1: 0,
             2: 0,
             3: 0,
             4: -1,
             5: -4,
             6: 0
         }
     }
     self.assertEqual(algorithm.max_flow, expected_max_flow)
     self.assertEqual(algorithm.flow, expected_flow)
Esempio n. 10
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