From c04d093a0f485dd4d6873f1b7e067cb09ecf4a92 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Tue, 24 Oct 2023 18:11:31 +0530 Subject: [PATCH 1/5] Add function docstrings, comments and type hints --- networking_flow/ford_fulkerson.py | 42 ++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/networking_flow/ford_fulkerson.py b/networking_flow/ford_fulkerson.py index 716ed508e679..e9b61b636863 100644 --- a/networking_flow/ford_fulkerson.py +++ b/networking_flow/ford_fulkerson.py @@ -6,15 +6,32 @@ """ -def bfs(graph, s, t, parent): - # Return True if there is node that has not iterated. - visited = [False] * len(graph) - queue = [] +def bfs(graph: list, s: int, t: int, parent: list) -> bool: + """ + This function returns True if there is node that has not iterated. + + Args: + graph (list): Adjacency matrix of graph + s (int): Source + t (int): Sink + parent (list): Parent list + + Returns: + bool: True if there is node that has not iterated. + """ + + visited = [False] * len(graph) # Mark all nodes as not visited + queue = [] # BFS queue + + # Source node queue.append(s) visited[s] = True while queue: + # Pop the front node u = queue.pop(0) + + # Traverse all adjacent nodes of u for ind in range(len(graph[u])): if visited[ind] is False and graph[u][ind] > 0: queue.append(ind) @@ -24,11 +41,23 @@ def bfs(graph, s, t, parent): return visited[t] -def ford_fulkerson(graph, source, sink): +def ford_fulkerson(graph: list, source: int, sink: int) -> int: + """ + This function returns maximum flow from source to sink in given graph. + + Args: + graph (list): Adjacency matrix of graph + source (int): Source + sink (int): Sink + + Returns: + int: Maximum flow + """ + # This array is filled by BFS and to store path parent = [-1] * (len(graph)) max_flow = 0 - while bfs(graph, source, sink, parent): + while bfs(graph, source, sink, parent): # While there is path from source to sink path_flow = float("Inf") s = sink @@ -45,6 +74,7 @@ def ford_fulkerson(graph, source, sink): graph[u][v] -= path_flow graph[v][u] += path_flow v = parent[v] + return max_flow From dbafd03a5d2a8c4bfd1a9143d86283a68b28e1ed Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Tue, 24 Oct 2023 18:15:49 +0530 Subject: [PATCH 2/5] Fix type mismatch --- networking_flow/ford_fulkerson.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/networking_flow/ford_fulkerson.py b/networking_flow/ford_fulkerson.py index e9b61b636863..ab2588a9e227 100644 --- a/networking_flow/ford_fulkerson.py +++ b/networking_flow/ford_fulkerson.py @@ -75,7 +75,7 @@ def ford_fulkerson(graph: list, source: int, sink: int) -> int: graph[v][u] += path_flow v = parent[v] - return max_flow + return int(max_flow) # Cast max_flow to int to fix the type mismatch issue graph = [ From 56c44568d741de9dca28424c1af0913f82cadeb1 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Tue, 24 Oct 2023 18:18:45 +0530 Subject: [PATCH 3/5] Fix type hint error --- networking_flow/ford_fulkerson.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/networking_flow/ford_fulkerson.py b/networking_flow/ford_fulkerson.py index ab2588a9e227..76855a4fcb29 100644 --- a/networking_flow/ford_fulkerson.py +++ b/networking_flow/ford_fulkerson.py @@ -58,7 +58,7 @@ def ford_fulkerson(graph: list, source: int, sink: int) -> int: parent = [-1] * (len(graph)) max_flow = 0 while bfs(graph, source, sink, parent): # While there is path from source to sink - path_flow = float("Inf") + path_flow = 1e9 # Infinite value s = sink while s != source: @@ -75,7 +75,7 @@ def ford_fulkerson(graph: list, source: int, sink: int) -> int: graph[v][u] += path_flow v = parent[v] - return int(max_flow) # Cast max_flow to int to fix the type mismatch issue + return max_flow graph = [ From 4366cb00ae3e61e77075f93065ed43e2bcc1672f Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Tue, 24 Oct 2023 18:19:52 +0530 Subject: [PATCH 4/5] Fix float to int error --- networking_flow/ford_fulkerson.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/networking_flow/ford_fulkerson.py b/networking_flow/ford_fulkerson.py index 76855a4fcb29..674685a14b65 100644 --- a/networking_flow/ford_fulkerson.py +++ b/networking_flow/ford_fulkerson.py @@ -58,7 +58,7 @@ def ford_fulkerson(graph: list, source: int, sink: int) -> int: parent = [-1] * (len(graph)) max_flow = 0 while bfs(graph, source, sink, parent): # While there is path from source to sink - path_flow = 1e9 # Infinite value + path_flow = int(1e9) # Infinite value s = sink while s != source: From 75cda8b5574bdbaceb7cc48d49ad869dd739701d Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Tue, 24 Oct 2023 18:27:11 +0530 Subject: [PATCH 5/5] Add doctests for binary_search_matrix.py --- matrix/binary_search_matrix.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/matrix/binary_search_matrix.py b/matrix/binary_search_matrix.py index 6f203b7a3484..ca55400d27f3 100644 --- a/matrix/binary_search_matrix.py +++ b/matrix/binary_search_matrix.py @@ -9,6 +9,10 @@ def binary_search(array: list, lower_bound: int, upper_bound: int, value: int) - 0 >>> binary_search(matrix, 0, len(matrix) - 1, 23) -1 + >>> binary_search(matrix, 0, len(matrix) - 1, 15) + 4 + >>> binary_search(matrix, 0, len(matrix) - 1, 7) + 2 """ r = int((lower_bound + upper_bound) // 2) @@ -39,6 +43,21 @@ def mat_bin_search(value: int, matrix: list) -> list: >>> target = 34 >>> mat_bin_search(target, matrix) [-1, -1] + >>> target = 5 + >>> mat_bin_search(target, matrix) + [1, 1] + >>> target = 23 + >>> mat_bin_search(target, matrix) + [4, 2] + >>> target = 30 + >>> mat_bin_search(target, matrix) + [4, 4] + >>> target = 19 + >>> mat_bin_search(target, matrix) + [1, 4] + >>> target = 20 + >>> mat_bin_search(target, matrix) + [-1, -1] """ index = 0 if matrix[index][0] == value: