Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions algorithms/graph/maximum_flow_bfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def maximum_flow_bfs(adjacency_matrix: list[list[int]]) -> int:

while tmp != 0:
new_array[path[tmp]][tmp] = new_array[path[tmp]][tmp] - min_flow
new_array[tmp][path[tmp]] += min_flow
tmp = path[tmp]

total = total + min_flow
Expand Down
1 change: 1 addition & 0 deletions algorithms/graph/maximum_flow_dfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def maximum_flow_dfs(adjacency_matrix: list[list[int]]) -> int:

while tmp != 0:
new_array[path[tmp]][tmp] = new_array[path[tmp]][tmp] - min_flow
new_array[tmp][path[tmp]] += min_flow
tmp = path[tmp]

total = total + min_flow
Expand Down
26 changes: 26 additions & 0 deletions tests/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,19 @@ def test_maximum_flow_bfs(self):

self.assertEqual(maximum_flow, 23)

def test_maximum_flow_bfs_reroutes_earlier_flow(self):
graph = [
[0, 1, 1, 0, 0, 0],
[0, 0, 0, 1, 1, 0],
[0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0],
]
# The first path is 0->1->3->5. Reaching flow 2 requires
# canceling 1->3 so the final paths use 0->1->4->5 and 0->2->3->5.
self.assertEqual(maximum_flow_bfs(graph), 2)


class TestMaximumFlowDfs(unittest.TestCase):
"""
Expand All @@ -190,6 +203,19 @@ def test_maximum_flow_dfs(self):

self.assertEqual(maximum_flow, 23)

def test_maximum_flow_dfs_reroutes_earlier_flow(self):
graph = [
[0, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 1, 0],
[0, 0, 0, 1, 1, 0],
[0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0],
]
# The first path is 0->2->4->5. Reaching flow 2 requires
# canceling 2->4 so the final paths use 0->1->4->5 and 0->2->3->5.
self.assertEqual(maximum_flow_dfs(graph), 2)


class TestAllPairsShortestPath(unittest.TestCase):
def test_all_pairs_shortest_path(self):
Expand Down
Loading