Add function docstrings, comments and type hints (#10893)

* Add function docstrings, comments and type hints

* Fix type mismatch

* Fix type hint error

* Fix float to int error

* Update ford_fulkerson.py

* Update ford_fulkerson.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update ford_fulkerson.py

---------

Co-authored-by: Christian Clauss <cclauss@me.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Hardik Pawar 2023-10-25 03:05:38 +05:30 committed by GitHub
parent 28302db941
commit fd227d8026
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,39 +1,95 @@
# Ford-Fulkerson Algorithm for Maximum Flow Problem
""" """
Ford-Fulkerson Algorithm for Maximum Flow Problem
* https://en.wikipedia.org/wiki/Ford%E2%80%93Fulkerson_algorithm
Description: Description:
(1) Start with initial flow as 0; (1) Start with initial flow as 0
(2) Choose augmenting path from source to sink and add path to flow; (2) Choose the augmenting path from source to sink and add the path to flow
""" """
graph = [
[0, 16, 13, 0, 0, 0],
[0, 0, 10, 12, 0, 0],
[0, 4, 0, 0, 14, 0],
[0, 0, 9, 0, 0, 20],
[0, 0, 0, 7, 0, 4],
[0, 0, 0, 0, 0, 0],
]
def bfs(graph, s, t, parent): def breadth_first_search(graph: list, source: int, sink: int, parents: list) -> bool:
# Return True if there is node that has not iterated. """
visited = [False] * len(graph) This function returns True if there is a node that has not iterated.
queue = []
queue.append(s) Args:
visited[s] = True graph: Adjacency matrix of graph
source: Source
sink: Sink
parents: Parent list
Returns:
True if there is a node that has not iterated.
>>> breadth_first_search(graph, 0, 5, [-1, -1, -1, -1, -1, -1])
True
>>> breadth_first_search(graph, 0, 6, [-1, -1, -1, -1, -1, -1])
Traceback (most recent call last):
...
IndexError: list index out of range
"""
visited = [False] * len(graph) # Mark all nodes as not visited
queue = [] # breadth-first search queue
# Source node
queue.append(source)
visited[source] = True
while queue: while queue:
u = queue.pop(0) u = queue.pop(0) # Pop the front node
for ind in range(len(graph[u])): # Traverse all adjacent nodes of u
if visited[ind] is False and graph[u][ind] > 0: for ind, node in enumerate(graph[u]):
if visited[ind] is False and node > 0:
queue.append(ind) queue.append(ind)
visited[ind] = True visited[ind] = True
parent[ind] = u parents[ind] = u
return visited[sink]
return visited[t]
def ford_fulkerson(graph, source, sink): def ford_fulkerson(graph: list, source: int, sink: int) -> int:
# This array is filled by BFS and to store path """
This function returns the maximum flow from source to sink in the given graph.
CAUTION: This function changes the given graph.
Args:
graph: Adjacency matrix of graph
source: Source
sink: Sink
Returns:
Maximum flow
>>> test_graph = [
... [0, 16, 13, 0, 0, 0],
... [0, 0, 10, 12, 0, 0],
... [0, 4, 0, 0, 14, 0],
... [0, 0, 9, 0, 0, 20],
... [0, 0, 0, 7, 0, 4],
... [0, 0, 0, 0, 0, 0],
... ]
>>> ford_fulkerson(test_graph, 0, 5)
23
"""
# This array is filled by breadth-first search and to store path
parent = [-1] * (len(graph)) parent = [-1] * (len(graph))
max_flow = 0 max_flow = 0
while bfs(graph, source, sink, parent):
path_flow = float("Inf") # While there is a path from source to sink
while breadth_first_search(graph, source, sink, parent):
path_flow = int(1e9) # Infinite value
s = sink s = sink
while s != source: while s != source:
# Find the minimum value in select path # Find the minimum value in the selected path
path_flow = min(path_flow, graph[parent[s]][s]) path_flow = min(path_flow, graph[parent[s]][s])
s = parent[s] s = parent[s]
@ -45,17 +101,12 @@ def ford_fulkerson(graph, source, sink):
graph[u][v] -= path_flow graph[u][v] -= path_flow
graph[v][u] += path_flow graph[v][u] += path_flow
v = parent[v] v = parent[v]
return max_flow return max_flow
graph = [ if __name__ == "__main__":
[0, 16, 13, 0, 0, 0], from doctest import testmod
[0, 0, 10, 12, 0, 0],
[0, 4, 0, 0, 14, 0],
[0, 0, 9, 0, 0, 20],
[0, 0, 0, 7, 0, 4],
[0, 0, 0, 0, 0, 0],
]
source, sink = 0, 5 testmod()
print(ford_fulkerson(graph, source, sink)) print(f"{ford_fulkerson(graph, source=0, sink=5) = }")