MCPcopy Create free account
hub / github.com/subbarayudu-j/TheAlgorithms-Python / Graph

Class Graph

dynamic_programming/floyd_warshall.py:3–20  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1import math
2
3class Graph:
4
5 def __init__(self, N = 0): # a graph with Node 0,1,...,N-1
6 self.N = N
7 self.W = [[math.inf for j in range(0,N)] for i in range(0,N)] # adjacency matrix for weight
8 self.dp = [[math.inf for j in range(0,N)] for i in range(0,N)] # dp[i][j] stores minimum distance from i to j
9
10 def addEdge(self, u, v, w):
11 self.dp[u][v] = w
12
13 def floyd_warshall(self):
14 for k in range(0,self.N):
15 for i in range(0,self.N):
16 for j in range(0,self.N):
17 self.dp[i][j] = min(self.dp[i][j], self.dp[i][k] + self.dp[k][j])
18
19 def showMin(self, u, v):
20 return self.dp[u][v]
21
22if __name__ == '__main__':
23 graph = Graph(5)

Callers 1

floyd_warshall.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected