0% found this document useful (0 votes)
18 views208 pages

24-Dijkstra AStar

Dijkstra AStar

Uploaded by

docdocopenup
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)
18 views208 pages

24-Dijkstra AStar

Dijkstra AStar

Uploaded by

docdocopenup
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/ 208

CS 106B

Lecture 25: Dijkstra's Algorithm 0


8 4
and the A* Algorithm
2
Friday, May 25, 2018 1 2
6
3 11 4
4 1
Programming Abstractions
Spring 2018
Stanford University 5 6 5
Computer Science Department 2
9
Lecturer: Chris Gregg
3
7
reading:
Programming Abstractions in C++, Chapter 18.6
Today's Topics

•Logistics
•YEAH hours next Tuesday.

•More on Graphs:
•Dijkstra's Algorithm
•A* Algorithm
Last time: DFS and BFS

Depth First Search: Keep searching


along a path until we need to backtrack: 1 2
not guaranteed shortest path.
3 4
Breadth First Search: Look at paths
containing neighbor of distance 1, then
5 6
neighbors of distance 2, etc., until a path
is found: guaranteed shortest path.
7
No Weights!

0
8 4
Depth First Search: Keep searching
2
along a path until we need to backtrack: 1 2
not guaranteed shortest path. 6
3 11 4
4 1
Breadth First Search: Look at paths
containing neighbor of distance 1, then
5 6 5
neighbors of distance 2, etc., until a path
2
is found: guaranteed shortest path. 9
3
7
Neither DFS or BFS dealt with weights!
Search Without Weights
Search without weights: What is the shortest path from A to D?

B C

A D
Search With Weights
Search without weights: What is the shortest path from A to D?

B C

Shortest Path: A-D

A D
Search With Weights
Search with weights: What is the shortest path from A to D?
(Assume the numbers are distances, and we want to minimize the overall
path distance)
5
B C

10 3

A D
12345
Search With Weights
Search with weights: What is the shortest path from A to D?
(Assume the numbers are distances, and we want to minimize the overall
path distance)
5
B C

10 3 Shortest Path: A-B-C-D

A D
12345
Search With Weights
Search with weights: What is the shortest path from A to D?
(Assume the numbers are distances, and we want to minimize the overall
path distance)
5
B C

10 3 Shortest Path: A-B-C-D

A D
12345
Our BFS would break! The "shortest" path with weights depends
on the weight!
BFS without weights...
If we use BFS to find the path (disregarding weights), we would use a
queue to enqueue each path.

5
B C

10 3

A D
12345
Dijkstra's Algorithm
A different algorithm, called "Dijstra's Algorithm" (after the computer
scientist Edsger Dijkstra) uses a priority queue to enqueue each path.

5
B C

10 3

A D
12345
Breadth First Search
bfs from v1 to v2:
create a queue of paths (a vector), q
q.enqueue(v1 path)
while q is not empty and v2 is not yet visited:
path = q.dequeue() 5
B C
v = last element in path
mark v as visited
if v is the end vertex, we can stop. 10 3
for each unvisited neighbor of v:
make new path with v's neighbor as last A D
element 12345
enqueue new path onto q
Dijkstra's Algorithm
dijkstra’s from v1 to v2:
create a priority queue of paths (a vector), q
q.enqueue(v1 path)
while q is not empty and v2 is not yet visited:
path = q.dequeue() 5
B C
v = last element in path
mark v as visited
if v is the end vertex, we can stop. 10 3
for each unvisited neighbor of v:
make new path with v's neighbor as last A D
element 12345
enqueue new path onto q
Dijkstra's Algorithm
Dijkstra's algorithm is what we call a "greedy" algorithm.

This means that the algorithm always takes the path that
is best at the given time -- e.g., starting from A, you
would prioritize the path from A-B (10) over the path 5
from A-D (12345). This is why we use a priority queue, B C
because the prioritization is handled with a priority
queue. 10 3

A D
12345
Dijkstra's Algorithm in Practice
• From the start vertex, explore the neighbor
nodes first, before moving to the next level A B 3 C
6
neighbors, in priority order. From A to I:
3 1 1
priority
Let's look at Dijkstra from a to i: queue:
D 1 E F
7
front
Path: A 9 4 7
Total Cost: 0 2 5
G H I
Vector<Vertex *> startPath
startPath.add(A,0)
pq.enqueue(startPath)
Visited Set: (empty)
Dijkstra's Algorithm in Practice
• From the start vertex, explore the neighbor
nodes first, before moving to the next level A B 3 C
6
neighbors, in priority order. From A to I:
3 1 1
priority
Let's look at Dijkstra from a to i: queue:
D 1 E F
7
front
Path: AB AD 9 4 7
Total Cost: 6 3 2 5
G H I
in while loop:
curPath = pq.dequeue() (path is A, priority is 0)
v = last element in curPath (v is A)
mark v as visited
Visited Set: A
enqueue all unvisited neighbor paths onto q,
with updated priorities based on new edge length
Dijkstra's Algorithm in Practice
• From the start vertex, explore the neighbor
nodes first, before moving to the next level A B 3 C
6
neighbors, in priority order. From A to I:
3 1 1
priority
Let's look at Dijkstra from a to i: queue:
D 1 E F
7
front
Path: ADG AB ADE 9 4 7
Total Cost: 12 6 4 2 5
G H I
in while loop:
curPath = pq.dequeue() (path is AD, priority is 3)
v = last element in curPath (v is D)
mark v as visited
Visited Set: A, D
enqueue all unvisited neighbor paths onto q, with
updated priorities based on new edge length
Dijkstra's Algorithm in Practice
• From the start vertex, explore the neighbor
nodes first, before moving to the next level A B 3 C
6
neighbors, in priority order. From A to I:
3 1 1
priority
Let's look at Dijkstra from a to i: queue:
D 1 E F
7
front
Path: ADG ADEF ADEH AB ADEB 9 4 7
Total Cost: 12 11 8 6 5 2 5
G H I
in while loop:
curPath = pq.dequeue() (path is ADE, priority is 4)
v = last element in curPath (v is E)
mark v as visited
Visited Set: A, D, E
enqueue all unvisited neighbor paths onto q, with
updated priorities based on new edge length
Dijkstra's Algorithm in Practice
• From the start vertex, explore the neighbor
nodes first, before moving to the next level A B 3 C
6
neighbors, in priority order. From A to I:
3 1 1
priority
Let's look at Dijkstra from a to i: queue:
D 1 E F
7
front
Path: ADG ADEF ADEBC ADEH AB 9 4 7
Total Cost: 12 11 8 8 6 2 5
G H I
in while loop:
curPath = pq.dequeue() (path is ADEB, priority is 5)
v = last element in curPath (v is B)
mark v as visited
Visited Set: A, D, E, B
enqueue all unvisited neighbor paths onto q, with
updated priorities based on new edge length
Dijkstra's Algorithm in Practice
• From the start vertex, explore the neighbor
nodes first, before moving to the next level A B 3 C
6
neighbors, in priority order. From A to I:
3 1 1
priority
Let's look at Dijkstra from a to i: queue:
D 1 E F
7
front
Path: ADG ADEF ABC ADEBC ADEH 9 4 7
Total Cost: 12 11 9 8 8 2 5
G H I
in while loop:
curPath = pq.dequeue() (path is AB, priority is 6)
v = last element in curPath (v is B)
mark v as visited
Visited Set: A, D, E, B
enqueue all unvisited neighbor paths onto q, with
updated priorities based on new edge length
Dijkstra's Algorithm in Practice
• From the start vertex, explore the neighbor
nodes first, before moving to the next level A B 3 C
6
neighbors, in priority order. From A to I:
3 1 1
priority
Let's look at Dijkstra from a to i: queue:
D 1 E F
7
front
Path: ADEHI ADG ADEF ADEHG ABC ADEBC 9 4 7
Total Cost: 13 12 11 10 9 8 2 5
G H I
in while loop:
curPath = pq.dequeue() (path is ADEH, priority is 8)
v = last element in curPath (v is H)
mark v as visited
Visited Set: A, D, E, B, H
enqueue all unvisited neighbor paths onto q, with
updated priorities based on new edge length
Note: cannot stop yet! ADEHI might not be shortest!
Dijkstra's Algorithm in Practice
• From the start vertex, explore the neighbor
nodes first, before moving to the next level A B 3 C
6
neighbors, in priority order. From A to I:
3 1 1
priority
Let's look at Dijkstra from a to i: queue:
D 1 E F
7
front
Path: ADEHI ADG ADEF ADEHG ADEBCF ABC 9 4 7
Total Cost: 13 12 11 10 9 9 2 5
G H I
in while loop:
curPath = pq.dequeue() (path is ADEBC, priority is 8)
v = last element in curPath (v is C)
mark v as visited
Visited Set: A, D, E, B, H, C
enqueue all unvisited neighbor paths onto q, with
updated priorities based on new edge length
Dijkstra's Algorithm in Practice
• From the start vertex, explore the neighbor
nodes first, before moving to the next level A B 3 C
6
neighbors, in priority order. From A to I:
3 1 1
priority
Let's look at Dijkstra from a to i: queue:
D 1 E F
7
front
Path: ADEHI ADG ADEF ABCF ADEHG ADEBCF 9 4 7
Total Cost: 13 12 11 10 10 9 2 5
G H I
in while loop:
curPath = pq.dequeue() (path is ABC, priority is 9)
v = last element in curPath (v is C)
mark v as visited
Visited Set: A, D, E, B, H, C
enqueue all unvisited neighbor paths onto q, with
updated priorities based on new edge length
Dijkstra's Algorithm in Practice
• From the start vertex, explore the neighbor
nodes first, before moving to the next level A B 3 C
6
neighbors, in priority order. From A to I:
3 1 1
priority
Let's look at Dijkstra from a to i: queue:
D 1 E F
7
front
Path: ADEBCFI ADEHI ADG ADEF ABCF ADEHG 9 4 7
Total Cost: 16 13 12 11 10 10 2 5
G H I
in while loop:
curPath = pq.dequeue() (path is ADEBCF, priority is 9)
v = last element in curPath (v is F)
mark v as visited
Visited Set: A, D, E, B, H, C
enqueue all unvisited neighbor paths onto q, with
updated priorities based on new edge length
Dijkstra's Algorithm in Practice
• From the start vertex, explore the neighbor
nodes first, before moving to the next level A B 3 C
6
neighbors, in priority order. From A to I:
3 1 1
priority
Let's look at Dijkstra from a to i: queue:
D 1 E F
7
front
Path: ADEBCFI ADEHI ADG ADEF ABCF 9 4 7
Total Cost: 16 13 12 11 10 2 5
G H I
in while loop:
curPath = pq.dequeue() (path is ADEHG, priority is 10)
v = last element in curPath (v is G)
mark v as visited
Visited Set: A, D, E, B, H, C, F, G
enqueue all unvisited neighbor paths onto q, with
updated priorities based on new edge length
(nothing to enqueue, as all neighbors were visited)
Dijkstra's Algorithm in Practice
• From the start vertex, explore the neighbor
nodes first, before moving to the next level A B 3 C
6
neighbors, in priority order. From A to I:
3 1 1
priority
Let's look at Dijkstra from a to i: queue:
D 1 E F
7
front
Path: ABCFI ADEBCFI ADEHI ADG ADEF 9 4 7
Total Cost: 17 16 13 12 11 2 5
G H I
in while loop:
curPath = pq.dequeue() (path is ABCF, priority is 10)
v = last element in curPath (v is F)
mark v as visited
Visited Set: A, D, E, B, H, C, F, G
enqueue all unvisited neighbor paths onto q, with
updated priorities based on new edge length
Dijkstra's Algorithm in Practice
• From the start vertex, explore the neighbor
nodes first, before moving to the next level A B 3 C
6
neighbors, in priority order. From A to I:
3 1 1
priority
Let's look at Dijkstra from a to i: queue:
D 1 E F
7
front
Path: ADEFI ABCFI ADEBCFI ADEHI ADG 9 4 7
Total Cost: 18 17 16 13 12 2 5
G H I
in while loop:
curPath = pq.dequeue() (path is ADEF, priority is 11)
v = last element in curPath (v is F)
mark v as visited
Visited Set: A, D, E, B, H, C, F, G
enqueue all unvisited neighbor paths onto q, with
updated priorities based on new edge length
Dijkstra's Algorithm in Practice
• From the start vertex, explore the neighbor
nodes first, before moving to the next level A B 3 C
6
neighbors, in priority order. From A to I:
3 1 1
priority
Let's look at Dijkstra from a to i: queue:
D 1 E F
7
front
Path: ADEFI ABCFI ADEBCFI ADEHI 9 4 7
Total Cost: 18 17 16 13 2 5
G H I
in while loop:
curPath = pq.dequeue() (path is ADG, priority is 12)
v = last element in curPath (v is F)
mark v as visited
Visited Set: A, D, E, B, H, C, F, G
enqueue all unvisited neighbor paths onto q, with
updated priorities based on new edge length
(nothing to enqueue, as all neighbors were visited)
Dijkstra's Algorithm in Practice
• From the start vertex, explore the neighbor
nodes first, before moving to the next level A B 3 C
6
neighbors, in priority order. From A to I:
3 1 1
priority
Let's look at Dijkstra from a to i: queue:
D 1 E F
7
front
Path: ADEFI ABCF ADG 9 4 7
Total Cost: 18 16 12 2 5
G H I
in while loop:
curPath = pq.dequeue() (path is ADEHI, priority is 13)
v = last element in curPath (v is I)
Stop! We've found the shortest path! Visited Set: A, D, E, B, H, C, F, G
ADEHI
Who Was Edsgar Dijkstra?

History of Computing Tidbit: Edsger Dijkstra


• The Dutch academic Edsger Dijkstra was another
giant in the field of computer science.
• He was one of the first scientists to call himself a
"programmer" (and he almost couldn't get married
because of it!)
• He started out with a degree in Theoretical Physics,
but became enthralled with computers in the early
1950s.
Who Was Edsgar Dijkstra?

Edsger Dijkstra
• Dijkstra was immensely influential in many fields of
computing: compilers, operating systems, concurrent
programming, software engineering, programming
languages, algorithm design, and teaching (among
others!)
• It would be hard to pin down what he is most
famous for because he has influenced so much CS.
Goto Considered Harmful
Edsger Dijkstra
• Dijkstra was also
influential in making
programming more
structured -- he wrote a
seminal paper titled, "Goto
Considered Harmful" where
he lambasted the idea of
the "goto" statement
(which exists in C++ --
you will rarely, if ever,
use it!)
Other Cool Dijkstra Facts

Other Reasons Dijkstra is cool:


• Might actually be Walter White
• Has the letters "ijk" adjacent in his name
(is that why we use i,j,k in our loops??)
• The Edsgar Dijkstra font! His early papers
were hand-written, and he had beautiful
handwriting. This font is the "Edsger
Dijkstra" font!
Dijkstra's is great, but we can do better!
If we want to travel from Stanford to
San Francisco, Dijkstra's algorithm will
look at path distances around
Stanford. But, we know something
about how to get to San Francisco --
we know that we generally need to go
Northwest from Stanford.

This is more information! Let's not only


prioritize by weights, but also give
some priority to the direction we
want to go. E.g., we will add more
information based on a heuristic,
which could be direction in the
case of a street map.
Let's look at Dijkstra where each edge has cost 1

!35
Dijkstra where each edge has cost 1

!36
Dijkstra where each edge has cost 1

!37
Dijkstra where each edge has cost 1

1?
1? 1?
1?

!38
Dijkstra where each edge has cost 1

1?
1? 1?
1?

!39
Dijkstra where each edge has cost 1

1?
1? 1?
1

!40
Dijkstra where each edge has cost 1

1?
1? 1?
2? 1 2?
2?

!41
Dijkstra where each edge has cost 1

1?
1? 1?
2? 1 2?
2?

!42
Dijkstra where each edge has cost 1

1?
1 1?
2? 1 2?
2?

!43
Dijkstra where each edge has cost 1

2? 1?
2? 1 1?
2? 1 2?
2?

!44
Dijkstra where each edge has cost 1

2? 1?
2? 1 1?
2? 1 2?
2?

!45
Dijkstra where each edge has cost 1

2? 1
2? 1 1?
2? 1 2?
2?

!46
Dijkstra where each edge has cost 1

2?
2? 1 2?
2? 1 1?
2? 1 2?
2?

!47
Dijkstra where each edge has cost 1

2?
2? 1 2?
2? 1 1
2? 1 2?
2?

!48
Dijkstra where each edge has cost 1

2?
2? 1 2?
2? 1 1 2?
2? 1 2?
2?

!49
Dijkstra where each edge has cost 1

2?
2? 1 2
2? 1 1 2?
2? 1 2?
2?

!50
Dijkstra where each edge has cost 1

2? 3?
2? 1 2 3?
2? 1 1 2?
2? 1 2?
2?

!51
Dijkstra where each edge has cost 1

2? 3?
2? 1 2 3?
2? 1 1 2?
2? 1 2
2?

!52
Dijkstra where each edge has cost 1

2? 3?
2? 1 2 3?
2? 1 1 2?
2? 1 2 3?
2? 3?

!53
Dijkstra where each edge has cost 1

2? 3?
2? 1 2 3?
2? 1 1 2
2? 1 2 3?
2? 3?

!54
Dijkstra where each edge has cost 1

2? 3?
2? 1 2 3?
2? 1 1 2 3?
2? 1 2 3?
2? 3?

!55
Dijkstra where each edge has cost 1

2? 3?
2? 1 2 3?
2? 1 1 2 3?
2 1 2 3?
2? 3?

!56
Dijkstra where each edge has cost 1

2? 3?
2? 1 2 3?
2? 1 1 2 3?
3? 2 1 2 3?
3? 2? 3?

!57
Dijkstra where each edge has cost 1

2? 3?
2? 1 2 3?
2? 1 1 2 3?
3? 2 1 2 3?
3? 2 3?
3?

!58
Dijkstra where each edge has cost 1

2? 3?
3? 2? 1 2 3?
3? 2 1 1 2 3?
3? 2 1 2 3?
3? 2 3?
3?

!59
Dijkstra where each edge has cost 1

3? 2? 3?
3? 2 1 2 3?
3? 2 1 1 2 3?
3? 2 1 2 3?
3? 2 3?
3?

!60
Dijkstra where each edge has cost 1

3?
3? 2 3?
3? 2 1 2 3?
3? 2 1 1 2 3?
3? 2 1 2 3?
3? 2 3?
3?

!61
Dijkstra where each edge has cost 1

3?
3? 2 3?
3? 2 1 2 3?
3? 2 1 1 2 3?
3? 2 1 2 3?
3? 2 3 4?
3? 4?

!62
Dijkstra where each edge has cost 1

3?
3? 2 3?
3? 2 1 2 3?
3? 2 1 1 2 3?
3? 2 1 2 3 4?
3? 2 3 4?
3? 4?

!63
Dijkstra where each edge has cost 1

3?
3? 2 3?
3? 2 1 2 3?
3? 2 1 1 2 3?
4? 3 2 1 2 3 4?
4? 3? 2 3 4?
3? 4?

!64
Dijkstra where each edge has cost 1

3?
3? 2 3?
4? 3? 2 1 2 3?
4? 3 2 1 1 2 3?
4? 3 2 1 2 3 4?
4? 3? 2 3 4?
3? 4?

!65
Dijkstra where each edge has cost 1

3? 4?
3? 2 3 4?
4? 3? 2 1 2 3?
4? 3 2 1 1 2 3?
4? 3 2 1 2 3 4?
4? 3? 2 3 4?
3? 4?

!66
Dijkstra where each edge has cost 1

3? 4?
4? 3? 2 3 4?
4? 3 2 1 2 3?
4? 3 2 1 1 2 3?
4? 3 2 1 2 3 4?
4? 3? 2 3 4?
3? 4?

!67
Dijkstra where each edge has cost 1

3? 4?
4? 3? 2 3 4?
4? 3 2 1 2 3 4?
4? 3 2 1 1 2 3?
4? 3 2 1 2 3 4?
4? 3? 2 3 4?
3? 4?

!68
Dijkstra where each edge has cost 1

4? 3? 4?
4? 3 2 3 4?
4? 3 2 1 2 3 4?
4? 3 2 1 1 2 3?
4? 3 2 1 2 3 4?
4? 3? 2 3 4?
3? 4?

!69
Dijkstra where each edge has cost 1
4?
4? 3 4?
4? 3 2 3 4?
4? 3 2 1 2 3 4?
4? 3 2 1 1 2 3?
4? 3 2 1 2 3 4?
4? 3? 2 3 4?
3? 4?

!70
Dijkstra where each edge has cost 1
4?
4? 3 4?
4? 3 2 3 4?
4? 3 2 1 2 3 4?
4? 3 2 1 1 2 3?
4? 3 2 1 2 3 4?
4? 3 2 3 4?
4? 3? 4?

!71
Dijkstra where each edge has cost 1
4?
4? 3 4?
4? 3 2 3 4?
4? 3 2 1 2 3 4?
4? 3 2 1 1 2 3?
4? 3 2 1 2 3 4?
4? 3 2 3 4?
4? 3 4?
4?
!72
Dijkstra where each edge has cost 1
4?
4? 3 4?
4? 3 2 3 4?
4? 3 2 1 2 3 4?
4? 3 2 1 1 2 3 4?
4? 3 2 1 2 3 4?
4? 3 2 3 4?
4? 3 4?
4?
!73
Dijkstra where each edge has cost 1
4?
5? 4? 3 4?
5? 4 3 2 3 4?
4? 3 2 1 2 3 4?
4? 3 2 1 1 2 3 4?
4? 3 2 1 2 3 4?
4? 3 2 3 4?
4? 3 4?
4?
!74
Dijkstra where each edge has cost 1
4?
5? 4? 3 4? 5?
5? 4 3 2 3 4 5?
4? 3 2 1 2 3 4?
4? 3 2 1 1 2 3 4?
4? 3 2 1 2 3 4?
4? 3 2 3 4?
4? 3 4?
4?
!75
Dijkstra where each edge has cost 1
4?
5? 4? 3 4? 5?
5? 4 3 2 3 4 5?
4? 3 2 1 2 3 4? 5?
4? 3 2 1 1 2 3 4 5?
4? 3 2 1 2 3 4? 5?
4? 3 2 3 4?
4? 3 4?
4?
!76
Dijkstra where each edge has cost 1
4?
5? 4? 3 4? 5?
5? 4 3 2 3 4 5?
4? 3 2 1 2 3 4 5?
4? 3 2 1 1 2 3 4 5?
4? 3 2 1 2 3 4? 5?
4? 3 2 3 4?
4? 3 4?
4?
!77
Dijkstra where each edge has cost 1
4?
5? 4? 3 4? 5?
5? 4 3 2 3 4 5?
4? 3 2 1 2 3 4 5?
4? 3 2 1 1 2 3 4 5?
4? 3 2 1 2 3 4? 5?
5? 4 3 2 3 4?
5? 4? 3 4?
4?
!78
Dijkstra where each edge has cost 1
4?
5? 4? 3 4? 5?
5? 4 3 2 3 4 5?
5? 4 3 2 1 2 3 4 5?
4? 3 2 1 1 2 3 4 5?
4? 3 2 1 2 3 4? 5?
5? 4 3 2 3 4?
5? 4? 3 4?
4?
!79
Dijkstra where each edge has cost 1
4?
5? 4? 3 4? 5?
5? 4 3 2 3 4 5?
5? 4 3 2 1 2 3 4 5?
4 3 2 1 1 2 3 4 5?
5? 4? 3 2 1 2 3 4? 5?
5? 4 3 2 3 4?
5? 4? 3 4?
4?
!80
Dijkstra where each edge has cost 1
4?
5? 4? 3 4? 5?
5? 4 3 2 3 4 5?
5? 4 3 2 1 2 3 4 5?
4 3 2 1 1 2 3 4 5?
5? 4 3 2 1 2 3 4? 5?
5? 4 3 2 3 4?
5? 4? 3 4?
4?
!81
Dijkstra where each edge has cost 1
5? 4?
5? 4 3 4? 5?
5? 4 3 2 3 4 5?
5? 4 3 2 1 2 3 4 5?
4 3 2 1 1 2 3 4 5?
5? 4 3 2 1 2 3 4? 5?
5? 4 3 2 3 4?
5? 4? 3 4?
4?
!82
Dijkstra where each edge has cost 1
5? 4?
5? 4 3 4? 5?
5? 4 3 2 3 4 5?
5? 4 3 2 1 2 3 4 5?
4 3 2 1 1 2 3 4 5?
5? 4 3 2 1 2 3 4 5?
5? 4 3 2 3 4? 5?
5? 4? 3 4?
4?
!83
Dijkstra where each edge has cost 1
5? 4?
5? 4 3 4? 5?
5? 4 3 2 3 4 5?
5? 4 3 2 1 2 3 4 5?
4 3 2 1 1 2 3 4 5?
5? 4 3 2 1 2 3 4 5?
5? 4 3 2 3 4? 5?
5? 4? 3 4 5?
4? 5?
!84
Dijkstra where each edge has cost 1
5? 4?
5? 4 3 4? 5?
5? 4 3 2 3 4 5?
5? 4 3 2 1 2 3 4 5?
4 3 2 1 1 2 3 4 5?
5? 4 3 2 1 2 3 4 5?
5? 4 3 2 3 4? 5?
5? 4? 3 4 5?
5? 4 5?
!85
Dijkstra where each edge has cost 1
5? 4?
5? 4 3 4? 5?
5? 4 3 2 3 4 5?
5? 4 3 2 1 2 3 4 5?
4 3 2 1 1 2 3 4 5?
5? 4 3 2 1 2 3 4 5?
5? 4 3 2 3 4? 5?
5? 4 3 4 5?
5? 4 5?
!86
Dijkstra where each edge has cost 1
5? 4?
5? 4 3 4? 5?
5? 4 3 2 3 4 5?
5? 4 3 2 1 2 3 4 5?
4 3 2 1 1 2 3 4 5?
5? 4 3 2 1 2 3 4 5?
5? 4 3 2 3 4 5?
5? 4 3 4 5?
5? 4 5?
!87
Dijkstra where each edge has cost 1
5? 4? 5?
5? 4 3 4 5?
5? 4 3 2 3 4 5?
5? 4 3 2 1 2 3 4 5?
4 3 2 1 1 2 3 4 5?
5? 4 3 2 1 2 3 4 5?
5? 4 3 2 3 4 5?
5? 4 3 4 5?
5? 4 5?
!88
Dijkstra where each edge has cost 1
5? 4 5?
5? 4 3 4 5?
5? 4 3 2 3 4 5?
5? 4 3 2 1 2 3 4 5?
4 3 2 1 1 2 3 4 5?
5? 4 3 2 1 2 3 4 5?
5? 4 3 2 3 4 5?
5? 4 3 4 5?
5? 4 5?
!89
Dijkstra where each edge has cost 1
5? 4 5? 6?
5? 4 3 4 5 6?
5? 4 3 2 3 4 5?
5? 4 3 2 1 2 3 4 5?
4 3 2 1 1 2 3 4 5?
5? 4 3 2 1 2 3 4 5?
5? 4 3 2 3 4 5?
5? 4 3 4 5?
5? 4 5?
!90
Dijkstra where each edge has cost 1
5? 4 5? 6?
5? 4 3 4 5 6?
5? 4 3 2 3 4 5?
5? 4 3 2 1 2 3 4 5?
4 3 2 1 1 2 3 4 5?
5? 4 3 2 1 2 3 4 5?
5? 4 3 2 3 4 5 6?
5? 4 3 4 5? 6?
5? 4 5?
!91
Dijkstra where each edge has cost 1
5? 4 5? 6?
6? 5? 4 3 4 5 6?
6? 5 4 3 2 3 4 5?
5? 4 3 2 1 2 3 4 5?
4 3 2 1 1 2 3 4 5?
5? 4 3 2 1 2 3 4 5?
5? 4 3 2 3 4 5 6?
5? 4 3 4 5? 6?
5? 4 5?
!92
Dijkstra where each edge has cost 1
5? 4 5? 6?
6? 5? 4 3 4 5 6?
6? 5 4 3 2 3 4 5?
5? 4 3 2 1 2 3 4 5?
4 3 2 1 1 2 3 4 5?
5? 4 3 2 1 2 3 4 5?
5? 4 3 2 3 4 5 6?
6? 5 4 3 4 5? 6?
6? 5? 4 5?
!93
Dijkstra where each edge has cost 1
5? 4 5? 6?
6? 5? 4 3 4 5 6?
6? 5 4 3 2 3 4 5?
5? 4 3 2 1 2 3 4 5?
4 3 2 1 1 2 3 4
5? 4 3 2 1 2 3 4 5?
5? 4 3 2 3 4 5 6?
6? 5 4 3 4 5? 6?
6? 5? 4 5?
!94
Dijkstra where each edge has cost 1
5? 4 5? 6?
6?Why
5? are4 we3looking
4 5 6?
in this direction?
6? 5 4 3 2 3 4 5?
5? 4 3 2 1 2 3 4 5?
4 3 2 1 1 2 3 4
5? 4 3 2 1 2 3 4 5?
5? 4 3 2 3 4 5 6?
6? 5 4 3 4 5? 6?
6? 5? 4 5?
!95
Dijkstra where each edge has cost 1
5? 4 5? 6?
6?Why
5? are 4 we3looking
4 5 6?
in this direction?
6? 5 4 3 2 3 4 5?
5? 4 3 2 1 2 3 4 5?
4 3 2 1 1 2 3 4
5? 4 3 2 1 2 3 4 5?
5? We
4 haven’t
3 2taken 3 4 5 6?
into account the
6?cost
5 from4 that
3 node
4 5? 6?
to the
6? 5? 4 5? goal

!96
Dijkstra Priority

priority(u) = distance(s, u)

distance(s,u) futureCost(s,u)
s u t

Priority of the path that ends in u

!97
Ideal Priority

priority(u) = distance(s, u) + futureCost(u, t)

distance(s,u) futureCost(s,u)
s u t

Priority of the path that ends in u

*note we will revise this slightly !98


Future Cost?

Columns apart

Rows apart

function h(node,goal) {
dRows = abs(node.row – goal.row);
dCols = abs(node.col – goal.col);
return dRows + dCols
}

!99
!100
!101
!102
1?

1? 1?

1?

!103
1?

1? 1?

1?

!104
1?
1+
1? 4?
1?

!105
1?
1+
1? 4?
1?

!106
1+
6?
1+
1? 4?
1?

!107
1+
6?
1+
1? 4?
1?

!108
1+
6?
1+
1? 4?
1+
6?

!109
1+
6?
1+
1? 4?
1+
6?

!110
1+
6?
1+ 1+
6? 4?
1+
6?

!111
1+
6?
1+ 1+
6? 4?
1+
6?

!112
1+
6?
1+ 1
6?
1+
5?
6?

!113
1+ 2+
5? 5?
6?
1+ 2+
1
6? 3?
1+ 2+
5? 5?
6?

!114
1+ 2+
5? 5?
6?
1+ 2+
1
6? 3?
1+ 2+
5? 5?
6?

!115
1+ 2+
5? 5?
6? 4?
1+ 1 2
6?
1+ 2+
5? 5?
6? 4?

!116
1+ 2+ 3+
5? 5?
6? 4? 4?
1+ 3+
1 2
6? 2?
1+ 2+ 3+
5? 5?
6? 4? 4?

!117
1+ 2+ 3+
5? 5?
6? 4? 4?
1+ 3+
1 2
6? 2?
1+ 2+ 3+
5? 5?
6? 4? 4?

!118
1+ 2+ 3+
5? 5?
6? 4? 4?
3?
1+ 1 2 3
6?
1+ 2+ 3+
5? 5?
6? 4? 4?
3?

!119
1+ 2+ 3+ 4+
5? 5?
6? 4? 4?
3? 3?
1+ 4+
1 2 3
6? 1?
1+ 2+ 3+ 4+
5? 5?
6? 4? 4?
3? 3?

!120
1+ 2+ 3+ 4+
5? 5?
6? 4? 4?
3? 3?
1+ 4+
1 2 3
6? 1?
1+ 2+ 3+ 4+
5? 5?
6? 4? 4?
3? 3?

!121
1+ 2+ 3+ 4+
5?
6? 5?
4? 4?
3? 3?
2?
1+ 1 2 3 4
6?
1+ 2+ 3+ 4+
5?
6? 5?
4? 4?
3? 3?
2?

!122
1+ 2+ 3+ 4+ 5+
5?
6? 5?
4? 4?
3? 3?
2? 2?
1+ 5+
1 2 3 4
6? 0?
1+ 2+ 3+ 4+ 5+
5?
6? 5?
4? 4?
3? 3?
2? 2?

!123
1+ 2+ 3+ 4+ 5+
5?
6? 5?
4? 4?
3? 3?
2? 2?
1+ 5+
1 2 3 4
6? 0?
1+ 2+ 3+ 4+ 5+
5?
6? 5?
4? 4?
3? 3?
2? 2?

!124
1+ 2+ 3+ 4+ 5+
5?
6? 5?
4? 4?
3? 3?
2? 2?
1+ 1 2 3 4
6?
1+ 2+ 3+ 4+ 5+
5?
6? 5?
4? 4?
3? 3?
2? 2?

!125
That was easy…

This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved.
Based on slides created by Keith Schwarz, Julie Zelenski, Jerry Cain, Eric Roberts, Mehran Sahami, Stuart Reges, Cynthia Lee, and others.
… a little too easy

This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved.
Based on slides created by Keith Schwarz, Julie Zelenski, Jerry Cain, Eric Roberts, Mehran Sahami, Stuart Reges, Cynthia Lee, and others.
Lets say we have
unknown blocks

!128
Ideal Priority

priority(u) = distance(s, u) + futureCost(u, t)

distance(s,u) futureCost(s,u)
s u t

Priority of the path that ends in u

*note we will revise this slightly !129


A* Priority

priority(u) = distance(s, u) + heuristic(u, t)

distance(s,u) heuristic(u,t)
a te o f
e r e s ti m
s u U n d c o stt
Futu r e

Priority of the path that ends in u

!130
Admissible Heuristic

Definition: An admissible heuristic


always underestimates the true
cost.

Thus: “even in the best case scenario, this path


is still terrible…”
!131
Admissible Heuristic

Even3in+ the
4 +best
5+
8? 7? 6?
case, this path is still
3+ 2 3 4 5+
8? bad 4?
3+ 2 1 2 3
8?
3+ 2 1 1 2
8?
3+ 7+
2 1 2 3
8? 2?
3+ 7+
2 3 4 5 6
8? 2?
3+ 4+ 5+ 6+ 7+
8? 7? 6? 5? 4?

!132
!133
Columns apart

Rows apart

!134
"Manhattan" distance

function h(start,goal) {
dRows = abs(start.row – goal.row);
dCols = abs(start.col – goal.col);
return dRows + dCols
}

!135
Recall Dijkstra...
Make a PriorityQueue todo-list of paths
Put a path with just the start in the todo-list
While the todo-list isn’t empty
1. Take a path out of the todo-list
2. Call the last node in the path “currNode”
3. If “currNode” is the goal, you are done.
4. If you have seen currNode before, skip it.
5. for all neighbors of currNode
Make a newPath = path + neighbor
Add the new path to the todo-list
Priority = pathLength

!136
A Star
Make a PriorityQueue todo-list of paths
Put a path with just the start in the todo-list
While the todo-list isn’t empty
1. Take a path out of the todo-list
2. Call the last node in the path “currNode”
3. If “currNode” is the goal, you are done.
4. If you have seen currNode before, skip it.
5. for all neighbors of currNode
Make a newPath = path + neighbor
Add the new path to the todo-list
Priority = pathLength + h(neighbor, goal)

!137
A Star
Make a PriorityQueue todo-list of paths
Put a path with just the start in the todo-list
While the todo-list isn’t empty
1. Take a path out of the todo-list
2. Call the last node in the path “currNode”
What is the
3. If “currNode” is priority of theyou
the goal, start
are done.
path?
4. If you have seen currNode before, skip it.
5. for all neighbors of currNode
Make a newPath = path + neighbor
Add the new path to the todo-list
Priority = pathLength + h(neighbor, goal)

!138
A Star
Make a PriorityQueue todo-list of paths
Put a path with just the start in the todo-list
While the todo-list isn’t empty
1. Take a path out of the todo-list
2. Call the last node in the path “currNode”
What is the
3. If “currNode” is priority of theyou
the goal, start
are done.
path?
4. If you have seen currNode before, skip it.
h(start, goal)
5. for all neighbors of currNode
Make a newPath = path + neighbor
Add the new path to the todo-list
Priority = pathLength + h(neighbor, goal)

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 8+
2 1 2 3 7
8? 1?
3+ 8+
2 3 4 5 6 7
8? 3?
3+ 4+ 5+ 6+ 7+ 8+
8? 7? 6? 5? 4? 3?

!192
3+ 4+ 5+ 6+ 7+
8? 7? 6? 5? 4?
3+ 7+
2 3 4 5 6
8? 2?
3+ 7+
2 1 2 3
8? 2?
3+ 2 1 1 2 8
8?
3+ 8+
2 1 2 3 7
8? 1?
3+ 8+
2 3 4 5 6 7
8? 3?
3+ 4+ 5+ 6+ 7+ 8+
8? 7? 6? 5? 4? 3?

!193
What Dijkstra would have selected!
8 7 6 5 4 5 6 7 8 9?

7 6 5 4 3 4 5 6 7 8 9?

6 5 4 3 2 3 4 5 6 7 8 9?

5 4 3 2 1 2 3 7 8 9?

4 3 2 1 1 2 8

5 4 3 2 1 2 3 7 8 9?

6 5 4 3 2 3 4 5 6 7 8 9?

7 6 5 4 3 4 5 6 7 8 9?

8 7 6 5 4 5 6 7 8 9?

!194
Why underestimate?

This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved.
Based on slides created by Keith Schwarz, Julie Zelenski, Jerry Cain, Eric Roberts, Mehran Sahami, Stuart Reges, Cynthia Lee, and others.
You only ignore paths that in the best case are
worse than your current path.

This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved.
Based on slides created by Keith Schwarz, Julie Zelenski, Jerry Cain, Eric Roberts, Mehran Sahami, Stuart Reges, Cynthia Lee, and others.
Using our heuristic, the path from start to goal
that goes through this node is at least cost 11

3+ 4+ 5+ 6+ 7+
8? 7? 6? 5? 4?
3+ 7+
2 3 4 5 6
8? 2?
3+ 7+
2 1 2 3
8? 2?
3+ 2 1 1 2 8
8?
3+ 8+
2 1 2 3 7
8? 1?
3+ 8+
2 3 4 5 6 7
8? 3?
3+ 4+ 5+ 6+ 7+ 8+
8? 7? 6? 5? 4? 3?

!197
Imagine if we overestimate
3+ 4+ 5+
8? 7? 6?
3+ 5+
2 3 4
8? 4?
3+ 2 1 2 3
8?
3+ 2 1 1 2
8?
3+ 2 1 2 3
8?
3+ 5+
2 3 4
8? 4?
3+ 4+ 5+
8? 7? 6?

!198
Imagine if we overestimate
3+ 4+ 5+
8? 7? 6?
3+ 5 +
2 3 4 20?
8?
3+ 2 1 2 3
8?
3+ 2 1 1 2
8?
3+ 2 1 2 3
8?
3+ 5 +
2 3 4 20?
8?
3+ 4+ 5+
8? 7? 6?

!199
More Detail on A*: Choice of Heuristic

priority(u) = distance(s, u) + heuristic(u, t)

distance(s,u) heuristic(u,t)
a te o f
e r e s ti m
s u U n d c o stt
Futu r e

We want to underestimate the cost of our heuristic, by why?


Let's look at the bounds of our choices:
heuristic(u,t) = 0
heuristic(u,t) = underestimate
heuristic(u,t) = perfect distance
heuristic(u,t) = overestimate !200
More Detail on A*: Choice of Heuristic

priority(u) = distance(s, u) + heuristic(u, t)

distance(s,u) heuristic(u,t)
s u t

We want to underestimate the cost of our heuristic, by why?


Let's look at the bounds of our choices:
heuristic(u,t) = 0
heuristic(u,t) = underestimate
Same as Dijkstra
heuristic(u,t) = perfect distance
heuristic(u,t) = overestimate
More Detail on A*: Choice of Heuristic

priority(u) = distance(s, u) + heuristic(u, t)

distance(s,u) heuristic(u,t)
s u t

We want to underestimate the cost of our heuristic, by why?


Let's look at the bounds of our choices:
heuristic(u,t) = 0 Will be the same or faster than
heuristic(u,t) = underestimate Dijkstra, and will find the shortest
heuristic(u,t) = perfect distance path (this is the only "admissible"
heuristic(u,t) = overestimate heuristic for A*.
More Detail on A*: Choice of Heuristic

priority(u) = distance(s, u) + heuristic(u, t)

distance(s,u) heuristic(u,t)
s u t

We want to underestimate the cost of our heuristic, by why?


Let's look at the bounds of our choices:
heuristic(u,t) = 0
heuristic(u,t) = underestimate Will only follow the best path, and
heuristic(u,t) = perfect distance will find the best path fastest (but
heuristic(u,t) = overestimate requires perfect knowledge)
More Detail on A*: Choice of Heuristic

priority(u) = distance(s, u) + heuristic(u, t)

distance(s,u) heuristic(u,t)
s u t

We want to underestimate the cost of our heuristic, by why?


Let's look at the bounds of our choices:
heuristic(u,t) = 0
heuristic(u,t) = underestimate Won't necessarily find
heuristic(u,t) = perfect distance shortest path (but might run
heuristic(u,t) = overestimate even faster)
Admissible Heuristic
Definition: An admissible heuristic always
underestimates the true cost.

https://media.giphy.com/media/GEPHf81p4svkI/giphy.gif
Why doesn't Google Maps Pre-Compute Directions?

• How many nodes are in the Google Maps graph?


• About 75 million
• How many sets of directions would they need to
generate?
• (roughly) N2
• How long would that take?
• 6 x 1015 seconds
• Or... 190 million years
What Heuristics Could Google Maps Use?

• As the crow flies


• Calculate the straight-line distance from A to B,
and divide by the speed on the fastest highway
• Landmark heuristic
• Find the distance from A and B to a landmark,
calculate the difference (distance < abs(A - B))
• All of these and more?
• You can use multiple heuristics and choose the
max
Extra Slides

You might also like