Vous êtes sur la page 1sur 19

Shortest Paths

0
8 A 4
2
8 7 2 1 3
B C D

5 3 9 8
2 5
E F

Shortest Paths 1
Outline and Reading
Weighted graphs (§7.1)
 Shortest path problem
 Shortest path properties
Dijkstra’s algorithm (§7.1.1)
 Algorithm
 Edge relaxation
The Bellman-Ford algorithm (§7.1.2)
Shortest paths in dags (§7.1.3)
All-pairs shortest paths (§7.2.1)

Shortest Paths 2
Weighted Graphs
In a weighted graph, each edge has an associated numerical
value, called the weight of the edge
Edge weights may represent, distances, costs, etc.
Example:
 In a flight route graph, the weight of an edge represents the
distance in miles between the endpoint airports

PVD
ORD
SFO
LGA
HNL
LAX DFW
MIA
Shortest Paths 3
Shortest Path Problem
Given a weighted graph and two vertices u and v, we want to
find a path of minimum total weight between u and v.
 Length of a path is the sum of the weights of its edges.
Example:
 Shortest path between Providence and Honolulu
Applications
 Internet packet routing
 Flight reservations
 Driving directions
PVD
ORD
SFO
LGA
HNL
LAX DFW
MIA
Shortest Paths 4
Shortest Path Properties
Property 1:
A subpath of a shortest path is itself a shortest path
Property 2:
There is a tree of shortest paths from a start vertex to all the other
vertices
Example:
Tree of shortest paths from Providence

PVD
ORD
SFO
LGA
HNL
LAX DFW
MIA
Shortest Paths 5
Dijkstra’s Algorithm
The distance of a vertex We grow a “cloud” of vertices,
v from a vertex s is the beginning with s and eventually
length of a shortest path covering all the vertices
between s and v We store with each vertex v a
Dijkstra’s algorithm label d(v) representing the
computes the distances distance of v from s in the
of all the vertices from a subgraph consisting of the cloud
given start vertex s and its adjacent vertices
Assumptions: At each step
 the graph is connected  We add to the cloud the vertex
 the edges are u outside the cloud with the
undirected smallest distance label, d(u)
 the edge weights are  We update the labels of the
nonnegative vertices adjacent to u

Shortest Paths 6
Edge Relaxation
Consider an edge e = (u,z)
such that d(u) = 50
 u is the vertex most recently d(z) = 75
u e
added to the cloud s z
 z is not in the cloud

The relaxation of edge e


updates distance d(z) as
follows:
d(u) = 50
d(z)  min{d(z),d(u) + weight(e)} d(z) = 60
u e
s z

Shortest Paths 7
Example
0 0
8 A 4 8 A 4
2 2
8 7 2 1 4 8 7 2 1 3
B C D B C D

 3 9  5 3 9 8
2 5 2 5
E F E F

0 0
8 A 4 8 A 4
2 2
8 7 2 1 3 7 7 2 1 3
B C D B C D

5 3 9 11 5 3 9 8
2 5 2 5
E F E F
Shortest Paths 8
Example (cont.)
0
8 A 4
2
7 7 2 1 3
B C D

5 3 9 8
2 5
E F
0
8 A 4
2
7 7 2 1 3
B C D

5 3 9 8
2 5
E F

Shortest Paths 9
Dijkstra’s Algorithm
A priority queue stores Algorithm DijkstraDistances(G, s)
the vertices outside the Q  new heap-based priority queue
for all v  G.vertices()
cloud
if v = s
 Key: distance setDistance(v, 0)
 Element: vertex else
Locator-based methods setDistance(v, )
 insert(k,e) returns a l  Q.insert(getDistance(v), v)
locator setLocator(v,l)
 replaceKey(l,k) changes while Q.isEmpty()
the key of an item u  Q.removeMin()
for all e  G.incidentEdges(u)
We store two labels { relax edge e }
with each vertex: z  G.opposite(u,e)
 Distance (d(v) label) r  getDistance(u) + weight(e)
 locator in priority if r < getDistance(z)
queue setDistance(z,r)
Q.replaceKey(getLocator(z),r)
Shortest Paths 10
Analysis
Graph operations
 Method incidentEdges is called once for each vertex
Label operations
 We set/get the distance and locator labels of vertex z O(deg(z)) times
 Setting/getting a label takes O(1) time
Priority queue operations
 Each vertex is inserted once into and removed once from the priority
queue, where each insertion or removal takes O(log n) time
 The key of a vertex in the priority queue is modified at most deg(w)
times, where each key change takes O(log n) time
Dijkstra’s algorithm runs in O((n + m) log n) time provided the
graph is represented by the adjacency list structure
 Recall that Sv deg(v) = 2m
The running time can also be expressed as O(m log n) since the
graph is connected
Shortest Paths 11
Extension
Using the template Algorithm DijkstraShortestPathsTree(G, s)
method pattern, we
can extend Dijkstra’s …
algorithm to return a
tree of shortest paths for all v  G.vertices()

from the start vertex
setParent(v, )
to all other vertices

We store with each
vertex a third label: for all e  G.incidentEdges(u)
 parent edge in the { relax edge e }
shortest path tree z  G.opposite(u,e)
In the edge relaxation r  getDistance(u) + weight(e)
step, we update the if r < getDistance(z)
parent label setDistance(z,r)
setParent(z,e)
Q.replaceKey(getLocator(z),r)

Shortest Paths 12
Why Dijkstra’s Algorithm
Works
Dijkstra’s algorithm is based on the greedy
method. It adds vertices by increasing distance.
 Suppose it didn’t find all shortest
distances. Let F be the first wrong 0
vertex the algorithm processed. 8 A 4
 When the previous node, D, on the 2
7 2 3
true shortest path was considered, B
7
C
1
D
its distance was correct.
3 9
 But the edge (D,F) was relaxed at 2
5 8
5
that time! E F

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


distance cannot be wrong. That is,
there is no wrong vertex.

Shortest Paths 13
Why It Doesn’t Work for
Negative-Weight Edges
Dijkstra’s algorithm is based on the greedy
method. It adds vertices by increasing distance.

0
8 A 4
 If a node with a negative 6
incident edge were to be added 7 7 5 1 4
late to the cloud, it could mess B C D
up distances for vertices already 0 -8
5 9
in the cloud. 2 5
E F

C’s true distance is 1, but


it is already in the cloud
with d(C)=5!
Shortest Paths 14
Bellman-Ford Algorithm
Works even with negative- Algorithm BellmanFord(G, s)
weight edges for all v  G.vertices()
Must assume directed if v = s
edges (for otherwise we setDistance(v, 0)
would have negative- else
weight cycles) setDistance(v, )
for i  1 to n-1 do
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)
a negative-weight cycle if it r  getDistance(u) + weight(e)
exists if r < getDistance(z)
setDistance(z,r)
 How?

Shortest Paths 15
Bellman-Ford Example
Nodes are labeled with their d(v) values
8 0 4 8 0 4
-2 -2
7 1 8 7 -2 1 4
     
3 9 3 9
-2 5 -2 5
   

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

Shortest Paths 17
1

DAG Example
Nodes are labeled with their d(v) values
1 1
8 0 4 8 0 4
-2 -2
3 7 2 1 4 3 8 7 2 -2 1 4 4
     
3 9 3 9
-5 5 -5 5
   
6 5 6 5

1 1
8 0 4 8 0 4
-2 -2
3 5 7 2 1 4
-1 3 7 2 1 4
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 (two steps) 18
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

Vous aimerez peut-être aussi