Try   HackMD

Ch24: Single-Source Shortest Paths

tags: algorithm

Exercises 24.1

24.1-1 Run the Bellman-Ford algorithm on the directed graph of Figure 24.4, using vertex
z
as the source. In each pass, relax edges in the same order as in the figure, and show the
d
and
π
values after each pass. Now, change the weight of edge
(z,x)
to 4 and run the algorithm again, using
s
as the source.

Order of traverse:

zystx







row_1


cluster_1

Original


cluster_2

Pass 1



s


s



t


t



s->t


6



y


y



s->y


7



x


x



t->x


5



t->y


8



z

0
z



t->z


-4



x->t


-2



y->x


-3



y->z


9



z->s


2



z->x


7



s2

2
s



t2

5
t



s2->t2


6



y2

9
y



s2->y2


7



x2

7
x



t2->x2


5



t2->y2


8



z2

0
z



t2->z2


-4



x2->t2


-2



y2->x2


-3



y2->z2


9



z2->s2


2



z2->x2


7









row_2


cluster_3

Pass 2


cluster_4

Pass 3



s3

2
s



t3

4
t



s3->t3


6



y3

9
y



s3->y3


7



x3

6
x



t3->x3


5



t3->y3


8



z3

0
z



t3->z3


-4



x3->t3


-2



y3->x3


-3



y3->z3


9



z3->s3


2



z3->x3


7



s4

2
s



t4

4
t



s4->t4


6



y4

9
y



s4->y4


7



x4

6
x



t4->x4


5



t4->y4


8



z4

0
z



t4->z4


-4



x4->t4


-2



y4->x4


-3



y4->z4


9



z4->s4


2



z4->x4


7









row_3

Pass 4 (final)


s5

2
s



t5

4
t



s5->t5


6



y5

9
y



s5->y5


7



x5

6
x



t5->x5


5



t5->y5


8



z5

0
z



t5->z5


-4



x5->t5


-2



y5->x5


-3



y5->z5


9



z5->s5


2



z5->x5


7



The graph with

w(z,x)=4 and
s
as the source can be obtained by using the same process.

24.1-2 Prove Corollary 24.3.

Corollary 24.3
Let

G=(V,E) be a weighted, directed graph with source vertex
s
and weight function
w:ER
, and assume that
G
contains no negative-weight cycles that are reachable from s. Then, for each vertex
vV
, there is a path from
s
to
v
if and only if BELLMAN-FORD terminates with
v.d<
when it is run on
G
. textbook, p.653

Proof

We will prove this by contradiction.
Let graph

G be initialized with a call to INITIALIZE- SINGLE-SOURCE, which sets
s.d=0
and
v.d=
for
vV{s}
.
At BELLMAN-FORD's termination, assume there exists a
v.d=
on the path
p=<v0,v1,...,vk>
from
sv
, where
s=v0
and
v=vk
. Since
v.d=δ(s,v)
for all
vV
at termination,

v.d=δ(s,vk)=δ(s,vk1)+w(vk1,vk)

We know

w(vk1,vk), so
δ(s,vk1)=
. The same can be said for
δ(s,vk1)
to
δ(s,v1)
, until

δ(s,v1)=δ(s,v0)+w(s,v1)=δ(s,s)+w(s,1)δ(s,s)=

This contradicts with

s.d=0, which proves there is no path from
s
to
v
if BELLMAN-FORD terminates with
v.d=
.

Exercises 24.2

24.2-1 Run DAG-SHORTEST-PATHS on the directed graph of Figure 24.5, using vertex
r
as the source.







dag

Original


r

0
r



s


s



r->s


5



t


t



r:s->t


3



s->t


2



x


x



s->x


6



t->x


7



y


y



t:s->y


4



z


z



t:s->z


2



x->y


-1



x->z


1



y->z


-2









dag

Explore r


r

0
r



s

5
s



r->s


5



t

3
t



r:s->t


3



s->t


2



x


x



s->x


6



t->x


7



y


y



t:s->y


4



z


z



t:s->z


2



x->y


-1



x->z


1



y->z


-2









dag

Explore s


r

0
r



s

5
s



r->s


5



t

3
t



r:s->t


3



s->t


2



x

11
x



s->x


6



t->x


7



y


y



t:s->y


4



z


z



t:s->z


2



x->y


-1



x->z


1



y->z


-2









dag

Explore t


r

0
r



s

5
s



r->s


5



t

3
t



r:s->t


3



s->t


2



x

10
x



s->x


6



t->x


7



y

7
y



t:s->y


4



z

5
z



t:s->z


2



x->y


-1



x->z


1



y->z


-2









dag

Explore x


r

0
r



s

5
s



r->s


5



t

3
t



r:s->t


3



s->t


2



x

10
x



s->x


6



t->x


7



y

7
y



t:s->y


4



z

5
z



t:s->z


2



x->y


-1



x->z


1



y->z


-2









dag

Explore y


r

0
r



s

5
s



r->s


5



t

3
t



r:s->t


3



s->t


2



x

10
x



s->x


6



t->x


7



y

7
y



t:s->y


4



z

5
z



t:s->z


2



x->y


-1



x->z


1



y->z


-2









dag

Explore z (final)


r

0
r



s

5
s



r->s


5



t

3
t



r:s->t


3



s->t


2



x

10
x



s->x


6



t->x


7



y

7
y



t:s->y


4



z

5
z



t:s->z


2



x->y


-1



x->z


1



y->z


-2



24.2-2 Suppose we change line 3 of DAG-SHORTEST-PATHS to read for the first |V| - 1 vertices, taken in topologically sorted order. Show that the procedure would remain correct.

DAG-SHORTEST-PATHS(G,w,s)
topologically sort the vertices of G INITIALIZE-SINGLE-SOURCE(G,s) for each vertex u, taken in topologically sorted order for each vertex v in G.Adj[u] RELAX(u,v,w)

Proof

Let topologically-sorted

V=<v1,v2,...,vk>, then
vk
has no child nodes. This shows that there does not exist a
vk+1
for which
δ(s,vk+1)δ(s,vk)+w(vk,vk+1)
could relax. Since G.Adj[u]
=
, the for loop on line 4 would go through 0 iterations. Therefore, it is safe to only consider the first
|V|1
vertices.

Exercises 24.3

24.3-1 Run Dijkstra’s algorithm on the directed graph of Figure 24.2, first using vertex
s
as the source and then using vertex
z
as the source. In the style of Figure 24.6, show the
d
and
π
values and the vertices in set
S
after each iteration of the while loop.







row_1


cluster_1

Original


cluster_2

S = {s}



s

0
s



t


t



s->t


3



y


y



s->y


5



x


x



t->x


6



t->y


2



z


z



x->z


2



y->t


1



y->x


4



y->z


6



z->s


3



z->x


7



s2

0
s



t2

3
t



s2->t2


3



y2

5
y



s2->y2


5



x2


x



t2->x2


6



t2->y2


2



z2


z



x2->z2


2



y2->t2


1



y2->x2


4



y2->z2


6



z2->s2


3



z2->x2


7









row_2


cluster_3

S = {s, t}


cluster_4

S = {s, t, y}



s3

0
s



t3

3
t



s3->t3


3



y3

5
y



s3->y3


5



x3

9
x



t3->x3


6



t3->y3


2



z3


z



x3->z3


2



y3->t3


1



y3->x3


4



y3->z3


6



z3->s3


3



z3->x3


7



s4

0
s



t4

3
t



s4->t4


3



y4

5
y



s4->y4


5



x4

9
x



t4->x4


6



t4->y4


2



z4

11
z



x4->z4


2



y4->t4


1



y4->x4


4



y4->z4


6



z4->s4


3



z4->x4


7









row_3


cluster_5

S = {s, t, y, x}


cluster_6

S = {s, t, y, x, z} (final)



s5

0
s



t5

3
t



s5->t5


3



y5

5
y



s5->y5


5



x5

9
x



t5->x5


6



t5->y5


2



z5

11
z



x5->z5


2



y5->t5


1



y5->x5


4



y5->z5


6



z5->s5


3



z5->x5


7



s6

0
s



t6

3
t



s6->t6


3



y6

5
y



s6->y6


5



x6

9
x



t6->x6


6



t6->y6


2



z6

11
z



x6->z6


2



y6->t6


1



y6->x6


4



y6->z6


6



z6->s6


3



z6->x6


7



The graph with vertex

z as the source can be obtained by using the same process.

24.3-2 Give a simple example of a directed graph with negative-weight edges for which Dijkstra’s algorithm produces incorrect answers. Why doesn’t the proof of Theorem 24.6 go through when negative-weight edges are allowed?

DIJKSTRA(G,w,s)
INITIALIZE-SINGLE-SOURCE(G,s) S = empty set Q = G.V while Q != empty set u = EXTRACT-MIN(Q) S = UNION(S,{u}) for each vertex v in G.Adj[u] RELAX(u,v,w)

Below is an example for which Dijkstra's algorithm produces an incorrect answer. The negative-weight edge

uz creates a negative cycle
yuzy
for which an even smaller weight can always be achieved by traversing the cycle indefinitely.







row_3

S = {s, x, t, (y, u, z = negative cycle)}


s6

0
s



x6

3
x



s6->x6


3



t6

5
t



s6->t6


5



y6

8
y



x6->y6


6



x6->t6


2



z6

1
z



y6->z6


2



u6

10
u



y6->u6


1



t6->x6


1



t6->y6


4



t6->z6


6



z6->s6


3



z6->y6


7



u6->z6


-9



Theorem 24.6 (Correctness of Dijkstra’s algorithm)
Dijkstra’s algorithm, run on a weighted, directed graph

G=(V,E) with non-negative weight function
w
and source
s
, terminates with
u.d=δ(s,u)
for all vertices
uV
.

Theorem 24.6 proves correctness by showing, for each vertex

uV,
u.d=δ(s,u)
at the time when
u
is added to set
S
. Once the equality is shown, it relies on the upper-bound property to uphold the equality at all times thereafter. Therefore, the algorithm would terminate with
u.d=δ(s,u)
for all vertices
uS=V
.

The proof of Theorem 24.6 illustrates a shortest path from

sp1xyp2u, on which
s,xS
and
y,uVS
. It makes a claim that
u
is the first vertex for which
u.dδ(s,u)
when it is added to
S
, then attempts to disprove this by contradiction.
By proving
y.d=δ(s,y)
and because all edge weights are non-negative, it arrives at the relations
y.d=δ(s,y)δ(s,u)u.d

Since
y,uVS
and
u
is chosen over
y
to be next included in set
S
,
u.dy.d
. Combined with the relation shown above,
y.d=δ(s,y)=δ(s,u)=u.d
, which contradicts its claim and completes the proof.

The reason why the proof of Theorem 24.6 does not go through when negative-weight edges are allowed is because we can no longer make the assumption that

δ(s,y)δ(s,u).
We know
y.d=δ(s,y)u.d
, but if
δ(s,y)>δ(s,u)
due to a negative-weight edge, it is possible that

  1. y.d=δ(s,y)=u.d>δ(s,u)
  2. δ(s,y)>u.d=a
    and
    δ(s,y)>δ(s,u)=b
    for which
    ab