










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 18
This page cannot be seen from the preview
Don't miss anything!











B.B. Karki, LSU CSC 3102
B.B. Karki, LSU CSC 3102
Dynamic programming
B.B. Karki, LSU CSC 3102
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 ”
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.
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
Two Two propertiesproperties are:are:
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)
n
n
n − k
k
n
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
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
Adjacency matrix
€
1 1 1 1
1 1 1 1
0 0 0 0
1 1 1 1
a b c d
Transitive closure
Digraph
B.B. Karki, LSU CSC 3102
Warshall’s algorithm constructs the transitive closure of a given digraph with n
vertices through a series of n -by- n boolean matrices
(0)
( k - 1)
( k )
( 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
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-1)
j k
(k)
B.B. Karki, LSU CSC 3102
// Implements Warshall’s algorithm
// Input: Adjacency matrix A of a digraph of n vertices
// Output: Transitive closure of the digraph
(0)
(k)
( k - 1)
( k - 1)
( k - 1)
(n)
3
B.B. Karki, LSU
CSC 3102
Find the distances (the lengths of the shortest paths) from each vertex to
all other vertices
ij
ij
a
b
d
c
€
0 ∞ 3 ∞
2 0 ∞ ∞
∞ 7 0 1
6 ∞ ∞ 0
a b c d
Weight matrix
€
0 10 3 4
2 0 5 6
7 7 0 1
6 16 9 0
a b c d
Distance matrix
Digraph
B.B. Karki, LSU CSC 3102
Floyd’s algorithm computes the distance matrix of a weighted graph with n
vertices through a series of n -by- n matrices
(0)
( k - 1)
( k )
( 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.
(0)
is simply the weight matrix
Does not allow any intermediate vertices in its paths
(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.
( 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 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)
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
//Implements Floyd’s algorithm
//Input: Weight matrix W of a graph of n vertices
//Output: The distance matrix of the shortest paths’ lengths
(0)
(k)
( k - 1)
( k - 1)
( k - 1)
(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.