Shortest Paths
CSE 373Data Structures
Shortest paths 2
Readings
• Reading Chapter 13› Sections 13.5 to 13.7
Shortest paths 3
Recall Path cost ,Path length• Path cost: the sum of the costs of each edge• Path length: the number of edges in the path
› Path length is the unweighted path cost
Seattle
San FranciscoDallas
Chicago
Salt Lake City
4
2 2
23
2 23
length(p) = 5cost(p) = 11
Shortest paths 4
Shortest Path Problems• Given a graph G = (V, E) and a “source” vertex s
in V, find the minimum cost paths from s to every vertex in V
• Many variations:› unweighted vs. weighted› cyclic vs. acyclic› pos. weights only vs. pos. and neg. weights › etc
Shortest paths 5
Why study shortest path problems?
• Traveling on a budget: What is the cheapest airline schedule from Seattle to city X?
• Optimizing routing of packets on the internet:› Vertices are routers and edges are network links with
different delays. What is the routing path with smallest total delay?
• Shipping: Find which highways and roads to take to minimize total delay due to traffic
• etc.
Shortest paths 6
Unweighted Shortest PathProblem: Given a “source” vertex s in an unweighted
directed graph G = (V,E), find the shortest path from s to all vertices in
G
A
C
B
D
F H
G
E
Source
Only interested
in path lengths
Shortest paths 7
Breadth-First Search Solution
• Basic Idea: Starting at node s, find vertices that can be reached using 0, 1, 2, 3, …, N-1 edges (works even for cyclic graphs!)
A
C
B
D
F H
G
E
Shortest paths 8
Breadth-First Search Alg.• Uses a queue to track vertices that are “nearby”• source vertex is s
Distance[s] := 0Enqueue(Q,s); Mark(s)//After a vertex is marked once
// it won’t be enqueued againwhile queue is not empty do
X := Dequeue(Q);for each vertex Y adjacent to X do
if Y is unmarked thenDistance[Y] := Distance[X] + 1;Previous[Y] := X;//if we want to record pathsEnqueue(Q,Y); Mark(Y);
• Running time = O(|V| + |E|)
Shortest paths 9
Example: Shortest Path length
A
C
B
D
F H
G
E
0
Queue Q = C
Shortest paths 10
Example (ct’d)
A
C
B
D
F H
G
E
0
Queue Q = A D E
1
1
1
Previouspointer
Indicates the vertex is marked
Shortest paths 11
Example (ct’d)
A
C
B
D
F H
G
E
0
Q = D E B
1
1
1
2
Shortest paths 12
Example (ct’d)
A
C
B
D
F H
G
E
0
Q = B G
1
1
1
2
2
Shortest paths 13
Example (ct’d)
A
C
B
D
F H
G
E
0
Q = F
1
1
1
2
2
3 4
Shortest paths 14
Example (ct’d)
A
C
B
D
F H
G
E
0
Q = H
1
1
1
2
2
3
Shortest paths 15
What if edges have weights?
• Breadth First Search does not work anymore › minimum cost path may have more edges than
minimum length path
A
C
B
D
F H
G
E
2 3
2 1
1
42
11
93
8
3
Shortest path (length)from C to A:C A (cost = 9)
Minimum Cost Path = C E D A(cost = 8)
Shortest paths 16
Dijkstra’s Algorithm for Weighted Shortest Path
• Classic algorithm for solving shortest path in weighted graphs (without negative weights)
• A greedy algorithm (irrevocably makes decisions without considering future consequences)
• Each vertex has a cost for path from initial vertex
Shortest paths 17
Dijkstra’s Algorithm
• Edsger Dijkstra(1930-2002)
Shortest paths 18
Basic Idea of Dijkstra’sAlgorithm (1959)
• Find the vertex with smallest cost that has not been “marked” yet.
• Mark it and compute the cost of its neighbors.• Do this until all vertices are marked.• Note that each step of the algorithm we are
marking one vertex and we won’t change our decision: hence the term “greedy” algorithm
• Works for directed and undirected graphs
Shortest paths 19
Dijkstra’s Shortest Path Algorithm
• Initialize the cost of s to 0, and all the rest of the nodes to ∞
• Initialize set S to be ∅› S is the set of nodes to which we have a shortest path
• While S is not all vertices› Select the node A with the lowest cost that is not in S
and identify the node as now being in S› for each node B adjacent to A
• if cost(A)+cost(A,B) < B’s currently known cost– set cost(B) = cost(A)+cost(A,B)– set previous(B) = A so that we can remember the path
Shortest paths 20
Example: Initialization
v1
v7v6
v2
v5v3 v4
4 1
2
103
64
22
85
1
0 ∞
∞ ∞
∞
Pick vertex not in S with lowest cost.
∞ ∞
Cost(source) = 0 Cost(all vertices but source) = ∞
Shortest paths 21
Example: Update Cost neighbors
v1
v7v6
v2
v5v3 v4
4 1
2
103
64
22
85
1
0 2
∞ ∞
1
∞ ∞
Cost(v2) = 2Cost(v4) = 1
Shortest paths 22
Example: pick vertex with lowest cost and add it to S
Pick vertex not in S with lowest cost, i.e., v4
v1
v7v6
v2
v5v3 v4
4 1
2
103
64
22
85
1
0 2
∞ ∞
1
∞ ∞
Shortest paths 23
Example: update neighbors
v1
v7v6
v2
v5v3 v4
4 1
2
103
64
22
85
1
0 2
3 3
1
9 5
Cost(v3) = 1 + 2 = 3 Cost(v5) = 1 + 2 = 3 Cost(v6) = 1 + 8 = 9 Cost(v7) = 1 + 4 = 5
Shortest paths 24
Example (Ct’d)
v1
v7v6
v2
v5v3 v4
4 1
2
103
64
22
85
1
0 2
3 3
1
Pick vertex not in S with lowest cost (v2) and update neighbors
9 5
Note : cost(v4) not updated since already in S and cost(v5) not updated since it is larger than previously computed
Shortest paths 25
Example: (ct’d)
v1
v7v6
v2
v5v3 v4
4 1
2
103
64
22
85
1
0 2
3 3
1
Pick vertex not in S (v5) with lowest cost and update neighbors
9 5No updating
Shortest paths 26
Example: (ct’d)
v1
v7v6
v2
v5v3 v4
4 1
2
103
64
22
85
1
0 2
3 3
1
Pick vertex not in S with lowest cost (v7) and update neighbors
8 5
Shortest paths 27
Example: (ct’d)
v1
v7v6
v2
v5v3 v4
4 1
2
103
64
22
85
1
0 2
3 3
1
6 5
Pick vertex not in S with lowest cost (v7) and update neighbors
Cost(v6) = min (8, 5+1) = 6
Previous cost
Shortest paths 28
Example (end)
v1
v7v6
v2
v5v3 v4
4 1
2
103
64
22
85
1
0 2
3 3
1
Pick vertex not in S with lowest cost (v6) and update neighbors
6 5
Shortest paths 29
Data Structures• Adjacency Lists
1234567
2 2G
0∞∞∞∞∞∞
C4 1
v1
v7v6
v2
v5v3 v4
4 1
2
103
64
22
85
1
4 3 5 101 4 6 53 2 5 27 6
6 1
6 8
7 4
nextcost
adj
Priority queue for finding and deleting lowest cost vertexand for decreasing costs (Binary Heap works)
P Q
previous cost priority queue pointers
Shortest paths 30
Priority Queue
v1
v7v6
v2
v5v3 v4
4 1
2
103
64
22
85
1
0 2
∞ ∞1
∞ ∞
2
5 7
3 6
02∞1∞∞∞
C
14
253
Q1234567
node number1
2 3
54
index in heap
1
1
Before the update, butafter find min.,i.e., v1 and v4have been “deletemin”
This is somewhat arbitrary and depends when the heap was first built
Shortest paths 31
Priority Queue
v1
v7v6
v2
v5v3 v4
4 1
2
103
64
22
85
1
0 2
3 ∞1
∞ ∞
2
5 7
3 6
0231∞∞∞
C
14
253
Q1234567
node number1
2 3
54
index in heap
141
update node 3decreasecost
Shortest paths 32
Priority Queue
v1
v7v6
v2
v5v3 v4
4 1
2
103
64
22
85
1
0 2
3 ∞1
∞ ∞
2
3 7
5 6
0231∞∞∞
C
12
453
Q1234567
node number1
2 3
54
index in heap
141
percolate up
Shortest paths 33
Time Complexity
• n vertices and m edges• Initialize data structures O(n+m)• Find min cost vertices O(n log n)
› n delete mins• Update costs O(m log n)
› Potentially m updates• Update previous pointers O(m)
› Potentially m updates• Total time O((n + m) log n) - very fast.
Shortest paths 34
Correctness
• Dijkstra’s algorithm is an example of a greedy algorithm
• Greedy algorithms always make choices that currently seem the best› Short-sighted – no consideration of long-term or global
issues› Locally optimal does not always mean globally optimal
• In Dijkstra’s case – choose the least cost node, but what if there is another path through other vertices that is cheaper?
Shortest paths 35
THE KNOWNCLOUD
G Next shortest path from inside the known cloud
P
“Cloudy” Proof
• If the path to G is the next shortest path, the path to P must be at least as long. Therefore, any path through P to G cannot be shorter!
Source
Least cost node
Shortest paths 36
Inside the Cloud (Proof)
• Everything inside the cloud has the correct shortest path
• Proof is by induction on the number of nodes in the cloud:› Base case: Initial cloud is just the source with
shortest path 0› Inductive hypothesis: cloud of k-1 nodes all have
shortest paths› Inductive step: choose the least cost node G
has to be the shortest path to G (previous slide). Add k-th node G to the cloud