Вы находитесь на странице: 1из 5

Single-Source Shortest Paths (Ch.

24)
The single-source shortest path problem (SSSP)

Single-Source Shortest Paths


Weights in SSSP algorithms include distances, times, hops,

input: a directed graph G = (V, E) with edge weights, and a

cost, etc. Used in lots of routing applications.

specific source node s.


Note: BFS finds the shortest paths for the special case when all

goal: find a minimum weight (shortest) path from s to every other

edge weights are 1. Running time = O(V + E)

node in V (sometimes we only want the cost of these paths)


It turns out that, in the worst case, finding the shortest path between s and a single

The result of a SSSP algorithm can be viewed as a tree

node t is no easier than finding the shortest paths between s and all other nodes

rooted at s, containing a shortest ( wrt weight) path from

sb=2
sc=5
sd=6
se=5
sf=3

sg=
sh=7
S

1
g

s to all other nodes.

2
h

3
f

Negative-weight cycles

1
g

6
2

Single-Source Shortest Paths

Some graphs may have negative-weight cycles and these are

Question: Can a shortest path contain a cycle?

a problem for SSSP algorithms.


What is the shortest path from a to d?
path a-c-e-d = -12+3-2 = -11
path a-c-e-d-a-c-e-d =
-12+3-2+(10-12+3-2)= -13

b
8

2
-12

c
6

No.

9
3

Suppose we have a shortest path p = v0, v1, ..., v k and

6
e

10

d
If we keep going around the cycle
(d-a-c-e-d), we keep shortening the
weight of the path. So the shortest path has weight -

and w(c) > 0. Then the path (obtained from splicing out c)
p' = v0, v1, ..., vi, v j+1, vj+2,...vk

-2

To avoid this problem, we require that the graph has no negative


weight cycles, otherwise the solution does not exist.

has w(p') = w(p) w(c) < w(p). So p cant be a shortest path.


Therefore, we can assume, wlog, that shortest paths have no cycles.
3

Dijkstras SSSP Algorithm

Dijkstras SSSP Algorithm

Algorithm SSSP-Dijkstra (G, s)


1. Q = //** Q is a priority queue
Tree nodes are
2. d[s] = 0 and insert s into Q
nodes extracted
3. for each v s
from Q (in S,
4.
d[v] =
the set of
5.
insert v into Q with key d[v]
shortest paths).
6. while Q
7.
u = extract-min(Q)
8.
for each outgoing neighbor of u
9.
d[v] = min{d[v], d[u] + wt(u,v)}
10.
endfor
Procedure relax (x, y)
d[y] = min{d[y], d[x] + wt(x, y)}
11. endwhile
Fringe nodes are nodes in Q with d[v] <
Unseen nodes are nodes in Q with d[v] =

c = vi, vi+1, ..., vj is a positive-weight cycle on p so that v i = vj

Algorithm SSSP- Dijkstra (G, s)


1. Q = //** Q is a priority queue
2. d[s] = 0 and insert s into Q
3. for each v s
4.
d[v] =
5.
insert v into Q with key d[v]
6. while Q
7.
u = extract-min(Q)
8.
for each (outgoing) neighbor of u
9.
d[v] = min{d[v], d[u] + wt(u,v)}
10.
endfor
2
11. endwhile

Trace execution
of Dijkstras algorithm
on graph below.

12

10

6
e

6
5

Algorithm SSSP- Dijkstra (G, s)


1. Q = //** Q is a priority queue
2. d[s] = 0 and insert s into Q
3. for each v s
4.
d[v] =
5.
insert v into Q with key d[v]
6. while Q
7.
u = extract-min(Q)
8.
for each neighbor of u
9.
d[v] = min{d[v], d[u] + wt(u,v)}
10.
endfor
11. endwhile

iteration 0:
Q a b c d e
d[v] 0
iteration 1: Q = Q {a}
Q xa b c d e
d[v] 0 2 12
iteration 2: Q = Q {b}
Q xa xb c d
d[v] 0 2 10

e
11
2
S

iteration 3: Q = Q {c}
Q xa x
b x
c d e
d[v] 0 2 10 16 11

12

10

b
8
c
6
d

12
10

b
8
c
6

12

b
8
c
6

9
3

Dijkstras SSSP Algorithm

2. Suppose we change line 6 to while |Q| > 1


This change causes the while loop to execute |V| - 1 times
instead of |V| times. Is this proposed algorithm correct? Why or
10
why not?

4
2

Running Time of Dijkstras SSSP Alg


Algorithm SSSP- Dijkstra (G, s)
Assume adjacency list
1. Q = //** Q is a priority queue
representation. The time
2. d[s] = 0 and insert s into Q
will depend on what?
3. for each v s
4.
d[v] =
Implementation of the
5.
insert v into Q with key d[v]
priority queue.
6. while Q
7.
u = extract-min(Q)
In general,
8.
for each neighbor of u
Steps 1-5: O(V) time
9.
d[v] = min{d[v], d[u] + wt(u,v)}
Steps 6-11:
10.
endfor
V iterations
11. endwhile

Steps 8-10: E iterations overall


(looks at each edge once)
Suppose decrease-key takes O(Y Q) time.
Total: O(VX Q + EY Q)

DONE

Algorithm SSSP- Dijkstra (G, s)


Exercise:
1. Q = //** Q is a priority queue
2. d[s] = 0 and insert s into Q
1. Give a simple example
3. for each v s
of a directed graph with
4.
d[v] =
negative weight edges
5.
insert v into Q with key d[v]
(but no neg wt cycles)
6. while Q
7.
u = extract-min(Q)
for which Dijkstras alg
8.
for each neighbor of u
produces incorrect
9.
d[v] = min{d[v], d[u] + wt(u,v)}
answers.
10.
endfor
11. endwhile

Q xa xb x
c x
d x
e
d[v] 0 2 10 13 11

10

9
3

iteration 5: Q = Q {d}

u d[a] d[b] d[c] d[d] d[e]

- 0

a x
2
12
b x
x
10 11
c x
x
x
16 11
e x
x
x
13
x
d x
x
x
x
x
2

Q xa x
b xc d x
e
d[v] 0 2 10 13 11

9
3

Dijkstras SSSP Algorithm


iter
Example: 0
1
2
3
4
5

iteration 4: Q = Q {e}

Algorithm SSSP- Dijkstra (G, s)


1. Q = //** Q is a priority queue
2. d[s] = 0 and insert s into Q
3. for each v s
4.
d[v] =
5.
insert v into Q with key d[v]
6. while Q
7.
u = extract-min(Q)
8.
for each neighbor of u
9.
d[v] = min{d[v], d[u] + wt(u,v)}
10.
endfor
11. endwhile

Suppose extract-min
takes O(XQ) time
11

Running Time of Dijkstras SSSP Alg


If G is dense (i.e., (V2) edges):
no point in being clever about extract-min. Store each
d[v] in the vth entry of an array. Each insert and decreasekey takes O(1) time. Extract-min takes O(V) time (why?)
Total time: O(V2 + E) = O(V 2)
If G is sparse (i.e., o(V 2) edges:
try to minimize extract-min, use binary heap (heapsort)
time for extract-min & decrease-key = O(lgV)
Total time: O(VlgV + ElgV) = O((V + E) lgV)

12

Correctness of Dijkstras SSSP Alg

Correctness of Dijkstras SSSP Alg

Let S = V Q be the nodes that have been removed from Q.


Lemma: For all nodes x V:
(a) if x S (x Q), then the shortest s to x path only uses nodes in S and
d[x] is its weight.
(b) if x S (x Q), then d[x] is weight of the shortest s to x path, all of
whose intermediate nodes are in S.

Lemma: For all nodes x V:


(a) if x S (x Q), then the shortest s to x path only uses nodes in S and
d[x] is its weight.
(b) if x S (x Q), then d[x] is weight of the shortest s to x path, all of
whose intermediate nodes are in S.

Induction Step: Show Lemma is true for iteration i.

Proof:
By induction on i, the number of iterations of the while loop.

Let u be the node selected in the ith iteration


(the node in Q with minimum d[u] value).

Basis: i = 1, S = {s}, and d[x] = if x is not a neighbor of s, and


otherwise d[x] = wt(s,x). So both (a) and (b) hold.

Proof of (a): (Assume x is in S)


case 1: x u. Then x was in S before iteration i, and by the IHOP,
we already had the best s to x path.

Inductive Hypothesis (IHOP): Assume true for iteration i -1.


13

Correctness of Dijkstras SSSP Alg

Correctness of Dijkstras SSSP Alg

Lemma: For all nodes x V:


(a) if x S (x Q), then the shortest s to x path only uses nodes in S and d[x] is
its weight.
(b) if x S (x Q), then d[x] is weight of the shortest s to x path, all of whose
intermediate nodes are in S.

case 2: x = u. Suppose in contradiction the shortest s to u path uses


some node r not in S after iteration i.
d[u] is wt of shortest s to u path with all
intermed nodes in S (IHOP)
d[r] is wt of shortest s to r path with all
intermed nodes in S (IHOP)
s
u
d[u] d[r] since alg picks u = x in
iteration i.
S (before i)

So...the shortest s to u path cant go


through r since there are no negative
weight edges.

Proof of (b) (cont):


Choose x that is not in S after iteration i (x u):

u
x

S (before i)

d[x] is wt of shortest s to x path with all


intermed. nodes in S (IHOP)
d[u] is wt of shortest s to u path by (a)

case 1: x is not a neighbor of u. Then


adding u to S doesnt change the best s
to x path with all internal nodes in S.
To see why not, suppose it did:
u there is a better s to v obtained by
going outside S...contradicts part (a)
so best s to x path and d[x] cant
x
change.
16

If s
u v is a shortest path in G for some u,v V, and if
d[u] = (s, u) at any time prior to relaxing edge (u, v), then
d[v] = (s, v) at all times afterward.

S (before i)

S (before i)

The Convergence property

Correctness of Dijkstras SSSP Alg

Proof of (b): (Assume x is not in S)


Choose x that is not in S after iteration i (x u):

case 2: x is a neighbor of u.
The algorithms checks to see if it is
better to go from s to x via u, or to
choose an edge from some other node
in S.

Exercise:
Why doesnt the proof of correctness for Dijkstras algorithm
go through when negative edge weights are allowed?

17

Algorithm SSSP-Dijkstra (G, s)


1. Q = //** Q is a priority queue
2. d[s] = 0 and insert s into Q
3. for each v s
4.
d[v] =
5.
insert v into Q with key d[v]
6. while Q
7.
u = extract-min(Q)
8.
for each neighbor of u
9.
relax (u, v)
10.
endfor
11. endwhile

Dijkstra's algorithm relies


on the property that a shortest
path is made up of segments
that are smaller shortest paths

Procedure relax (x,y)


d[y] = min{d[y], d[x] + wt(x,y)}
18

Bellman-Ford SSSP Algorithm

The Path-relaxation property


If p = v0, v1, ..., vk is a shortest path from s = v 0 to vk, and
the edges of p are relaxed in the order (v 0, v1), (v1, v2),...
(vk-1, vk), then d[vk] = (s, vk). This property holds regardless
of any other relaxation steps that occur, even if these other
steps are intermixed with relaxations of the edges of p.
Algorithm SSSP-Dijkstra (G, s)
1. Q = //** Q is a priority queue
2. d[s] = 0 and insert s into Q
3. for each v s
4.
d[v] =
5.
insert v into Q with key d[v]
6. while Q
7.
u = extract-min(Q)
8.
for each neighbor of u
9.
relax (u, v)
10.
endfor
11. endwhile

Note: Dijkstra's algorithm does


not ensure that the edges are
examined in any particular order.
but it does ensure that, at the time
u is extracted from Q,
d[u] = (s, u).

Computes single-source shortest paths even when some edges


have negative weight.
Can detect if there are any negative-weight cycles in the graph.

The algorithm has 2 parts:


Part 1: Computing shortest paths tree:
|V| - 1 iterations.
Iteration i computes the shortest path
from s using paths of up to i edges.
Part 2: Checking for negative-weight cycles.

Procedure relax (x,y)


d[y] = min{d[y], d[x] + wt(x,y)}
19

Bellman-Ford SSSP Algorithm


Algorithm SSSP-Bellman-Ford (G, s)
1. d[s] = 0
2. for each v s
3.
d[v] =
4. for i = 1 to |V| - 1
5.
for each edge (u, v) E
6.
d[v] = min{d[v], d[u] + wt(u,v)}
7. for each edge (u, v) E
8.
if d[v] > d[u] + wt(u, v)
9.
return false
10. return true

20

Bellman-Ford SSSP Algorithm

Note:
This is same
decrease-key
operation as in
Dijkstras alg.

Algorithm SSSP-Bellman-Ford (G, s)


1. d[s] = 0
2. for each v s
Procedure relax (x,y)
3.
d[v] =
d[y] = min{d[y], d[x] + wt(x,y)}
4. for i = 1 to |V| - 1
5.
for each edge (u, v) E
6.
relax(u,v)
7. for each edge (u, v) E
8.
if d[v] > d[u] + wt(u, v)
9.
return false
10. return true

21

Correctness of Bellman-Ford
Algorithm

22

Correctness of Bellman-Ford
Algorithm
Theorem:
Suppose there are no negative-weight cycles in G.
Then the Bellman-Ford algorithm returns true with
d[v] = (s,v) for all vertices v reachable from s. If there is
a negative cycle, Bellman-Ford returns false.

Theorem
Suppose there are no negative-weight cycles in G.
After |V| - 1 iterations of the for loop, d[v] = (s,v) for all
vertices v that are reachable from s.
Proof:
-If v is reachable from s, then there is an acyclic path from
s to v, say s = u 0, u1, u2, ..., uk = v, where k < |V|.
-There are k edges in this path.
-By the path relaxation property, after the first pass, u0, u1 is
a shortest path; after the second pass, u 0, u 1, u2 is a shortest
path; after k passes, u 0, u1, u2, ..., uk is a shortest path.
23

Proof:
-For the first case, see the previous theorem.
-For the second case, there is a path of length at least
|V| whose cost is less than the shortest acyclic path, so
we have d[v] > d[u] + wt(u,v) for some u,v in the path.

24

Complexity of Bellman-Ford
Algorithm

SSSPs in DAGs
If the graph is a DAG, we can use a topological sort
on the vertices and compute the shortest path from a
single source in O(V + E) time

Initialization = O(V)
decrease-key is called (|V| - 1) |E| times
Test for any negative-weight cycle = O(E)

input: directed acyclic graph (DAG)


output: ordering of nodes s.t. if (u,v) E, then u comes before
v in ordering
Topological-Sort (G)
1. call DFS(G) to compute finishing times f[v] for each v
2. as each vertex is finished, insert it at front of a linked list
3. return the linked list of vertices

Total: O(VE) -- so more expensive than


Dijkstra's, but also more general, since it works
for graphs with negative edge weights.

25

26

Alternate topological sort

Property of a DAG

The in-degree of vertex u is the number of incoming


edges incident on u. The out-degree of vertex u is
the number of outgoing edges incident on u.
input: directed acyclic graph (DAG)
output: ordering of nodes s.t. if (u,v) E, then u comes before
v in ordering
Topological-Sort (G)
1. while V is not empty
2.
remove a vertex u of in-degree 0 and all its outgoing edges.
3.
insert u at the tail of a linked list of vertices

Why does the previous algorithm work?


Claim: a DAG G must have some vertex with no
incoming edges. Why?
Suppose, in contradiction, that every vertex in G has at least
one incoming edge. Choose a vertex v0. Trace the edge
incoming at v 0 to its source, v 1. Since v 1 must have an
incoming edge, we can follow that edge to its source, v2. If we
continue backtracking in this fashion, since there are a finite
number of vertices, we will eventually return to a previously
visited vertex. At this point, we will have discovered a cycle,
which is a contradiction to our assumption that G is a DAG.
Therefore, a DAG has at least one vertex with no incoming
edge (a similar argument holds for outgoing edges).

How could we implement this to run in O(V + E) time?


27

28

SSSPs in DAGs

SSSPs in DAGs

Note: This algorithm will work with negative weight edges


in the DAG.

In a topologically sorted list of vertices, all edges will go from


left to right. Once all outgoing edges at a node u have been
relaxed, u will never be revisited. Since we process the nodes
in topologically-sorted order, the nodes at 1 hop from s will be
finished before those at 2 hops, etc. By the path-relaxation
property, all shortest paths will be found.

input: directed acyclic graph (DAG)


output: ordering of nodes s.t. if (u,v) E, then u comes before
v in ordering
DAG-Shortest-Paths (G, s)
1. Topological-Sort(G)
2. d[s] = 0
3. for each v s
4.
d[v] =
5. for each vertex u, taken in topologically sorted order
6.
for each vertex v adjacent to u
7.
relax(u, v)

s
0
1

q
2
7

r
-3

List the shortest path distance from s to every other node in the
above graph.
29

30

Вам также может понравиться