Docsity

Algorithm for Computing Shortest Paths and Transitive Closure using Dynamic Programming - , Study notes of Computer Science

Partial preview of the text

Download Algorithm for Computing Shortest Paths and Transitive Closure using Dynamic Programming - and more Study notes Computer Science in PDF only on Docsity!

B.B. Karki, LSU CSC 3102

Algorithm Design Techniques

 Brute force

 Divide-and-conquer

 Decrease-and-conquer

 Transform-and-conquer

 Space-and-time tradeoffs

Dynamic programming

 Greedy techniques

B.B. Karki, LSU CSC 3102

Dynamic Programming

B.B. Karki, LSU CSC 3102

Binomial Coefficient

Binomial formula:

Binomial formula:

Two Two propertiesproperties are:are:

  SolvingSolving thethe recurrencerecurrence relationrelation byby thethe dynamicdynamic programmingprogramming method:method:

 Record the values of the binomial coefficients in a table (

Record the values of the binomial coefficients in a table ( n

n +1 by

+1 by k

k +1)

( a + b )

n

= C ( n ,0) a

n

+ .........+ C ( n , k ) a

n − k

b

k

+ ..........+ C ( n , n ) b

n

C ( n , k ) = C ( n − 1 , k − 1 ) + C ( n − 1 , k )
C ( n ,0) = C ( n , n ) = 1

for n > k > 0

n 1 C( n,k)

n -1 1 C( n-1, k-1) C( n-1, k)

0 1 2 …. k-1 k

B.B. Karki, LSU CSC 3102

Pseudocode

Algorithm Binomial ( n, k )

// Computes C(n,k) by the dynamic programming algorithm

// Input: A pair of nonnegative integers n ≥ k ≥ 0

// Output: The value of C( n,k)

for i ← 1 to n do

for j ← 1 to min( i, k) do

if j = 0 or j = i

C[ i, j] ← 1

else C[ i, j] ← C[ i - 1, j - 1 ] + C[ i - 1, j]

return C

[

n, k]

Time efficiency: Θ( nk )

Space efficiency: Θ( nk )

B.B. Karki, LSU CSC 3102

Matrices for Transitive Closure

 Warshall’s algorithm constructs the transitive closure of a given digraph with n

vertices through a series of n -by- n boolean matrices

R

(0)

, …… R

( k - 1)

, R

( k )

, …….., R

( n )

 The r

ij

element of matrix R

( k )

( k = 0, 1, …. n ) is equal to 1 if and only if there exists

a directed path from the i

th

vertex to the j

th

vertex with each intermediate vertex, if

any, numbered not higher than k.

R

(0)

is simply the adjacency matrix

 Does not allow any intermediate vertices in its paths

R

(1)

contains the information about paths that can use the first vertex as intermediate

 May contain more 1 ’s than R

(0)

.

 In general, each subsequent matrix in the series has one more vertex to use as

intermediate for its paths than its predecessor.

 The matrix R

( n )

reflects paths that can use all n vertices of the digraph as

intermediate. This is the digraph’s transitive closure.

B.B. Karki, LSU

CSC 3102

R

( k )

and R

( k -1)

 We can compute all the elements of each matrix R

( k )

from its immediate predecessor

R

( k - 1)

in the series

 For R

( k )

, means:

v

i

, a list of intermediate vertices each numbered not higher than k , v

j

 Two situations regarding this path are possible:

 First: The list of its intermediate vertices does not contain the k th vertex

 Then this path has intermediate vertices numbered not higher than k - 1

 Second: The path does contain the k th vertex v

k

among its intermediate

vertices so the v

k

occurs only once in that list

v

i

, vertices numbered ≤ k - 1, v

k

, vertices numbered ≤ k - 1, v

j

 First part: and second part:

r

ij

( k )

r

ik

( k −1)

= 1

€

r

kj

( k −1)

= 1

€

r

ij

( k −1)

= 1

B.B. Karki, LSU CSC 3102

Pseudocode

Algorithm Warshall ( A[0... n, 1 … n] )

// Implements Warshall’s algorithm

// Input: Adjacency matrix A of a digraph of n vertices

// Output: Transitive closure of the digraph

R

(0)

← A

for k ← 1 to n do

for i ← 1 to n do

for j ← 1 to n do

R

(k)

[ i, j] ← R

( k - 1)

[ i, j] or R

( k - 1)

[ i, k] and R

( k - 1)

[ k, j]

return R

(n)

Time efficiency: Θ( n

3

Improve by restructuring the innermost loop.

Space efficiency: Separate matrices needed for recording intermediate results.

Avoid using extra memory for storing elements of the algorithm’s

intermediate matrices

B.B. Karki, LSU CSC 3102

Example

a

b

c d

€

0 1 0 0

0 0 0 1

0 0 0 0

1 0 1 0

a b c d

a

b

c

d

Adjacency matrix: paths

with no intermediate

vertices

R

(0)

Digraph

€

0 1 0 0

0 0 0 1

0 0 0 0

1 1 1 0

a b c d

a

b

c

d

R

(1)

€

0 1 0 1

0 0 0 1

0 0 0 0

1 1 1 1

a b c d

a

b

c

d

R

(2)

€

0 1 0 1

0 0 0 1

0 0 0 0

1 1 1 1

a b c d

a

b

c

d

R

(3)

Paths with intermediate vertices

numbered no higher than 1, that

is, just vertex a. One new path.

Paths with intermediate vertices

numbered no higher than 2, that

is, just vertices a and b. Two

new paths.

Paths with intermediate

vertices numbered no

higher than 3, that is, a , b

and c. No new paths.

€

1 1 1 1

1 1 1 1

0 0 0 0

1 1 1 1

a

b

c

d

R

(4)

Paths with intermediate vertices

numbered no higher than 4, that

is, a , b , c and d. New five paths

are added. New five paths.

a b c d

B.B. Karki, LSU CSC 3102

Distance Matrices

 Floyd’s algorithm computes the distance matrix of a weighted graph with n

vertices through a series of n -by- n matrices

D

(0)

, …… D

( k - 1)

, D

( k )

, …….., D

( n )

 The d

ij

element of matrix D

( k )

( k = 0, 1, …. n ) is equal to the length of the shortest

path among all paths from the i th vertex to the j th vertex with each intermediate

vertex, if any, numbered not higher than k.

 D

(0)

is simply the weight matrix

 Does not allow any intermediate vertices in its paths

 D

(1)

contains the lengths of the shortest paths that can use the first vertex as

intermediate

 In general, each subsequent matrix in the series has one more vertex to use as

intermediate for its shortest paths than its predecessors.

 D

( k )

from D

( k - 1)

with one more vertex ( k th vertex) used as intermediate

 The matrix D

( n )

contains the lengths of the shortest paths among all paths that can

use all n vertices of the digraph as intermediate. This is the distance matrix being

sought.

B.B. Karki, LSU

CSC 3102

D

( k )

and D

( k- 1)

 We can compute all the elements of each matrix D

( k )

from its immediate

predecessor D

( k - 1)

in the series.

 For D

( k )

, means: shortest paths

v

i

, a list of intermediate vertices each numbered not higher than k , v

j

 Partition all such paths into two disjoint subsets:

 The list of its intermediate vertices does not contain the k th vertex. Then this path has

intermediate vertices numbered not higher than k - 1

= shortest length

 The path does contain the k th vertex v

k

among its intermediate vertices so the v

k

occurs

only once in that list

thus means

v

i

, vertices numbered ≤ k - 1, v

k

, vertices numbered ≤ k - 1, v

j

Each of the paths is made up of and

d

ij

( k )

d

ij

( k )

€

d

ik

( k −1)

€

d

kj

( k −1)

€

d

ij

( k −1)

B.B. Karki, LSU CSC 3102

Pseudocode

Algorithm Floyd ( W[0... n, 1 … n] )

//Implements Floyd’s algorithm

//Input: Weight matrix W of a graph of n vertices

//Output: The distance matrix of the shortest paths’ lengths

D

(0)

← W

for k ← 1 to n do

for i ← 1 to n do

for j ← 1 to n do

D

(k)

[ i, j] ← min { D

( k - 1)

[ i, j], D

( k - 1)

[ i, k] + D

( k - 1)

[ k, j]}

return D

(n)

Time efficiency: Θ( n

3

Space efficiency: Separate matrices for recording intermediate results

Both time and space efficiencies can be improved as in the case of Warshall’s algorithm.

B.B. Karki, LSU CSC 3102

Example

€

0 ∞ 3 ∞

2 0 ∞ ∞

∞ 7 0 1

6 ∞ ∞ 0

a b c d

a

b

c

d

Weight matrix: Lengths of

the shortest paths with no

intermediate vertices

D

(0)

€

0 ∞ 3 ∞

2 0 5 ∞

∞ 7 0 1

6 ∞ 9 0

a b c d

a

b

c

d

D

1)

€

0 ∞ 3 ∞

2 0 5 ∞

9 7 0 1

6 ∞ 9 0

a b c d

a

b

c

d

D

(2)

€

0 10 3 4

2 0 5 6

9 7 0 1

6 16 9 0

a b c d

a

b

c

d

D

(3)

Lengths of the shortest paths with

intermediate vertices numbered

no higher than 1, that is, just

vertex a. Two paths, bc and dc.

Lengths of the shortest paths

with intermediate vertices

numbered no higher than 2,

that is, just vertices a and b.

One path ca.

Lengths of the shortest pats

with intermediate vertices

numbered no higher than 3,

that is, a , b and c. Four paths,

ab, ad, bd and db.

€

0 10 3 4

2 0 5 6

7 7 0 1

6 16 9 0

a

b

c

d

D

(4)

Lengths of the shortest paths

with intermediate vertices

numbered no higher than 4,

that is, a , b , c and d.

A new shortest path from a to c

a

b

c d

a b c d