Announcements
Reminder: We will be running the full autograder for project 2ab on ~4/2.
Note: We are probably going to significantly revise the extension system for post spring break.
Announcements
Will be back to pre-recorded lectures after break. Sorry, life was too busy to record them this week.
CS61B
Lecture 25: Shortest Paths
Graph Problems
Last time, saw two ways to find paths in a graph.
Which is better?
Problem | Problem Description | Solution | Efficiency (adj. list) |
s-t paths | Find a path from s to every reachable vertex. | DepthFirstPaths.java | O(V+E) time Θ(V) space |
s-t shortest paths | Find a shortest path from s to every reachable vertex. | BreadthFirstPaths.java | O(V+E) time Θ(V) space |
BFS vs. DFS for Path Finding
Possible considerations:
BFS vs. DFS for Path Finding
BreadthFirstSearch for Google Maps
As we discussed last time, BFS would not be a good choice for a google maps style navigation application.
Let’s see a quick example.
Breadth First Search for Mapping Applications
Suppose we’re trying to get from s to t.
The reason the places are in Chinese is because I took this diagram from my Chinese language version of 61B.
s
t
Breadth First Search for Mapping Applications
Suppose we’re trying to get from s to t.
s
t
Breadth First Search for Mapping Applications
BFS yields the wrong route from s to t.
s
t
10
20
40
40
80
40
70
s
t
20
40
40
80
40
70
10
BFS Result
Correct Result
Dijkstra’s Algorithm
Problem: Single Source Single Target Shortest Paths
Goal: Find the shortest paths from source vertex s to some target vertex t.
Challenge: Try to find the shortest path from town 0 to town 5.
1
2
3
4
5
6
0
s
5
2
1
15
3
2
11
5
1
1
4
1
Problem: Single Source Single Target Shortest Paths
Goal: Find the shortest paths from source vertex s to some target vertex t.
1
2
3
4
5
6
0
s
5
2
1
15
3
2
11
5
1
1
4
1
Best path from 0 to 5 is
The path 0 -> 2 -> 5 only involves three towns, but total length is 16 miles.
Problem: Single Source Single Target Shortest Paths
Goal: Find the shortest paths from source vertex s to some target vertex t.
Observation: Solution will always be a path with no cycles (assuming non-negative weights).
Goal: Find the shortest paths from source vertex s to some target vertex t.
1
2
3
4
5
6
0
s
5
2
1
15
3
2
11
5
1
1
4
1
v distTo[] edgeTo[]
0 0.0 -
1 2.0 0→1
2 - -
3 - -
4 5.0 1→4
5 9.0 4→5
6 - -
Shortest path from s=0 to t=5
Problem: Single Source Shortest Paths
Goal: Find the shortest paths from source vertex s to every other vertex.
Challenge: Try to write out the solution for this graph.
1
2
3
4
5
6
0
s
5
2
1
15
3
2
11
5
1
1
4
1
Problem: Single Source Shortest Paths
Goal: Find the shortest paths from source vertex s to every other vertex.
v distTo[] edgeTo[]
0 0.0 -
1 2.0 0→1
2 1.0 0→1
3 11.0 6→3
4 5.0 1→4
5 9.0 4→5
6 10.0 4→6
1
2
3
4
5
6
0
s
5
2
1
15
3
2
11
5
1
1
4
1
Shortest paths from s=0
Observation: Solution will always be a tree.
SPT Edge Count: http://yellkey.com/?
If G is a connected edge-weighted graph with V vertices and E edges, how many edges are in the Shortest Paths Tree (SPT) of G? [assume every vertex is reachable]
1
2
3
4
5
6
0
s
SPT Edge Count
If G is a connected edge-weighted graph with V vertices and E edges, how many edges are in the Shortest Paths Tree (SPT) of G? [assume every vertex is reachable]
V: 7
Number of edges in SPT is 6
Always V-1:
1
2
3
4
5
6
0
s
Finding a Shortest Paths Tree (By Hand)
What is the shortest paths tree for the graph below? Note: Source is A.
B
C
A
s
5
5
D
1
2
2
1
Finding a Shortest Paths Tree (By Hand)
What is the shortest paths tree for the graph below?
4
B
C
A
s
5
5
D
1
1
0
2
2
2
1
Creating an Algorithm
Let’s create an algorithm for finding the shortest paths.
Will start with a bad algorithm and then successively improve it.
∞
B
C
A
s
5
5
D
1
∞
∞
∞
2
2
1
Finding a Shortest Paths Tree Algorithmically (Incorrect)
Bad algorithm #1: Perform a depth first search. When you visit v:
dfs(A):
Add A->B to SPT
Add A->C to SPT
dfs(B):
Add B->D to SPT
A already in SPT.
dfs(C):
B already in SPT.
D already in SPT.
7
B
C
A
s
5
5
D
1
1
0
5
3
2
1
B
C
A
s
5
1
D
1
∞
0
∞
∞
3
2
5
5
1
B
C
A
s
5
1
5
D
1
1
0
5
∞
3
2
7
Finding a Shortest Paths Tree Algorithmically (Incorrect)
Bad algorithm #2: Perform a depth first search. When you visit v:
dfs(A):
A->B is 5, < than ∞
A->C is 1, < than ∞
dfs(B):
B->D is 5 + 2 = 7, better than ∞.
B->A is 5 + 3 = 8, worse than 0.
dfs(C):
C->B is 1 + 1 = 2, better than 5.
C->D is 1 + 5 = 6, better than 7.
B
C
A
s
5
1
D
1
∞
0
∞
∞
3
2
5
We’ll call this process “edge relaxation”.
Improvements:
1
5
B
C
A
s
5
1
5
D
1
1
0
5
∞
3
2
7
7
B
C
A
s
5
5
D
1
1
0
5
3
2
1
2
6
Finding a Shortest Paths Tree Algorithmically (Incorrect)
Dijkstra’s Algorithm: Perform a best first search (closest first). When you visit v:
A has lowest dist, so dfs(A):
A->B is 5, < than ∞
A->C is 1, < than ∞
C has lowest dist, so dfs(C):
C->B is 1 + 1 = 2, better than 5.
C->D is 1 + 5 = 6, better than ∞.
B has lowest dist, so dfs(B):
B->A is 2 + 3 = 5, worse than 0.
B->D is 2 + 2 = 4, better than 6.
B
C
A
s
5
1
D
1
∞
0
∞
∞
3
2
5
Improvements:
1
5
B
C
A
s
5
1
5
D
1
1
0
5
∞
3
2
2
6
6
B
C
A
s
5
5
D
1
1
0
2
3
2
1
4
Dijkstra’s Algorithm Demo
Insert all vertices into fringe PQ, storing vertices in order of distance from source.
Repeat: Remove (closest) vertex v from PQ, and relax all edges pointing from v.
1
2
3
4
5
6
0
s
5
2
1
15
3
2
11
5
1
1
4
0
∞
∞
∞
∞
∞
∞
1
Dijkstra’s Correctness and Runtime
Dijkstra’s Algorithm Pseudocode
Key invariants:
Important properties:
Dijkstra’s:
Relaxing an edge p → q with weight w:
Guaranteed Optimality
Dijkstra’s Algorithm:
Dijkstra’s is guaranteed to return a correct result if all edges are non-negative.
Guaranteed Optimality
Dijkstra’s is guaranteed to be optimal so long as there are no negative edges.
Proof sketch: Assume all edges have non-negative weights.
Negative Edges
Dijkstra’s Algorithm:
Dijkstra’s can fail if graph has negative weight edges. Why?
33
34
-67
82
101
1
14
Negative Edges
Dijkstra’s Algorithm:
Dijkstra’s can fail if graph has negative weight edges. Why?
33
34
-67
82
101
1
14
34
Even though vertex 34 has greater distTo at the time of its visit, it is still able to modify the distTo of a visited (white) vertex.
Dijkstra’s Algorithm Runtime
Priority Queue operation count, assuming binary heap based PQ:
Overall runtime: O(V*log(V) + V*log(V) + E*logV).
| # Operations | Cost per operation | Total cost |
PQ add | V | O(log V) | O(V log V) |
PQ removeSmallest | V | O(log V) | O(V log V) |
PQ changePriority | E | O(log V) | O(E log V) |
A*
Single Target Dijkstra’s
Is this a good algorithm for a navigation application?
The Problem with Dijkstra’s
Dijkstra’s will explore every place within nearly two thousand miles of Denver before it locates NYC.
The Problem with Dijkstra’s
We have only a single target in mind, so we need a different algorithm. How can we do better?
How can we do Better?
Explore eastwards first?
Introducing A*
Simple idea:
Compared to Dijkstra’s which only considers d(source, v).
Example: Henderson is farther than Englewood, but probably overall better for getting to NYC.
A* Demo, with s = 0, goal = 6.
Insert all vertices into fringe PQ, storing vertices in order of d(source, v) + h(v, goal).
Repeat: Remove best vertex v from PQ, and relax all edges pointing from v.
1
2
3
4
5
6
0
s
5
2
1
15
3
2
11
5
1
1
4
0
∞
∞
∞
∞
∞
∞
1
#
0
1
2
3
4
5
6
h(v, goal)
1
3
15
2
5
∞
0
Heuristic h(v, goal) estimates that distance from 2 to 6 is 15.
A* Heuristic Example
How do we get our estimate?
For the map to the right, what could we use?
A* Heuristic Example
How do we get our estimate?
For the map to the right, what could we use?
/** h(v, goal) DOES NOT CHANGE as algorithm runs. */
public method h(v, goal) {
return computeLineDistance(v.latLong, goal.latLong);
}
A* vs. Dijkstra’s Algorithm
http://qiao.github.io/PathFinding.js/visual/
Note, if edge weights are all equal (as here), Dijkstra’s algorithm is just breadth first search.
This is a good tool for understanding distinction between order in which nodes are visited by the algorithm vs. the order in which they appear on the shortest path.
A* Heuristics
(188 Preview)
Impact of Heuristic Quality
Suppose we throw up our hands and say we don’t know anything, and just set h(v, goal) = 0 miles. What happens?
What if we just set h(v, goal) = 10000 miles?
A* Algorithm:
Visit vertices in order of d(Denver, v) + h(v, goal), where h(v, goal) is an estimate of the distance from v to NYC.
Impact of Heuristic Quality
Suppose we throw up our hands and say we don’t know anything, and just set h(v, goal) = 0 miles. What happens?
What if we just set h(v, goal) = 10000 miles?
A* Algorithm:
Visit vertices in order of d(Denver, v) + h(v, goal), where h(v, goal) is an estimate of the distance from v to NYC.
Impact of Heuristic Quality
Suppose you use your impressive geography knowledge and decide that the midwestern states of Illinois and Indiana are in the middle of nowhere: h(Indianapolis, goal)=h(Chicago, goal)=...=100000.
Impact of Heuristic Quality
Suppose you use your impressive geography knowledge and decide that the midwestern states of Illinois and Indiana are in the middle of nowhere: h(Indianapolis, goal)=h(Chicago, goal)=...=100000.
Heuristics and Correctness
For our version of A* to give the correct answer, our A* heuristic must be:
This is an artificial intelligence topic, and is beyond the scope of our course.
Our heuristic was inadmissible and inconsistent.
Consistency and Admissibility (EXTRA: Beyond Course Scope)
All consistent heuristics are admissible.
Admissibility and consistency are sufficient conditions for certain variants of A*.
Heuristics that Yield Correct NYC Route
Admissible
Consistent
Our version of A* is called “A* graph search”. There’s another version called “A* tree search”. You’ll learn about it in 188.
Summary: Shortest Paths Problems
Single Source, Multiple Targets:
Single Source, Single Target:
Graph Problems
Problem | Problem Description | Solution | Efficiency |
paths | Find a path from s to every reachable vertex. | DepthFirstPaths.java | O(V+E) time Θ(V) space |
shortest paths | Find the shortest path from s to every reachable vertex. | BreadthFirstPaths.java | O(V+E) time Θ(V) space |
shortest weighted paths | Find the shortest path, considering weights, from s to every reachable vertex. | DijkstrasSP.java | O(E log V) time Θ(V) space |
shortest weighted path | Find the shortest path, consider weights, from s to some target vertex | A*: Same as Dijkstra’s but with h(v, goal) added to priority of each vertex. | Time depends on heuristic. Θ(V) space |