-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdjikstra_matrix.py
58 lines (47 loc) · 1.36 KB
/
djikstra_matrix.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
import heapq
def djikstra(G, s, t):
n = len(G)
d = [float('inf') for v in range(n)]
visited = [False for v in range(n)]
parent = [None for v in range(n)]
heap = []
heapq.heappush(heap, (0, s))
d[s] = 0
visited[s] = True
while heap:
w_u, u = heapq.heappop(heap)
for i in range(n):
if G[u][i] != -1 and not visited[i]:
if d[i] > d[u] + G[u][i]:
d[i] = d[u] + G[u][i]
heapq.heappush(heap, (d[i], i))
return d
def djikstra_better(G, s):
n = len(G)
parent = [False for _ in range(n)]
dist = [float('inf') for _ in range(n)]
visited = [False for _ in range(n)]
dist[s] = 0
while True:
min_dist = float('inf')
min_u = -1
for u in range(n):
if not visited[u] and dist[u] < min_dist:
min_u = u
min_dist = dist[u]
if min_u == -1:
break
visited[min_u] = True
u = min_u
for v in range(n):
if not visited[v] and G[u][v] != -1 and dist[v] > dist[u] + G[u][v]:
dist[v] = dist[u] + G[u][v]
parent[v] = u
return dist
G = [[-1, 1, 5, -1, -1],
[1, -1, 2, 8, 7],
[5, 2, -1, 3, -1],
[-1, 8, 3, -1, 1],
[-1, 7, -1, 1, -1]]
print(djikstra(G, 0, 1))
print(djikstra_better(G, 0))