DS 8 - Dijkstra's & A - Worksheet KEY
DS 8 - Dijkstra's & A - Worksheet KEY
Name:___________________
Find the shortest distance from ‘S’ to ‘E’, using Dijkstra's Algorithm.
1. Fill in 0 for the distance of the location that stores ‘S’ and set linked from to null
2. Find the location with the smallest distance that is not marked as complete (referred to
as current)
3. If current is the position with ‘E’ stop, otherwise is not marked complete go to step 3
4. For each neighboring location (referred to as neighbor)
a. Let distanceToNeighbor be current.distance + neighbor.terrianCost
b. If neighbor.distance is not set or (distanceToNeighbor < neighbor.distance) go
to step c, otherwise continue to next neighbor
c. Set neighbor.distance to distanceToNeighbor
d. Set Linked from to current
5. Set current.state to complete.
TerrainCosts: (S-0), (E-0), (G-1), (F-2), (H-3), (W-7), (M-12)
M1 H2 H3 S4 Fs
(0,0) ∞
it is one
(0,1) ∞
3
(0,2) ∞
complete
4
(0,3) ∞
0 complete
2
(0,4) ∞
5
0,3 Cotz complete
on6 (1,0)
1,1
∞
17 0 17 FOUND ET
(1,1) ∞
PM7 complete
am
is complete
(1,2) ∞
(1,3) ∞
a complete
(1,4) ∞
10 0,4 2 1 3 complete
(2,0)
2 1
∞
27 19
2,2 5 7 complete
(2,1) ∞
12 12
13 (2,2) ∞
complete
a (2,3)
3 it
∞
I complete
is (2,4)
1,4
∞
32 S complete
Name:___________________
Find the shortest distance from ‘S’ to ‘E’, using Dijkstra's Algorithm.
1. Fill in 0 for the distance of the location that stores ‘S’ and set linked from to null
2. Find the location with the smallest distance that is not marked as complete (referred to
as current)
3. If current is the position with ‘E’ stop, otherwise is not marked complete go to step 3
4. For each neighboring location (referred to as neighbor)
a. Let distanceToNeighbor be current.distance + neighbor.terrianCost
b. If neighbor.distance is not set or (distanceToNeighbor < neighbor.distance) go
to step c, otherwise continue to next neighbor
c. Set neighbor.distance to distanceToNeighbor
d. Set Linked from to current
5. Set current.state to complete.
TerrainCosts: (S-0), (E-0), (G-1), (F-2), (H-3), (W-7), (M-12)
F1 H2 M3 F4 E5
1 (0,0)
1,0
∞
22 4 complete
2
(0,1)
1,1
∞
0 3 3 complete
(0,2) ∞
3 011 3 12 15
4
(0,3) ∞
1,3 4 2 6
885
(0,4)
1,4
∞
Sto S FOUND ET
(1,0) ∞
0 2 complete
7
(1,1) huh ∞
complete
8
(1,2)
1,1
∞
0 3 3 complete
9 (1,3)
1,2
∞
3 1 4 complete
Nolo (1,4) ∞
complete
(2,0) ∞
complete
12
(2,1) ∞
complete
13
(2,2) ∞
2,1 1 12 13
4 12
(2,3)
1,3
∞
16
(2,4) ∞
1,4 Str 17
Name:___________________
Find the shortest distance from ‘A’ to ‘C’, using Dijkstra's Algorithm.
1. Fill in 0 for the distance of node ‘A’ and set linked from to null
2. Find the location with the smallest distance that is not marked as complete (referred to
as current)
3. If current is the node with ‘C’ stop, otherwise is not marked complete go to step 3
4. For each neighboring location (referred to as neighbor)
a. Let distanceToNeighbor be current.distance + edgeCost from
current->neighbor
b. If neighbor.distance is not set or (distanceToNeighbor < neighbor.distance) go
to step c, otherwise continue to next neighbor
c. Set neighbor.distance to distanceToNeighbor
d. Set Linked from to current
5. Set current.state to complete.
A
null ∞
0 complete
B
A ∞
0 7 7
C ∞
1 2 3 FOUND C
D
E ∞
17 8
E
A ∞
0 1 I complete
Name:___________________
Find the shortest distance from ‘H’ to ‘F’, using Dijkstra's Algorithm.
1. Fill in 0 for the distance of node ‘H’ and set linked from to null
2. Find the location with the smallest distance that is not marked as complete (referred to
as current)
3. If current is the node with ‘F’ stop, otherwise is not marked complete go to step 3
4. For each neighboring location (referred to as neighbor)
a. Let distanceToNeighbor be current.distance + edgeCost from
current->neighbor
b. If neighbor.distance is not set or (distanceToNeighbor < neighbor.distance) go
to step c, otherwise go to step 5
c. Set neighbor.distance to distanceToNeighbor
d. Set Linked from to current
5. Set current.state to complete.
To
A ∞ a
B ∞
mn C 14 5
To
C ∞
m H 01 1 complete
E 34
D ∞
m
7
at E ∞
M C 12 3 complete
FOUNDFT
F ∞
G ∞
a É 9184 I complete
H ∞
an null 0 complete
Name:___________________
(0,0)
it I
compete
(0,1)
(0,2)
3 complete
a (0,3)
complete
5
(0,4)
0,3 7 10 2 2 5 complete
(1,0)
UND E
shotta
11 1
(1,1)
8 (1,2) of complete
a (1,3)
complete
(1,4)
7 21 3 4 complete
(2,0)
1,3
2 1 20 127 19 1
12 (2,1)
complete
1 323 9 off's complete
(2,2)
is
(2,3)
complete
9 3 2 complete
(2,4)
1,4 S S
Name:___________________
(0,0)
(0,1)
GI 3 3 complete
(0,2)
0,1 14 1012 12 2
4
(0,3)
1,3 8 217 1
(0,4)
FOND
i Iii shake
(1,0)
(1,1)
complete
8 (1,2)
1,1 7 13 4 3 complete
9 (1,3)
1,2 7 41 5 2 complete
10 (1,4)
7 5 1 6 1 complete
(2,0)
1,3
(2,1)
12
13
14
(2,2)
(2,3)
20
20
iii
5 12 3
1,3 17
is (2,4)
1,4 20 6 12 18 2
Name:___________________
A
B 6 0 474 2
B null 3 0 3 complete
C
B 0 22 3
B 03 2
D
5
B 4 03
E
1 complete
7 4 275 2
F
shortest
G
H
E 4 3 174 0 FOUND G path