CSC 3102: Algorithm Design - Lecture Notes (Warshall's & Floyd's Shortest Path Algorithms), Exams of Computer Science

These lecture notes from a computer science course cover warshall's algorithm and floyd's algorithm for finding the transitive closure and shortest paths in a graph, respectively. Topics include brute force, divide-and-conquer, dynamic programming, and the use of matrices in algorithm design.

Typology: Exams

Pre 2010

Uploaded on 08/31/2009

koofers-user-nio
koofers-user-nio 🇺🇸

10 documents

1 / 18

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
B.B. Karki, LSU
0.1
CSC 3102
CSC 3102: Final Exam
December 8, Monday
3:00 PM to 5:00 PM
213 Tureaud Hall
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download CSC 3102: Algorithm Design - Lecture Notes (Warshall's & Floyd's Shortest Path Algorithms) and more Exams Computer Science in PDF only on Docsity!

B.B. Karki, LSU CSC 3102

CSC 3102: Final Exam

December 8, Monday

3:00 PM to 5:00 PM

213 Tureaud Hall

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

Basics

Dynamic Programming is a general algorithm design technique:

Dynamic Programming is a general algorithm design technique:

  Invented by Richard Bellman in the 1950s to solve optimization problems

Invented by Richard Bellman in the 1950s to solve optimization problems

  “

Programming

Programming ”

here means

here means “

planning

planning ”

  MainMain idea:idea:

 Solve several smaller (overlapping)

Solve several smaller (overlapping) subproblems

subproblems

  Record solutions in a table so that eachRecord solutions in a table so that each subproblemsubproblem is solved only onceis solved only once

 Final state of the table will be (or contain) solution.

Final state of the table will be (or contain) solution.

Examples:

Examples:

 Computing a binomial coefficient

Computing a binomial coefficient

 Warshall

Warshall ’

s

s algorithm for transitive closure

algorithm for transitive closure

 Floyd

Floyd ’

s algorithm for all-pairs shortest paths

s algorithm for all-pairs shortest paths

 Constructing an optimal binary search tree.

Constructing an optimal binary search tree.

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

nk

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

Warshall’s Algorithm

 The transitive closure of a directed graph is the n -by- n boolean matrix T = { t

ij

}, in

which the element t

ij

in the i th row and the j th column is 1 if there exists a directed

path from the i th vertex to j th vertex; otherwise, t

ij

is 0.

 Performing traversal (DFS or BFS) for every vertex as a starting point yields the

transitive closure in its entirety.

 Warshall’s algorithm is an algorithm based on the idea of dynamic programming.

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

A =

1 1 1 1

1 1 1 1

0 0 0 0

1 1 1 1

a b c d

a

b

c

d

Transitive closure

T =

Digraph

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

Rule for Changing Zeros

 Formula for generating the elements of matrix R

( k )

from the elements of matrix R

( k - 1)

 Following rule now follows from this formula:

 If an element r

ij

is 1 in R

( k - 1)

, it remains 1 in R

( k )

 If an element r

ij

is 0 in R

( k - 1)

, it has to be changed to 1 in R

( k )

if and only if the

element in its row i and column k and the element in its column j and row k are

both 1 ’s in R

( k - 1)

r

ij

( k )

r

ij

( k −1)

or ( and )

r

ik

( k −1)

r

kj

( k −1)

j k

k

i

R

(k-1)

j k

k

i

R

(k)

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

Floyd’s Algorithm

 Solves all-pairs shortest-paths problem

 Find the distances (the lengths of the shortest paths) from each vertex to

all other vertices

 Record the lengths of the shortest paths in an n - by - n matrix D = { d

ij

in which the element d

ij

in the i th row and the j th column indicates

the length of the shortest path from the i th vertex to the j th vertex.

 Floyd’s algorithm generates the distance matrix.

a

b

d

c

0 ∞ 3 ∞

2 0 ∞ ∞

∞ 7 0 1

6 ∞ ∞ 0

a b c d

a

b

c

d

Weight matrix

W =

0 10 3 4

2 0 5 6

7 7 0 1

6 16 9 0

a b c d

a

b

c

d

Distance matrix

D =

Digraph

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

Recurrence for the Shortest Paths

 Recurrence for D

( k )

from the elements of matrix D

( k - 1)

 Following rule now follows from this formula:

 Replace by the sum of the elements and if and only if the latter

is smaller than its current value.

d

ij

( k )

= min{ d

ij

( k − 1 )

, d

ik

( k −1)

  • d

kj

( k −1)

}

for k ≥ 1,

d

ij

( 0 )

= w

ij

d

ij

( k −1)

d

ik

( k −1)

d

kj

( k −1)

v

i

v

j

v

k

d

ij

( k −1)

d

ik

( k −1)

d

kj

( 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.