0% found this document useful (0 votes)
17 views8 pages

DS 8 - Dijkstra's & A - Worksheet KEY

Uploaded by

newhrishi06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views8 pages

DS 8 - Dijkstra's & A - Worksheet KEY

Uploaded by

newhrishi06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Mrs B

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

E M H F G up when there is a tie for distance


W W G F F
I processed the first one in
the chart
Results Tables:
Location (row, column) Linked From Distance Status

(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

F S H G G when there is a tie for distance


951 I processed the first one in
F G M M M
Results Tables: the chart
Location (row, column) Linked From Distance Status

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.

Node Linked From Distance Status

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

Node Linked From Distance Status

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:___________________

Find the shortest distance from ‘S’ to ‘E’, using A*.


1. Fill in f=4, g=0, h=4 for the location that stores ‘S’ and set linked from to null
2. Find the location with the smallest h that is not marked as complete (referred to as
current)
3. For each neighboring location (referred to as neighbor)
a. If neighbor.g is not set or neighbor.g > (current.g + neighbor.terrainCost) go
to step b, otherwise continue to next neighbor
b. Set Linked from to current
c. Set neighbor.g to (current.f + neighbor.terrainCost)
d. Set neighbor.h to (abs(row difference to E) + abs(column difference to E))
e. Set neighbor.f to (neighbor.g + neighbor.f)
4. Set current.state to complete.
5. If current stores ‘E’ stop, otherwise go to step 2
TerrainCosts: (S-0), (E-0), (G-1), (F-2), (H-3), (W-7), (M-12)
M1 H2 H3 S4 Fs

E M H F G is when there is a tie for lowest f


value use h as the tiebreaker
W W G F F and process the tied node with
Results Tables: the lowest h value
Location (row, Linked From f g h Complete
column)

(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:___________________

Find the shortest distance from ‘S’ to ‘E’, using A*.


1. Fill in f=3, g=0, h=3 for the location that stores ‘S’ and set linked from to null
2. Find the location with the smallest h that is not marked as complete (referred to as
current)
3. For each neighboring location (referred to as neighbor)
a. If neighbor.g is not set or neighbor.g > (current.g + neighbor.terrainCost) go
to step b, otherwise continue to next neighbor
b. Set Linked from to current
c. Set neighbor.g to (current.f + neighbor.terrainCost)
d. Set neighbor.h to (abs(row difference to E) + abs(column difference to E))
e. Set neighbor.f to (neighbor.g + neighbor.f)
4. Set current.state to complete.
5. If current stores ‘E’ stop, otherwise go to step 2
TerrainCosts: (S-0), (E-0), (G-1), (F-2), (H-3), (W-7), (M-12)
F1 S2 M3 F4 Es

F G H G G up when there is a tie for lowest f


value use h as the tiebreaker
F G M M M and process the tied node with
Results Tables: the lowest h value
Location (row, Linked From f g h Complete
column)

(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:___________________

Find the shortest distance from ‘B’ to ‘A’, using A*.


1. Fill in f=1, g=0, h=1 for the location that stores ‘S’ and set linked from to null
2. Find the location with the smallest h that is not marked as complete (referred to as
current)
3. For each neighboring location (referred to as neighbor)
a. If neighbor.g is not set or neighbor.g > (current.g + neighbor.terrainCost) go
to step b, otherwise continue to next neighbor
b. Set Linked from to current
c. Set neighbor.g to (current.f + neighbor.terrainCost)
d. Set neighbor.h to (measured distance in inches rounded up to the next inch)
e. Set neighbor.f to (neighbor.g + neighbor.f)
4. Set current.state to complete.

Node Linked From f g h Complete


shortest
7 6 FOUNDA Path
Bit 708176
A
7 0
B
complete
C
B 5 03 3 2 complete
D
11 36 9 2
BCC 9 6 68781395
E
1
Name:___________________

Find the shortest distance from ‘B’ to ‘G’, using A*.


1. Fill in f=1, g=0, h=1 for the location that stores ‘S’ and set linked from to null
2. Find the location with the smallest h that is not marked as complete (referred to as
current)
3. For each neighboring location (referred to as neighbor)
a. If neighbor.g is not set or neighbor.g > (current.g + neighbor.terrainCost) go
to step b, otherwise continue to next neighbor
b. Set Linked from to current
c. Set neighbor.g to (current.f + neighbor.terrainCost)
d. Set neighbor.h to (measured distance in inches rounded up to the next inch)
e. Set neighbor.f to (neighbor.g + neighbor.f)
4. Set current.state to complete.

Node Linked From f g h Complete

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy