3
3
4
4
DIJKSTRA(graph G, start vertex s, destination vertex d):
5
5
6
- //all nodes initially unexplored
7
-
8
- 1 - let H = min heap data structure, initialized with 0 and s [here 0 indicates
9
- the distance from start vertex s]
10
- 2 - while H is non-empty:
11
- 3 - remove the first node and cost of H, call it U and cost
12
- 4 - if U has been previously explored:
13
- 5 - go to the while loop, line 2 //Once a node is explored there is no need
14
- to make it again
15
- 6 - mark U as explored
16
- 7 - if U is d:
17
- 8 - return cost // total cost from start to destination vertex
18
- 9 - for each edge(U, V): c=cost of edge(U,V) // for V in graph[U]
19
- 10 - if V explored:
20
- 11 - go to next V in line 9
21
- 12 - total_cost = cost + c
22
- 13 - add (total_cost,V) to H
6
+ // all nodes initially unexplored
7
+
8
+ 1 - let H = min heap data structure, initialized with (0, s)
9
+ // 0 is the distance from start vertex s
10
+ 2 - let costs = dictionary to store minimum costs to reach each node,
11
+ initialized with {s: 0}
12
+ 3 - while H is non-empty:
13
+ 4 - remove the first node and cost from H, call them U and cost
14
+ 5 - if U has been previously explored:
15
+ 6 - continue // skip further processing and go back to while loop, line 3
16
+ 7 - mark U as explored
17
+ 8 - if U is d:
18
+ 9 - return cost // total cost from start to destination vertex
19
+ 10 - for each neighbor V and edge cost c of U in G:
20
+ 11 - if V has been previously explored:
21
+ 12 - continue to next neighbor V in line 10
22
+ 13 - total_cost = cost + c
23
+ 14 - if total_cost is less than costs.get(V, ∞):
24
+ 15 - update costs[V] to total_cost
25
+ 16 - add (total_cost, V) to H
26
+
27
+ // At the end, if destination d is not reachable, return -1
23
28
24
29
You can think at cost as a distance where Dijkstra finds the shortest distance
25
30
between vertices s and v in a graph G. The use of a min heap as H guarantees
34
39
import heapq
35
40
36
41
37
- def dijkstra (graph , start , end ) :
42
+ def dijkstra (graph : dict [ str , list [ tuple [ str , int ]]], start : str , end : str ) -> int :
38
43
"""Return the cost of the shortest path between vertices start and end.
39
44
40
45
>>> dijkstra(G, "E", "C")
@@ -44,31 +49,37 @@ def dijkstra(graph, start, end):
44
49
>>> dijkstra(G3, "E", "F")
45
50
3
46
51
"""
52
+ heap : list [tuple [int , str ]] = [(0 , start )] # (cost, node)
53
+ visited : set [str ] = set ()
54
+ costs : dict [str , int ] = {start : 0 } # Store minimum costs to reach each node
47
55
48
- heap = [(0 , start )] # cost from start node,end node
49
- visited = set ()
50
56
while heap :
51
- ( cost , u ) = heapq .heappop (heap )
57
+ cost , u = heapq .heappop (heap )
52
58
if u in visited :
53
59
continue
54
60
visited .add (u )
55
61
if u == end :
56
62
return cost
63
+
57
64
for v , c in graph [u ]:
58
65
if v in visited :
59
66
continue
60
- next_item = cost + c
61
- heapq .heappush (heap , (next_item , v ))
67
+ next_cost = cost + c
68
+ # Only push to heap if a cheaper path is found
69
+ if next_cost < costs .get (v , float ("inf" )):
70
+ costs [v ] = next_cost
71
+ heapq .heappush (heap , (next_cost , v ))
72
+
62
73
return - 1
63
74
64
75
65
76
G = {
66
- "A" : [[ "B" , 2 ], [ "C" , 5 ] ],
67
- "B" : [[ "A" , 2 ], [ "D" , 3 ], [ "E" , 1 ], [ "F" , 1 ] ],
68
- "C" : [[ "A" , 5 ], [ "F" , 3 ] ],
69
- "D" : [[ "B" , 3 ] ],
70
- "E" : [[ "B" , 4 ], [ "F" , 3 ] ],
71
- "F" : [[ "C" , 3 ], [ "E" , 3 ] ],
77
+ "A" : [( "B" , 2 ), ( "C" , 5 ) ],
78
+ "B" : [( "A" , 2 ), ( "D" , 3 ), ( "E" , 1 ), ( "F" , 1 ) ],
79
+ "C" : [( "A" , 5 ), ( "F" , 3 ) ],
80
+ "D" : [( "B" , 3 ) ],
81
+ "E" : [( "B" , 4 ), ( "F" , 3 ) ],
82
+ "F" : [( "C" , 3 ), ( "E" , 3 ) ],
72
83
}
73
84
74
85
r"""
@@ -80,10 +91,10 @@ def dijkstra(graph, start, end):
80
91
----------------- 3 --------------------
81
92
"""
82
93
G2 = {
83
- "B" : [[ "C" , 1 ] ],
84
- "C" : [[ "D" , 1 ] ],
85
- "D" : [[ "F" , 1 ] ],
86
- "E" : [[ "B" , 1 ], [ "F" , 3 ] ],
94
+ "B" : [( "C" , 1 ) ],
95
+ "C" : [( "D" , 1 ) ],
96
+ "D" : [( "F" , 1 ) ],
97
+ "E" : [( "B" , 1 ), ( "F" , 3 ) ],
87
98
"F" : [],
88
99
}
89
100
@@ -96,12 +107,12 @@ def dijkstra(graph, start, end):
96
107
-------- 2 ---------> G ------- 1 ------
97
108
"""
98
109
G3 = {
99
- "B" : [[ "C" , 1 ] ],
100
- "C" : [[ "D" , 1 ] ],
101
- "D" : [[ "F" , 1 ] ],
102
- "E" : [[ "B" , 1 ], [ "G" , 2 ] ],
110
+ "B" : [( "C" , 1 ) ],
111
+ "C" : [( "D" , 1 ) ],
112
+ "D" : [( "F" , 1 ) ],
113
+ "E" : [( "B" , 1 ), ( "G" , 2 ) ],
103
114
"F" : [],
104
- "G" : [[ "F" , 1 ] ],
115
+ "G" : [( "F" , 1 ) ],
105
116
}
106
117
107
118
short_distance = dijkstra (G , "E" , "C" )
0 commit comments