From 96f150551c60c934fee04aea07f5a37aba3e8f07 Mon Sep 17 00:00:00 2001 From: asmitannu <143717144+asmitannu@users.noreply.github.com> Date: Sun, 15 Dec 2024 17:31:19 +0400 Subject: [PATCH 1/8] Update graphs_floyd_warshall.py --- graphs/graphs_floyd_warshall.py | 164 +++++++++++++++++--------------- 1 file changed, 85 insertions(+), 79 deletions(-) diff --git a/graphs/graphs_floyd_warshall.py b/graphs/graphs_floyd_warshall.py index aaed9ac5df8b..2b1b2cc84263 100644 --- a/graphs/graphs_floyd_warshall.py +++ b/graphs/graphs_floyd_warshall.py @@ -1,102 +1,108 @@ # floyd_warshall.py """ -The problem is to find the shortest distance between all pairs of vertices in a -weighted directed graph that can have negative edge weights. -""" - +The problem is to find and return the shortest distance between all pairs of vertices +in a weighted directed graph that can have negative edge weights. -def _print_dist(dist, v): - print("\nThe shortest path matrix using Floyd Warshall algorithm\n") - for i in range(v): - for j in range(v): - if dist[i][j] != float("inf"): - print(int(dist[i][j]), end="\t") - else: - print("INF", end="\t") - print() +https://docs.python.org/3/library/doctest.html +""" -def floyd_warshall(graph, v): +def floyd_warshall(graph:list[list[float]], vertex:int) -> tuple: + #1. For all edges from v to n, distance[i][j] = weight(edge(i, j)). + + #2. distance[i][j] = min(distance[i][j], distance[i][k] + distance[k][j]). + + #3. Step 2 is true for each pair of vertices.Repeat for k vertex in the graph. + + #4. Whenever distance[i][j] is given a new minimum value, + # next vertex[i][j] = next vertex[i][k]. + """ :param graph: 2D array calculated from weight[edge[i, j]] - :type graph: List[List[float]] + :param v: number of vertices - :type v: int + :return: shortest distance between all vertex pairs + distance[u][v] will contain the shortest distance from vertex u to v. - - 1. For all edges from v to n, distance[i][j] = weight(edge(i, j)). - 3. The algorithm then performs distance[i][j] = min(distance[i][j], distance[i][k] + - distance[k][j]) for each possible pair i, j of vertices. - 4. The above is repeated for each vertex k in the graph. - 5. Whenever distance[i][j] is given a new minimum value, next vertex[i][j] is - updated to the next vertex[i][k]. + + # doctests: + + >>> graph = [[0, 3, float('inf')], [2, 0, float('inf')],[float('inf'), 7, 0]] + >>> floyd_warshall(graph, 3)[0] + [[0, 3, inf], [2, 0, inf], [9, 7, 0]] + + >>> graph = [[0, 1, 4],[float('inf'), 0, 2],[float('inf'), float('inf'), 0]] + >>> floyd_warshall(graph, 3)[0] + [[0, 1, 3], [inf, 0, 2], [inf, inf, 0]] + + >>> graph = [[0, 0, 0],[0, 0, 0],[0, 0, 0]] + >>> floyd_warshall(graph, 3)[0] + [[0, 0, 0], [0, 0, 0], [0, 0, 0]] + + #Graph with all edge weights = infinity + + >>> graph = [[float('inf'), float('inf'), float('inf')], + ... [float('inf'), float('inf'), float('inf')], + ... [float('inf'), float('inf'), float('inf')]] + >>> floyd_warshall(graph, 3)[0] + [[inf, inf, inf], [inf, inf, inf], [inf, inf, inf]] + + + #Handling negetive weighted graph: + + >>> graph = [[0, -2, float('inf')],[float('inf'), 0, 3],[4, float('inf'), 0]] + >>> floyd_warshall(graph, 3)[0] + [[0, -2, 1], [7, 0, 3], [4, 2, 0]] + + + #Handling negetive weighted cycle: + + >>> graph = [[0, -1, float('inf')],[float('inf'), 0, -2],[-3, float('inf'), 0]] + >>> floyd_warshall(graph, 3)[0] + [[-6, -7, -9], [-5, -6, -8], [-9, -10, -12]] + + + #Number of vertex in function arguement should match number of vertex in graph: + + >>> graph = [[0, -1, float('inf')],[float('inf'), 0, -2]] + >>> floyd_warshall(graph, 3)[0] + Traceback (most recent call last): + ... + IndexError: list index out of range + + + #Graph data type should be a 2D list: + + >>> graph = "strings not allowed" + >>> floyd_warshall(graph, 3)[0] + Traceback (most recent call last): + ... + IndexError: string index out of range """ - dist = [[float("inf") for _ in range(v)] for _ in range(v)] + dist = [[float("inf") for _ in range(vertex)] for _ in range(vertex)] - for i in range(v): - for j in range(v): + for i in range(vertex): + for j in range(vertex): dist[i][j] = graph[i][j] - # check vertex k against all other vertices (i, j) - for k in range(v): + + for k in range(vertex): # looping through rows of graph array - for i in range(v): + + for i in range(vertex): # looping through columns of graph array - for j in range(v): + + for j in range(vertex): if ( dist[i][k] != float("inf") and dist[k][j] != float("inf") and dist[i][k] + dist[k][j] < dist[i][j] - ): + ): dist[i][j] = dist[i][k] + dist[k][j] + return dist, vertex - _print_dist(dist, v) - return dist, v - - -if __name__ == "__main__": - v = int(input("Enter number of vertices: ")) - e = int(input("Enter number of edges: ")) - - graph = [[float("inf") for i in range(v)] for j in range(v)] - - for i in range(v): - graph[i][i] = 0.0 - - # src and dst are indices that must be within the array size graph[e][v] - # failure to follow this will result in an error - for i in range(e): - print("\nEdge ", i + 1) - src = int(input("Enter source:")) - dst = int(input("Enter destination:")) - weight = float(input("Enter weight:")) - graph[src][dst] = weight - - floyd_warshall(graph, v) - - # Example Input - # Enter number of vertices: 3 - # Enter number of edges: 2 - - # # generated graph from vertex and edge inputs - # [[inf, inf, inf], [inf, inf, inf], [inf, inf, inf]] - # [[0.0, inf, inf], [inf, 0.0, inf], [inf, inf, 0.0]] - - # specify source, destination and weight for edge #1 - # Edge 1 - # Enter source:1 - # Enter destination:2 - # Enter weight:2 - - # specify source, destination and weight for edge #2 - # Edge 2 - # Enter source:2 - # Enter destination:1 - # Enter weight:1 - - # # Expected Output from the vertice, edge and src, dst, weight inputs!! - # 0 INF INF - # INF 0 2 - # INF 1 0 +if __name__== "__main__": + import doctest + doctest.testmod() From e2cc72909a37a4061fd633aa3bd132b5ea87c951 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 15 Dec 2024 13:38:12 +0000 Subject: [PATCH 2/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- graphs/graphs_floyd_warshall.py | 75 +++++++++++++++++---------------- 1 file changed, 39 insertions(+), 36 deletions(-) diff --git a/graphs/graphs_floyd_warshall.py b/graphs/graphs_floyd_warshall.py index 2b1b2cc84263..a6693c9a327b 100644 --- a/graphs/graphs_floyd_warshall.py +++ b/graphs/graphs_floyd_warshall.py @@ -1,79 +1,80 @@ # floyd_warshall.py """ -The problem is to find and return the shortest distance between all pairs of vertices +The problem is to find and return the shortest distance between all pairs of vertices in a weighted directed graph that can have negative edge weights. https://docs.python.org/3/library/doctest.html """ -def floyd_warshall(graph:list[list[float]], vertex:int) -> tuple: - #1. For all edges from v to n, distance[i][j] = weight(edge(i, j)). - - #2. distance[i][j] = min(distance[i][j], distance[i][k] + distance[k][j]). - - #3. Step 2 is true for each pair of vertices.Repeat for k vertex in the graph. - - #4. Whenever distance[i][j] is given a new minimum value, + +def floyd_warshall(graph: list[list[float]], vertex: int) -> tuple: + # 1. For all edges from v to n, distance[i][j] = weight(edge(i, j)). + + # 2. distance[i][j] = min(distance[i][j], distance[i][k] + distance[k][j]). + + # 3. Step 2 is true for each pair of vertices.Repeat for k vertex in the graph. + + # 4. Whenever distance[i][j] is given a new minimum value, # next vertex[i][j] = next vertex[i][k]. - + """ :param graph: 2D array calculated from weight[edge[i, j]] - + :param v: number of vertices - + :return: shortest distance between all vertex pairs - + distance[u][v] will contain the shortest distance from vertex u to v. - + # doctests: - + >>> graph = [[0, 3, float('inf')], [2, 0, float('inf')],[float('inf'), 7, 0]] >>> floyd_warshall(graph, 3)[0] [[0, 3, inf], [2, 0, inf], [9, 7, 0]] - + >>> graph = [[0, 1, 4],[float('inf'), 0, 2],[float('inf'), float('inf'), 0]] >>> floyd_warshall(graph, 3)[0] [[0, 1, 3], [inf, 0, 2], [inf, inf, 0]] - + >>> graph = [[0, 0, 0],[0, 0, 0],[0, 0, 0]] >>> floyd_warshall(graph, 3)[0] [[0, 0, 0], [0, 0, 0], [0, 0, 0]] - + #Graph with all edge weights = infinity - + >>> graph = [[float('inf'), float('inf'), float('inf')], ... [float('inf'), float('inf'), float('inf')], ... [float('inf'), float('inf'), float('inf')]] >>> floyd_warshall(graph, 3)[0] [[inf, inf, inf], [inf, inf, inf], [inf, inf, inf]] - - + + #Handling negetive weighted graph: - + >>> graph = [[0, -2, float('inf')],[float('inf'), 0, 3],[4, float('inf'), 0]] >>> floyd_warshall(graph, 3)[0] [[0, -2, 1], [7, 0, 3], [4, 2, 0]] - - + + #Handling negetive weighted cycle: - + >>> graph = [[0, -1, float('inf')],[float('inf'), 0, -2],[-3, float('inf'), 0]] >>> floyd_warshall(graph, 3)[0] [[-6, -7, -9], [-5, -6, -8], [-9, -10, -12]] - - + + #Number of vertex in function arguement should match number of vertex in graph: - + >>> graph = [[0, -1, float('inf')],[float('inf'), 0, -2]] >>> floyd_warshall(graph, 3)[0] Traceback (most recent call last): ... IndexError: list index out of range - - + + #Graph data type should be a 2D list: - + >>> graph = "strings not allowed" >>> floyd_warshall(graph, 3)[0] Traceback (most recent call last): @@ -87,22 +88,24 @@ def floyd_warshall(graph:list[list[float]], vertex:int) -> tuple: for j in range(vertex): dist[i][j] = graph[i][j] # check vertex k against all other vertices (i, j) - + for k in range(vertex): # looping through rows of graph array - + for i in range(vertex): # looping through columns of graph array - + for j in range(vertex): if ( dist[i][k] != float("inf") and dist[k][j] != float("inf") and dist[i][k] + dist[k][j] < dist[i][j] - ): + ): dist[i][j] = dist[i][k] + dist[k][j] return dist, vertex -if __name__== "__main__": + +if __name__ == "__main__": import doctest + doctest.testmod() From f58a653c5a229ce6a5cb0b573bab2812dea5c6b1 Mon Sep 17 00:00:00 2001 From: asmitannu <143717144+asmitannu@users.noreply.github.com> Date: Sun, 15 Dec 2024 17:45:11 +0400 Subject: [PATCH 3/8] Update graphs_floyd_warshall.py --- graphs/graphs_floyd_warshall.py | 77 ++++++++++++++++----------------- 1 file changed, 37 insertions(+), 40 deletions(-) diff --git a/graphs/graphs_floyd_warshall.py b/graphs/graphs_floyd_warshall.py index a6693c9a327b..09389e9f9772 100644 --- a/graphs/graphs_floyd_warshall.py +++ b/graphs/graphs_floyd_warshall.py @@ -1,80 +1,79 @@ # floyd_warshall.py """ -The problem is to find and return the shortest distance between all pairs of vertices +The problem is to find and return the shortest distance between all pairs of vertices in a weighted directed graph that can have negative edge weights. https://docs.python.org/3/library/doctest.html """ - -def floyd_warshall(graph: list[list[float]], vertex: int) -> tuple: - # 1. For all edges from v to n, distance[i][j] = weight(edge(i, j)). - - # 2. distance[i][j] = min(distance[i][j], distance[i][k] + distance[k][j]). - - # 3. Step 2 is true for each pair of vertices.Repeat for k vertex in the graph. - - # 4. Whenever distance[i][j] is given a new minimum value, +def floyd_warshall(graph:list[list[float]], vertex:int) -> tuple: + #1. For all edges from v to n, distance[i][j] = weight(edge(i, j)). + + #2. distance[i][j] = min(distance[i][j], distance[i][k] + distance[k][j]). + + #3. Step 2 is true for each pair of vertices.Repeat for k vertex in the graph. + + #4. Whenever distance[i][j] is given a new minimum value, # next vertex[i][j] = next vertex[i][k]. - + """ :param graph: 2D array calculated from weight[edge[i, j]] - + :param v: number of vertices - + :return: shortest distance between all vertex pairs - + distance[u][v] will contain the shortest distance from vertex u to v. - + # doctests: - + >>> graph = [[0, 3, float('inf')], [2, 0, float('inf')],[float('inf'), 7, 0]] >>> floyd_warshall(graph, 3)[0] [[0, 3, inf], [2, 0, inf], [9, 7, 0]] - + >>> graph = [[0, 1, 4],[float('inf'), 0, 2],[float('inf'), float('inf'), 0]] >>> floyd_warshall(graph, 3)[0] [[0, 1, 3], [inf, 0, 2], [inf, inf, 0]] - + >>> graph = [[0, 0, 0],[0, 0, 0],[0, 0, 0]] >>> floyd_warshall(graph, 3)[0] [[0, 0, 0], [0, 0, 0], [0, 0, 0]] - + #Graph with all edge weights = infinity - + >>> graph = [[float('inf'), float('inf'), float('inf')], ... [float('inf'), float('inf'), float('inf')], ... [float('inf'), float('inf'), float('inf')]] >>> floyd_warshall(graph, 3)[0] [[inf, inf, inf], [inf, inf, inf], [inf, inf, inf]] - - + + #Handling negetive weighted graph: - + >>> graph = [[0, -2, float('inf')],[float('inf'), 0, 3],[4, float('inf'), 0]] >>> floyd_warshall(graph, 3)[0] [[0, -2, 1], [7, 0, 3], [4, 2, 0]] - - + + #Handling negetive weighted cycle: - + >>> graph = [[0, -1, float('inf')],[float('inf'), 0, -2],[-3, float('inf'), 0]] >>> floyd_warshall(graph, 3)[0] [[-6, -7, -9], [-5, -6, -8], [-9, -10, -12]] - - - #Number of vertex in function arguement should match number of vertex in graph: - + + + #Number of vertex in function argument should match number of vertex in graph: + >>> graph = [[0, -1, float('inf')],[float('inf'), 0, -2]] >>> floyd_warshall(graph, 3)[0] Traceback (most recent call last): ... IndexError: list index out of range - - + + #Graph data type should be a 2D list: - + >>> graph = "strings not allowed" >>> floyd_warshall(graph, 3)[0] Traceback (most recent call last): @@ -88,24 +87,22 @@ def floyd_warshall(graph: list[list[float]], vertex: int) -> tuple: for j in range(vertex): dist[i][j] = graph[i][j] # check vertex k against all other vertices (i, j) - + for k in range(vertex): # looping through rows of graph array - + for i in range(vertex): # looping through columns of graph array - + for j in range(vertex): if ( dist[i][k] != float("inf") and dist[k][j] != float("inf") and dist[i][k] + dist[k][j] < dist[i][j] - ): + ): dist[i][j] = dist[i][k] + dist[k][j] return dist, vertex - -if __name__ == "__main__": +if __name__== "__main__": import doctest - doctest.testmod() From 989d84ed1ef8a69964e8377f9ac3da91ac46fe00 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 15 Dec 2024 13:45:33 +0000 Subject: [PATCH 4/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- graphs/graphs_floyd_warshall.py | 75 +++++++++++++++++---------------- 1 file changed, 39 insertions(+), 36 deletions(-) diff --git a/graphs/graphs_floyd_warshall.py b/graphs/graphs_floyd_warshall.py index 09389e9f9772..9407fcdfa56a 100644 --- a/graphs/graphs_floyd_warshall.py +++ b/graphs/graphs_floyd_warshall.py @@ -1,79 +1,80 @@ # floyd_warshall.py """ -The problem is to find and return the shortest distance between all pairs of vertices +The problem is to find and return the shortest distance between all pairs of vertices in a weighted directed graph that can have negative edge weights. https://docs.python.org/3/library/doctest.html """ -def floyd_warshall(graph:list[list[float]], vertex:int) -> tuple: - #1. For all edges from v to n, distance[i][j] = weight(edge(i, j)). - - #2. distance[i][j] = min(distance[i][j], distance[i][k] + distance[k][j]). - - #3. Step 2 is true for each pair of vertices.Repeat for k vertex in the graph. - - #4. Whenever distance[i][j] is given a new minimum value, + +def floyd_warshall(graph: list[list[float]], vertex: int) -> tuple: + # 1. For all edges from v to n, distance[i][j] = weight(edge(i, j)). + + # 2. distance[i][j] = min(distance[i][j], distance[i][k] + distance[k][j]). + + # 3. Step 2 is true for each pair of vertices.Repeat for k vertex in the graph. + + # 4. Whenever distance[i][j] is given a new minimum value, # next vertex[i][j] = next vertex[i][k]. - + """ :param graph: 2D array calculated from weight[edge[i, j]] - + :param v: number of vertices - + :return: shortest distance between all vertex pairs - + distance[u][v] will contain the shortest distance from vertex u to v. - + # doctests: - + >>> graph = [[0, 3, float('inf')], [2, 0, float('inf')],[float('inf'), 7, 0]] >>> floyd_warshall(graph, 3)[0] [[0, 3, inf], [2, 0, inf], [9, 7, 0]] - + >>> graph = [[0, 1, 4],[float('inf'), 0, 2],[float('inf'), float('inf'), 0]] >>> floyd_warshall(graph, 3)[0] [[0, 1, 3], [inf, 0, 2], [inf, inf, 0]] - + >>> graph = [[0, 0, 0],[0, 0, 0],[0, 0, 0]] >>> floyd_warshall(graph, 3)[0] [[0, 0, 0], [0, 0, 0], [0, 0, 0]] - + #Graph with all edge weights = infinity - + >>> graph = [[float('inf'), float('inf'), float('inf')], ... [float('inf'), float('inf'), float('inf')], ... [float('inf'), float('inf'), float('inf')]] >>> floyd_warshall(graph, 3)[0] [[inf, inf, inf], [inf, inf, inf], [inf, inf, inf]] - - + + #Handling negetive weighted graph: - + >>> graph = [[0, -2, float('inf')],[float('inf'), 0, 3],[4, float('inf'), 0]] >>> floyd_warshall(graph, 3)[0] [[0, -2, 1], [7, 0, 3], [4, 2, 0]] - - + + #Handling negetive weighted cycle: - + >>> graph = [[0, -1, float('inf')],[float('inf'), 0, -2],[-3, float('inf'), 0]] >>> floyd_warshall(graph, 3)[0] [[-6, -7, -9], [-5, -6, -8], [-9, -10, -12]] - - + + #Number of vertex in function argument should match number of vertex in graph: - + >>> graph = [[0, -1, float('inf')],[float('inf'), 0, -2]] >>> floyd_warshall(graph, 3)[0] Traceback (most recent call last): ... IndexError: list index out of range - - + + #Graph data type should be a 2D list: - + >>> graph = "strings not allowed" >>> floyd_warshall(graph, 3)[0] Traceback (most recent call last): @@ -87,22 +88,24 @@ def floyd_warshall(graph:list[list[float]], vertex:int) -> tuple: for j in range(vertex): dist[i][j] = graph[i][j] # check vertex k against all other vertices (i, j) - + for k in range(vertex): # looping through rows of graph array - + for i in range(vertex): # looping through columns of graph array - + for j in range(vertex): if ( dist[i][k] != float("inf") and dist[k][j] != float("inf") and dist[i][k] + dist[k][j] < dist[i][j] - ): + ): dist[i][j] = dist[i][k] + dist[k][j] return dist, vertex -if __name__== "__main__": + +if __name__ == "__main__": import doctest + doctest.testmod() From d4bd7d7ced3721ff00af5ad553d4f3df7682896d Mon Sep 17 00:00:00 2001 From: asmitannu <143717144+asmitannu@users.noreply.github.com> Date: Thu, 19 Dec 2024 19:14:39 +0400 Subject: [PATCH 5/8] Update graphs/graphs_floyd_warshall.py Co-authored-by: Christian Clauss --- graphs/graphs_floyd_warshall.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/graphs/graphs_floyd_warshall.py b/graphs/graphs_floyd_warshall.py index 9407fcdfa56a..1760a9c2efe8 100644 --- a/graphs/graphs_floyd_warshall.py +++ b/graphs/graphs_floyd_warshall.py @@ -2,9 +2,6 @@ """ The problem is to find and return the shortest distance between all pairs of vertices in a weighted directed graph that can have negative edge weights. - -https://docs.python.org/3/library/doctest.html - """ From f3d60125bf3334ab244ba68e81479502b773a0e1 Mon Sep 17 00:00:00 2001 From: asmitannu <143717144+asmitannu@users.noreply.github.com> Date: Thu, 19 Dec 2024 19:36:37 +0400 Subject: [PATCH 6/8] Update graphs_floyd_warshall.py --- graphs/graphs_floyd_warshall.py | 48 ++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/graphs/graphs_floyd_warshall.py b/graphs/graphs_floyd_warshall.py index 1760a9c2efe8..75a7a3b39f35 100644 --- a/graphs/graphs_floyd_warshall.py +++ b/graphs/graphs_floyd_warshall.py @@ -100,9 +100,51 @@ def floyd_warshall(graph: list[list[float]], vertex: int) -> tuple: ): dist[i][j] = dist[i][k] + dist[k][j] return dist, vertex - - + if __name__ == "__main__": import doctest - doctest.testmod() + + v = int(input("Enter number of vertices: ")) + e = int(input("Enter number of edges: ")) + + graph = [[float("inf") for i in range(v)] for j in range(v)] + + for i in range(v): + graph[i][i] = 0.0 + + # src and dst are indices that must be within the array size graph[e][v] + # failure to follow this will result in an error + for i in range(e): + print("\nEdge ", i + 1) + src = int(input("Enter source:")) + dst = int(input("Enter destination:")) + weight = float(input("Enter weight:")) + graph[src][dst] = weight + + floyd_warshall(graph, v) + + # Example Input + # Enter number of vertices: 3 + # Enter number of edges: 2 + + # # generated graph from vertex and edge inputs + # [[inf, inf, inf], [inf, inf, inf], [inf, inf, inf]] + # [[0.0, inf, inf], [inf, 0.0, inf], [inf, inf, 0.0]] + + # specify source, destination and weight for edge #1 + # Edge 1 + # Enter source:1 + # Enter destination:2 + # Enter weight:2 + + # specify source, destination and weight for edge #2 + # Edge 2 + # Enter source:2 + # Enter destination:1 + # Enter weight:1 + + # # Expected Output from the vertice, edge and src, dst, weight inputs!! + # 0 INF INF + # INF 0 2 + # INF 1 0 From f48d6c852ab2013a6c02009c62b9e280292187c6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 19 Dec 2024 15:38:13 +0000 Subject: [PATCH 7/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- graphs/graphs_floyd_warshall.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/graphs/graphs_floyd_warshall.py b/graphs/graphs_floyd_warshall.py index 75a7a3b39f35..6a210692ae5b 100644 --- a/graphs/graphs_floyd_warshall.py +++ b/graphs/graphs_floyd_warshall.py @@ -100,11 +100,13 @@ def floyd_warshall(graph: list[list[float]], vertex: int) -> tuple: ): dist[i][j] = dist[i][k] + dist[k][j] return dist, vertex - + + if __name__ == "__main__": import doctest + doctest.testmod() - + v = int(input("Enter number of vertices: ")) e = int(input("Enter number of edges: ")) From f1e09cd1af3a19ae883ad39a26d07b74d189d351 Mon Sep 17 00:00:00 2001 From: asmitannu <143717144+asmitannu@users.noreply.github.com> Date: Wed, 8 Jan 2025 21:59:49 +0530 Subject: [PATCH 8/8] Update graphs/graphs_floyd_warshall.py Co-authored-by: Tianyi Zheng --- graphs/graphs_floyd_warshall.py | 1 - 1 file changed, 1 deletion(-) diff --git a/graphs/graphs_floyd_warshall.py b/graphs/graphs_floyd_warshall.py index 6a210692ae5b..31674511f638 100644 --- a/graphs/graphs_floyd_warshall.py +++ b/graphs/graphs_floyd_warshall.py @@ -1,4 +1,3 @@ -# floyd_warshall.py """ The problem is to find and return the shortest distance between all pairs of vertices in a weighted directed graph that can have negative edge weights.