L16 Graph Part05
L16 Graph Part05
L16 Graph Part05
Graph-Based Algorithms
CSE373: Design
and Analysis of
Shortest Path Problems
•
Modeling problems as graph problems:
• 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)
Shortest Path Problems
•
What is shortest path ?
§ shortest length between two vertices for an unweighted
graph:
§ smallest cost between two vertices for a weighted graph:
B 210 B
A A
450
60 190
C unweighted C weighted
graph graph
200 130
D D
E E
Shortest Path Problems
•
Input:
t x
6
• Directed graph G = (V, E) 3 9
3
4
• Weight function w : E → R s 0
2 1
2 7
k 5 3
•
Weight of path pw(=p )v0,
v1,
w( v.i 1., .vi,) vk 5 11
i 1
6
y z
•
Shortest-path weightp from u to v:
•.
All the shortest-paths algorithms start with
INITIALIZE-SINGLE-SOURCE
Relaxation
•
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
s s
updateud[v] and [v]
v u v
2 2
5 9 5 6
RELAX(u, v, w) RELAX(u, v, w)
u v u v
2 2
5 7 5 6
•.
All the single-source shortest-paths algorithms
• start by calling INIT-SINGLE-SOURCE
• then relax edges
•.
The algorithms differ in the order and how many
times they relax each edge
Dijkstra’s Algorithm
•
Single-source shortest path problem:
• No negative-weight edges: w(u, v) > 0 (u, v) E
•
Maintains two sets of vertices:
• S = vertices whose final shortest-path weights have
already been determined
•
Repeatedly select a vertex u V – S, with the
minimum shortest-path estimate d[v]
Dijkstra (G, w, s)
1. INITIALIZE-SINGLE-SOURCE(V, s) t 1 x
9
2. S← 10
2 3 4 6
s 0
3. Q ← V[G] 5 7
2
4. while Q y z
t 1 x
5. do u ← EXTRACT-MIN(Q) 10
10 9
6. S ← S {u} s 0
2 3 4 6
5 7
7. for each vertex v Adj[u] 5
2
y z
8. do RELAX(u, v, w)
Example
t 1 x t 1 x
10
8 14 8 1413
10 9 10 9
2 3 4 6 2 3 4 6
s 0 s 0
5 7 5 7
5 7 5
2
7
2
y z y z
t x t 1 x
1
8 13 9 8 9
10 9 10 9
2 4 2 3 4 6
s 0 3 6 s 0
7 5 7
5
5 7 5 7
2 2
y z y z
Dijkstra’s Pseudo Code
•
Graph G, weight function w, root s
relaxing
edges
Dijkstra (G, w, s)
1. INITIALIZE-SINGLE-SOURCE(V,(V)
s)
2. S←
O(V) build min-heap
3. Q ← V[G]
Executed O(V) times
4. while Q O(lgV)
5. do u ← EXTRACT-MIN(Q)
6. S ← S {u}
O(E) times; O(lgV)
7. for each vertex v Adj[u]
8. do RELAX(u, v, w)
Dijkstra’s Running Time
a b
-4 h i
3 -1 2
3 4
c 6 d g
5 8
s 0 5 11 - -8 3
-3
y
2 3 7
- - j
e -6 f
Negative-Weight Edges
•
s a: only one path
(s, a) = w(s, a) = 3
•
s b: only one path
(s, b) = w(s, a) + w(a, b) = -1
a b
-4 h i
3 -1 2
3 4
c 6 d g
5 8
s 0 5 11 - -8 3
-3
y
2 3 7
- - j
e -6 f
Negative-Weight Edges
•
s c: infinitely many paths
s, c, s, c, d, c, s, c, d, c, d, c
•
cycle c, d, c has positive weight (6 - 3 = 3)
a b
-4 h i
3 -1 2
3 4
c 6 d g
5 8
s 0 5 11 - -8 3
-3
y
2 3 7
- - j
e -6 f
Negative-Weight Edges
•
s e: infinitely many paths:
• s, e, s, e, f, e, s, e, f, e, f, e
• cycle e, f, e has negative weight: 3 + (- 6) = -3
• many paths from s to e with arbitrarily large negative
weights
• (s, e) = a- no shortest
b
path exists between s and e
-4 h i
• Similarly:3 (s, f) = --1, 4(s, g) = - 2 h, i, j not
3
c 6 d g reachable
5 8
s 0 5 11 - 3
from s
-8
-3
y
2 3 7
- - j
e -6 f
(s, h) = (s, i) = (s, j) =
Negative-Weight Edges
•
Negative-weight edges may form negative-weight
cycles
•
If such cycles are reachable from the source: (s, v)
is not properly defined
a b
-4 h i
3 -1 2
3 4
c 6 d g
5 8
s 0 5 11 - -8 3
-3
y
2 3 7
- - j
e -6 f
Cycles
•
Can shortest paths contain cycles?
•
Negative-weight cycles No!
•
Positive-weight cycles: No!
• By removing the cycle we can get a shorter path
•
We will assume that when we are finding shortest
paths, the paths will have no cycles
Bellman-Ford Algorithm
•
Single-source shortest paths problem
• Computes d[v] and [v] for all v V
•
Allows negative edge weights
•
Returns:
• TRUE if no negative-weight cycles are reachable from the
source s
• FALSE otherwise no solution exists
•
Idea:
• Traverse all the edges |V – 1| times, every time performing
a relaxation step of each edge
BELLMAN-FORD(V, E, w, s)
1. INITIALIZE-SINGLE-SOURCE(V, s)
2. for i ← 1 to |V| - 1
3. do for each edge (u, v) E
4. do RELAX(u, v, w)
5. for each edge (u, v) E
6. do if d[v] > d[u] + w(u, v)
7. then return FALSE
8. return TRUE
Example
t 5 x
6 -2
-3
8 7
s 0
-4
7 2
9
y z
E: (t, x), (t, y), (t, z), (x, t), (y, x), (y, z), (z, x), (z, s), (s, t), (s, y)
Example
t 5 x t x
Pass 1 5
6
Pass 2
6 4
11
6 -2 6 -2
-3 -3
8 7 8
s 0 7
s 0
-4 -4
7 2 7 2
7
7 2
9 9
y z y z
Pass 3 t 5 x Pass 4 t 5 x
2
6 4
11
2
6 4
11
6 -2 6 -2
-3 -3
8 7 8 7
s 0 s 0
-4 -4
7 2 7 2
7 2
7 2
-2
9 9
y z y z
E: (t, x), (t, y), (t, z), (x, t), (y, x), (y, z), (z, x), (z, s), (s, t), (s, y)
Detecting Negative Cycles
•
for each edge (u, v) E
•
do if d[v] > d[u] + w(u, v)
•
then return FALSE
•
return TRUE
s b s b s b
2 2 2
0 0
-3 2
-6
-3 -1
2
-8 3 -8 3 -8 3
5
5
2
c c c
4. do RELAX(u, v, w)
O(E)
5. for each edge (u, v) E
6. do if d[v] > d[u] + w(u, v)
7. then return FALSE
8. return TRUE
•
Are shortest-paths well defined in a DAG?
• Yes, (negative-weight) cycles cannot exist
DAG-SHORTEST-PATHS(G, w, s)
1. topologically sort the vertices of G (V+E)
2. INITIALIZE-SINGLE-SOURCE(V, s) (V)
6 1
r s t x y z
5 2 7 -1 -2
0 2
6
3 4 2
Example
6 1
r s t x y z
5 2 7 -1 -2
0 2 6 6
4
3 4 2
6 1
r s t x y z
5 2 7 -1 -2
0 2 6 6
5 4
3 4 2
6 1
r s t x y z
5 2 7 -1 -2
0 2 6 5 4
3
3 4 2
Example
6 1
r s t x y z
5 2 7 -1 -2
0 2 6 5 3
3 4 2