SlideShare una empresa de Scribd logo
1 de 39
Dynamic Programming

Md. Shakil Ahmed
Software Engineer
Astha it research & consultancy ltd.
Dhaka, Bangladesh
Dynamic Programming
• Algorithmic technique that systematically
records the answers to sub-problems in a table
and re-uses those recorded results (rather than
re-computing them).
• Simple Example: Calculating the Nth
Fibonacci number.
      Fib(N) = Fib(N-1) + Fib(N-2)
Introduction
Topic Focus:
• Unidirectional TSP
•   Coin Change
•   LCS
•   LIS
•   The Partition Problem
• String Partition
• TSP (Bitmask)
• Matrix-chain multiplication
Unidirectional TSP



Output minimal-weight path, and the second line is the cost of a minimal path.

Input:                           Output:
56                               123445
341286                           16
618274
593995
841326
372864
Source Code
fix= infinity;                 for(j=m-1;j>0;j--)
                                for(i=0;i<n;i++)
for(i=0;i<n;i++)
                                {
 for(j=0;j<m;j++)                if(si[i][j-1] > si[i][j] + sa[i][j-1] || (si[i][j-1] ==
 {                             si[i][j] + sa[i][j-1] && t1[i][j-1] > i))
   scanf("%lld",&sa[i][j]);      {
   si[i][j]=fix;                            si[i][j-1]=si[i][j]+sa[i][j-1];
                                            t1[i][j-1]=i;
   t1[i][j]=-1;
                                  }
 }                               k=i-1;
 for(i=0;i<n;i++)                if(k<0)
 si[i][m-1]=sa[i][m-1];          k=k+n;
                                 if(si[k][j-1] > si[i][j] + sa[k][j-1] || (si[k][j-1] ==
                               si[i][j] + sa[k][j-1] && t1[k][j-1] > i))
                                 {
                                            si[k][j-1]=si[i][j]+sa[k][j-1];
                                            t1[k][j-1]=i;
                               }
Source Code
k=i+1;                                       printf("%ld",k+1);
k=k%n;
  if(si[k][j-1] > si[i][j] + sa[k][j-1] ||    j=0;
     (si[k][j-1] == si[i][j] + sa[k][j-1]
     && t1[k][j-1] > i))                      while(t1[k][j]!=-1)
  {                                           {
                                               printf(" %ld",t1[k][j]+1);
     si[k][j-1]=si[i][j]+sa[k][j-1];
                                               k=t1[k][j];
     t1[k][j-1]=i;                             j++;
   }                                          }
 }
min=fix;                                      printf("n%lldn",min);
 for(i=0;i<n;i++)
                                             Sample Problems:
 if(si[i][0]<min)
                                             •UVA Online Judge: 116.
 {
min=si[i][0];
k=i;
}
Coin change

• Coin change is the problem of finding the
  number of ways to make change for a target
  amount given a set of denominations. It is
  assumed that there is an unlimited supply
  of coins for each denomination. An example
  will be finding change for target amount 4
  using change of 1,2,3 for which the solutions
  are (1,1,1,1), (2,2), (1,1,2), (1,3).
Coin Change Source Code
long s[6], sa[7600]={0},   for(j=0;j<=4;j++)
  i, j, k;                 {
                             for(i=0;i<=7500;i++)
                             {
s[0]=1;                          if(i+s[j]>7500)
s[1]=5;                          break;
s[2]=10;
s[3]=25;                           sa[i+s[j]]+=sa[i];
                               }
s[4]=50;
                           }

sa[0]=1;                   while(scanf("%ld",&i)==1)
                           {
                               printf("%ldn",sa[i]);
                           }
Coin change
• Sample Problems:
1.UVA Online Judge: 147, 166, 357, 674, 10306,
  10313, 11137, 11517.
The longest common subsequence
           (LCS) problem
• A string : A = b a c a d
• A subsequence of A: deleting 0 or more symbols
   from A (not necessarily consecutive).
e.g. ad, ac, bac, acad, bacad, bcd.
• Common subsequences of A = b a c a d and
    B = a c c b a d c b : ad, ac, bac, acad.
• The longest common subsequence (LCS) of A and
   B:
  a c a d.
The LCS algorithm
• Let A = a1 a2 … am and B = b1 b2 … bn
• Let Li,j denote the length of the longest common
  subsequence of a1 a2 … ai and b1 b2 … bj.
•  Li,j = Li-1,j-1 + 1        if ai=bj
          max{ Li-1,j, Li,j-1 } if ai≠bj
   L0,0 = L0,j = Li,0 = 0 for 1≤i≤m, 1≤j≤n.
• Time complexity: O(mn)
• The dynamic programming approach for solving
  the LCS problem:
   L1,1    L1,2   L1,3


   L2,1    L2,2


   L3,1




                             Lm,n
Tracing back in the LCS algorithm
• e.g. A = b a c a d, B = a c c b a d c b
                              B
                  a   c   c   b a   d   c   b
              0   0   0   0   0   0 0   0   0
         b    0   0   0   0   1   1 1   1   1
         a    0   1   1   1   1   2 2   2   2
       A c    0   1   2   2   2   2 2   3   3
         a    0   1   2   2   2   3 3   3   3
         d    0   1   2   2   2   3 4   4   4

• After all Li,j’s have been found, we can trace back to
  find the longest common subsequence of A and B.
LCS Source Code
void LCS(){
 for(i=0;i<=m;i++)
    for(j=0;j<=n;j++)                         else if(c[i-1][j]>=c[i][j-1])
    {                                          {
                                                         c[i][j]=c[i-1][j];
          c[i][j]=0;                                      b[i][j]='2';
          b[i][j]='0';                         }
     }                                          else
                                                {
 for(i=1;i<=m;i++)                                       c[i][j]=c[i][j-1];
    for(j=1;j<=n;j++)                                    b[i][j]='3';
                                                 }
    {
                                               }
          if(x[i]==y[j])                      }
          {
                     c[i][j]=c[i-1][j-1]+1;
                     b[i][j]='1';
          }
void printSequence(long int i,long int j)   Sample Problems:
{                                           • UVA Online Judge: 10066,
   if(i==0||j==0)                              10405.
   return ;
   if(b[i][j]=='1')
   {
   printSequence (i-1,j-1);
   printf(“%c”,x[i]);
   }
   else if(b[i][j]=='2')
          printSequence (i-1,j);
   else
          printSequence (i,j-1);
}
Longest Increasing Subsequence
• The longest increasing subsequence problem is to find a
   subsequence of a given sequence in which the subsequence
   elements are in sorted order, lowest to highest, and in which
   the subsequence is as long as possible. This subsequence is
   not necessarily contiguous.
• A simple way of finding the longest increasing subsequence is
   to use the Longest Common Subsequence (
   Dynamic Programming) algorithm.
1. Make a sorted copy of the sequence A, denoted
   as B. O(nlog(n)) time.
2. Use Longest Common Subsequence on
   with A and B. O(n2) time.
Simple LIS
function lis_length( a )
   n := a.length
   q := new Array(n)
   for k from 0 to n:
          max := 0;
          for j from 0 to k, if a[k] > a[j]:
                     if q[j] > max, then set max = q[j].
          q[k] := max + 1;
   max := 0
   for i from 0 to n:
          if q[i] > max, then set max = q[i].
   return max;
Simple LIS
• Complexity O(n^2)             for(j=i+1;j<n;j++)
for(i=0;i<n;i++)                     If(A[j]>=A[i] && Len[j]<Len[i]+1)
{                                        {
    scanf(“%ld”,&A[i]);                  Len[j]=Len[i]+1;
                                         Pre[j]=i;
    Len[i]=1;
                                         }
    Pre = -1;                   }
}                               For sequence Print:
Max = 0; Position = 0;          void Show(long h1)
for(i=0;i<n;i++)                {
                                         If(Pre[h1]!=-1)
{                                                  Show(Pre[h1]);
    if(Len[i]>Max)                       Printf(“%ldn”,A[h1]);
    {                           }
          Max = Len[i];
                                Show(Position);
          Position = i;
    }
Efficient LIS
• Complexity O(n*log(n))
L=0
for i = 1, 2, ... n:
   binary search for the largest positive j ≤ L
   such that X[M[j]] < X[i] (or set j = 0 if no such value exists)
         P[i] = M[j]
   if j == L or X[i] < X[M[j+1]]:
         M[j+1] = i
   L = max(L, j+1)
Efficient LIS
long BinarySearch(long left,long right,long value)
{
long mid = (left+right)/2;
while(left<=right)
{
   if(temp[mid]==value)
          break;
   else if(temp[mid]<value)
          left = mid+1;
   else
          right = mid – 1;
   mid = (left+right)/2;
}
return mid;
}
Efficient LIS
void LIS(long N)                                if(mid+1>=n)
{ long n=0,i,mid;                               {
   for(i=0;i<N;i++)                             temp[n]=sa[i]; tempP[n]=i;
   {                                            n++;
   if(n==0)                                     }
   {      temp[n]=sa[i];tempP[n]=i;             else if(temp[mid+1]>sa[i])
          A[i]=1;n++;       }                   {
   else                                         temp[mid+1]=sa[i];
   {                                            tempP[mid+1]=i;
   mid = BinarySearch(0,n-1,sa[i]);             }
   while(mid>0&&temp[mid]>sa[i])        }
          mid–;                         else if(temp[0]>sa[i])
                                        {
   if(temp[mid]<sa[i])
                                        temp[0]=sa[i];
   {
                                        tempP[0]=i;
          A[i]=mid+2;P[i]=tempP[mid];
                                        }         }      }
                                        }
Efficient LIS
void show(long h1)             for(i=0;i<n;i++)
   {                           {
   if(P[h1]==-1)               scanf(“%ld”,&sa[i]);
   printf(“%ld”,sa[h1]);       P[i]=-1;
   else                        }
   {                           LIS(n);
   show(P[h1]);                max = 0;
   printf(” %ld”,sa[h1]);      x = 0;
   }                           for(i=0;i<n;i++)
   }                           if(A[i]>max)
                               {
                               max = A[i];
                               x = i;
                               }
                               printf(“%ldn”,max);
                               show(x);
LIS
• Sample Problems:
1.UVA Online Judge: 111, 231, 437, 481, 497,
  1196, 10131, 10534, 11456, 11790.
The Partition Problem


Given a set of positive integers, A = {a1, a2, …, an}. The question is to select a subset
B of A such that the sum of the numbers in B equals the sum of the numbers not in
B, i.e.,        . We may assume that the sum of all numbers in A is 2K, an even
number. We now propose a dynamic programming solution. For 1 ≤ i ≤ n and 0 ≤ j
≤ K,
          ∑ ai =      ∑         aj
        ai ∈B      a j ∈ A− B


define P[i, j] = True if there exists a subset of the first i
numbers a1 through ai whose sum equals j;                                     False otherwise.
Thus, P[i, j] = True if either j = 0 or if (i = 1 and j = a1). When i > 1, we have the
following recurrence:                                                            P[i, j] = P[i – 1,
j] or (P[i – 1, j – ai] if j – ai ≥ 0)                That is, in order for P[i, j] to be true,
either there exists a subset of the first i – 1 numbers whose sum equals j, or whose
sum equals j – ai (this latter case would use the solution of P[i – 1, j – ai] and add the
number ai. The value P[n, K] is the answer.
Example: Suppose A = {2, 1, 1, 3, 5} contains 5 positive integers. The sum of
these number is 2+1+1+3+5=12, an even number. The partition problem
computes the truth value of P[5, 6] using a tabular approach as follows:



                             Because j ≠ a1
               j
      i               0 1 2 3 4 5 6

    a1 = 2            T F T F F F FT T T T
    a2 = 1            F F FT T T T T F FT T
    a3 = 1             T T T T TT T T T T T
    a4 = 3            T                                P[4,5] = (P[3,5] or P[3,
    a5 = 5                                             5 – a4] )




             Always        The time complexity is O(nK); the space complexity
             true          O(K).
The Partition Problem Source Code
sum = 0;
for(i=0;i<n;i++)
    sum += A[i];
                                 for(i=sum/2;i>=0;i--)
                                         if(Flag[i]==1)
for(i=0;i<=sum/2;i++)                              break;
    Flag[i]=0;
                                 Dif = (sum – i) – i;
Flag[0]=1;

for(i=0;i<n;i++)
    for(j=sum/2-A[i];j>=0;j--)
    if(Flag[j]==1)
           Flag[j+A[i]]=1;
String Partition

A string of digits instead of a list of integers. There are many ways to split a
   string of digits into a list of non-zero-leading (0 itself is allowed) 32-bit
   signed integers. What is the maximum sum of the resultant integers if the
   string is split appropriately? A string of at most 200 digits.
Input:
11111111111111111111111111111111111111111111111111111
Output:
5555555666
Source Code
Limit = 2147483647;                          if(j==i&&input[j]==‘0’)
Len = strlen(input);                                  break;
for(i=0;i<=Len;i++)                                }
    Max[i]=0;                                }
for(i=0;i<Len;i++)
                                             printf(“%lldn”,Max[Len]);
{
    Num = 0;                                 Sample Problems:
    for(j=i;j<Len;j++)                       •UVA Online Judge: 11258.
    {
          Num = Num * 10 + input[j]-’0’;
          if(Num>Limit)
                    break;
          if(Num+Max[i]>Max[j+1])
                    Max[j+1] = Num+Max[i];
The traveling salesperson (TSP)
            problem
• e.g. a directed graph :
                               2
              1                                2
                               2
                          10
                      4
          6       5                        9       3
                      8
                               7
              4                                3
                               4

                                         1             2    3     4   
• Cost matrix:                 1           ∞           2    10    5     
                               2           2           ∞     9    ∞     
                               3           4           3    ∞     4     
                               4           6           8     7    ∞     
          
TSP
                                         (1,2,3)   4    (1,2,3,4)
                                 9
                                                                    6
                                 ¡Û      (1,2,4)   7    (1,2,4,3)
                         (1,2)
                                                                    4
                2
                                     3   (1,3,2)   ¡Û   (1,3,2,4)   6
                    10
          (1)            (1,3)                                          1
                                                                    2
                                     4   (1,3,4)   8    (1,3,4,2)
                5                                                   4

                         (1,4)                     9
                                     8   (1,4,2)        (1,4,2,3)
                                                                    2
                                 7
                                         (1,4,3)   3    (1,4,3,2)

• A multistage graph can describe all possible tours of a directed
  graph.
• Find the shortest path:
  (1, 4, 3, 2, 1) 5+7+3+2=17
BIT MASK
• you are in node 1, and you have to visit node
  2 5 & 10. Now, you have to fine the minimum
  cost for this operation.
• you have to visit l number of node, like l = 3.
  then node[] = 2, 3, 4
  & your source is 1. now you have to find the
  minimum cost of the tour.
• C[][] = cost matrix of the nodes.
BIT MASK Source Code
P[0]=1;                      for(i=0;i<l;i++)
for(i=1;i<=15;i++ )          {
{                               if( C[ source ][ node[i] ]!=MAX )
    P[i] = P[i-1]*2;            {
                                      bit[ P[i] ][i] = C[ source ][ node[i] ];
}                                }
                             }
source = 1;                  for(i=0;i<P[l];i++)
                             {
                                 for(j=0;j<l;j++)
for(i=0;i<=P[l];i++)
                                 {
{                                     if( bit[i][j]!=MAX )
    for(j=0;j<l;j++)                  {
    {                                       for(k=0;k<l;k++)
          bit[i][j] = MAX;                  {
    }                                               if( (i%P[k+1])/P[k]==0 )
}                                                   {
BIT MASK Source Code
if( C[node[j]][node[k]]!=MAX )                       int res = MAX;
{
     if( bit[ i+P[k] ][k] > bit[i][j] + C[node[j]]   for(i=0;i<l;i++)
     [node[k]] )                                          if(bit[P[l]-1][i]!=MAX)
     {                                                        if(res > bit[P[l]-1][i])
            bit[ i+P[k] ][k] = bit[i][j] +                         res = bit[P[l]-1][i];
     C[node[j]][node[k]];
     }                                               printf(“%dn”,res);
}
}
}
}
}
}
Sample
• UVA Online Judge: 10651, 216, 10496, 11813,
  10718.
Matrix-chain multiplication
• n matrices A1, A2, …, An with size
  p0 × p1, p1 × p2, p2 × p3, …, pn-1 × pn
 To determine the multiplication order such that # of scalar
  multiplications is minimized.
• To compute Ai × Ai+1, we need pi-1pipi+1 scalar
  multiplications.

e.g. n=4, A1: 3 × 5, A2: 5 × 4, A3: 4 × 2, A4: 2 × 5
   ((A1 × A2) × A3) × A4, # of scalar multiplications:
        3 * 5 * 4 + 3 * 4 * 2 + 3 * 2 * 5 = 114
   (A1 × (A2 × A3)) × A4, # of scalar multiplications:
        3 * 5 * 2 + 5 * 4 * 2 + 3 * 2 * 5 = 100
   (A1 × A2) × (A3 × A4), # of scalar multiplications:
        3 * 5 * 4 + 3 * 4 * 5 + 4 * 2 * 5 = 160
• Let m(i, j) denote the minimum cost for computing
  Ai × Ai+1 × … × Aj
            0
                                                         if i = j
  m(i, j) = 
            min{ m(i, k) + m(k + 1, j) + p i −1p k p i } if i < j
             i≤ k < j
• Time complexity : O(n3)
• Computation sequence :
Matrix-chain multiplication
// Matrix Ai has dimension p[i-1] x p[i] for i = 1..n
Matrix-Chain-Order(int p[]) {
// length[p] = n + 1
 n = p.length - 1;
// m[i,j] = Minimum number of scalar multiplications (i.e., cost)
// needed to compute the matrix A[i]A[i+1]...A[j] = A[i..j]
// cost is zero when multiplying one matrix
for (i = 1; i <= n; i++)
    m[i,i] = 0;

for (L=2; L<=n; L++) { // L is chain length
    for (i=1; i<=n-L+1; i++) {
           j = i+L-1;
Matrix-chain multiplication
  m[i,j] = MAXINT;
  for (k=i; k<=j-1; k++) {
  // q = cost/scalar multiplications
  q = m[i,k] + m[k+1,j] + p[i-1]*p[k]*p[j];
  if (q < m[i,j])
  {
          m[i,j] = q;
         // s[i,j] = Second auxiliary table that stores k
         // k = Index that achieved optimal
         cost s[i,j] = k;
  }
}}}}
Thanks!

Más contenido relacionado

La actualidad más candente

Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
Ehtisham Ali
 

La actualidad más candente (20)

Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Parallel sorting Algorithms
Parallel  sorting AlgorithmsParallel  sorting Algorithms
Parallel sorting Algorithms
 
Genetic algorithms in Data Mining
Genetic algorithms in Data MiningGenetic algorithms in Data Mining
Genetic algorithms in Data Mining
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
 
Branch and bound
Branch and boundBranch and bound
Branch and bound
 
simple problem to convert NFA with epsilon to without epsilon
simple problem to convert NFA with epsilon to without epsilonsimple problem to convert NFA with epsilon to without epsilon
simple problem to convert NFA with epsilon to without epsilon
 
Design & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture NotesDesign & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture Notes
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
 
Design and analysis of algorithms
Design and analysis of algorithmsDesign and analysis of algorithms
Design and analysis of algorithms
 
Knapsack problem using greedy approach
Knapsack problem using greedy approachKnapsack problem using greedy approach
Knapsack problem using greedy approach
 
Greedy Algorithm
Greedy AlgorithmGreedy Algorithm
Greedy Algorithm
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
 
Analysis and Design of Algorithms
Analysis and Design of AlgorithmsAnalysis and Design of Algorithms
Analysis and Design of Algorithms
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Daa
DaaDaa
Daa
 
Lecture 4 asymptotic notations
Lecture 4   asymptotic notationsLecture 4   asymptotic notations
Lecture 4 asymptotic notations
 

Destacado (6)

Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programming
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
 
Operations research
Operations researchOperations research
Operations research
 
Transportation Problem In Linear Programming
Transportation Problem In Linear ProgrammingTransportation Problem In Linear Programming
Transportation Problem In Linear Programming
 
5.3 dynamic programming
5.3 dynamic programming5.3 dynamic programming
5.3 dynamic programming
 
Linear programing
Linear programingLinear programing
Linear programing
 

Similar a Dynamic programming

Cn os-lp lab manual k.roshan
Cn os-lp lab manual k.roshanCn os-lp lab manual k.roshan
Cn os-lp lab manual k.roshan
riturajj
 
Dynamic1
Dynamic1Dynamic1
Dynamic1
MyAlome
 
11 1. multi-dimensional array eng
11 1. multi-dimensional array eng11 1. multi-dimensional array eng
11 1. multi-dimensional array eng
웅식 전
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
klyni777
 
lecture 24
lecture 24lecture 24
lecture 24
sajinsc
 

Similar a Dynamic programming (20)

Cn os-lp lab manual k.roshan
Cn os-lp lab manual k.roshanCn os-lp lab manual k.roshan
Cn os-lp lab manual k.roshan
 
Introduction to Polyhedral Compilation
Introduction to Polyhedral CompilationIntroduction to Polyhedral Compilation
Introduction to Polyhedral Compilation
 
Numerical Method Assignment
Numerical Method AssignmentNumerical Method Assignment
Numerical Method Assignment
 
Wu Mamber (String Algorithms 2007)
Wu  Mamber (String Algorithms 2007)Wu  Mamber (String Algorithms 2007)
Wu Mamber (String Algorithms 2007)
 
C programs
C programsC programs
C programs
 
Dynamic1
Dynamic1Dynamic1
Dynamic1
 
11 1. multi-dimensional array eng
11 1. multi-dimensional array eng11 1. multi-dimensional array eng
11 1. multi-dimensional array eng
 
C Code and the Art of Obfuscation
C Code and the Art of ObfuscationC Code and the Art of Obfuscation
C Code and the Art of Obfuscation
 
Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"
 
Yoyak ScalaDays 2015
Yoyak ScalaDays 2015Yoyak ScalaDays 2015
Yoyak ScalaDays 2015
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
 
parallel programming.ppt
parallel programming.pptparallel programming.ppt
parallel programming.ppt
 
Data Structure: Algorithm and analysis
Data Structure: Algorithm and analysisData Structure: Algorithm and analysis
Data Structure: Algorithm and analysis
 
Brief intro to clojure
Brief intro to clojureBrief intro to clojure
Brief intro to clojure
 
lecture 24
lecture 24lecture 24
lecture 24
 
Longest Common Sub-sequence (LCS)
Longest Common Sub-sequence (LCS)Longest Common Sub-sequence (LCS)
Longest Common Sub-sequence (LCS)
 
Lowest common ancestor
Lowest common ancestorLowest common ancestor
Lowest common ancestor
 
Time complexity
Time complexityTime complexity
Time complexity
 
Advanced data structure
Advanced data structureAdvanced data structure
Advanced data structure
 
CS323: Longest Common Subsequences
CS323: Longest Common SubsequencesCS323: Longest Common Subsequences
CS323: Longest Common Subsequences
 

Más de Shakil Ahmed (20)

Algorithm
AlgorithmAlgorithm
Algorithm
 
B-tree & R-tree
B-tree & R-treeB-tree & R-tree
B-tree & R-tree
 
Prototype pattern
Prototype patternPrototype pattern
Prototype pattern
 
Observer pattern
Observer patternObserver pattern
Observer pattern
 
Mediator pattern
Mediator patternMediator pattern
Mediator pattern
 
Flyweight pattern
Flyweight patternFlyweight pattern
Flyweight pattern
 
Facade pattern
Facade patternFacade pattern
Facade pattern
 
Composite pattern
Composite patternComposite pattern
Composite pattern
 
Command pattern
Command patternCommand pattern
Command pattern
 
Chain of responsibility
Chain of responsibilityChain of responsibility
Chain of responsibility
 
Builder pattern
Builder patternBuilder pattern
Builder pattern
 
Bridge pattern
Bridge patternBridge pattern
Bridge pattern
 
Adapter pattern
Adapter patternAdapter pattern
Adapter pattern
 
Proxy pattern
Proxy patternProxy pattern
Proxy pattern
 
iOS 5
iOS 5iOS 5
iOS 5
 
Ios development
Ios developmentIos development
Ios development
 
Graph
GraphGraph
Graph
 
Segment tree
Segment treeSegment tree
Segment tree
 
Tree & bst
Tree & bstTree & bst
Tree & bst
 
Trie tree
Trie treeTrie tree
Trie tree
 

Último

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 

Último (20)

Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 

Dynamic programming

  • 1. Dynamic Programming Md. Shakil Ahmed Software Engineer Astha it research & consultancy ltd. Dhaka, Bangladesh
  • 2. Dynamic Programming • Algorithmic technique that systematically records the answers to sub-problems in a table and re-uses those recorded results (rather than re-computing them). • Simple Example: Calculating the Nth Fibonacci number. Fib(N) = Fib(N-1) + Fib(N-2)
  • 3. Introduction Topic Focus: • Unidirectional TSP • Coin Change • LCS • LIS • The Partition Problem • String Partition • TSP (Bitmask) • Matrix-chain multiplication
  • 4. Unidirectional TSP Output minimal-weight path, and the second line is the cost of a minimal path. Input: Output: 56 123445 341286 16 618274 593995 841326 372864
  • 5. Source Code fix= infinity; for(j=m-1;j>0;j--) for(i=0;i<n;i++) for(i=0;i<n;i++) { for(j=0;j<m;j++) if(si[i][j-1] > si[i][j] + sa[i][j-1] || (si[i][j-1] == { si[i][j] + sa[i][j-1] && t1[i][j-1] > i)) scanf("%lld",&sa[i][j]); { si[i][j]=fix; si[i][j-1]=si[i][j]+sa[i][j-1]; t1[i][j-1]=i; t1[i][j]=-1; } } k=i-1; for(i=0;i<n;i++) if(k<0) si[i][m-1]=sa[i][m-1]; k=k+n; if(si[k][j-1] > si[i][j] + sa[k][j-1] || (si[k][j-1] == si[i][j] + sa[k][j-1] && t1[k][j-1] > i)) { si[k][j-1]=si[i][j]+sa[k][j-1]; t1[k][j-1]=i; }
  • 6. Source Code k=i+1; printf("%ld",k+1); k=k%n; if(si[k][j-1] > si[i][j] + sa[k][j-1] || j=0; (si[k][j-1] == si[i][j] + sa[k][j-1] && t1[k][j-1] > i)) while(t1[k][j]!=-1) { { printf(" %ld",t1[k][j]+1); si[k][j-1]=si[i][j]+sa[k][j-1]; k=t1[k][j]; t1[k][j-1]=i; j++; } } } min=fix; printf("n%lldn",min); for(i=0;i<n;i++) Sample Problems: if(si[i][0]<min) •UVA Online Judge: 116. { min=si[i][0]; k=i; }
  • 7. Coin change • Coin change is the problem of finding the number of ways to make change for a target amount given a set of denominations. It is assumed that there is an unlimited supply of coins for each denomination. An example will be finding change for target amount 4 using change of 1,2,3 for which the solutions are (1,1,1,1), (2,2), (1,1,2), (1,3).
  • 8. Coin Change Source Code long s[6], sa[7600]={0}, for(j=0;j<=4;j++) i, j, k; { for(i=0;i<=7500;i++) { s[0]=1; if(i+s[j]>7500) s[1]=5; break; s[2]=10; s[3]=25; sa[i+s[j]]+=sa[i]; } s[4]=50; } sa[0]=1; while(scanf("%ld",&i)==1) { printf("%ldn",sa[i]); }
  • 9. Coin change • Sample Problems: 1.UVA Online Judge: 147, 166, 357, 674, 10306, 10313, 11137, 11517.
  • 10. The longest common subsequence (LCS) problem • A string : A = b a c a d • A subsequence of A: deleting 0 or more symbols from A (not necessarily consecutive). e.g. ad, ac, bac, acad, bacad, bcd. • Common subsequences of A = b a c a d and B = a c c b a d c b : ad, ac, bac, acad. • The longest common subsequence (LCS) of A and B: a c a d.
  • 11. The LCS algorithm • Let A = a1 a2 … am and B = b1 b2 … bn • Let Li,j denote the length of the longest common subsequence of a1 a2 … ai and b1 b2 … bj. •  Li,j = Li-1,j-1 + 1 if ai=bj max{ Li-1,j, Li,j-1 } if ai≠bj L0,0 = L0,j = Li,0 = 0 for 1≤i≤m, 1≤j≤n.
  • 12. • Time complexity: O(mn) • The dynamic programming approach for solving the LCS problem: L1,1 L1,2 L1,3 L2,1 L2,2 L3,1 Lm,n
  • 13. Tracing back in the LCS algorithm • e.g. A = b a c a d, B = a c c b a d c b B a c c b a d c b 0 0 0 0 0 0 0 0 0 b 0 0 0 0 1 1 1 1 1 a 0 1 1 1 1 2 2 2 2 A c 0 1 2 2 2 2 2 3 3 a 0 1 2 2 2 3 3 3 3 d 0 1 2 2 2 3 4 4 4 • After all Li,j’s have been found, we can trace back to find the longest common subsequence of A and B.
  • 14. LCS Source Code void LCS(){ for(i=0;i<=m;i++) for(j=0;j<=n;j++) else if(c[i-1][j]>=c[i][j-1]) { { c[i][j]=c[i-1][j]; c[i][j]=0; b[i][j]='2'; b[i][j]='0'; } } else { for(i=1;i<=m;i++) c[i][j]=c[i][j-1]; for(j=1;j<=n;j++) b[i][j]='3'; } { } if(x[i]==y[j]) } { c[i][j]=c[i-1][j-1]+1; b[i][j]='1'; }
  • 15. void printSequence(long int i,long int j) Sample Problems: { • UVA Online Judge: 10066, if(i==0||j==0) 10405. return ; if(b[i][j]=='1') { printSequence (i-1,j-1); printf(“%c”,x[i]); } else if(b[i][j]=='2') printSequence (i-1,j); else printSequence (i,j-1); }
  • 16. Longest Increasing Subsequence • The longest increasing subsequence problem is to find a subsequence of a given sequence in which the subsequence elements are in sorted order, lowest to highest, and in which the subsequence is as long as possible. This subsequence is not necessarily contiguous. • A simple way of finding the longest increasing subsequence is to use the Longest Common Subsequence ( Dynamic Programming) algorithm. 1. Make a sorted copy of the sequence A, denoted as B. O(nlog(n)) time. 2. Use Longest Common Subsequence on with A and B. O(n2) time.
  • 17. Simple LIS function lis_length( a ) n := a.length q := new Array(n) for k from 0 to n: max := 0; for j from 0 to k, if a[k] > a[j]: if q[j] > max, then set max = q[j]. q[k] := max + 1; max := 0 for i from 0 to n: if q[i] > max, then set max = q[i]. return max;
  • 18. Simple LIS • Complexity O(n^2) for(j=i+1;j<n;j++) for(i=0;i<n;i++) If(A[j]>=A[i] && Len[j]<Len[i]+1) { { scanf(“%ld”,&A[i]); Len[j]=Len[i]+1; Pre[j]=i; Len[i]=1; } Pre = -1; } } For sequence Print: Max = 0; Position = 0; void Show(long h1) for(i=0;i<n;i++) { If(Pre[h1]!=-1) { Show(Pre[h1]); if(Len[i]>Max) Printf(“%ldn”,A[h1]); { } Max = Len[i]; Show(Position); Position = i; }
  • 19. Efficient LIS • Complexity O(n*log(n)) L=0 for i = 1, 2, ... n: binary search for the largest positive j ≤ L such that X[M[j]] < X[i] (or set j = 0 if no such value exists) P[i] = M[j] if j == L or X[i] < X[M[j+1]]: M[j+1] = i L = max(L, j+1)
  • 20. Efficient LIS long BinarySearch(long left,long right,long value) { long mid = (left+right)/2; while(left<=right) { if(temp[mid]==value) break; else if(temp[mid]<value) left = mid+1; else right = mid – 1; mid = (left+right)/2; } return mid; }
  • 21. Efficient LIS void LIS(long N) if(mid+1>=n) { long n=0,i,mid; { for(i=0;i<N;i++) temp[n]=sa[i]; tempP[n]=i; { n++; if(n==0) } { temp[n]=sa[i];tempP[n]=i; else if(temp[mid+1]>sa[i]) A[i]=1;n++; } { else temp[mid+1]=sa[i]; { tempP[mid+1]=i; mid = BinarySearch(0,n-1,sa[i]); } while(mid>0&&temp[mid]>sa[i]) } mid–; else if(temp[0]>sa[i]) { if(temp[mid]<sa[i]) temp[0]=sa[i]; { tempP[0]=i; A[i]=mid+2;P[i]=tempP[mid]; } } } }
  • 22. Efficient LIS void show(long h1) for(i=0;i<n;i++) { { if(P[h1]==-1) scanf(“%ld”,&sa[i]); printf(“%ld”,sa[h1]); P[i]=-1; else } { LIS(n); show(P[h1]); max = 0; printf(” %ld”,sa[h1]); x = 0; } for(i=0;i<n;i++) } if(A[i]>max) { max = A[i]; x = i; } printf(“%ldn”,max); show(x);
  • 23. LIS • Sample Problems: 1.UVA Online Judge: 111, 231, 437, 481, 497, 1196, 10131, 10534, 11456, 11790.
  • 24. The Partition Problem Given a set of positive integers, A = {a1, a2, …, an}. The question is to select a subset B of A such that the sum of the numbers in B equals the sum of the numbers not in B, i.e., . We may assume that the sum of all numbers in A is 2K, an even number. We now propose a dynamic programming solution. For 1 ≤ i ≤ n and 0 ≤ j ≤ K, ∑ ai = ∑ aj ai ∈B a j ∈ A− B define P[i, j] = True if there exists a subset of the first i numbers a1 through ai whose sum equals j; False otherwise. Thus, P[i, j] = True if either j = 0 or if (i = 1 and j = a1). When i > 1, we have the following recurrence: P[i, j] = P[i – 1, j] or (P[i – 1, j – ai] if j – ai ≥ 0) That is, in order for P[i, j] to be true, either there exists a subset of the first i – 1 numbers whose sum equals j, or whose sum equals j – ai (this latter case would use the solution of P[i – 1, j – ai] and add the number ai. The value P[n, K] is the answer.
  • 25. Example: Suppose A = {2, 1, 1, 3, 5} contains 5 positive integers. The sum of these number is 2+1+1+3+5=12, an even number. The partition problem computes the truth value of P[5, 6] using a tabular approach as follows: Because j ≠ a1 j i 0 1 2 3 4 5 6 a1 = 2 T F T F F F FT T T T a2 = 1 F F FT T T T T F FT T a3 = 1 T T T T TT T T T T T a4 = 3 T P[4,5] = (P[3,5] or P[3, a5 = 5 5 – a4] ) Always The time complexity is O(nK); the space complexity true O(K).
  • 26. The Partition Problem Source Code sum = 0; for(i=0;i<n;i++) sum += A[i]; for(i=sum/2;i>=0;i--) if(Flag[i]==1) for(i=0;i<=sum/2;i++) break; Flag[i]=0; Dif = (sum – i) – i; Flag[0]=1; for(i=0;i<n;i++) for(j=sum/2-A[i];j>=0;j--) if(Flag[j]==1) Flag[j+A[i]]=1;
  • 27. String Partition A string of digits instead of a list of integers. There are many ways to split a string of digits into a list of non-zero-leading (0 itself is allowed) 32-bit signed integers. What is the maximum sum of the resultant integers if the string is split appropriately? A string of at most 200 digits. Input: 11111111111111111111111111111111111111111111111111111 Output: 5555555666
  • 28. Source Code Limit = 2147483647; if(j==i&&input[j]==‘0’) Len = strlen(input); break; for(i=0;i<=Len;i++) } Max[i]=0; } for(i=0;i<Len;i++) printf(“%lldn”,Max[Len]); { Num = 0; Sample Problems: for(j=i;j<Len;j++) •UVA Online Judge: 11258. { Num = Num * 10 + input[j]-’0’; if(Num>Limit) break; if(Num+Max[i]>Max[j+1]) Max[j+1] = Num+Max[i];
  • 29. The traveling salesperson (TSP) problem • e.g. a directed graph : 2 1 2 2 10 4 6 5 9 3 8 7 4 3 4     1  2  3  4    • Cost matrix: 1    ∞  2  10  5    2    2  ∞  9  ∞    3    4  3  ∞  4    4    6  8  7  ∞     
  • 30. TSP (1,2,3) 4 (1,2,3,4) 9 6 ¡Û (1,2,4) 7 (1,2,4,3) (1,2) 4 2 3 (1,3,2) ¡Û (1,3,2,4) 6 10 (1) (1,3) 1 2 4 (1,3,4) 8 (1,3,4,2) 5 4 (1,4) 9 8 (1,4,2) (1,4,2,3) 2 7 (1,4,3) 3 (1,4,3,2) • A multistage graph can describe all possible tours of a directed graph. • Find the shortest path: (1, 4, 3, 2, 1) 5+7+3+2=17
  • 31. BIT MASK • you are in node 1, and you have to visit node 2 5 & 10. Now, you have to fine the minimum cost for this operation. • you have to visit l number of node, like l = 3. then node[] = 2, 3, 4 & your source is 1. now you have to find the minimum cost of the tour. • C[][] = cost matrix of the nodes.
  • 32. BIT MASK Source Code P[0]=1; for(i=0;i<l;i++) for(i=1;i<=15;i++ ) { { if( C[ source ][ node[i] ]!=MAX ) P[i] = P[i-1]*2; { bit[ P[i] ][i] = C[ source ][ node[i] ]; } } } source = 1; for(i=0;i<P[l];i++) { for(j=0;j<l;j++) for(i=0;i<=P[l];i++) { { if( bit[i][j]!=MAX ) for(j=0;j<l;j++) { { for(k=0;k<l;k++) bit[i][j] = MAX; { } if( (i%P[k+1])/P[k]==0 ) } {
  • 33. BIT MASK Source Code if( C[node[j]][node[k]]!=MAX ) int res = MAX; { if( bit[ i+P[k] ][k] > bit[i][j] + C[node[j]] for(i=0;i<l;i++) [node[k]] ) if(bit[P[l]-1][i]!=MAX) { if(res > bit[P[l]-1][i]) bit[ i+P[k] ][k] = bit[i][j] + res = bit[P[l]-1][i]; C[node[j]][node[k]]; } printf(“%dn”,res); } } } } } }
  • 34. Sample • UVA Online Judge: 10651, 216, 10496, 11813, 10718.
  • 35. Matrix-chain multiplication • n matrices A1, A2, …, An with size p0 × p1, p1 × p2, p2 × p3, …, pn-1 × pn To determine the multiplication order such that # of scalar multiplications is minimized. • To compute Ai × Ai+1, we need pi-1pipi+1 scalar multiplications. e.g. n=4, A1: 3 × 5, A2: 5 × 4, A3: 4 × 2, A4: 2 × 5 ((A1 × A2) × A3) × A4, # of scalar multiplications: 3 * 5 * 4 + 3 * 4 * 2 + 3 * 2 * 5 = 114 (A1 × (A2 × A3)) × A4, # of scalar multiplications: 3 * 5 * 2 + 5 * 4 * 2 + 3 * 2 * 5 = 100 (A1 × A2) × (A3 × A4), # of scalar multiplications: 3 * 5 * 4 + 3 * 4 * 5 + 4 * 2 * 5 = 160
  • 36. • Let m(i, j) denote the minimum cost for computing Ai × Ai+1 × … × Aj 0  if i = j m(i, j) =  min{ m(i, k) + m(k + 1, j) + p i −1p k p i } if i < j  i≤ k < j • Time complexity : O(n3) • Computation sequence :
  • 37. Matrix-chain multiplication // Matrix Ai has dimension p[i-1] x p[i] for i = 1..n Matrix-Chain-Order(int p[]) { // length[p] = n + 1 n = p.length - 1; // m[i,j] = Minimum number of scalar multiplications (i.e., cost) // needed to compute the matrix A[i]A[i+1]...A[j] = A[i..j] // cost is zero when multiplying one matrix for (i = 1; i <= n; i++) m[i,i] = 0; for (L=2; L<=n; L++) { // L is chain length for (i=1; i<=n-L+1; i++) { j = i+L-1;
  • 38. Matrix-chain multiplication m[i,j] = MAXINT; for (k=i; k<=j-1; k++) { // q = cost/scalar multiplications q = m[i,k] + m[k+1,j] + p[i-1]*p[k]*p[j]; if (q < m[i,j]) { m[i,j] = q; // s[i,j] = Second auxiliary table that stores k // k = Index that achieved optimal cost s[i,j] = k; } }}}}