From 8fef69687f8f455a03e1eb2c5c62434ee817b421 Mon Sep 17 00:00:00 2001 From: Yashcodes04 Date: Wed, 12 Mar 2025 09:14:11 +0530 Subject: [PATCH 1/3] Contributes to #9943 --- graphs/graphs_floyd_warshall.py | 57 +++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/graphs/graphs_floyd_warshall.py b/graphs/graphs_floyd_warshall.py index aaed9ac5df8b..855ba8e2692e 100644 --- a/graphs/graphs_floyd_warshall.py +++ b/graphs/graphs_floyd_warshall.py @@ -14,36 +14,57 @@ def _print_dist(dist, v): else: print("INF", end="\t") print() - - def floyd_warshall(graph, v): """ - :param graph: 2D array calculated from weight[edge[i, j]] + Computes the shortest paths in a weighted graph with positive or negative edge weights (but with no negative cycles). + + :param graph: 2D list where each element represents the weight from i to j. :type graph: List[List[float]] - :param v: number of vertices + :param v: Number of vertices in the graph. :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]. - """ + :return: A tuple containing the distances matrix and number of vertices. + + Example: + >>> v = 3 + >>> inf = float('inf') + >>> graph = [ + ... [0, 1, inf], + ... [inf, 0, 1], + ... [1, inf, 0] + ... ] + >>> dist, _ = floyd_warshall(graph, v) # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS + + The shortest path matrix using Floyd Warshall algorithm + + 0 1 2 + 2 0 1 + 1 2 0 + >>> [int(x) if x != inf else 'INF' for row in dist for x in row] + [0, 1, 2, 2, 0, 1, 1, 2, 0] + + Handling a graph with no edges: + >>> v = 3 + >>> graph = [[inf]*3 for _ in range(3)] + >>> for i in range(3): graph[i][i] = 0 + >>> dist, _ = floyd_warshall(graph, v) # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS + + The shortest path matrix using Floyd Warshall algorithm + + 0 INF INF + INF 0 INF + INF INF 0 + >>> [int(x) if x != inf else 'INF' for row in dist for x in row] + [0, 'INF', 'INF', 'INF', 0, 'INF', 'INF', 'INF', 0] + """ dist = [[float("inf") for _ in range(v)] for _ in range(v)] for i in range(v): for j in range(v): dist[i][j] = graph[i][j] - # check vertex k against all other vertices (i, j) for k in range(v): - # looping through rows of graph array for i in range(v): - # looping through columns of graph array for j in range(v): if ( dist[i][k] != float("inf") @@ -57,6 +78,8 @@ def floyd_warshall(graph, v): if __name__ == "__main__": + import doctest + doctest.testmod() v = int(input("Enter number of vertices: ")) e = int(input("Enter number of edges: ")) From 472e77e35f7e8cfd2975694ef748068cd1e9a1d3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 12 Mar 2025 03:56:15 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- graphs/graphs_floyd_warshall.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/graphs/graphs_floyd_warshall.py b/graphs/graphs_floyd_warshall.py index 855ba8e2692e..31f32cf30799 100644 --- a/graphs/graphs_floyd_warshall.py +++ b/graphs/graphs_floyd_warshall.py @@ -14,6 +14,8 @@ def _print_dist(dist, v): else: print("INF", end="\t") print() + + def floyd_warshall(graph, v): """ Computes the shortest paths in a weighted graph with positive or negative edge weights (but with no negative cycles). @@ -79,6 +81,7 @@ def floyd_warshall(graph, v): if __name__ == "__main__": import doctest + doctest.testmod() v = int(input("Enter number of vertices: ")) e = int(input("Enter number of edges: ")) From d5873ecd1c447a404c3e418853c2ffd1f942ee04 Mon Sep 17 00:00:00 2001 From: Yashcodes04 Date: Wed, 12 Mar 2025 09:37:07 +0530 Subject: [PATCH 3/3] Contributes to #9943 (updated) --- graphs/graphs_floyd_warshall.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphs/graphs_floyd_warshall.py b/graphs/graphs_floyd_warshall.py index 31f32cf30799..ffc80a6c1d32 100644 --- a/graphs/graphs_floyd_warshall.py +++ b/graphs/graphs_floyd_warshall.py @@ -18,7 +18,7 @@ def _print_dist(dist, v): def floyd_warshall(graph, v): """ - Computes the shortest paths in a weighted graph with positive or negative edge weights (but with no negative cycles). + Computes the shortest paths in a weighted graph. :param graph: 2D list where each element represents the weight from i to j. :type graph: List[List[float]]