Docsity

Bellman Ford Algorithm-Advance Analysis Design-Lecture Slides, Slides of Design and Analysis of Algorithms

Partial preview of the text

Download Bellman Ford Algorithm-Advance Analysis Design-Lecture Slides and more Slides Design and Analysis of Algorithms in PDF only on Docsity!

Lecture No. 34

Proof: Bellman-Ford Algorithm

and

Shortest Paths in Directed Acyclic Graphs

  • Bellman-Ford Algorithm
    • Analysis– Proof -^

Shortest path in Directed Acyclic Graphs– Assumptions– Algorithm– Analysis– Proof of correctness

Today Covered

Statement: Let

G = (V, E) be

•^

directed, with source

s,

•^

a weighted, with weight function

w : E

R
,^

and

•^

contains no negative-weight cycle reachable from

s.

Then, after the

|V| - 1

iterations of the

for

loop of

lines 2-4 of BELLMAN-FORD, we have

d[v] =

δ

(s, v)

for all vertices

v

that are reachable from

s.

Path relaxation property•^

If p

v

v

v

k^

, be a shortest path from s = v

0

to v

k^

and edges of p are relaxed in the order (

v

v

( v

v

v

k-

,^

v

), then d( k

v

) = k

δ

( s

,^

v

) k

Lemma 1

Proof•^

We prove it using path-relaxation property.

-^

Consider any vertex

v

that is reachable from

s

•^

And let

p

v

v

v

k^

, be any acyclic shortest

path from

s

to

v,

where

v

0

s

and

v

k^

v

•^

As there are k+1 vertices in the path p, hence theremust be k edges in p.

-^

Because Graph has |V| vertices and path pcontains no cycle, hence path

p

has at most |

V

edges, and therefore,

k

V
•^

Each of the |V| - 1 iterations of the for loop of lines2-4 relaxes all E edges.

Lemma 1 (Contd..)

Let BELLMAN-FORD be run on weighted, directedgraph G = (V, E), with source vertex s, and weightfunction w : E

R.
•^

If G contains no negative-weight cycles that arereachable from s, then– d[v] =

δ

(s, v) for all vertices v

V, and

the algorithm

returns TRUE

  • the predecessor subgraph G

π

is shortest-paths

tree rooted at s.

•^

If G does contain a negative weight cycle reachablefrom s, then the algorithm returns FALSE.

Theorem : Correctness of Bellman-Ford algorithm

Case 1

Suppose graph G contains no negative-weightcycles that are reachable from the source s.

-^

We first prove the claim that at termination,

d

[ v

] =

δ

( s

,^

v

) for all vertices

v

V
  • If

v

is reachable from

s

,^

Lemma above

proves it.

  • If

v

is not reachable from

s

, then claim follows

from no-path property.

•^

The predecessor subgraph property, along with theclaim, implies that

G

π^

is a shortest-paths tree.

(Once d[v] =

δ

(s, v) for all v

V, the predecessor

sub-graph is a shortest paths tree rooted at s)

Proof

•^

Assume for the purpose of contradiction that theBellman-Ford algorithm returns TRUE.

-^

Thus,

d

[ v

]^ i

d

[ v

i -

] +

w

( v

i -

,^

v

) for i

i

k

•^

Summing the inequalities around cycle

c

gives us

Contd..

^

k i

k i

i

i

i

k i

i

i

i

k i

i

v

v w

v d

v v w v d v d

1

1

1

1

1

1

1

1

) , ( ] [ ( ))

, ( ] [ ( ] [

-^

Since

v

0

=

v

, each vertex in k

c

appears exactly once in each

of the summations and, and so

-^

Of course

d

[ vi

] is finite for

i^

= 1, 2,...,

k

. Thus,

•^

Which contradicts inequality (A). And hence itproves the theorem

Contd..^ 

k i

i

k i

i^

v d

v d

1

1

1

]

[

] [^ 

k i

i

i^

v

v

w

•^

By relaxing edges of Directed Acyclic Graph (dag) G = (V, E)

according to topological sort of vertices

single source shortest path can be computed in 

(V + E)

time

•^

Shortest paths are always well defined in a dag– Since even if there are negative-weight edges no

negative weight cycle exists.

•^

It starts topologically sorting dag, to impose linearordering of vertices.• If there is path from u to v then u precedes v.

-^

Shortest Paths in Directed Acyclic GraphsEach vertex and each edge that leaves the vertex isprocessed that is why this approach is well defined

TOPOLOGICAL-SORT (
G
  1. Call DFS(
G

) to compute

f

[

v ] of each vertex

v

V
  1. Set an empty linked list
L
= Ø.
  1. When a vertex

v

is colored black, assign it

f

v ).

  1. Insert

v

onto the front of the linked list,

L

v}

. L

5. return

the linked list.

  1. The rank of each node is its position in the linked list

started from the head of the list.

Example

Algorithm : Topological Sort

Total Running Time =

(V + E)

y

t

s

5

2

7

- -

6

1

3

4

2

r

x

z

SSSP in Directed Acyclic Graphs

For each vertex

v

V(G)

d

[ v

]^

π

[ v

]^
NIL

Considering

s

as root node

d

[ s

]^

The vertices are taken intopologically sorted order

SSSP in Directed Acyclic Graphs

y

t

s

5

2

7

- -

6

1

3

4

2

r

x

z^ Considering vertex

r

Adj

[ r

] =

s, t

d

[ s

] >

d

[ r

] +

w

( r, s

But (0 <

d

[ t

] >

d

[ r

] +

w

( r, t

But (

SSSP in Directed Acyclic Graphs^2

y

t

s 5

7

- -

6

1

3

4

2

r

x

z

Considering vertex

t

Adj

[ t ] =

x, y, z d [

x ] >

d

[ t

] +

w

( t, x

)

But (6 < 9) d [

y ] >

d

[ t

] +

w

( t, y

)

(∞

2 + 4)^ d

[ y

]^ ←

d

[ t

] +

w

( t, y

)

2 + 4 = 6

π[

y ]

←

t

d [

z ] >

d

[ t ] +

w

( t, z

)

(∞

2 + 2)^ d

[ z

]^ ←

d

[ t ] +

w

( t, z

)

2 + 2 = 4

π[

z ]

←

t

-

SSSP in Directed Acyclic Graphs^2

y

t

s 5

7

-

6

1

3

4

2

r

x

Considering vertex z

x

Adj

[ x

] =

y, z

d [

y ] >

d

[ x

] +

w

( x, y

)

(6 > 6 + (-1))

d [

y ]

←

d

[ x

] +

w

( x, y

)

6 + (-1) = 5

π[

y ]

←

x

d [

z ] >

d

[ x

] +

w

( x, z

)

But (4 < 6 + 1)