Computes the shortest paths between all pairs of nodes using the Floyd-Warshall algorithm. >>> g = Graph(3) >>> g.add_edge(0, 1, 1) >>> g.add_edge(1, 2, 2) >>> g.floyd_warshall() >>> g.show_min(0, 2) 3 >>> g.show_min
(self)
| 24 | self.dp[u][v] = w |
| 25 | |
| 26 | def floyd_warshall(self): |
| 27 | """ |
| 28 | Computes the shortest paths between all pairs of |
| 29 | nodes using the Floyd-Warshall algorithm. |
| 30 | |
| 31 | >>> g = Graph(3) |
| 32 | >>> g.add_edge(0, 1, 1) |
| 33 | >>> g.add_edge(1, 2, 2) |
| 34 | >>> g.floyd_warshall() |
| 35 | >>> g.show_min(0, 2) |
| 36 | 3 |
| 37 | >>> g.show_min(2, 0) |
| 38 | inf |
| 39 | """ |
| 40 | for k in range(self.n): |
| 41 | for i in range(self.n): |
| 42 | for j in range(self.n): |
| 43 | self.dp[i][j] = min(self.dp[i][j], self.dp[i][k] + self.dp[k][j]) |
| 44 | |
| 45 | def show_min(self, u, v): |
| 46 | """ |