Skip to content

Commit f9bb554

Browse files
authored
fix ford_fulkerson.py
1 parent 4b54674 commit f9bb554

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

networking_flow/ford_fulkerson.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
"""
99

1010
from collections import deque
11-
from typing import List
1211

1312
graph = [
1413
[0, 16, 13, 0, 0, 0],
@@ -20,7 +19,9 @@
2019
]
2120

2221

23-
def breadth_first_search(graph: List[List[int]], source: int, sink: int, parents: List[int]) -> bool:
22+
def breadth_first_search(
23+
graph: list, source: int, sink: int, parents: list
24+
) -> bool:
2425
"""
2526
This function returns True if there is a node that has not iterated.
2627
@@ -43,28 +44,28 @@ def breadth_first_search(graph: List[List[int]], source: int, sink: int, parents
4344
num_nodes = len(graph)
4445
visited = [False] * num_nodes
4546
queue = deque()
46-
47+
4748
queue.append(source)
4849
visited[source] = True
49-
50+
5051
while queue:
5152
current_node = queue.popleft()
52-
53+
5354
# If we reached the sink, we can stop early
5455
if current_node == sink:
5556
return True
56-
57+
5758
# Check all adjacent nodes
5859
for neighbor, capacity in enumerate(graph[current_node]):
5960
if not visited[neighbor] and capacity > 0:
6061
visited[neighbor] = True
6162
parents[neighbor] = current_node
6263
queue.append(neighbor)
63-
64+
6465
return visited[sink]
6566

6667

67-
def ford_fulkerson(graph: List[List[int]], source: int, sink: int) -> int:
68+
def ford_fulkerson(graph: list, source: int, sink: int) -> int:
6869
"""
6970
This function returns the maximum flow from source to sink in the given graph.
7071
@@ -100,7 +101,7 @@ def ford_fulkerson(graph: List[List[int]], source: int, sink: int) -> int:
100101
# Find the minimum residual capacity along the path
101102
path_flow = float('inf')
102103
current_node = sink
103-
104+
104105
# Find the minimum capacity in the path
105106
while current_node != source:
106107
parent_node = parents[current_node]
@@ -125,4 +126,4 @@ def ford_fulkerson(graph: List[List[int]], source: int, sink: int) -> int:
125126
from doctest import testmod
126127

127128
testmod()
128-
print(f"{ford_fulkerson(graph, source=0, sink=5) = }")
129+
print(f"{ford_fulkerson(graph, source=0, sink=5) = }")

0 commit comments

Comments
 (0)