Lec-24

advertisement
CS 253: Algorithms
Chapter 24
Shortest Paths
Credit: Dr. George Bebis
Shortest Path Problems

How can we find the shortest route between two points
on a road map?

Model the problem as a graph problem:
◦ Road map is a weighted graph:
vertices = cities
edges = road segments between cities
edge weights = road distances
◦ Goal: find a shortest path between two vertices (cities)
2
Shortest Path Problem

t
Input:
3
◦ Directed graph G = (V, E)
3
◦ Weight function w : E → R

s
0
Weight of path p = v0, v1, . . . , vk
k
w( p)   w(vi 1 , vi )
1
2
5
y
9
4
3
5
x
6
2
6
7
11
z
i 1

Shortest-path weight from u to v:
δ(u, v) = min
w(p) : u
∞

p
v if there exists a path from u to v
otherwise
Note: there might be multiple shortest paths from u to v
Negative-Weight Edges

Negative-weight edges may form negative-weight cycles

If such cycles are reachable from the source,
then δ(s, v) is not properly defined!
◦ Keep going around the cycle, and get
a
w(s, v) = -  for all v on the cycle
3
s
0
5
c
y
2
e

b
-4
d
6
4
8
-3
7
3
-6
f
Therefore, negative-weight edges will not be considered here
4
g
Cycles

Can shortest paths contain cycles?
No!

Negative-weight cycles
◦ Shortest path is not well defined (we will not consider this case)

If there is a positive-weight cycle, we can get a shorter path by
removing the cycle.

Zero-weight cycles?
◦ No reason to use them
◦ Can remove them and obtain a path with the same weight
Shortest-Paths Notation
For each vertex v  V:

δ(s, v): shortest-path weight

d[v]: shortest-path weight estimate
◦ Initially, d[v]=∞
◦ d[v]δ(s,v) as algorithm progresses

[v] = predecessor of v on a shortest path from s
t
◦ If no predecessor, [v] = NIL
3
◦  induces a tree—shortest-path tree
3
s
1
2
0
5
y
9
4
3
5
x
6
6
2
7
11
z
Initialization
Alg.: INITIALIZE-SINGLE-SOURCE(V, s)
1.
2.
3.
for each v  V
do d[v] ← 
[v] ← NIL
4.
d[s] ← 0

All the shortest-paths algorithms start with
INITIALIZE-SINGLE-SOURCE
7
Relaxation Step

Relaxing an edge (u, v) = testing whether we can improve the
shortest path to v found so far by going through u
If d[v] > d[u] + w(u, v)
we can improve the shortest path to v
 d[v]=d[u]+w(u,v)
 [v] ← u
After relaxation:
d[v]  d[u] + w(u, v)
s
u
5
2
s
v
u
9
5
2
5
2
6
RELAX(u, v, w)
RELAX(u, v, w)
u
v
v
u
7
5
2
v
6
no change
Dijkstra’s Algorithm

Single-source shortest path problem:
◦ No negative-weight edges: w(u, v) > 0,  (u, v)  E

Vertices in (V – S) reside in a min-priority queue
◦ Keys in Q are estimates of shortest-path weights d[u]

Repeatedly select a vertex u  (V – S), with the minimum shortest-path
estimate d[u]

Relax all edges leaving u
STEPS
1)
Extract a vertex u from Q (i.e., u has the highest priority)
2)
Insert u to S
3)
Relax all edges leaving u
4)
Update Q
Dijkstra (G, w, s)
S=<>
Q=<s,t,x,z,y>
t
2
0

10

6
7
5

y
2

z
s
4
3
6
7
5

5
y
10

9
2
0
x
1
10
4
3
Q=<y,t,x,z>
t
9
10
s
x
1

S=<s>
2

z
Example (cont.)
t
8
10
2
0
6
7
5
5
y
S=<s,y>
2
13
14
9
10
4
3
x
1
8
9
10
s
t
x
14

1
s
2
0
Q=<z,t,x>
6
7
5
7

z
4
3
5
y
S=<s,y,z>
2
7
z
Q=<t,x>
Example (cont.)
t
1
8
2
0
t
13
9
8
4
3
6
5
y
S=<s,y,z,t>
2
7
z
Q=<x>
9
9
2
0
4
3
6
7
5
7
5
s
x
1
10
9
10
s
x
5
y
2
S=<s,y,z,t,x>
7
z
Q=<>
Dijkstra (G, w, s)
1.
INITIALIZE-SINGLE-SOURCE(V, s)
2.
S← s
3.
Q ← V[G]
4.
5.
while Q  
(V) build min-heap
Executed (V) times
do u ← EXTRACT-MIN(Q)
6.
S ← S  {u}
7.
for each vertex v  Adj[u]
8.
do RELAX(u, v, w)
9.
Update Q (DECREASE_KEY)
Running time:
(V)
(lgV)
(VlgV)
(E) times (max)
(ElgV)
(lgV)
(VlgV + ElgV) = (ElgV)
Dijkstra’s SSSP Algorithm (adjacency matrix)
2
f
a
1
S
c
1
e
a
b
c
d
d
e
new L[i] = Min{ L[i], L[k] + W[k, i] } f
1
2
b
0
c d e f
5 1  
5
3
5
b
L [.] =
a
1
4
where k is the newly-selected intermediate node
and W[.] is the distance between k and i
a b
0 1
1 0
3 5
 1
 
2 
c
3
5
0
2
1

d

1
2
0
4

e


1
4
0
5
f
2



5
0
SSSP cont.
2
f
a
1
L [.] =
5
3
new L [.] =
5
b
c
1
e
a
b
c
d
d
e
new L[i] = Min{ L[i], L[k] + W[k, i] } f
1
2
4
where k is the newly-selected intermediate node
and W[.] is the distance between k and i
a
1
b
0
c d e f
5 1  
a b c d e f
1 0 3 1 5 
a b
0 1
1 0
3 5
 1
 
2 
c
3
5
0
2
1

d

1
2
0
4

e


1
4
0
5
f
2



5
0
2
f
a
1
L [.] =
5
3
new L [.] =
5
b
c
1
e
a
b
c
d
d
e
new L[i] = Min{ L[i], L[k] + W[k, i] } f
1
2
4
where k is the newly-selected intermediate node
and W[.] is the distance between k and i
a
1
b
0
c d e
3 1 5
f

a b c d e f
1 0 3 1 5 3
a b
0 1
1 0
3 5
 1
 
2 
c
3
5
0
2
1

d

1
2
0
4

e


1
4
0
5
f
2



5
0
2
f
a
1
a
1
L [.] =
5
3
5
c
2
1
d
1
e
4
c d e f
3 1 5 3
a b c d e f
1 0 3 1 4 3
new L [.] =
b
b
0
a
b
c
d
e
f
a b
0 1
1 0
3 5
 1
 
2 
c
3
5
0
2
1

d

1
2
0
4

e


1
4
0
5
f
2



5
0
2
f
a
1
a
1
L [.] =
5
3
5
c
2
1
d
1
e
4
c d e f
3 1 4 3
a b c d e f
1 0 3 1 4 3
new L [.] =
b
b
0
a
b
c
d
e
f
a b
0 1
1 0
3 5
 1
 
2 
c
3
5
0
2
1

d

1
2
0
4

e


1
4
0
5
f
2



5
0
2
f
a
1
c
1
e
4
d
Running time:
(V2)
(ElgV)
Which one is better?
b
0
c d e f
3 1 4 3
a b c d e f
1 0 3 1 4 3
new L [.] =
2
1
L [.] =
5
3
5
b
a
1
a
b
c
d
e
f
a b
0 1
1 0
3 5
 1
 
2 
c
3
5
0
2
1

d

1
2
0
4

e


1
4
0
5
f
2



5
0
(array representation)
(Min-Heap+Adjacency List)
All-Pairs Shortest Paths
Given:
 Directed graph G = (V, E)
 Weight function w : E → R
2
4
3
1
3
8
7
1
5
4
Compute:
6
 The shortest paths between all pairs of vertices in a graph
 Result: an n × n matrix of shortest-path distances δ(u, v)

3
2
We can run Dijkstra’s algorithm once from each vertex:
◦ O(VElgV) with binary heap and adjacency-list representation
◦ if the graph is dense  O(V3lgV)
◦ We can achieve O(V3) by using an adjacency-matrix.
9
Problem 1

We are given a directed graph G=(V, E) on which each edge (u,v)
has an associated value r(u,v), which is a real number in the
range 0 ≤ r(u,v) ≤ 1 that represents the reliability of a
communication channel from vertex u to vertex v.

We interpret r(u,v) as the probability that the channel from u to v
will not fail, and we assume that these probabilities are
independent.

Design an efficient algorithm to find the most reliable path
between two given vertices.
21
Problem 1 (cont.)
◦ r(u,v) = Pr(channel from u to v will not fail)
◦ Assuming that the probabilities are independent, the reliability of a
path p=<v1,v2,…,vk> is:
r(v1,v2) r(v2,v3) … r(vk-1,vk)
Solution 1: modify Dijkstra’s algorithm
◦ Perform relaxation as follows:
if d[v] < d[u] w(u,v) then
d[v] = d[u] w(u,v)
◦ Use “EXTRACT_MAX” instead of “EXTRACT_MIN”
22
Problem 1 (cont.)

Solution 2: use Dijkstra’s algorithm without any modifications!
◦ We want to find the channel with the highest reliability, i.e.,
max p

r (u, v)
( u ,v ) p
◦ But Dijkstra’s algorithm computes
min p

w(u, v)
( u ,v ) p
◦ Take the log
lg(max p

( u ,v ) p
r (u, v))  max p

( u ,v ) p
lg(r (u, v))
Problem 1 (cont.)

Turn this into a minimization problem by taking the
negative:
 min p


( u ,v ) p
lg(r (u, v))  min p
Run Dijkstra’s algorithm using
w(u, v)   lg(r (u, v))

( u ,v ) p
 lg(r (u, v))
Download