KEMBAR78
Dynamic Program Problems | PPTX
Binomial coefficient
Algorithm: Binomial_coefficient(n , k)
// purpose: to compute binomial by dynamic programming
//input: non negative integers such as n ≥ k ≥ 0
//output: value of nCk also designated as C(n,k)
for i = 0 to n do
for j = 0 to min(I,k) do
if(j=0 or i = j)
c[i,j] = 1
else
c[i,j] = c[i-1 , j-1] + c[i-1 , j]
end if
end for
end for
Return c[n,k]
Warshall’s algorithm
Algorithm: warshall(n, A, p)
// purpose: to compute transitive closure(path matrix)
//input: adjacency matrix A of size n x n
//output: transitive closure(path matrix) of size n x n
Step1: // make a copy of adjacency matrix
for i=0 to n-1 do
for j = 0 to n-1 do
p[i,j] = A[i,j]
end for
end for
Step2: // find the transitive closure(path matrix)
for k =0 to n-1 do
for i=0 to n-1 do
for j=0 to n-1 do
if(p[i,j]=0 and (if(p[i,k] =1 and p[k,j] =1)) then
p[i,j]=1
end if
end for
end for
end for
step 3: return
Floyds algorithm
Algorithm: Floyd(n, cost , D)
// purpose: to implement Floyd's algorithm for all pairs
shortest path.
//input: cost adjacency matrix cost of size n x n.
//output: shortest distance matrix of size n x n.

// make a copy of cost adjacency matrix
for i=0 to n-1 do
for j = 0 to n-1 do
D[i,j] = cost[i,j]
end for
end for
// find the all pairs shortest path
for k =0 to n-1 do
for i=0 to n-1 do
for j=0 to n-1 do
D[i,j]= min( D[i,j], D[i,k] + D[k,j] )
end for
end for
end for
return
Knapsack algorithm
Algorithm: KNAPSACK (n, m, w, p, v)
// purpose: to find the optimal solution for the knapsack
problem using dynamic programming.
//input: n - Number of objects to be selected.
//
m - capacity of the knapsack.
//
w – weights of all the objects.
//
p – profits of all the objects.
//output: v - the optimal solution for the number of
objects selected with specified remaining capacity.
for i =0 to n do
for j = 0 to m do
if( i = 0 or j = 0 )
v[i , j] = 0
else if (w[i] > j )
v[i , j] = v[ i -1 , j]
else
v[i , j] = max( v[i-1 , j] , v[i-1 , j-w[i]] + p[i])
end if
end for
end for
return

Dynamic Program Problems

  • 1.
  • 2.
    Algorithm: Binomial_coefficient(n ,k) // purpose: to compute binomial by dynamic programming //input: non negative integers such as n ≥ k ≥ 0 //output: value of nCk also designated as C(n,k) for i = 0 to n do for j = 0 to min(I,k) do if(j=0 or i = j) c[i,j] = 1 else c[i,j] = c[i-1 , j-1] + c[i-1 , j] end if end for end for Return c[n,k]
  • 3.
    Warshall’s algorithm Algorithm: warshall(n,A, p) // purpose: to compute transitive closure(path matrix) //input: adjacency matrix A of size n x n //output: transitive closure(path matrix) of size n x n Step1: // make a copy of adjacency matrix for i=0 to n-1 do for j = 0 to n-1 do p[i,j] = A[i,j] end for end for
  • 4.
    Step2: // findthe transitive closure(path matrix) for k =0 to n-1 do for i=0 to n-1 do for j=0 to n-1 do if(p[i,j]=0 and (if(p[i,k] =1 and p[k,j] =1)) then p[i,j]=1 end if end for end for end for step 3: return
  • 5.
    Floyds algorithm Algorithm: Floyd(n,cost , D) // purpose: to implement Floyd's algorithm for all pairs shortest path. //input: cost adjacency matrix cost of size n x n. //output: shortest distance matrix of size n x n. // make a copy of cost adjacency matrix for i=0 to n-1 do for j = 0 to n-1 do D[i,j] = cost[i,j] end for end for
  • 6.
    // find theall pairs shortest path for k =0 to n-1 do for i=0 to n-1 do for j=0 to n-1 do D[i,j]= min( D[i,j], D[i,k] + D[k,j] ) end for end for end for return
  • 7.
    Knapsack algorithm Algorithm: KNAPSACK(n, m, w, p, v) // purpose: to find the optimal solution for the knapsack problem using dynamic programming. //input: n - Number of objects to be selected. // m - capacity of the knapsack. // w – weights of all the objects. // p – profits of all the objects. //output: v - the optimal solution for the number of objects selected with specified remaining capacity.
  • 8.
    for i =0to n do for j = 0 to m do if( i = 0 or j = 0 ) v[i , j] = 0 else if (w[i] > j ) v[i , j] = v[ i -1 , j] else v[i , j] = max( v[i-1 , j] , v[i-1 , j-w[i]] + p[i]) end if end for end for return