You are on page 1of 14

1

Mailam Engineering College


(Approved by AICTE, New Delhi, Affiliated to Anna University, Chennai
& Accredited by National Board of Accreditation (NBA), New Delhi)

Mailam (Po), Villupuram (Dt). Pin: 604 304


DEPARTMENT OF COMPUTER APPLICATIONS
DESIGN AND ANALYSIS OF ALGORITHMS MC9223
UNIT III

DYNAMIC PROGRAMMING

Computing a binomial coefficient Warshalls and Floyd algorithm Optimal


binary search tree Knapsack problem Memory functions.
Part A
1. Define Dynamic programming.
[NOV 11 & NOV 12]
Dynamic programming is a technique for solving problems with overlapping sub
problems. Dynamic programming is an algorithm design method, where the solution to the
main problem can be viewed as the result of a sequence of decisions. It avoids the
calculation of same stuff twice, by keeping the table of known results of sub-problems.
2. What are the basic elements of dynamic programming?

Sub structure
Table structure
Bottom up computations.

3. What are the applications of dynamic programming?


Fibonacci numbers
Longest increasing sequence
Minimum weight triangulation
The partition problem
Appropriate string matching
4. What are the steps involved in dynamic programming?
Develop a mathematical relation that can express any solution and sub solutions for
problem.
Prove the principle of optimality.
Develop a recurrence relation that is a solution to its sub solution using mathematical
notation in step 1.
5. What is binomial coefficient?
It is denoted by C(n,k) is the number of combinations ( subsets) of k elements from
an n-element set( 0 <= k <=n)
6. What is Warshalls algorithm?
Warshalls algorithm is used to find the transitive closure of a given directed graph.
The transitive closure of a directed graph with n vertices can be defined as the n by n

Prepared By
Mrs.A.Subathra Devi AP / MCA

2
matrix. In which the element in the ith row and jth column is 1 if there exists a nontrivial
directed path from the ith vertex to the jth vertex. Otherwise 0.
7. What is an adjacency matrix?
The adjacency matrix A= { aij } of a directed graph is the Boolean matrix that has 1
I its ith row and jth column if and only if there is a directed edge from the ith vertex to the
jth vertex.
8. What is transitive closure? State the algorithm used to find transitive closure?
[NOV 10]
The transitive closure of a directed graph with n vertices can be defined as the n by n
Boolean matrix T={ tij}, in which the element in the ith row (1<=i<=n) and the jth column
(1<=j<=n) is 1 if there exists a non-trivial directed path from the ith vertex to the jth
vertex, otherwise tij is 0.
9. What is Floyds algorithm?
Floyd algorithm is a graph analysis algorithm for finding shortest paths in a weighted
graph is the minimum sum of weighted edges for all paths between the pair.
All pairs shortest path problem is to finding the minimum weight path between any
two vertices in the graph. The distant matrix gives the weight of edges for adjacent vertices.
10. What Are the Conditions Involved In The Floyds Algorithm?
Construct the adjacency matrix.
Set the diagonal elements to zero
Ak[i,j]=min
Ak-1[i,j]
Ak-1[i,k]and Ak-1[k,j]
11. Write the Procedure For The Floyds Algorithm.
Floyd [var A: array [1. n, 1n] of real.
Var C: array [1. n, 1. n] of real]
Var i,j,k:integer
Begin
For i=1 to n do
For j=1 to n do
A [i,j]=C[i,j] //constructing the adjacency matrix//
For i=1 to n do
A [i,i]=0
For k=1 to n do
For i=1 to n do
For j=1 to n do
If A [i, k]+A [k, j]<A [i, j] then
A [i, j]=A [i, k]+A [k, j];
End
12. Write the procedure to find the transitive closure of a directed graph using
warshalls algorithm. [JUN 11]
Warshall(A[1..n,1..n])
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]

Prepared By
Mrs.A.Subathra Devi AP / MCA

3
Return R(n)
13. What is meant by optimal solution?
A solution to an optimization problem which minimizes the objective function is
termed as an optimal solution. An optimization problem is a computational problem in which
the object is to find the best of all possible solutions. More formally, find a solution in the
feasible region which has the minimum value of the objective function.
14. Find the number of comparisons made by the sequential search in the worst
case and the best case.
The number of comparisons made by the sequential search in the worst and best
case are one and n key comparisons respectively.
15. What is the difference between dynamic programming and divide and
conquer?
Dynamic Programming

Divide And Conquer

1. Divide the given problem into many subproblems. Find the individual solutions and
Combine them to get the solution for the
main problem.

1. Many decisions and sequences are


guaranteed and all the overlapping subinstances are considered.

2. Follows top down technique.

2. Follows bottom up technique.

3. Split the input only at specific points.

3. Split the input at every possible point


rather than at a particular point.

4. Each problem is independent.

4. Sub-problems are dependent on the main


problem.

16. Define sub structure, table structure and bottom up computations.


A problem is said to have optimal substructure if the globally optimal solution can be
constructed from locally optimal solutions to sub problems. We can either build up solutions
of sub-problems from small to large (bottom up) or we can save results of solutions of subproblems in a table (top down + memorization).This forms the table structure.
17. Define multi stage graph.
A multistage graph is a graph
G=(V,E) with V partitioned into K >= 2 disjoint subsets such that if (a,b) is in E,
then a is in Vi , and b is in Vi+1 for some subsets in the partition;
and | V1 | = | VK | = 1.
18. Define Vertex.
The vertex s in V1 is called the source; the vertex t in VK is called the sink. G is
usually assumed to be a weighted graph. The cost of a path from node v to node w is sum
of the costs of edges in the path. The "multistage graph problem" is to find the minimum
cost path from s to t. Each set Vi is called a stage in the graph.
19. What is principal of optimality? [NOV 11 & JUN 12]

Prepared By
Mrs.A.Subathra Devi AP / MCA

4
It states that in an optimal sequence of decisions, each sub sequence must be
optimal. To use dynamic programming the problem must observe the principle of optimality
that whatever the initial state is, remaining decisions must be optimal with regard the state
following from the first decision.
20. Derive the formula for the Floyds algorithm and running time of the algorithm.
for k<--1 to n do
for i<--1 to n do
for j<--1 to n do
D[i,j]=min{[i,j],D[i,k]+D[k,j]}
RUNNING TIME OF THE ALGORITHM: O(n^3)
21. What are the preconditions for the Floyds algorithm?
The precondition for the Floyds algorithm is that the graph should not have a cycle
of negative length or cost.
22. What is the importance of memory functions in dynamic programming? [JUN
12, NOV 10 & 12]
Memory functions combine the strength of top-down and bottom-up approaches. The
goal is to get a method that solves only sub problems that are necessary and does it only
once. Such a method is based on using memory functions.
23. Give the recurrence formula and initial condition for finding binomial
coefficient.
k
i-1
n
k
A(n, k) =
1+
1
i =1 j =1 i = k+1 j = 1
24. What is the optimal solution of 0/1 knapsack problem?
The knapsack problem or rucksack problem is a problem in combinatorial
optimization. Given n items of known weights w1,w2----wn and values v1----vn and a
knapsack of capacity w, Find the most valuable subset of the items that fit in to the
knapsack ie, find a subset of the largest value among them.
25. What is root table?
The root table is a table which has 0 to n columns and 1 to n+ 1 row. The root value
is given by R [1, n]. The k values the entries in the main table is entered into the root table.
26. How many binary search trees can be formed with n keys? [JUN 11]
Let us take a tree with 5 nodes (n=5).

Prepared By
Mrs.A.Subathra Devi AP / MCA

5
It will have only 6 (ie,5+1) null branches. In general, a binary tree with n nodes has
exactly n+1 null nodes.
27. Write the Criterion for an optimal tree?
Each optimal binary search tree is composed of a root and (at most) two optimal
subtrees, the left and the right.
28. Write the procedure for Binomial Coefficient?
Algorithm Binomial(n, k)
for i 0 to n do // fill out the table row wise
for i = 0 to min(i, k) do
if j==0 or j==i then C[i, j] 1 // IC
else C[i, j] C[i-1, j-1] + C[i-1, j] // recursive relation
return C[n, k]
29. Write the time Efficiency of OBST?
An optimal binary search tree for n keys and n + 1 intervals with known request
frequencies can be constructed in O(n3) time.
30. How to calculate the knapsack using dynamic programming?
V [i,j] = { Max {V[i-1,j], Vi+V[i-1, j-Wi], if j-Wi>0}

-------------- (1)

V [i-1, j],

-------------- (2)

Then,

if j-Wi<0}

V [0, j] =0 for j>0 and V [i, 0] = 0 for i >0


31. In the given binary tree, using array, at which location can you store the node
4?

Answer: At location 6
Explanation:

Where LCn means Left Child of node n and RCn means Right Child of node n

Prepared By
Mrs.A.Subathra Devi AP / MCA

Part B
1. How to compute Binomial coefficient. Discuss its space and time complexity
with Dynamic programming. Give example.
[NOV 10 & JUN 11]
Computing binomial coefficients is non optimization problem but can be solved using
dynamic programming. Binomial coefficients are represented by C(n, k) or (nk) and can be
used to represent the coefficients of a binomial:
(a + b)n = C(n, 0)an + ... + C(n, k)an-kbk + ... + C(n, n)bn
The recursive relation is defined by the prior power
C(n, k) = C(n-1, k-1) + C(n-1, k) for n > k > 0
IC C(n, 0) = C(n, n) = 1
Dynamic algorithm constructs a nxk table, with the first column and diagonal filled out using
the IC. Construct the table:

The table is then filled out iteratively, row by row using the recursive relation.
Algorithm:
Algorithm Binomial(n, k)
for i 0 to n do // fill out the table row wise
for i = 0 to min(i, k) do
if j==0 or j==i then C[i, j] 1 // IC
else C[i, j] C[i-1, j-1] + C[i-1, j] // recursive relation
return C[n, k]
The cost of the algorithm is filing out the table. Addition is the basic operation.
Because k n, the sum needs to be split into two parts because only the half the table
needs to be filled out for i < k and remaining part of the table is filled out across the entire
row.
A(n, k) = sum for upper triangle + sum for the lower rectangle
= i=1k j=1i-1 1 + i=1n j=1k 1
= i=1k (i-1) + i=1n k

Prepared By
Mrs.A.Subathra Devi AP / MCA

7
= (k-1)k/2 + k(n-k) (nk)
2. Discuss about Warshalls algorithm in detail.

[NOV 11 & NOV 12]

Warshall's algorithm determines whether there is a path between any two nodes in
the graph. It does not give the number of the paths between two nodes. Compute all paths
containing node 1, then all paths containing nodes 1 or 2 or 1 and 2, and so on, until we
compute all paths with intermediate nodes selected from the set {1, 2, n}.
Warshall's algorithm uses the adjacency matrix to find the transitive closure of a
directed graph.
Warshalls algorithm is used to find the transitive closure of a given directed graph.
The transitive closure of a directed graph with n vertices can be defined as the n by n
matrix. In which the element in the ith row and jth column is 1 if there exists a nontrivial
directed path from the ith vertex to the jth vertex. Otherwise 0.
Transitive closure:
The transitive closure of a directed graph with n vertices can be defined as the n-byn boolean matrix T, in which the element in the ith row and jth column is 1 if there exist a
directed path from the ith vertex to the jth vertex, otherwise it is zero.
Transitive Closure can be solved by graph transversal for each vertex in the graph. If
a vertex is reached then the corresponding matrix element is filled with 1.
Assuming that the graph was represented by an adjacency matrix then the cost is
(n3) where n is the number of vertices in the graph. For a sparse graph, adjacency list is
more appropriate, then the cost is ((n+m)n).
Warshall's Algorithm Outline and Correctness:
Warshall's algorithm calculates the transitive closure by generating a sequence of n
matrices, where n is the number of vertices.
R(0), ..., R(k-1), R(k), ... , R(n)
Recall that a path in a simple graph can be defined by a sequence of vertices.
The definition of the element at the ith row and jth column in the kth matrix (R(k)),
rij
is one if and only if there exist a path from vi to vj such that all the intermediate vertex,
wq is among the first k vertices, ie. 1 q k.
(k)

The R(0) matrix represent paths without any intermediate vertices, so it is the
adjacency matrix. The R(n) matrix has ones if there is a path between the vertices with
intermediate vertices from any of the n vertices of the graph, so it is the transitive closure.
Consider the case rij(k) is one and rij(k-1) = 0. This can occur only if that there is an
intermediate path through vk from from vi to vj. More specifically the list of vertices has the
form
vi, wq (where 1 q < k), vk. wq (where 1 q < k), vj

Prepared By
Mrs.A.Subathra Devi AP / MCA

This can happen only if rik(k-1) = rkj(k-1) = 1. Note the k subscript


If rij(k-1) = 1 then rij(k) should be one.
rij(k) = rij(k-1) or (rik(k-1) and rkj(k-1))
Structure of Warshalls algorithm:

Algorithm:
Algorithm Warshall(A[1...n, 1...n]) // A is the adjacency matrix
R(0) A
for k 1 to n do
for i 1 to n do
for j 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)

The worst case cost is (n3), so it is not better than the brute force algorithm. In fact, for a
sparse graph the brute force algorithm is faster.
Time Complexity:
3. Explain how floyds algorithm is used in finding the shortest path. Give example.
[NOV 10 & JUN 12]
Floyd algorithm is a graph analysis algorithm for finding shortest paths in a weighted
graph is the minimum sum of weighted edges for all paths between the pair. All pairs
shortest path problem is to finding the minimum weight path between any two vertices in
the graph.

Prepared By
Mrs.A.Subathra Devi AP / MCA

9
The distant matrix gives the weight of edges for adjacent vertices. Note that if the
two vertices are not adjacent then the corresponding distant matrix entry is .
Floyd's Algorithm and correctness
Floyd's Algorithm is very similar to Warshall's algorithm calculates the minimal
distance using a sequence of n matrices, where n is the number of vertices
D(0), ..., D(k-1), D(k), ... , D(n)
The i, j entry in D(k), dij(k), is the minimal distance of path between vertices, vi to vj
such that all the intermediate vertex, wq is among the first k vertices, ie. 1 q k.
Note that D(0) is the distance matrix and D(n) is the solution that we are seeking.
Consider the all the paths from vi to vj with intermediate vertices less than k, they can be
divided into sets:
1. Paths with no vertex numbered k
2. Paths with a vertex numbered k
In set 1, all paths with no vertex numbered k, the minimal distance is dij(k-1)
In set 2, all paths with a vertex numbered k, the minimal path will visit the vk only once.
Then the paths be split into parts at vk:
vi, wq (where 1 q < k), vk. wq (where 1 q < k), vj
The shortest path for set 2 is min(dik(k-1) + dkj(k-1)).
Let W represent the weight of the path. Then
WP({vi,...,vj}) = i<qjD[q-1, q]
The minimal path from vi to vj is then the minimal of the two sets:
or dij(k) = min(dij(k-1), dik(k-1) + dkj(k-1))
Algorithm:
Algorithm Floyd(W[1...n, 1...n]) // W is the weight distances
D(0) W
for k 1 to n do // iteration through distance matrices
for i 1 to n do
for j to n do
D(k)[i, j] min(D(k-1)[i, j], (D(k-1)[i, k] + D(k-1)[k, j]))
(n)
return D
Time Complexity is (n3)
4. Write a suitable example explain Optimal Binary Search Trees problem using
dynamic programming algorithm.
[JUN 11 , NOV 10, 11 & 12]
A binary search tree is a tree where the key values are stored in the internal nodes,
the external nodes (leaves) are null nodes, and the keys are ordered lexicographically. I.e.
for each internal node all the keys in the left subtree are less than the keys in the node, and
all the keys in the right subtree are greater.

Prepared By
Mrs.A.Subathra Devi AP / MCA

10
When we know the probabilities of searching each one of the keys, it is quite easy to
compute the expected cost of accessing the tree. An OBST is a BST which has minimal
expected cost.
Example:
Key

-5

13

21

Probabilities 1/8 1/32 1/16 1/32 1/4 1/2

The expectation-value of a search is:

It's clear that this tree is not optimal. - It is easy to see that if the 21 is closer to the root,
given its high probability, the tree will have a lower expected cost.
Criterion for an optimal tree:
Each optimal binary search tree is composed of a root and (at most) two optimal
subtrees, the left and the right.
Method:
The criterion for optimality gives a dynamic programming algorithm. For the root we
select one value to be stored in the node.
Once this choice is made, the set of keys which go into the left subtree and right subtree is
completely defined, because the tree is lexicographically ordered. The left and right subtrees
are now constructed recursively (optimally). This gives the recursive definition of the optim
cost: Let

denote the probability of accessing key

probabilities from

, let

denote the sum of the

to

Prepared By
Mrs.A.Subathra Devi AP / MCA

11
The explanation of the formula is easy once we see that the first term corresponds to
the left subtree, which is one level lower than the root, the second term corresponds to the
root and the 3 to the right subtree. Every cost is multiplied by its probability.
For simplicity we set
and
so
simplifies to
. This
procedure is exponential if applied directly. However, the optimal trees are only constructed
over contiguous sets of keys, and there are at most

different sets of contiguous keys.

In this case we store the optimal cost of a subtree in a matrix

The Matrix-entry

will contain the cost of an optimal subtree constructed with the keys to
We now fill the matrix diagonal by diagonal. It is custumary to fill the matrix with
that we save a lot of multiplications and divisions. Let

then

An optimal tree with one node is just the node itself (no other choice), so the
diagonal of

is easy to fill:

Algorithm:
OPTIMAL-BST(p, q, n)
for i 1 to n + 1
do e[i, i - 1] qi-1
w[i, i - 1] qi-1
for l 1 to n
do for i 1 to n - l + 1
do j i + l - 1
e[i, j]
w[i, j] w[i, j - 1] + pj + qj
for r i to j
do t e[i, r - 1] + e[r + 1, j] + w[i, j]
if t < e[i, j]
then e[i, j] t
root[i, j] r
return e and root
Time Efficiency:
An optimal binary search tree for n keys and n + 1 intervals with known request
frequencies can be constructed in O(n3) time.
5. Describe briefly about Memory functions.

[NOV 11, JUN 12 & 11]

Dynamic programming solves problems that have a recurrence relation. Using the
recurrences directly in a recursive algorithm is a top-down technique. It has the
disadvantage that it solves common sub problem multiple times. This leads to poor
efficiency, exponential.

Prepared By
Mrs.A.Subathra Devi AP / MCA

12

The dynamic programming technique is bottom-up, and solving all the sub-problems
only once. This has the disadvantage that some of the sub-problems may not have been
necessary to solve.
The technique uses a top-down approach, recursive algorithm, with table of subproblem solution. Before determining the solution recursively the algorithm checks if the sub
problem has already been solved by checking the table.
If the table has a valid value then the algorithm uses the table value else it proceeds
with the recursive solution.
Memory function algorithm for the knapsack problem initializes V[i, j] to -1 except
row 0 and column 0, which is initialize to 0.
Algorithm:
Algorithm MFKnapsack(i, j) // i, j represent the sub problem
if V[i, j] < 0 // meaning not already calculated
if j < Weights[i] then
value MFKnapsack(i-1, j)
else
value max(MFKnapsack(i-1, j), Values[i] + MFKnapsack(i-1, jWeights[i])
V[i, j] value // put valid value in the table for both cases
return V[i, j]

6. Explain the knapsack problem. Give relevant example. [NOV 10, 11 & JUN 12]
The knapsack problem or rucksack problem is a problem in combinatorial
optimization: Given a set of items, each with a weight and a value, determine the number of
each item to include in a collection so that the total weight is less than a given limit and the
total value is as large as possible.
It derives its name from the problem faced by someone who is constrained by a
fixed-size knapsack and must fill it with the most useful items.
The problem often arises in resource allocation with financial constraints. A similar
problem also appears in combinatorics, complexity theory, cryptography and applied
mathematics.
Rules to fill the table:
To design a dynamic programming algorithm to solve knapsack problem we need to
derive a recurrence relation that expresses the solution to the instances of knapsack in
terms of solution to its smaller sub instances.

Prepared By
Mrs.A.Subathra Devi AP / MCA

13
Divide the subset of first i'th item that fit into the knapsack of capacity j'.
This can be divided into two categories.
1. Among the subsets that do not include the ith item then the value of optimal subset
is V[i-1,j].
2. Among the subsets that include ith item then the value of optimal subset is
Vi+V[i-1,j-Wi].
From this we can derive two equations or formulae to calculate they are given below:
V [i,j] = { Max {V[i-1,j], Vi+V[i-1, j-Wi], if j-Wi>0}

-------------- (1)

V [i-1, j],
Then,
V [0, j] =0 for j>0 and V [i, 0] = 0 for i >0

-------------- (2)

if j-Wi<0}

Algorithm:
Algorithm Knapsack(i, j) // i, j represent the sub problem
if V[i, j] < 0 // meaning not already calculated
if j < Weights[i] then
value Knapsack(i-1, j)
else
value max(Knapsack(i-1, j), Values[i] + Knapsack(i-1, j-Weights[i])
V[i, j] value // put valid value in the table for both cases
return V[i, j]

This solution will therefore run in


we use only a 1-dimensional array
this array
only

times, rewriting from

time and

space. Additionally, if

to store the current optimal values and pass over


to

every time, we get the same result for

space.

Prepared By
Mrs.A.Subathra Devi AP / MCA

14

ANNA UNIVERSITY QUESTIONS


Part - A
1. Define Dynamic programming.
2. What is transitive closure?

[Ref. No.: 1]
State the algorithm used to find transitive closure?

[Ref.No.:8]
3. Write the procedure to find the transitive closure of a directed graph using warshalls
algorithm.

[Ref. No.: 12]

4. What is principal of optimality?

[Ref. No.: 19]

5. What is the importance of memory functions in dynamic programming?

[Ref. No.: 22]

6. How many binary search trees can be formed with n keys?

[Ref. No.: 26]

Part B
1. How to compute Binomial coefficient. Discuss its space and time complexity with Dynamic
programming. Give example.

[Ref. No.: 1]

2. Discuss about Warshalls algorithm in detail.

[Ref. No.: 2]

3. Explain how floyds algorithm is used in finding the shortest path. Give example.
[Ref. No.: 3]
4. Write a suitable example explain Optimal Binary Search Trees problem using dynamic
programming algorithm.

[Ref. No.: 4]

5. Describe briefly about Memory functions.

[Ref. No.: 6]

6 . Explain the knapsack problem. Give relevant example.

[Ref. No.: 5]

Prepared By
Mrs.A.Subathra Devi AP / MCA

You might also like