-
Notifications
You must be signed in to change notification settings - Fork 3
/
dijkstra.py
90 lines (69 loc) · 1.89 KB
/
dijkstra.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
'''
Dijkstra's Algorithm
How to Run:
Scroll to bottom, change values for 'G' and 's', run the code in terminal
'''
import math
def dijkstra_list(dictionary,src):
n = len(dictionary)
dist=[math.inf]*n
dist[src]=0
visited=[0]*n
queue=[]
queue.append([0,src])
while(len(queue)>0):
t = queue.pop()
u = t[1]
visited[u]+=1
for v in dictionary[u]:
weight, end_vertex = v[0],v[1]
# print(u,end_vertex)
if dist[end_vertex] > dist[u]+weight:
dist[end_vertex] = dist[u]+weight
if visited[end_vertex]==0:
queue.append([weight,end_vertex])
queue = sorted(queue)
return dist
def dijkstra_mat(G,src,ln):
dist = [math.inf] * ln
dist[src] = 0
found = [0] * ln
u = src
for _ in range(ln):
minm = math.inf
for u1 in range(ln):
if dist[u1] < minm and not found[u1]:
minm = dist[u1]
u = u1
found[u]+=1
for v in range(ln):
if((not found[v] and G[u][v]) and (dist[v] > dist[u] + G[u][v])):
dist[v] = dist[u] + G[u][v]
return dist
def driver(G,s):
adjacency_list = mat_to_list(G)
output = dijkstra_list(adjacency_list,s)
print(f"\nusing adjacency list...............")
for i,j in enumerate(output):
print(f"\t\t {i} : {j}")
print("\n\n")
ln = len(G)
out = dijkstra_mat(G,s,ln)
print(f"using adjacency matrix...............")
for i,j in enumerate(output):
print(f"\t\t {i} : {j}")
print()
def mat_to_list(G):
dictionary={}
n = len(G)
for i in range(n):
t = []
for j in range(n):
if G[i][j]>0:
t.append([G[i][j],j])
dictionary[i]=t
return dictionary
if __name__ == "__main__":
G = [[0,4,2],[4,0,1],[2,1,0]]
s = 0
driver(G,s)