-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dijkstra-Algorithm.py
64 lines (51 loc) · 1.91 KB
/
Dijkstra-Algorithm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class Graph:
def print_result(self, distance):
print("\nVertex\t\tDistance")
for i in range(len(distance)):
print(f"{i}\t\t{distance[i]}")
def vertex_with_min_distance(self, dist, vis):
min_dist = float("Inf")
for i in range(len(dist)):
if not vis[i] and dist[i] < min_dist:
min_dist = dist[i]
min_ver = i
return min_ver
def dijkstra(self, cost_mat, src):
V = len(cost_mat)
visited = [False]*V
distance = [float("Inf")]*V
distance[src] = 0
# Calculating shortest possible distances for other vertices
for _ in range(V):
w = self.vertex_with_min_distance(distance, visited)
visited[w] = True
for v in range(V):
if not visited[v]:
if cost_mat[w][v] > 0 and distance[v] > distance[w]+cost_mat[w][v]:
distance[v] = distance[w]+cost_mat[w][v]
self.print_result(distance)
def input_matrix(n):
mat = [0]*n
for i in range(n):
row = list(map(int, (input().split())))
mat[i] = row
return mat
if __name__ == "__main__":
my_graph = Graph()
g = [[0, 4, 0, 0, 0, 0, 0, 8, 0],
[4, 0, 8, 0, 0, 0, 0, 11, 0],
[0, 8, 0, 7, 0, 4, 0, 0, 2],
[0, 0, 7, 0, 9, 14, 0, 0, 0],
[0, 0, 0, 9, 0, 10, 0, 0, 0],
[0, 0, 4, 14, 10, 0, 2, 0, 0],
[0, 0, 0, 0, 0, 2, 0, 1, 6],
[8, 11, 0, 0, 0, 0, 1, 0, 7],
[0, 0, 2, 0, 0, 0, 6, 7, 0]
]
n = int(input("Enter number of vertices : "))
print("\nEnter your cost matrix (row-wise)")
print("\nNOTE :- If there is no edge between vertices insert 0\n\n")
cost_matrix = input_matrix(n)
src = int(input("Enter your source vertices : (integer like 0,1,2,...) : "))
my_graph.dijkstra(cost_matrix, src)
# my_graph.dijkstra(g,0) example