ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 13.06.2025
Просмотров: 4196
Скачиваний: 0
Dijkstra’s Algorithm
Algorithm Start “ready set” with start node. In loop select node with shortest distance in every step, then compute distances to all of its neighbors and store path predecessors. Add current node to “ready set”; loop finishes when all nodes are included.
1. Init
Set start distance to 0, dist[s]=0, others to infinite: dist[i]=f (for izs), Set Ready = { } .
2. Loop until all nodes are in Ready
Select node n with shortest known distance that is not in Ready set Ready = Ready + {n} .
FOR each neighbor node m of n
IF dist[n]+edge(n,m) < dist[m] /* shorter path found */ THEN { dist[m] = dist[n]+edge(n,m);
pre[m] = n;
}
DE
f |
f |
|||||||||
From s to: |
S |
a |
b |
c |
d |
|||||
Distance |
0 |
f |
f |
f |
f |
|||||
6 |
||||||||||
Predecessor |
- |
- |
- |
- |
- |
|||||
Step 0: Init list, no predecessors |
||||||||||
f |
f |
|||||||||
Ready = {} |
||||||||||
F |
G |
|||||||||
DE
10 |
f |
||||||||
S |
a |
b |
c |
d |
|||||
From s to: |
|||||||||
Distance |
0 |
10 |
f |
5 |
9 |
||||
Predecessor |
- |
s |
- |
s |
s |
||||
6 |
|||||||||
Step 1: |
Closest node is s, add to Ready |
||||||
Update distances and pred. to all neighbors of s |
|||||||
5 |
9 |
||||||
Ready = {S} |
|||||||
F |
G |
||||||
Figure 14.8: Dijkstra’s algorithm step 0 and 1
207
14 Localization and Navigation
DE
8 |
14 |
S |
a |
b |
c |
d |
|||||
From s to: |
|||||||||||
Distance |
0 |
10 8 |
14 |
5 |
9 |
7 |
|||||
Predecessor |
- |
s |
c |
c |
s |
s |
c |
||||
6 |
|||||||||||
Step 2: Next closest node is c, add to Ready |
|||||
Update distances and pred. for a and d |
|||||
5 |
7 |
||||
Ready = {S, c} |
|||||
F |
G |
||||
DE
8 13
From s to: |
S |
a |
b |
c |
d |
||||
Distance |
0 |
8 |
14 13 |
5 |
7 |
||||
6 |
Predecessor |
- |
c |
c |
d |
s |
c |
||
Step 3: |
Next closest node is d, add to Ready |
||||||
Update distance and pred. for b |
|||||||
5 |
7 |
||||||
Ready = {s, c, d} |
|||||||
F |
G |
||||||
DE
8 |
9 |
|||||||||
S |
a |
b |
c |
d |
||||||
From s to: |
||||||||||
Distance |
0 |
8 |
13 9 |
5 |
7 |
|||||
6 |
Predecessor |
- |
c |
d |
a |
s |
c |
|||
Step 4: |
Next closest node is a, add to Ready |
||||||
Update distance and pred. for b |
|||||||
5 |
7 |
||||||
Ready = {S, a, c, d} |
|||||||
F |
G |
||||||
DE
8 |
9 |
||||||||
S |
a |
b |
c |
d |
|||||
From s to: |
|||||||||
Distance |
0 |
8 |
9 |
5 |
7 |
||||
6 |
Predecessor |
- |
c |
a |
s |
c |
|||
Step 5: Closest node is b, add to Ready |
|||||
5 |
7 |
check all neighbors of s |
|||
Ready = {S, a, b, c, d} complete! |
|||||
F |
G |
||||
Figure 14.9: Dijkstra’s algorithm steps 2-5
208
Dijkstra’s Algorithm
Example Consider the nodes and distances in Figure 14.8. On the left hand side is the distance graph, on the right-hand side is the table with the shortest distances found so far and the immediate path predecessors that lead to this distance.
In the beginning (initialization step), we only know that start node S is reachable with distance 0 (by definition). The distances to all other nodes are infinite and we do not have a path predecessor recorded yet. Proceeding from step 0 to step 1, we have to select the node with the shortest distance from all nodes that are not yet included in the Ready set. Since Ready is still empty, we have to look at all nodes. Clearly S has the shortest distance (0), while all other nodes still have an infinite distance.
For step 1, Figure 14.8 bottom, S is now included into the Ready set and the distances and path predecessors (equal to S) for all its neighbors are being updated. Since S is neighbor to nodes a, c, and d, the distances for these three nodes are being updated and their path predecessor is being set to S.
When moving to step 2, we have to select the node with the shortest path among a, b, c, d, as S is already in the Ready set. Among these, node c has the shortest path (5). The table is updated for all neighbors of c, which are S, a, b, and d. As shown in Figure 14.9, new shorter distances are found for a, b, and d, each entering c as their immediate path predecessor.
In the following steps 3 through 5, the algorithm’s loop is repeated, until finally, all nodes are included in the Ready set and the algorithm terminates. The table now contains the shortest path from the start node to each of the other nodes, as well as the path predecessor for each node, allowing us to reconstruct the shortest path.
D |
E |
S |
a |
b |
c |
d |
|||
8 |
9 |
From s to: |
|||||||
Distance |
0 |
8 |
9 |
5 |
7 |
||||
Predecessor |
- |
c |
a |
s |
c |
||||
Example: Find shortest path S o b |
|||||||||
6 |
|||||||||
dist[b] = 9 |
|||||||||
pre[b] = a |
|||||||||
pre[a] = c |
|||||||||
5 |
7 |
pre[c] = S |
|||
F |
G |
Shortest path: S o c o a o b, length is 9 |
|||
Figure 14.10: Determine shortest path
Figure 14.10 shows how to construct the shortest path from each node’s predecessor. For finding the shortest path between S and b, we already know the shortest distance (9), but we have to reconstruct the shortest path backwards from b, by following the predecessors:
pre[b]=a, pre[a]=c, pre[c]=S
Therefore, the shortest path is: S o c o a o b
209