Docsity

Single Source Shortest Path-Advance Analysis Design-Lecture Slides, Slides of Design and Analysis of Algorithms

Partial preview of the text

Download Single Source Shortest Path-Advance Analysis Design-Lecture Slides and more Slides Design and Analysis of Algorithms in PDF only on Docsity!

Lecture No. 33

Single-Source Shortest Path

  • Road map problem• Linking road map problem with graph theory• Paths and Shortest paths• Cycles and their role in finding shortest paths• The Bellman-Ford Algorithm
    • Initialization of graphs– Relaxation property– Algorithm design and analysis– Proof of correctness
      • Applications• Conclusion

Today Covered

Road map problem•^

This problem can be modeled as a graph problem

-^

Road map is a weighted graph:

set of

vertices

= set of cities

set of

edges

= road segments between cities

edge weight

= length between two cities

  • Goal: find a shortest path between two vertices

Linking Road Map with Graph Theoryi.e. between two cities

•^

In a

shortest path problem

, a weighted, directed graph

G
= (V, E)

is given with weight function

w : E

R

mapping

edges to real-valued weights.

-^

The

weight

of path

p =

is the sum of the

weights of its constituents edges

Weight of a Path

^
vk
v
v^
,^

1 0

v

w(v

1 2 1 1 0 1

i

1 i

k

k

k i

v v w v v w v v w p w

Variants of Shortest Path

-^

Single-source shortest path^ – G = (V, E)

find a shortest path from a given source

vertex s to each vertex v

V

-^

Single-destination shortest path^ – Find a shortest path to a given destination vertex

t

from

each vertex v

  • Reverse the direction of each edge

single-source

-^

Single-pair shortest path^ – Find a shortest path from u to v for given vertices u and v– Solve the single-source problem

-^

All-pairs shortest-paths^ – Find shortest path for every pair of vertices u and v of G

Statement

Given a weighted, directed graph

G = (V, E)

with

weight function

w : E

R

, let p =

v

, v 1

,..., v 2

k^

be

a shortest path from vertex

v

1

to vertex

v

, for any k

i, j

such that

^1

i^

j

k

, let

p

ij^

v

, vi

i+

,..., v

j^

be

subpath of

p

from

v

to vertex i^

v

.^ j

Then,

p

ij^

is a

shortest path from

v

to i^

v

.j

Proof•^

We prove this lemma by contradiction

-^

If we decompose path

p

into

v

1

v

^  i

v

^  j

v

, k

then we have that

w

( p

w

( p

1 i

w

( p

) + ij

w

( p

) jk

Lemma : subpath of a shortest path, a shortest path

p^ ij

p^ 1i

p^ jk

Why Positive Cycle Not?

•^

s

a: only one path

(s, a) = w(s, a) = 3

-^

s

b: only one path

(s, b) =

w(s, a) + w(a, b) = -

•^

s

c: infinitely many paths

s, c

s, c, d, c

s, c, d, c, d, c

•^

cycle has positive weight (6 - 3 = 3) s, c

^

shortest path with weight

(s, c) = w(s, c) = 5,

•^

Positive cycle increases length of paths

s

-

3

a c

-b d

e^

f

g

4

5

6

8

-

2

(^3) -

7

Why Negative Cycle Not?

s

-

3

a c

-b d

e^

f

g

4

5

6

8

-

2

(^3) -

7

-^

s^

e: infinitely many paths:

s, e

,

s, e, f, e

,

s, e, f, e, f, e

^

etc.

  • cycle

e, f, e

^

has negative weight: 3 + (- 6) = -

  • paths from s to e with arbitrarily large negative weights–

(s, e) =-

 

no shortest path exists between s and e

  • Similarly:

(s, f) = -

,^

(s, g) = -

•^

There may be edges with negative weight.

-^

A cycle

p

v

,v 0

,…,v 1

,vk

0

is a

negative cycle

such

that

w(p)

•^

If a graph

G = (V, E)

contains no negative weight

cycle reachable from the source

s

, then for all

v

V

, shortest path

δ

(s, v)

remains well defined.

•^

If there is a negative weight cycles reachable froms, then shortest path weight are not well defined.

-^

If there is a path from u to v that contains a negativecycle, then shortest path is defined as

(u, v) = -

When is no shortest path?

Summary of cycles in SPP

•^

Can shortest paths contain cycles?

-^

Negative-weight cycles:

-^

Positive-weight cycles:– By removing the cycle we can get a shorter path

-^

Zero-weight cycles– No reason to use them– Can remove them to obtain a path with similar weight Note•^

We will assume that when we are findingshortest paths, the paths will have no cycles

No! No!

•^

Let

G = (V, E)

be a weighted, directed graph with weight

function

w : E

R

and assume that

G

contains no

negative weight cycles reachable from the source vertex

s

V

, so that shortest paths are well defined.

•^

A shortest path tree rooted at

s

is a directed subgraph

G’=(V’, E’)

, where

V’
V

and

E’
E
•^

Shortest path are not necessarily unique and neither areshortest path trees.

Shortest Path Rooted Tree

•^

Shortest path are neither necessarily– unique and– nor shortest path trees

3

6

3

t^

x

s

(^35)

1

2

3

2

7

6 6 4

y^

z

0

9

5

11

3

t^

x

s

5

1

2

3

2

7

6 4

y^

z

0

9

5

11

3

t^

x

s

5

1

2

3

2

7

(^646)

y^

z

0

9

5

11

3

Shortest path not unique

Relaxation

Relaxation

•^

Relaxing

edge (u, v), testing whether we can

improve shortest path to v found so far through u–

If d[v] > d[u] + w(u, v)

we can improve the shortest path to v

update d[v] and

[v]

5

9

2

u^

v

5

7

2

u^

v RELAX(u, v, w)

5

6

2

u^

v

5

6

2

u^

v RELAX(u, v, w) d[v]

d[u] + w(u, v)

s^

s

d[v]

≥

d[u] + w(u, v)

Contd..

Running time =

V

Initialization and Relaxation

INITIALIZE-SINGLE-SOURCE (
G
,^

s )

for

each vertex

v

V
[ G
]

do

d

[ v

]^

π

[ v

]^
NIL

d

[ s

]^
RELAX (

u

,^

v ,

w

if

d

[ v

] >

d

[ u

] +

w

( u

,^

v )

then

d

[ v

]^

d

[ u

] +

w

( u

,^

v )

π

[ v

]^

u

Note:All the single-source shortest-paths algorithms, start by calling

INIT-SINGLE-SOURCE then relax edges. The algorithmsdiffer in the order and how many times they relax each edge