You are on page 1of 4

Shortest Path 5/15/2002 11:40 AM

Outline and Reading


Weighted graphs (§7.1)
Shortest Paths „ Shortest path problem
„ Shortest path properties
0 Dijkstra’s algorithm (§7.1.1)
8 A 4
2 „ Algorithm
8 2 3
B
7
C
1
D „ Edge relaxation
5 3 9 8 The Bellman-Ford algorithm (§7.1.2)
2 5
E F Shortest paths in dags (§7.1.3)
All-pairs shortest paths (§7.2.1)

Shortest Paths 1 Shortest Paths 2

Weighted Graphs Shortest Path Problem


Given a weighted graph and two vertices u and v, we want to
In a weighted graph, each edge has an associated numerical find a path of minimum total weight between u and v.
value, called the weight of the edge „ Length of a path is the sum of the weights of its edges.
Edge weights may represent, distances, costs, etc. Example:
Example: „ Shortest path between Providence and Honolulu
„ In a flight route graph, the weight of an edge represents the Applications
distance in miles between the endpoint airports
„ Internet packet routing
„ Flight reservations
849 PVD Driving directions
1843 ORD 2
„
849 PVD
SFO 14 1843 ORD 2
SFO 14
802

1205

43 LGA

802
17

1205
337

7 43 LGA
138 10 17
337

HNL 2555 99 7
LAX 1233
HNL 2555 138 10
99
DFW 1120 1233
LAX DFW 1120
MIA
MIA
Shortest Paths 3 Shortest Paths 4

Shortest Path Properties Dijkstra’s Algorithm


Property 1: The distance of a vertex We grow a “cloud” of vertices,
A subpath of a shortest path is itself a shortest path v from a vertex s is the beginning with s and eventually
Property 2: length of a shortest path covering all the vertices
There is a tree of shortest paths from a start vertex to all the other between s and v
vertices
We store with each vertex v a
Dijkstra’s algorithm label d(v) representing the
Example:
computes the distances distance of v from s in the
Tree of shortest paths from Providence
of all the vertices from a subgraph consisting of the cloud
849 PVD given start vertex s and its adjacent vertices
1843 ORD 2
SFO 14 Assumptions: At each step
802

the graph is connected We add to the cloud the vertex


1205

43 LGA „ „

17 u outside the cloud with the


337

7 the edges are


138 10 „

HNL 2555 99 undirected smallest distance label, d(u)


1233
LAX DFW 1120 „ the edge weights are „ We update the labels of the
vertices adjacent to u
MIA nonnegative

Shortest Paths 5 Shortest Paths 6

1
Shortest Path 5/15/2002 11:40 AM

Edge Relaxation Example


0 0
Consider an edge e = (u,z) 8 A 4 8 A 4
such that d(u) = 50 2 2
10 d(z) = 75 8 7 2 1 4 8 7 2 1 3
„ u is the vertex most recently e B C D B C D
added to the cloud u
s z
∞ 3 9 ∞ 5 3 9 8
„ z is not in the cloud
2 5 2 5
E F E F
The relaxation of edge e
updates distance d(z) as 0 0
follows: 8 A 4 8 A 4
d(u) = 50
d(z) ← min{d(z),d(u) + weight(e)} 10 d(z) = 60 2 2
u e 8 7 2 1 3 7 7 2 1 3
s z B C D B C D

5 3 9 11 5 3 9 8
2 5 2 5
E F E F
Shortest Paths 7 Shortest Paths 8

Example (cont.) Dijkstra’s Algorithm


0 A priority queue stores Algorithm DijkstraDistances(G, s)
A 4 the vertices outside the Q ← new heap-based priority queue
8
for all v ∈ G.vertices()
2 cloud
if v = s
7 7 2 1 3 „ Key: distance
B C D setDistance(v, 0)
„ Element: vertex else
5 3 9 8 Locator-based methods setDistance(v, ∞)
2 5 insert(k,e) returns a l ← Q.insert(getDistance(v), v)
E F „
locator setLocator(v,l)
0
replaceKey(l,k) changes while ¬Q.isEmpty()
8 A 4 „
the key of an item u ← Q.removeMin()
2 for all e ∈ G.incidentEdges(u)
7 2 3 We store two labels { relax edge e }
7 1
B C D with each vertex: z ← G.opposite(u,e)
3 9 „ Distance (d(v) label) r ← getDistance(u) + weight(e)
5 8
2 5 „ locator in priority if r < getDistance(z)
E F queue setDistance(z,r)
Q.replaceKey(getLocator(z),r)
Shortest Paths 9 Shortest Paths 10

Analysis Extension
Graph operations Using the template Algorithm DijkstraShortestPathsTree(G, s)
„ Method incidentEdges is called once for each vertex method pattern, we
Label operations can extend Dijkstra’s …
„ We set/get the distance and locator labels of vertex z O(deg(z)) times algorithm to return a
tree of shortest paths for all v ∈ G.vertices()
„ Setting/getting a label takes O(1) time …
from the start vertex
Priority queue operations setParent(v, ∅)
to all other vertices
„ Each vertex is inserted once into and removed once from the priority …
queue, where each insertion or removal takes O(log n) time We store with each
„ The key of a vertex in the priority queue is modified at most deg(w) vertex a third label: for all e ∈ G.incidentEdges(u)
times, where each key change takes O(log n) time „ parent edge in the { relax edge e }
shortest path tree z ← G.opposite(u,e)
Dijkstra’s algorithm runs in O((n + m) log n) time provided the
In the edge relaxation r ← getDistance(u) + weight(e)
graph is represented by the adjacency list structure
step, we update the if r < getDistance(z)
„ Recall that Σv deg(v) = 2m parent label setDistance(z,r)
The running time can also be expressed as O(m log n) since the setParent(z,e)
graph is connected Q.replaceKey(getLocator(z),r)

Shortest Paths 11 Shortest Paths 12

2
Shortest Path 5/15/2002 11:40 AM

Why Dijkstra’s Algorithm Why It Doesn’t Work for


Works Negative-Weight Edges
Dijkstra’s algorithm is based on the greedy Dijkstra’s algorithm is based on the greedy
method. It adds vertices by increasing distance. method. It adds vertices by increasing distance.
„ Suppose it didn’t find all shortest
distances. Let F be the first wrong 0 0
vertex the algorithm processed. 8 A 4 8 A 4
2 „ If a node with a negative 6
„ When the previous node, D, on the
7 7 2 1 3 incident edge were to be added 7 7 5 1 4
true shortest path was considered, B C D B C D
late to the cloud, it could mess
its distance was correct.
3 9 up distances for vertices already 0 -8
But the edge (D,F) was relaxed at 5 8 5 9
„ 2 5 in the cloud. 2 5
that time! E F E F

„ Thus, so long as d(F)>d(D), F’s


distance cannot be wrong. That is, C’s true distance is 1, but
there is no wrong vertex. it is already in the cloud
with d(C)=5!
Shortest Paths 13 Shortest Paths 14

Bellman-Ford Algorithm Bellman-Ford Example


Nodes are labeled with their d(v) values
Works even with negative- Algorithm BellmanFord(G, s)
weight edges for all v ∈ G.vertices() 8 0 4 8 0 4
Must assume directed if v = s -2 -2
edges (for otherwise we setDistance(v, 0) 7 1 8 7 -2 1 4
else ∞ ∞ ∞ ∞ ∞ ∞
would have negative-
weight cycles) setDistance(v, ∞) 3 9 3 9
for i ← 1 to n-1 do -2 5 -2 5
Iteration i finds all shortest for each e ∈ G.edges() ∞ ∞ ∞ ∞
paths that use i edges. { relax edge e }
Running time: O(nm). u ← G.origin(e)
Can be extended to detect z ← G.opposite(u,e) 8 0 4 8 0 4
a negative-weight cycle if it r ← getDistance(u) + weight(e)
-2 -2
exists if r < getDistance(z)
setDistance(z,r) 5 8 7 1 -1 7 1
„ How? -2 4 5 -2 -1
1 3 9 3 9
-2 6 9 5 -2 4 5
∞ ∞ 1 9
Shortest Paths 15 Shortest Paths 16

DAG-based Algorithm DAG Example


Nodes are labeled with their d(v) values
1 1
Algorithm DagDistances(G, s)
for all v ∈ G.vertices() 8 0 4 8 0 4
Works even with if v = s -2 -2
negative-weight edges setDistance(v, 0) 4 8 -2 4 4
3 7 2 1 3 7 2 1
Uses topological order else ∞ ∞ ∞ ∞ ∞ ∞
Doesn’t use any fancy setDistance(v, ∞) 3 9 3 9
data structures Perform a topological sort of the vertices -5 5 -5 5
for u ← 1 to n do {in topological order} ∞ ∞ ∞ ∞
Is much faster than 6 5 6 5
for each e ∈ G.outEdges(u)
Dijkstra’s algorithm { relax edge e } 1 1
Running time: O(n+m). z ← G.opposite(u,e) 8 0 4 8 0 4
r ← getDistance(u) + weight(e)
-2 -2
if r < getDistance(z) 5 4 4
3 7 2 1 -1 3 7 2 1
setDistance(z,r) 8 -2 4 5 -2 -1
3 9 3 9
-5 1 7 5 -5 0 4 5
∞ ∞ 1 7
6 5 6 5
Shortest Paths 17 Shortest Paths (two steps) 18

3
Shortest Path 5/15/2002 11:40 AM

All-Pairs Shortest Paths


Find the distance Algorithm AllPair(G) {assumes vertices 1,…,n}
between every pair of for all vertex pairs (i,j)
vertices in a weighted if i = j
directed graph G. D0[i,i] ← 0
else if (i,j) is an edge in G
We can make n calls to
D0[i,j] ← weight of edge (i,j)
Dijkstra’s algorithm (if no else
negative edges), which D0[i,j] ← + ∞
takes O(nmlog n) time. for k ← 1 to n do
Likewise, n calls to for i ← 1 to n do
Bellman-Ford would take for j ← 1 to n do
O(n2m) time. Dk[i,j] ← min{Dk-1[i,j], Dk-1[i,k]+Dk-1[k,j]}
We can achieve O(n3) return Dn
time using dynamic i Uses only vertices numbered 1,…,k
programming (similar to (compute weight of this edge)
the Floyd-Warshall Uses only vertices
j

algorithm). numbered 1,…,k-1 k Uses only vertices


numbered 1,…,k-1
Shortest Paths 19

You might also like