SlideShare una empresa de Scribd logo
1 de 50
Descargar para leer sin conexión
Dynamic Programming
            Problems




   Dynamic Programming

       League of Programmers
              ACA, IIT Kanpur




           October 22, 2012




League of Programmers   Dynamic Programming
Dynamic Programming
                             Problems

Outline




  1   Dynamic Programming


  2   Problems




                 League of Programmers   Dynamic Programming
Dynamic Programming
                              Problems

Dynamic Programming
  What is DP?
  DP is another technique for problems with optimal substructure:
  An optimal solution to a problem contains optimal solutions to
  subproblems. This doesn't necessarily mean that every optimal
  solution to a subproblem will contribute to the main solution.




                  League of Programmers   Dynamic Programming
Dynamic Programming
                              Problems

Dynamic Programming
  What is DP?
  DP is another technique for problems with optimal substructure:
  An optimal solution to a problem contains optimal solutions to
  subproblems. This doesn't necessarily mean that every optimal
  solution to a subproblem will contribute to the main solution.
       For divide and conquer (top down), the subproblems are
       independent so we can solve them in any order.




                  League of Programmers   Dynamic Programming
Dynamic Programming
                              Problems

Dynamic Programming
  What is DP?
  DP is another technique for problems with optimal substructure:
  An optimal solution to a problem contains optimal solutions to
  subproblems. This doesn't necessarily mean that every optimal
  solution to a subproblem will contribute to the main solution.
       For divide and conquer (top down), the subproblems are
       independent so we can solve them in any order.
       For greedy algorithms (bottom up), we can always choose the
       "right" subproblem by a greedy choice.




                 League of Programmers   Dynamic Programming
Dynamic Programming
                               Problems

Dynamic Programming
  What is DP?
  DP is another technique for problems with optimal substructure:
  An optimal solution to a problem contains optimal solutions to
  subproblems. This doesn't necessarily mean that every optimal
  solution to a subproblem will contribute to the main solution.
       For divide and conquer (top down), the subproblems are
       independent so we can solve them in any order.
       For greedy algorithms (bottom up), we can always choose the
       "right" subproblem by a greedy choice.
       In dynamic programming, we solve many subproblems and
       store the results: not all of them will contribute to solving the
       larger problem. Because of optimal substructure, we can be
       sure that at least some of the subproblems will be useful

                   League of Programmers   Dynamic Programming
Dynamic Programming
                              Problems

Dynamic Programming



  Steps to solve a DP problem




                 League of Programmers   Dynamic Programming
Dynamic Programming
                              Problems

Dynamic Programming



  Steps to solve a DP problem
    1 Dene subproblems




                 League of Programmers   Dynamic Programming
Dynamic Programming
                              Problems

Dynamic Programming



  Steps to solve a DP problem
    1 Dene subproblems

    2 Write down the recurrence that relates subproblems




                 League of Programmers   Dynamic Programming
Dynamic Programming
                              Problems

Dynamic Programming



  Steps to solve a DP problem
    1 Dene subproblems

    2 Write down the recurrence that relates subproblems

    3 Recognize and solve the base cases




                 League of Programmers   Dynamic Programming
Dynamic Programming
                              Problems

Fibonacci Sequence



  Naive Recursive Function
  int Fib(int n){
    if(n==1 || n==2)
      return 1;
    return Fib(n-1)+Fib(n-2)
  }




                 League of Programmers   Dynamic Programming
Dynamic Programming
                             Problems

Fibonacci Sequence




  DP Solution O(n)
  Fib[1] = Fib[2] = 1;
  for(i=3;iN;i++)
    Fib[i] = Fib[i-1]+Fib[i-2]




                League of Programmers   Dynamic Programming
Dynamic Programming
                              Problems

Problem


  Problem 2
  Given n, nd the number of dierent ways to write n as the sum of
  1, 3, 4
  Example: for n = 5, the answer is 6
  5 = 1+1+1+1+1
  = 1+1+3
  = 1+3+1
  = 3+1+1
  = 1+4




                 League of Programmers   Dynamic Programming
Dynamic Programming
                            Problems

Problem


  Dene Subproblems




                League of Programmers   Dynamic Programming
Dynamic Programming
                              Problems

Problem


  Dene Subproblems
      Dn be the number of ways to write n as the sum of 1, 3, 4




                 League of Programmers   Dynamic Programming
Dynamic Programming
                              Problems

Problem


  Dene Subproblems
      Dn be the number of ways to write n as the sum of 1, 3, 4
      Find the recurrence
                         Dn = Dn −       1   + Dn −3 + Dn −4




                 League of Programmers        Dynamic Programming
Dynamic Programming
                              Problems

Problem


  Dene Subproblems
      Dn be the number of ways to write n as the sum of 1, 3, 4
      Find the recurrence
                         Dn = Dn −       1   + Dn −3 + Dn −4
      Solve the base cases
      D =1
        0

      Dn = 0 for all negative n




                 League of Programmers        Dynamic Programming
Dynamic Programming
                              Problems

Problem


  Dene Subproblems
      Dn be the number of ways to write n as the sum of 1, 3, 4
      Find the recurrence
                         Dn = Dn −       1   + Dn −3 + Dn −4
      Solve the base cases
      D =1
        0

      Dn = 0 for all negative n
      Alternatively, can set: D = D = D = 1, D = 2
                                  0           1      2          3




                 League of Programmers        Dynamic Programming
Dynamic Programming
                               Problems

Problem




  Implementation
  D[0]=D[1]=D[2]=1; D[3]=2;
  for(i=4;i=n;i++)
    D[i]=D[i-1]+D[i-3]+D[i-4];




                   League of Programmers   Dynamic Programming
Dynamic Programming
                             Problems

LCS


 Problem 3
 Given two strings x and y, nd the longest common subsequence
 (LCS) and print its length.
 Example:
 x: ABCBDAB
 y: BDCABC
 BCAB is the longest subsequence found in both sequences, so
 the answer is 4




                League of Programmers   Dynamic Programming
Dynamic Programming
                        Problems

LCS



 Analysis




            League of Programmers   Dynamic Programming
Dynamic Programming
                             Problems

LCS



 Analysis
     There are 2m subsequences of X.
     Testing a subsequence (length k) takes time O(k + n).
     So brute force algorithm is O(n ∗ 2m ).




                League of Programmers   Dynamic Programming
Dynamic Programming
                             Problems

LCS



 Analysis
     There are 2m subsequences of X.
     Testing a subsequence (length k) takes time O(k + n).
     So brute force algorithm is O(n ∗ 2m ).
     Divide and conquer or Greedy algorithm?




                League of Programmers   Dynamic Programming
Dynamic Programming
                              Problems

LCS



 Analysis
     There are 2m subsequences of X.
     Testing a subsequence (length k) takes time O(k + n).
     So brute force algorithm is O(n ∗ 2m ).
     Divide and conquer or Greedy algorithm?
          No, can't tell what initial division or greedy choice to make.




                 League of Programmers   Dynamic Programming
Dynamic Programming
                              Problems

LCS


 Analysis
     There are 2m subsequences of X.
     Testing a subsequence (length k) takes time O(k + n).
     So brute force algorithm is O(n ∗ 2m ).
     Divide and conquer or Greedy algorithm?
          No, can't tell what initial division or greedy choice to make.


 Thus, none of the approaches we have learned so far work here!!!




                 League of Programmers   Dynamic Programming
Dynamic Programming
                              Problems

LCS

 Analysis
     There are 2m subsequences of X.
     Testing a subsequence (length k) takes time O(k + n).
     So brute force algorithm is O(n ∗ 2m ).
     Divide and conquer or Greedy algorithm?
          No, can't tell what initial division or greedy choice to make.


 Thus, none of the approaches we have learned so far work here!!!

 Intuition
 A LCS of two sequences has as a prex a LCS of prexes of the
 sequences. So, We concentrate on LCS for smaller problems, i.e
 simply removes the last (common) element.

                 League of Programmers   Dynamic Programming
Dynamic Programming
                           Problems

LCS


 Dene Subproblems




               League of Programmers   Dynamic Programming
Dynamic Programming
                              Problems

LCS


 Dene Subproblems
     Let Di ,j be the length of the LCS of x      1...i   and y1  ...j




                 League of Programmers   Dynamic Programming
Dynamic Programming
                              Problems

LCS


 Dene Subproblems
     Let Di ,j be the length of the LCS of x      1...i   and y1  ...j
     Find the recurrence




                 League of Programmers   Dynamic Programming
Dynamic Programming
                                    Problems

LCS


 Dene Subproblems
     Let Di ,j be the length of the LCS of x             1...i   and y1  ...j
     Find the recurrence
          If   xi   =   yj   , they both contribute to the LCS. In this case,

                                         Di ,j = Di −1,j −1 + 1




                        League of Programmers   Dynamic Programming
Dynamic Programming
                                     Problems

LCS


 Dene Subproblems
     Let Di ,j be the length of the LCS of x                    1...i   and y 1 ...j
     Find the recurrence
          If   xi   =   yj   , they both contribute to the LCS. In this case,

                                              Di ,j = Di −1,j −1 + 1
          Either        xi   or   yj   does not contribute to the LCS, so one can be

          dropped. Otherwise,

                                          Di ,j = max {Di −1,j , Di ,j −1 }




                        League of Programmers         Dynamic Programming
Dynamic Programming
                                      Problems

LCS


 Dene Subproblems
     Let Di ,j be the length of the LCS of x                  1...i   and y1  ...j
     Find the recurrence
           If   xi   =   yj   , they both contribute to the LCS. In this case,

                                              Di ,j = Di −1,j −1 + 1
           Either        xi   or   yj   does not contribute to the LCS, so one can be

           dropped. Otherwise,

                           Di ,j = max {Di −1,j , Di ,j −1 }
      Find and solve the base cases: Di , = D ,j = 0     0        0




                         League of Programmers       Dynamic Programming
Dynamic Programming
                              Problems

LCS

 Implementation
 for(i=0;i=n;i++) D[i][0]=0;
 for(j=0;j=m;j++) D[0][j]=0;
 for(i=1;i=n;i++) {
   for(j=1;j=m;j++) {
     if(x[i]==y[j])
       D[i][j]=D[i-1][j-1]+1;
     else
       D[i][j]=max(D[i-1][j],D[i][j-1]);
   }
 }



                  League of Programmers   Dynamic Programming
Dynamic Programming
                               Problems

LCS




 Recovering the LCS
 Modify the algorithm to also build a matrix D[1 . . . n; 1 . . . m],
 recording how the solutions to subproblems were arrived at.




                  League of Programmers   Dynamic Programming
Dynamic Programming
                               Problems

Longest Non Decresing Subsequence



  Problem 4
  Given an array [1 , 2 , 5, 2, 8, 6, 3, 6, 9, 7]. Find a subsequence
  which is non decreasing and of maximum length.
  1-5-8-9 forms a non decreasing subsequence
  So does 1-2-2-6-6-7 but it is longer




                   League of Programmers   Dynamic Programming
Dynamic Programming
                             Problems

LNDS

 Subproblem
 Length of LNDS ending at i th location




                 League of Programmers   Dynamic Programming
Dynamic Programming
                              Problems

LNDS

 Subproblem
 Length of LNDS ending at i th location

 Implementation
 for(i=0;i100;i++) {
   max=0;
   for(j=0;ji;j++) {
     if(A[i]=A[j]  L[j]max)
       max = L[j];
   }
   L[i] = max+1;
 }


                  League of Programmers   Dynamic Programming
Dynamic Programming
                              Problems

Problem




  Problem 5
  Given a tree, color nodes black as many as possible without
  coloring two adjacent nodes.




                  League of Programmers   Dynamic Programming
Dynamic Programming
                            Problems

Problem


  Dene Subproblems




                League of Programmers   Dynamic Programming
Dynamic Programming
                               Problems

Problem


  Dene Subproblems
      we arbitrarily decide the root node r




                  League of Programmers   Dynamic Programming
Dynamic Programming
                              Problems

Problem


  Dene Subproblems
      we arbitrarily decide the root node r
      Bv : the optimal solution for a subtree having v as the root,
      where we color v black




                  League of Programmers   Dynamic Programming
Dynamic Programming
                              Problems

Problem


  Dene Subproblems
      we arbitrarily decide the root node r
      Bv : the optimal solution for a subtree having v as the root,
      where we color v black
      Wv : the optimal solution for a subtree having v as the root,
      where we don't color v




                  League of Programmers   Dynamic Programming
Dynamic Programming
                              Problems

Problem


  Dene Subproblems
      we arbitrarily decide the root node r
      Bv : the optimal solution for a subtree having v as the root,
      where we color v black
      Wv : the optimal solution for a subtree having v as the root,
      where we don't color v
      The answer is max{Br ,Wr }




                  League of Programmers   Dynamic Programming
Dynamic Programming
                             Problems

Problem

  Find The Recurrence
  Observation




                League of Programmers   Dynamic Programming
Dynamic Programming
                               Problems

Problem

  Find The Recurrence
  Observation
       Once v's color is determined, its subtrees can be solved
       independently




                  League of Programmers   Dynamic Programming
Dynamic Programming
                               Problems

Problem

  Find The Recurrence
  Observation
       Once v's color is determined, its subtrees can be solved
       independently
           If v is colored, its children must not be colored


                                Bv   =1+     u ∈child (v )   Wu




                  League of Programmers   Dynamic Programming
Dynamic Programming
                               Problems

Problem

  Find The Recurrence
  Observation
       Once v's color is determined, its subtrees can be solved
       independently
           If v is colored, its children must not be colored


                                Bv   =1+     u ∈child (v )   Wu
           If v is not colored, its children can have any color


                                Wv   =1+      u ∈child (v )   Bu




                  League of Programmers   Dynamic Programming
Dynamic Programming
                               Problems

Problem

  Find The Recurrence
  Observation
       Once v's color is determined, its subtrees can be solved
       independently
           If v is colored, its children must not be colored


                                Bv   =1+     u ∈child (v )   Wu
           If v is not colored, its children can have any color


                            Wv       =1+      u ∈child (v )   Bu
      Base cases: leaf nodes



                  League of Programmers   Dynamic Programming
Dynamic Programming
                             Problems

Outline




  1   Dynamic Programming


  2   Problems




                 League of Programmers   Dynamic Programming
Dynamic Programming
                            Problems

Problems
  Added on the contest on VOC http://ahmed-aly.com/voc/
  Contest ID: 2616
  Name: ACA, IITK LOP 03
  Author: pnkjjindal
  Links:
   1   http://spoj.pl/problems/ACTIV
   2   http://www.spoj.pl/problems/COINS
   3   http://spoj.pl/problems/IOIPALIN
   4   http://spoj.pl/problems/ADFRUITS
   5   http://www.spoj.pl/problems/RENT
   6   http://www.spoj.pl/problems/M3TILE
   7   http://www.spoj.pl/problems/IOPC1203
   8   http://www.spoj.pl/problems/NGON

                League of Programmers   Dynamic Programming

Más contenido relacionado

Similar a Dynamic Programming Problems and Solutions

Dynamic programming class 16
Dynamic programming class 16Dynamic programming class 16
Dynamic programming class 16Kumar
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programmingJay Nagar
 
Programming for problem solving ppts unit 1
Programming for problem solving ppts unit 1Programming for problem solving ppts unit 1
Programming for problem solving ppts unit 1lakshmi lingutla
 
Dynamic programming prasintation eaisy
Dynamic programming prasintation eaisyDynamic programming prasintation eaisy
Dynamic programming prasintation eaisyahmed51236
 
352735344 rsh-qam11-tif-10-doc
352735344 rsh-qam11-tif-10-doc352735344 rsh-qam11-tif-10-doc
352735344 rsh-qam11-tif-10-docBookStoreLib
 
352735344 rsh-qam11-tif-10-doc
352735344 rsh-qam11-tif-10-doc352735344 rsh-qam11-tif-10-doc
352735344 rsh-qam11-tif-10-docFiras Husseini
 
Why computer programming
Why computer programmingWhy computer programming
Why computer programmingTUOS-Sam
 
9781111530532 ppt ch13
9781111530532 ppt ch139781111530532 ppt ch13
9781111530532 ppt ch13Terry Yoast
 
Algorithm lecture Dynamic programming
Algorithm lecture Dynamic programmingAlgorithm lecture Dynamic programming
Algorithm lecture Dynamic programmingrabiul souvon
 
Duality in Linear Programming Problem
Duality in Linear Programming ProblemDuality in Linear Programming Problem
Duality in Linear Programming ProblemRAVI PRASAD K.J.
 
What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...Simplilearn
 
9781285852744 ppt ch15
9781285852744 ppt ch159781285852744 ppt ch15
9781285852744 ppt ch15Terry Yoast
 
Balaji-opt-lecture6-act.ppt
Balaji-opt-lecture6-act.pptBalaji-opt-lecture6-act.ppt
Balaji-opt-lecture6-act.pptJamesGreen666883
 
Solving Optimization Problems using the Matlab Optimization.docx
Solving Optimization Problems using the Matlab Optimization.docxSolving Optimization Problems using the Matlab Optimization.docx
Solving Optimization Problems using the Matlab Optimization.docxwhitneyleman54422
 

Similar a Dynamic Programming Problems and Solutions (20)

dynamic-programming
dynamic-programmingdynamic-programming
dynamic-programming
 
Dynamic programming class 16
Dynamic programming class 16Dynamic programming class 16
Dynamic programming class 16
 
Dynamic pgmming
Dynamic pgmmingDynamic pgmming
Dynamic pgmming
 
Dynamicpgmming
DynamicpgmmingDynamicpgmming
Dynamicpgmming
 
Recursion in Java
Recursion in JavaRecursion in Java
Recursion in Java
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
DP Project Report
DP Project ReportDP Project Report
DP Project Report
 
Programming for problem solving ppts unit 1
Programming for problem solving ppts unit 1Programming for problem solving ppts unit 1
Programming for problem solving ppts unit 1
 
Dynamic programming prasintation eaisy
Dynamic programming prasintation eaisyDynamic programming prasintation eaisy
Dynamic programming prasintation eaisy
 
Pintu ram
Pintu ramPintu ram
Pintu ram
 
352735344 rsh-qam11-tif-10-doc
352735344 rsh-qam11-tif-10-doc352735344 rsh-qam11-tif-10-doc
352735344 rsh-qam11-tif-10-doc
 
352735344 rsh-qam11-tif-10-doc
352735344 rsh-qam11-tif-10-doc352735344 rsh-qam11-tif-10-doc
352735344 rsh-qam11-tif-10-doc
 
Why computer programming
Why computer programmingWhy computer programming
Why computer programming
 
9781111530532 ppt ch13
9781111530532 ppt ch139781111530532 ppt ch13
9781111530532 ppt ch13
 
Algorithm lecture Dynamic programming
Algorithm lecture Dynamic programmingAlgorithm lecture Dynamic programming
Algorithm lecture Dynamic programming
 
Duality in Linear Programming Problem
Duality in Linear Programming ProblemDuality in Linear Programming Problem
Duality in Linear Programming Problem
 
What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
 
9781285852744 ppt ch15
9781285852744 ppt ch159781285852744 ppt ch15
9781285852744 ppt ch15
 
Balaji-opt-lecture6-act.ppt
Balaji-opt-lecture6-act.pptBalaji-opt-lecture6-act.ppt
Balaji-opt-lecture6-act.ppt
 
Solving Optimization Problems using the Matlab Optimization.docx
Solving Optimization Problems using the Matlab Optimization.docxSolving Optimization Problems using the Matlab Optimization.docx
Solving Optimization Problems using the Matlab Optimization.docx
 

Más de Pankaj Prateek

Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK
Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITKAdvanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK
Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITKPankaj Prateek
 
02 greedy, d&c, binary search
02 greedy, d&c, binary search02 greedy, d&c, binary search
02 greedy, d&c, binary searchPankaj Prateek
 

Más de Pankaj Prateek (6)

Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK
Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITKAdvanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK
Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK
 
05 graph
05 graph05 graph
05 graph
 
06 segment trees
06 segment trees06 segment trees
06 segment trees
 
04 maths
04 maths04 maths
04 maths
 
02 greedy, d&c, binary search
02 greedy, d&c, binary search02 greedy, d&c, binary search
02 greedy, d&c, binary search
 
01 basics and stl
01 basics and stl01 basics and stl
01 basics and stl
 

Dynamic Programming Problems and Solutions

  • 1. Dynamic Programming Problems Dynamic Programming League of Programmers ACA, IIT Kanpur October 22, 2012 League of Programmers Dynamic Programming
  • 2. Dynamic Programming Problems Outline 1 Dynamic Programming 2 Problems League of Programmers Dynamic Programming
  • 3. Dynamic Programming Problems Dynamic Programming What is DP? DP is another technique for problems with optimal substructure: An optimal solution to a problem contains optimal solutions to subproblems. This doesn't necessarily mean that every optimal solution to a subproblem will contribute to the main solution. League of Programmers Dynamic Programming
  • 4. Dynamic Programming Problems Dynamic Programming What is DP? DP is another technique for problems with optimal substructure: An optimal solution to a problem contains optimal solutions to subproblems. This doesn't necessarily mean that every optimal solution to a subproblem will contribute to the main solution. For divide and conquer (top down), the subproblems are independent so we can solve them in any order. League of Programmers Dynamic Programming
  • 5. Dynamic Programming Problems Dynamic Programming What is DP? DP is another technique for problems with optimal substructure: An optimal solution to a problem contains optimal solutions to subproblems. This doesn't necessarily mean that every optimal solution to a subproblem will contribute to the main solution. For divide and conquer (top down), the subproblems are independent so we can solve them in any order. For greedy algorithms (bottom up), we can always choose the "right" subproblem by a greedy choice. League of Programmers Dynamic Programming
  • 6. Dynamic Programming Problems Dynamic Programming What is DP? DP is another technique for problems with optimal substructure: An optimal solution to a problem contains optimal solutions to subproblems. This doesn't necessarily mean that every optimal solution to a subproblem will contribute to the main solution. For divide and conquer (top down), the subproblems are independent so we can solve them in any order. For greedy algorithms (bottom up), we can always choose the "right" subproblem by a greedy choice. In dynamic programming, we solve many subproblems and store the results: not all of them will contribute to solving the larger problem. Because of optimal substructure, we can be sure that at least some of the subproblems will be useful League of Programmers Dynamic Programming
  • 7. Dynamic Programming Problems Dynamic Programming Steps to solve a DP problem League of Programmers Dynamic Programming
  • 8. Dynamic Programming Problems Dynamic Programming Steps to solve a DP problem 1 Dene subproblems League of Programmers Dynamic Programming
  • 9. Dynamic Programming Problems Dynamic Programming Steps to solve a DP problem 1 Dene subproblems 2 Write down the recurrence that relates subproblems League of Programmers Dynamic Programming
  • 10. Dynamic Programming Problems Dynamic Programming Steps to solve a DP problem 1 Dene subproblems 2 Write down the recurrence that relates subproblems 3 Recognize and solve the base cases League of Programmers Dynamic Programming
  • 11. Dynamic Programming Problems Fibonacci Sequence Naive Recursive Function int Fib(int n){ if(n==1 || n==2) return 1; return Fib(n-1)+Fib(n-2) } League of Programmers Dynamic Programming
  • 12. Dynamic Programming Problems Fibonacci Sequence DP Solution O(n) Fib[1] = Fib[2] = 1; for(i=3;iN;i++) Fib[i] = Fib[i-1]+Fib[i-2] League of Programmers Dynamic Programming
  • 13. Dynamic Programming Problems Problem Problem 2 Given n, nd the number of dierent ways to write n as the sum of 1, 3, 4 Example: for n = 5, the answer is 6 5 = 1+1+1+1+1 = 1+1+3 = 1+3+1 = 3+1+1 = 1+4 League of Programmers Dynamic Programming
  • 14. Dynamic Programming Problems Problem Dene Subproblems League of Programmers Dynamic Programming
  • 15. Dynamic Programming Problems Problem Dene Subproblems Dn be the number of ways to write n as the sum of 1, 3, 4 League of Programmers Dynamic Programming
  • 16. Dynamic Programming Problems Problem Dene Subproblems Dn be the number of ways to write n as the sum of 1, 3, 4 Find the recurrence Dn = Dn − 1 + Dn −3 + Dn −4 League of Programmers Dynamic Programming
  • 17. Dynamic Programming Problems Problem Dene Subproblems Dn be the number of ways to write n as the sum of 1, 3, 4 Find the recurrence Dn = Dn − 1 + Dn −3 + Dn −4 Solve the base cases D =1 0 Dn = 0 for all negative n League of Programmers Dynamic Programming
  • 18. Dynamic Programming Problems Problem Dene Subproblems Dn be the number of ways to write n as the sum of 1, 3, 4 Find the recurrence Dn = Dn − 1 + Dn −3 + Dn −4 Solve the base cases D =1 0 Dn = 0 for all negative n Alternatively, can set: D = D = D = 1, D = 2 0 1 2 3 League of Programmers Dynamic Programming
  • 19. Dynamic Programming Problems Problem Implementation D[0]=D[1]=D[2]=1; D[3]=2; for(i=4;i=n;i++) D[i]=D[i-1]+D[i-3]+D[i-4]; League of Programmers Dynamic Programming
  • 20. Dynamic Programming Problems LCS Problem 3 Given two strings x and y, nd the longest common subsequence (LCS) and print its length. Example: x: ABCBDAB y: BDCABC BCAB is the longest subsequence found in both sequences, so the answer is 4 League of Programmers Dynamic Programming
  • 21. Dynamic Programming Problems LCS Analysis League of Programmers Dynamic Programming
  • 22. Dynamic Programming Problems LCS Analysis There are 2m subsequences of X. Testing a subsequence (length k) takes time O(k + n). So brute force algorithm is O(n ∗ 2m ). League of Programmers Dynamic Programming
  • 23. Dynamic Programming Problems LCS Analysis There are 2m subsequences of X. Testing a subsequence (length k) takes time O(k + n). So brute force algorithm is O(n ∗ 2m ). Divide and conquer or Greedy algorithm? League of Programmers Dynamic Programming
  • 24. Dynamic Programming Problems LCS Analysis There are 2m subsequences of X. Testing a subsequence (length k) takes time O(k + n). So brute force algorithm is O(n ∗ 2m ). Divide and conquer or Greedy algorithm? No, can't tell what initial division or greedy choice to make. League of Programmers Dynamic Programming
  • 25. Dynamic Programming Problems LCS Analysis There are 2m subsequences of X. Testing a subsequence (length k) takes time O(k + n). So brute force algorithm is O(n ∗ 2m ). Divide and conquer or Greedy algorithm? No, can't tell what initial division or greedy choice to make. Thus, none of the approaches we have learned so far work here!!! League of Programmers Dynamic Programming
  • 26. Dynamic Programming Problems LCS Analysis There are 2m subsequences of X. Testing a subsequence (length k) takes time O(k + n). So brute force algorithm is O(n ∗ 2m ). Divide and conquer or Greedy algorithm? No, can't tell what initial division or greedy choice to make. Thus, none of the approaches we have learned so far work here!!! Intuition A LCS of two sequences has as a prex a LCS of prexes of the sequences. So, We concentrate on LCS for smaller problems, i.e simply removes the last (common) element. League of Programmers Dynamic Programming
  • 27. Dynamic Programming Problems LCS Dene Subproblems League of Programmers Dynamic Programming
  • 28. Dynamic Programming Problems LCS Dene Subproblems Let Di ,j be the length of the LCS of x 1...i and y1 ...j League of Programmers Dynamic Programming
  • 29. Dynamic Programming Problems LCS Dene Subproblems Let Di ,j be the length of the LCS of x 1...i and y1 ...j Find the recurrence League of Programmers Dynamic Programming
  • 30. Dynamic Programming Problems LCS Dene Subproblems Let Di ,j be the length of the LCS of x 1...i and y1 ...j Find the recurrence If xi = yj , they both contribute to the LCS. In this case, Di ,j = Di −1,j −1 + 1 League of Programmers Dynamic Programming
  • 31. Dynamic Programming Problems LCS Dene Subproblems Let Di ,j be the length of the LCS of x 1...i and y 1 ...j Find the recurrence If xi = yj , they both contribute to the LCS. In this case, Di ,j = Di −1,j −1 + 1 Either xi or yj does not contribute to the LCS, so one can be dropped. Otherwise, Di ,j = max {Di −1,j , Di ,j −1 } League of Programmers Dynamic Programming
  • 32. Dynamic Programming Problems LCS Dene Subproblems Let Di ,j be the length of the LCS of x 1...i and y1 ...j Find the recurrence If xi = yj , they both contribute to the LCS. In this case, Di ,j = Di −1,j −1 + 1 Either xi or yj does not contribute to the LCS, so one can be dropped. Otherwise, Di ,j = max {Di −1,j , Di ,j −1 } Find and solve the base cases: Di , = D ,j = 0 0 0 League of Programmers Dynamic Programming
  • 33. Dynamic Programming Problems LCS Implementation for(i=0;i=n;i++) D[i][0]=0; for(j=0;j=m;j++) D[0][j]=0; for(i=1;i=n;i++) { for(j=1;j=m;j++) { if(x[i]==y[j]) D[i][j]=D[i-1][j-1]+1; else D[i][j]=max(D[i-1][j],D[i][j-1]); } } League of Programmers Dynamic Programming
  • 34. Dynamic Programming Problems LCS Recovering the LCS Modify the algorithm to also build a matrix D[1 . . . n; 1 . . . m], recording how the solutions to subproblems were arrived at. League of Programmers Dynamic Programming
  • 35. Dynamic Programming Problems Longest Non Decresing Subsequence Problem 4 Given an array [1 , 2 , 5, 2, 8, 6, 3, 6, 9, 7]. Find a subsequence which is non decreasing and of maximum length. 1-5-8-9 forms a non decreasing subsequence So does 1-2-2-6-6-7 but it is longer League of Programmers Dynamic Programming
  • 36. Dynamic Programming Problems LNDS Subproblem Length of LNDS ending at i th location League of Programmers Dynamic Programming
  • 37. Dynamic Programming Problems LNDS Subproblem Length of LNDS ending at i th location Implementation for(i=0;i100;i++) { max=0; for(j=0;ji;j++) { if(A[i]=A[j] L[j]max) max = L[j]; } L[i] = max+1; } League of Programmers Dynamic Programming
  • 38. Dynamic Programming Problems Problem Problem 5 Given a tree, color nodes black as many as possible without coloring two adjacent nodes. League of Programmers Dynamic Programming
  • 39. Dynamic Programming Problems Problem Dene Subproblems League of Programmers Dynamic Programming
  • 40. Dynamic Programming Problems Problem Dene Subproblems we arbitrarily decide the root node r League of Programmers Dynamic Programming
  • 41. Dynamic Programming Problems Problem Dene Subproblems we arbitrarily decide the root node r Bv : the optimal solution for a subtree having v as the root, where we color v black League of Programmers Dynamic Programming
  • 42. Dynamic Programming Problems Problem Dene Subproblems we arbitrarily decide the root node r Bv : the optimal solution for a subtree having v as the root, where we color v black Wv : the optimal solution for a subtree having v as the root, where we don't color v League of Programmers Dynamic Programming
  • 43. Dynamic Programming Problems Problem Dene Subproblems we arbitrarily decide the root node r Bv : the optimal solution for a subtree having v as the root, where we color v black Wv : the optimal solution for a subtree having v as the root, where we don't color v The answer is max{Br ,Wr } League of Programmers Dynamic Programming
  • 44. Dynamic Programming Problems Problem Find The Recurrence Observation League of Programmers Dynamic Programming
  • 45. Dynamic Programming Problems Problem Find The Recurrence Observation Once v's color is determined, its subtrees can be solved independently League of Programmers Dynamic Programming
  • 46. Dynamic Programming Problems Problem Find The Recurrence Observation Once v's color is determined, its subtrees can be solved independently If v is colored, its children must not be colored Bv =1+ u ∈child (v ) Wu League of Programmers Dynamic Programming
  • 47. Dynamic Programming Problems Problem Find The Recurrence Observation Once v's color is determined, its subtrees can be solved independently If v is colored, its children must not be colored Bv =1+ u ∈child (v ) Wu If v is not colored, its children can have any color Wv =1+ u ∈child (v ) Bu League of Programmers Dynamic Programming
  • 48. Dynamic Programming Problems Problem Find The Recurrence Observation Once v's color is determined, its subtrees can be solved independently If v is colored, its children must not be colored Bv =1+ u ∈child (v ) Wu If v is not colored, its children can have any color Wv =1+ u ∈child (v ) Bu Base cases: leaf nodes League of Programmers Dynamic Programming
  • 49. Dynamic Programming Problems Outline 1 Dynamic Programming 2 Problems League of Programmers Dynamic Programming
  • 50. Dynamic Programming Problems Problems Added on the contest on VOC http://ahmed-aly.com/voc/ Contest ID: 2616 Name: ACA, IITK LOP 03 Author: pnkjjindal Links: 1 http://spoj.pl/problems/ACTIV 2 http://www.spoj.pl/problems/COINS 3 http://spoj.pl/problems/IOIPALIN 4 http://spoj.pl/problems/ADFRUITS 5 http://www.spoj.pl/problems/RENT 6 http://www.spoj.pl/problems/M3TILE 7 http://www.spoj.pl/problems/IOPC1203 8 http://www.spoj.pl/problems/NGON League of Programmers Dynamic Programming