Repository navigation
Expand file tree
/
Copy pathDijkstra.py
More file actions
61 lines (50 loc) · 2.13 KB
/
Copy pathDijkstra.py
File metadata and controls
61 lines (50 loc) · 2.13 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
58
59
60
61
def dijkstra(graph,start,end):
#initialize the distance of all vertices to infinity
distance = {vertex : float ("infinity") for vertex in graph}
#initialize the distance of the start vertex to 0
distance[start] = 0
#initialize the parent of all vertices to None
parent = {vertex : None for vertex in graph}
#store vertices with minimum distance from the start vertex
queue = []
#add the start vertex to the queue
queue.append(start)
while queue:
curr_vertex = queue.pop(0)
#for each neighbor of the current vertex
for neighbor, weight in graph[curr_vertex].items():
if distance[neighbor] > distance[curr_vertex] + weight:
distance[neighbor] = distance[curr_vertex] + weight
parent[neighbor] = curr_vertex
"""
- If the distance of a neighbor v from the start vertex is greater than the distance of current vertex + weight of the edge,
- set the distance of v to the distance of current vertex + weight of the edge.
- set the parent of v to the current vertex.
"""
queue.append(neighbor)
"""
- add the neighbor to the queue.
- The above steps are repeated until the queue becomes empty.
"""
path = []
#construct the path from the start vertex to the end vertex
while end:
path.append(end)
end = parent[end]
"""
- The above steps are repeated until the end vertex becomes None.
- The path from the start vertex to the end vertex is constructed by backtracking from the end vertex to the start vertex.
"""
path.reverse()
return distance[path[-1]], path
graph = {
'A': {'B': 4, 'C': 4},
'B': {'A': 4, 'C': 2},
'C': {'A': 4, 'B': 2, 'D': 3, 'E': 1, 'F': 6},
'D': {'C': 3, 'F': 2},
'E': {'C': 1, 'F': 3},
'F': {'C': 6, 'D': 2, 'E': 3},
}
distance, path = dijkstra(graph, 'A', 'F')
print("Shortest distance:", distance)
print("Path:", path)