









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










B.B. Karki, LSU CSC 3102
Dynamic programming
B.B. Karki, LSU CSC 3102
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
// 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)
[
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
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:
ij
( k )
r
ik
( k −1)
= 1
€
r
kj
( k −1)
= 1
€
r
ij
( k −1)
= 1
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
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: paths
with no intermediate
vertices
(0)
Digraph
€
0 1 0 0
0 0 0 1
0 0 0 0
1 1 1 0
a b c d
(1)
€
0 1 0 1
0 0 0 1
0 0 0 0
1 1 1 1
a b c d
(2)
€
0 1 0 1
0 0 0 1
0 0 0 0
1 1 1 1
a b c d
(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
(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
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
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
ij
( k )
ij
( k )
€
d
ik
( k −1)
€
d
kj
( k −1)
€
d
ij
( 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.
B.B. Karki, LSU CSC 3102
€
0 ∞ 3 ∞
2 0 ∞ ∞
∞ 7 0 1
6 ∞ ∞ 0
a b c d
Weight matrix: Lengths of
the shortest paths with no
intermediate vertices
(0)
€
0 ∞ 3 ∞
2 0 5 ∞
∞ 7 0 1
6 ∞ 9 0
a b c d
1)
€
0 ∞ 3 ∞
2 0 5 ∞
9 7 0 1
6 ∞ 9 0
a b c d
(2)
€
0 10 3 4
2 0 5 6
9 7 0 1
6 16 9 0
a b c 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
(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