-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDijkstras_Algorithm.py
More file actions
57 lines (42 loc) · 1.37 KB
/
Copy pathDijkstras_Algorithm.py
File metadata and controls
57 lines (42 loc) · 1.37 KB
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
import heapq
import random
#from ER_graph_simulation import graph_vertices
#from ER_graph_simulation import n
def dijkstra(graph, graph_vertices, n):
distances = [float('inf')] * n
predecessors = [None] * n
distances[0] = 0
visited = [False] * n
for _ in range(n):
min_distance = float('inf')
u = None
for i in range(n):
if not visited[i] and distances[i] < min_distance:
min_distance = distances[i]
u = i
if u is None:
break
visited[u] = True
for v in range(n):
if graph[u][v] != 0 and not visited[v]:
alt = distances[u] + graph[u][v]
if alt < distances[v]:
if u == 'None':
print("aaaaaaaaa")
distances[v] = alt
predecessors[v] = u
return distances, predecessors
def get_path(graph, predecessors, start_vertex, end_vertex):
path = []
current = end_vertex
while current != start_vertex:
path.insert(0, current)
current = predecessors[current]
if current == None:
return [], []
weights = []
for i in range(len(path) - 1):
u = path[i]
v = path[i + 1]
weights.append(graph[u][v])
return path, weights