Shortest Paths Weighted Graphs Shortest Paths Shortest Path

Shortest Path
5/2/2017 6:10 PM
Presentation for use with the textbook, Algorithm Design and
Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015
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:

Shortest Paths



In a flight route graph, the weight of an edge represents the
distance in miles between the endpoint airports
PVD
ORD
SFO
LGA
HNL
© 2015 Goodrich and Tamassia
Shortest Paths
1
Shortest Paths

© 2015 Goodrich and Tamassia
Example:

Applications




A subpath of a shortest path is itself a shortest path
There is a tree of shortest paths from a start vertex to all the other
vertices
Example:
Internet packet routing
Flight reservations
Driving directions
Tree of shortest paths from Providence
PVD
ORD
LGA
HNL
LAX
DFW
Shortest Paths


The distance of a vertex
v from a vertex s is the
length of a shortest path
between s and v
Dijkstra’s algorithm
computes the distances
of all the vertices from a
given start vertex s
Assumptions:



the graph is connected
the edges are
undirected
the edge weights are
nonnegative
© 2015 Goodrich and Tamassia



3

Shortest Paths
DFW
© 2015 Goodrich and Tamassia
MIA
Shortest Paths
4
Edge Relaxation
We grow a “cloud” of vertices,
beginning with s and eventually
covering all the vertices
We store with each vertex v a
label D[v] representing the
distance of v from s in the
subgraph consisting of the cloud
and its adjacent vertices
At each step

LAX
MIA
Dijkstra’s Algorithm

PVD
ORD
SFO
LGA
© 2015 Goodrich and Tamassia
2
Property 2:
Shortest path between Providence and Honolulu
SFO
HNL
MIA
Shortest Paths
Property 1:
Length of a path is the sum of the weights of its edges.

DFW
Shortest Path Properties
Given a weighted graph and two vertices u and v, we want to
find a path of minimum total weight between u and v.

LAX
We add to the cloud the vertex
u outside the cloud with the
smallest distance label, D[u]
We update the labels of the
vertices adjacent to u
5

Consider an edge e = (u,z)
such that



D[u] = 50
u is the vertex most recently
added to the cloud
z is not in the cloud
s
The relaxation of edge e
updates distance d(z) as
follows:
e
D[u] = 50
D[z]  min{D[z], D[u] + weight(e)}
s
© 2015 Goodrich and Tamassia
u
Shortest Paths
u
e
D[z] = 75
z
D[z] = 60
z
6
1
Shortest Path
5/2/2017 6:10 PM
Dijkstra’s Algorithm: Details
Example
A
8
B
2
8
7
8
© 2015 Goodrich and Tamassia
Shortest Paths
7
A
A
8
B
2
7
7
5
2
C
3
2
1
E
8
F

5
A
2
7
7
C
2
3
5
4

1
9
D
3

5
F

9

© 2015 Goodrich and Tamassia
Shortest Paths
9
5
B
A
7
7
5
E
2
8
5
F
2
C
0
2
3
3
D
4
1
3
D
9
8
F
Shortest Paths
5
8
A
8
B
2
7
C
3
5
E
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
Recall that
Sv deg(v) = 2m
Shortest Paths
10
Why It Doesn’t Work for NegativeWeight Edges
Dijkstra’s algorithm is based on the greedy
method. It adds vertices by increasing distance.
0
2
We set/get the distance and locator labels of vertex z O(deg(z)) times
Setting/getting a label takes O(1) time
The running time can also be expressed as O(m log n) since the
graph is connected
4

2
7
We find all the incident edges once for each vertex
© 2015 Goodrich and Tamassia
Dijkstra’s algorithm is based on the greedy
method. It adds vertices by increasing distance.

3
1
Dijkstra’s algorithm runs in O((n + m) log n) time provided the
graph is represented by the adjacency list/map structure

8
E
Shortest Paths
Suppose it didn’t find all shortest
distances. Let w be the first wrong
vertex the algorithm processed.
When the previous node, u, on the
true shortest path was considered,
its distance was correct
But the edge (u,w) was relaxed at
that time!
Thus, so long as D[w]>D[u], w’s
distance cannot be wrong. That is,
there is no wrong vertex
3
4
2
Priority queue operations

0
Why Dijkstra’s Algorithm Works

Label operations

2

C
8
D
11
F


B

1
9
Graph operations
3
8
© 2015 Goodrich and Tamassia
2
7
5
E
2
4


D
C
3
5
B
2
8
0
Analysis of Dijkstra’s Algorithm
4
9
7
0
4
© 2015 Goodrich and Tamassia
Example (cont.)
0

A
8
D
F
2
5
E
2
1
9
E
8
B
4
2
3

2
C
0
1
9
D
8
F
3
5
If a node with a negative
incident edge were to be added
late to the cloud, it could mess
up distances for vertices already
in the cloud.
(u,w) = (D,F) in this example
11
A
8
B
2
6
7
7
C
0
5
E
0
4
5
1
-8
D
9
F
4
5
C’s true distance is 1, but
it is already in the cloud
with d(C)=5!
© 2015 Goodrich and Tamassia
Shortest Paths
12
2
Shortest Path
5/2/2017 6:10 PM
Bellman-Ford Algorithm





Bellman-Ford Algorithm: Details
Works even with negative-weight edges
Must assume directed edges (for otherwise
we would have negative-weight cycles)
Iteration i finds all shortest paths that use i
edges.
Running time: O(nm).
Can be extended to detect a negative-weight
cycle if it exists
How?

© 2015 Goodrich and Tamassia
Shortest Paths
13
© 2015 Goodrich and Tamassia
Bellman-Ford Example
0
4
7

-2
1

3
8

-2
7

9

5
0
-2
4
7
1
-2
6

-2
1


5
9
0
8
1
9
© 2015 Goodrich and Tamassia
9

4
-1
5
7
3
5
-2
1
1
9


4
-2
4

-2
-2
3

4

-2
5 8

3

8
0
8
-2
14
DAG-based Algorithm
Nodes are labeled with their D[v] values
8
Shortest Paths
4
9
Shortest Paths

-1

We can produce a specialized shortestpath algorithm for directed acyclic
graphs (DAGs)
Works even with negative-weight edges
Uses topological order
Doesn’t use any fancy data structures
Is much faster than Dijkstra’s algorithm
Running time: O(n+m).
5
15
DAG-based Algorithm: Details
© 2015 Goodrich and Tamassia
Shortest Paths
16
DAG Example
Nodes are labeled with their d(v) values
1
1
0
8
4
-2
3

7
-5

2
1

3
4
0
8
3


8
-2
2
7
9
3

6
5
-5

5
3
8
-5
© 2015 Goodrich and Tamassia
Shortest Paths
17
5

4
1

3
4
5
5
0
8
-2
7

1
0
2
4
1
9
6
1
8

4
-2
4
-2
4
1
-2
9
6
© 2015 Goodrich and Tamassia
7

4
-1
5
3
5
-5
5
Shortest Paths
2
7
0
1
3
6
4
1
-2
9
7
(two steps)
4
-1
5
5
18
3
Shortest Path
5/2/2017 6:10 PM
All-Pairs Shortest Paths




Algorithm AllPair(G) {assumes vertices 1,…,n}
Find the distance
for all vertex pairs (i,j)
between every pair of
if i = j
vertices in a weighted
D0[i,i]  0
directed graph G.
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
else
no negative edges),
D0[i,j]  + 
which takes O(nmlog n)
for k  1 to n do
time.
for i  1 to n do
for j  1 to n do
Likewise, n calls to
Dk[i,j]  min{Dk-1[i,j], Dk-1[i,k]+Dk-1[k,j]}
Bellman-Ford would take
return Dn
O(n2m) time.
We can achieve O(n3)
Uses only vertices numbered 1,…,k
i
(compute weight of this edge)
time using dynamic
j
programming (similar to Uses only vertices
Uses only vertices
the Floyd-Warshall
numbered 1,…,k-1
k
numbered 1,…,k-1
algorithm).
© 2015 Goodrich and Tamassia
Shortest Paths
19
4