Introduction to Algorithms
                           6.046J/18.401J
                                      LECTURE 15
                                      Dynamic Programming
                                      • Longest common
                                        subsequence
                                      • Optimal substructure
                                      • Overlapping subproblems


                   Prof. Charles E. Leiserson
November 7, 2005    Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson   L15.1
Dynamic programming
     Design technique, like divide-and-conquer.
Example: Longest Common Subsequence (LCS)
• Given two sequences x[1 . . m] and y[1 . . n], find
  a longest subsequence common to them both.




  November 7, 2005   Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson   L15.2
Dynamic programming
     Design technique, like divide-and-conquer.
Example: Longest Common Subsequence (LCS)
• Given two sequences x[1 . . m] and y[1 . . n], find
  a longest subsequence common to them both.
        “a” not “the”




  November 7, 2005   Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson   L15.3
Dynamic programming
     Design technique, like divide-and-conquer.
Example: Longest Common Subsequence (LCS)
• Given two sequences x[1 . . m] and y[1 . . n], find
  a longest subsequence common to them both.
        “a” not “the”
x: A B         C    B     D A B

y: B           D       C           A           B          A


  November 7, 2005   Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson   L15.4
Dynamic programming
     Design technique, like divide-and-conquer.
Example: Longest Common Subsequence (LCS)
• Given two sequences x[1 . . m] and y[1 . . n], find
  a longest subsequence common to them both.
        “a” not “the”
x: A B         C    B     D A B
                                            BCBA =
                                            LCS(x, y)
y: B      D C       A B          A
                                functional notation,
                                but not a function
  November 7, 2005   Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson   L15.5
Brute-force LCS algorithm
  Check every subsequence of x[1 . . m] to see
  if it is also a subsequence of y[1 . . n].




November 7, 2005   Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson   L15.6
Brute-force LCS algorithm
  Check every subsequence of x[1 . . m] to see
  if it is also a subsequence of y[1 . . n].
  Analysis
  • Checking = O(n) time per subsequence.
  • 2m subsequences of x (each bit-vector of
    length m determines a distinct subsequence
    of x).
  Worst-case running time = O(n2m)
                           = exponential time.
November 7, 2005   Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson   L15.7
Towards a better algorithm
Simplification:
1. Look at the length of a longest-common
   subsequence.
2. Extend the algorithm to find the LCS itself.




November 7, 2005   Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson   L15.8
Towards a better algorithm
Simplification:
1. Look at the length of a longest-common
   subsequence.
2. Extend the algorithm to find the LCS itself.
Notation: Denote the length of a sequence s
by | s |.




November 7, 2005   Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson   L15.9
Towards a better algorithm
Simplification:
1. Look at the length of a longest-common
   subsequence.
2. Extend the algorithm to find the LCS itself.
Notation: Denote the length of a sequence s
by | s |.
Strategy: Consider prefixes of x and y.
• Define c[i, j] = | LCS(x[1 . . i], y[1 . . j]) |.
• Then, c[m, n] = | LCS(x, y) |.
November 7, 2005   Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson   L15.10
Recursive formulation
Theorem.
                    c[i–1, j–1] + 1           if x[i] = y[j],
 c[i, j] =          max{c[i–1, j], c[i, j–1]} otherwise.




 November 7, 2005    Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson   L15.11
Recursive formulation
Theorem.
           c[i–1, j–1] + 1           if x[i] = y[j],
 c[i, j] = max{c[i–1, j], c[i, j–1]} otherwise.
Proof. Case x[i] = y[ j]:
             1       2                   i                                                m
      x:                                                    L
             1       2                       =      j                      n
      y:                                                       L




  November 7, 2005       Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson       L15.12
Recursive formulation
Theorem.
           c[i–1, j–1] + 1           if x[i] = y[j],
 c[i, j] = max{c[i–1, j], c[i, j–1]} otherwise.
Proof. Case x[i] = y[ j]:
              1       2                   i                                                m
       x:                                                    L
              1       2                       =      j                      n
       y:                                                       L
Let z[1 . . k] = LCS(x[1 . . i], y[1 . . j]), where c[i, j]
= k. Then, z[k] = x[i], or else z could be extended.
Thus, z[1 . . k–1] is CS of x[1 . . i–1] and y[1 . . j–1].
   November 7, 2005       Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson       L15.13
Proof (continued)
Claim: z[1 . . k–1] = LCS(x[1 . . i–1], y[1 . . j–1]).
  Suppose w is a longer CS of x[1 . . i–1] and
  y[1 . . j–1], that is, | w | > k–1. Then, cut and
  paste: w || z[k] (w concatenated with z[k]) is a
  common subsequence of x[1 . . i] and y[1 . . j]
  with | w || z[k] | > k. Contradiction, proving the
  claim.




  November 7, 2005   Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson   L15.14
Proof (continued)
Claim: z[1 . . k–1] = LCS(x[1 . . i–1], y[1 . . j–1]).
  Suppose w is a longer CS of x[1 . . i–1] and
  y[1 . . j–1], that is, | w | > k–1. Then, cut and
  paste: w || z[k] (w concatenated with z[k]) is a
  common subsequence of x[1 . . i] and y[1 . . j]
  with | w || z[k] | > k. Contradiction, proving the
  claim.
Thus, c[i–1, j–1] = k–1, which implies that c[i, j]
= c[i–1, j–1] + 1.
Other cases are similar.
  November 7, 2005   Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson   L15.15
Dynamic-programming
         hallmark #1

                   Optimal substructure
              An optimal solution to a problem
                (instance) contains optimal
                 solutions to subproblems.




November 7, 2005   Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson   L15.16
Dynamic-programming
         hallmark #1

                   Optimal substructure
              An optimal solution to a problem
                (instance) contains optimal
                 solutions to subproblems.


     If z = LCS(x, y), then any prefix of z is
     an LCS of a prefix of x and a prefix of y.

November 7, 2005   Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson   L15.17
Recursive algorithm for LCS
  LCS(x, y, i, j)
    if x[i] = y[ j]
        then c[i, j] ← LCS(x, y, i–1, j–1) + 1
        else c[i, j] ← max{ LCS(x, y, i–1, j),
                            LCS(x, y, i, j–1)}




November 7, 2005   Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson   L15.18
Recursive algorithm for LCS
  LCS(x, y, i, j)
    if x[i] = y[ j]
        then c[i, j] ← LCS(x, y, i–1, j–1) + 1
        else c[i, j] ← max{ LCS(x, y, i–1, j),
                            LCS(x, y, i, j–1)}
  Worst-case: x[i] ≠ y[ j], in which case the
  algorithm evaluates two subproblems, each
  with only one parameter decremented.

November 7, 2005   Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson   L15.19
Recursion tree
m = 3, n = 4:                            3,4
                                          3,4

            2,4
            2,4                                                        3,3
                                                                        3,3

    1,4
     1,4               2,3
                        2,3                                    2,3
                                                                2,3              3,2
                                                                                  3,2

                1,3
                 1,3          2,2
                              2,2                      1,3
                                                        1,3           2,2
                                                                       2,2




  November 7, 2005     Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson   L15.20
Recursion tree
m = 3, n = 4:                            3,4
                                          3,4

            2,4
            2,4                                                        3,3
                                                                        3,3

    1,4
     1,4               2,3
                        2,3                                    2,3
                                                                2,3              3,2
                                                                                  3,2    m+n

                1,3
                 1,3          2,2
                              2,2                      1,3
                                                        1,3           2,2
                                                                       2,2



 Height = m + n ⇒ work potentially exponential.

  November 7, 2005     Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson   L15.21
Recursion tree
m = 3, n = 4:                            3,4
                                          3,4

            2,4
            2,4                   same                                 3,3
                                                                        3,3
                               subproblem
    1,4
     1,4               2,3
                        2,3                                    2,3
                                                                2,3              3,2
                                                                                  3,2    m+n

                1,3
                 1,3          2,2
                              2,2                      1,3
                                                        1,3           2,2
                                                                       2,2



 Height = m + n ⇒ work potentially exponential.,
 but we’re solving subproblems already solved!
  November 7, 2005     Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson   L15.22
Dynamic-programming
         hallmark #2

                Overlapping subproblems
              A recursive solution contains a
                “small” number of distinct
            subproblems repeated many times.




November 7, 2005   Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson   L15.23
Dynamic-programming
         hallmark #2

                Overlapping subproblems
              A recursive solution contains a
                “small” number of distinct
            subproblems repeated many times.


  The number of distinct LCS subproblems for
  two strings of lengths m and n is only mn.


November 7, 2005   Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson   L15.24
Memoization algorithm
Memoization: After computing a solution to a
subproblem, store it in a table. Subsequent calls
check the table to avoid redoing work.




November 7, 2005   Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson   L15.25
Memoization algorithm
Memoization: After computing a solution to a
subproblem, store it in a table. Subsequent calls
check the table to avoid redoing work.
LCS(x, y, i, j)
  if c[i, j] = NIL
      then if x[i] = y[j]
           then c[i, j] ← LCS(x, y, i–1, j–1) + 1                                    same
           else c[i, j] ← max{ LCS(x, y, i–1, j),                                    as
                                                                                     before
                               LCS(x, y, i, j–1)}


 November 7, 2005   Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson    L15.26
Memoization algorithm
Memoization: After computing a solution to a
subproblem, store it in a table. Subsequent calls
check the table to avoid redoing work.
LCS(x, y, i, j)
  if c[i, j] = NIL
      then if x[i] = y[j]
           then c[i, j] ← LCS(x, y, i–1, j–1) + 1                                    same
           else c[i, j] ← max{ LCS(x, y, i–1, j),                                    as
                                                                                     before
                               LCS(x, y, i, j–1)}
Time = Θ(mn) = constant work per table entry.
Space = Θ(mn).
 November 7, 2005   Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson    L15.27
Dynamic-programming
           algorithm
IDEA:                              A B C B D                                          A B
Compute the                      0 0 0 0 0 0
                                 0 0 0 0 0 0                                          0 0
                                                                                      0 0
table bottom-up.
                               B 0 0 1 1 1 1
                                 0 0 1 1 1 1                                          1 1
                                                                                      1 1
                               D 0 0 1 1 1 2
                                 0 0 1 1 1 2                         2 2
                                                                     2 2
                               C 0 0 1
                                 0 0 1                         2 2 2 2 2
                                                               2 2 2 2 2
                               A 0 1 1
                                 0 1 1                         2 2 2 3 3
                                                               2 2 2 3 3
                               B 0 1 2
                                 0 1 2                         2 3 3 3 4
                                                               2 3 3 3 4
                               A 0 1 2
                                 0 1 2                         2 3 3 4 4
                                                               2 3 3 4 4
  November 7, 2005   Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson   L15.28
Dynamic-programming
           algorithm
IDEA:                              A B C B D                                          A B
Compute the                      0 0 0 0 0 0
                                 0 0 0 0 0 0                                          0 0
                                                                                      0 0
table bottom-up.
                               B 0 0 1 1 1 1
                                 0 0 1 1 1 1                                          1 1
                                                                                      1 1
Time = Θ(mn).                  D 0 0 1 1 1 2                         2 2
                                 0 0 1 1 1 2                         2 2
                               C 0 0 1
                                 0 0 1                         2 2 2 2 2
                                                               2 2 2 2 2
                               A 0 1 1
                                 0 1 1                         2 2 2 3 3
                                                               2 2 2 3 3
                               B 0 1 2
                                 0 1 2                         2 3 3 3 4
                                                               2 3 3 3 4
                               A 0 1 2
                                 0 1 2                         2 3 3 4 4
                                                               2 3 3 4 4
  November 7, 2005   Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson   L15.29
Dynamic-programming
           algorithm
IDEA:                              A B C B D                                          A B
Compute the                      0 0 0 0 0 0
                                 0 0 0 0 0 0                                          0 0
                                                                                      0 0
table bottom-up.
                               B 0 0 1 1 1 1
                                 0 0 1 1 1 1                                          1 1
                                                                                      1 1
Time = Θ(mn).                  D 0 0 1 1 1 2                         2 2
                                 0 0 1 1 1 2                         2 2
Reconstruct
                               C 0 0 1
                                 0 0 1                         2 2 2 2 2
                                                               2 2 2 2 2
LCS by tracing
backwards.                     A 0 1 1
                                 0 1 1                         2 2 2 3 3
                                                               2 2 2 3 3
                               B 0 1 2
                                 0 1 2                         2 3 3 3 4
                                                               2 3 3 3 4
                               A 0 1 2
                                 0 1 2                         2 3 3 4 4
                                                               2 3 3 4 4
  November 7, 2005   Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson   L15.30
Dynamic-programming
           algorithm
IDEA:                              A B C B D                                          A B
Compute the                      0 0 0 0 0 0
                                 0 0 0 0 0 0                                          0 0
                                                                                      0 0
table bottom-up.
                               B 0 0 1 1 1 1
                                 0 0 1 1 1 1                                          1 1
                                                                                      1 1
Time = Θ(mn).                  D 0 0 1 1 1 2                         2 2
                                 0 0 1 1 1 2                         2 2
Reconstruct
                               C 0 0 1
                                 0 0 1                         2 2 2 2 2
                                                               2 2 2 2 2
LCS by tracing
backwards.                     A 0 1 1
                                 0 1 1                         2 2 2 3 3
                                                               2 2 2 3 3
Space = Θ(mn).                 B 0 1 2
                                 0 1 2                         2 3 3 3 4
                                                               2 3 3 3 4
Exercise:                      A 0 1 2
                                 0 1 2                         2 3 3 4 4
                                                               2 3 3 4 4
O(min{m, n}).
  November 7, 2005   Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson   L15.31

More Related Content

PPTX
Pert 05 aplikasi clustering
PDF
Lossy Kernelization
PDF
Biconnectivity
PDF
(DL hacks輪読) Variational Inference with Rényi Divergence
PDF
Quantum Search and Quantum Learning
PDF
Linear Discriminant Analysis and Its Generalization
PDF
Welcome to International Journal of Engineering Research and Development (IJERD)
PDF
On Convolution of Graph Signals and Deep Learning on Graph Domains
Pert 05 aplikasi clustering
Lossy Kernelization
Biconnectivity
(DL hacks輪読) Variational Inference with Rényi Divergence
Quantum Search and Quantum Learning
Linear Discriminant Analysis and Its Generalization
Welcome to International Journal of Engineering Research and Development (IJERD)
On Convolution of Graph Signals and Deep Learning on Graph Domains

What's hot (20)

PDF
Talk icml
PDF
QMC Program: Trends and Advances in Monte Carlo Sampling Algorithms Workshop,...
PDF
slides_low_rank_matrix_optim_farhad
PDF
QMC Program: Trends and Advances in Monte Carlo Sampling Algorithms Workshop,...
PDF
Program on Quasi-Monte Carlo and High-Dimensional Sampling Methods for Appli...
PDF
Strong (Weak) Triple Connected Domination Number of a Fuzzy Graph
PDF
Gradient Estimation Using Stochastic Computation Graphs
PDF
QMC Program: Trends and Advances in Monte Carlo Sampling Algorithms Workshop,...
PDF
Amirim Project - Threshold Functions in Random Simplicial Complexes - Avichai...
PDF
Ball Packings and Fat Voronoi Diagrams
PDF
(DL hacks輪読) Deep Kernel Learning
PDF
QMC Program: Trends and Advances in Monte Carlo Sampling Algorithms Workshop,...
PDF
Entity Linking via Graph-Distance Minimization
PPTX
Fdtd ppt for mine
PPT
20070823
PDF
Mid semexam | Theory of Computation | Akash Anand | MTH 401A | IIT Kanpur
PDF
11.0001www.iiste.org call for paper.differential approach to cardioid distrib...
PDF
1.differential approach to cardioid distribution -1-6
PDF
Higher-Order (F, α, β, ρ, d) –Convexity for Multiobjective Programming Problem
PDF
Bird’s-eye view of Gaussian harmonic analysis
Talk icml
QMC Program: Trends and Advances in Monte Carlo Sampling Algorithms Workshop,...
slides_low_rank_matrix_optim_farhad
QMC Program: Trends and Advances in Monte Carlo Sampling Algorithms Workshop,...
Program on Quasi-Monte Carlo and High-Dimensional Sampling Methods for Appli...
Strong (Weak) Triple Connected Domination Number of a Fuzzy Graph
Gradient Estimation Using Stochastic Computation Graphs
QMC Program: Trends and Advances in Monte Carlo Sampling Algorithms Workshop,...
Amirim Project - Threshold Functions in Random Simplicial Complexes - Avichai...
Ball Packings and Fat Voronoi Diagrams
(DL hacks輪読) Deep Kernel Learning
QMC Program: Trends and Advances in Monte Carlo Sampling Algorithms Workshop,...
Entity Linking via Graph-Distance Minimization
Fdtd ppt for mine
20070823
Mid semexam | Theory of Computation | Akash Anand | MTH 401A | IIT Kanpur
11.0001www.iiste.org call for paper.differential approach to cardioid distrib...
1.differential approach to cardioid distribution -1-6
Higher-Order (F, α, β, ρ, d) –Convexity for Multiobjective Programming Problem
Bird’s-eye view of Gaussian harmonic analysis
Ad

Viewers also liked (20)

PDF
Pldi09 semantics aware trace analysis
PPT
lecture 23
PDF
Agile Financial Times
PPT
lect
PPTX
Project report aditi paul1
PPTX
L4 RE Processes
PDF
Db2 partitioning
PDF
20121021 bspapproach tiskin
DOCX
Relational / XML DB -SQL Server & Oracle Database
PDF
Connor McDonald Partitioning
PPTX
Bmc hist unit3.1_(outbreak of war in europe)
PDF
Unlocking the Mystery of MDX
PDF
RDAP14: Developing an RDM Educational Service Using the New England Collabora...
PPTX
RDAP14: DataONE: Data Observation Network for Earth
DOCX
Sybase SQL AnyWhere12
PPTX
Simplifying SQL with CTE's and windowing functions
PPTX
Cameron Hawthorne - E Bus Sales analysis with Olap and Disco
Pldi09 semantics aware trace analysis
lecture 23
Agile Financial Times
lect
Project report aditi paul1
L4 RE Processes
Db2 partitioning
20121021 bspapproach tiskin
Relational / XML DB -SQL Server & Oracle Database
Connor McDonald Partitioning
Bmc hist unit3.1_(outbreak of war in europe)
Unlocking the Mystery of MDX
RDAP14: Developing an RDM Educational Service Using the New England Collabora...
RDAP14: DataONE: Data Observation Network for Earth
Sybase SQL AnyWhere12
Simplifying SQL with CTE's and windowing functions
Cameron Hawthorne - E Bus Sales analysis with Olap and Disco
Ad

Similar to Mychurch File Upload (20)

PPT
csce411-set8.ppthhfhgfhgfugfghhfguyguugu
PPT
Free video lectures for mca
PDF
Longest common subsequence
PPT
lecture 24
PPT
17-dynprog2.ppt
PPT
17-dynprog2 17-dynprog2 17-dynprog2 17-dynprog2
PPTX
Longest Common Subsequence
PPT
Longest common subsequence(dynamic programming).
PDF
Longest common subsequence
PPTX
Longest Common Subsequence
PPT
Longest common substring and Longest common subsequence
PPT
Dynamic Programming
PPTX
8_dynamic_algorithm powerpoint ptesentation.pptx
PPT
Dynamic1
PPT
Longest common subsequences in Algorithm Analysis
PPT
Dynamic Programing_LCS.ppt
PDF
Unit 2_final DESIGN AND ANALYSIS OF ALGORITHMS.pdf
PDF
Lecture_DynamicProgramming test12345.pdf
PPT
Dynamic programming
csce411-set8.ppthhfhgfhgfugfghhfguyguugu
Free video lectures for mca
Longest common subsequence
lecture 24
17-dynprog2.ppt
17-dynprog2 17-dynprog2 17-dynprog2 17-dynprog2
Longest Common Subsequence
Longest common subsequence(dynamic programming).
Longest common subsequence
Longest Common Subsequence
Longest common substring and Longest common subsequence
Dynamic Programming
8_dynamic_algorithm powerpoint ptesentation.pptx
Dynamic1
Longest common subsequences in Algorithm Analysis
Dynamic Programing_LCS.ppt
Unit 2_final DESIGN AND ANALYSIS OF ALGORITHMS.pdf
Lecture_DynamicProgramming test12345.pdf
Dynamic programming

More from Joe Suh (20)

DOCX
Mychurch File Upload
DOCX
Mychurch File Upload
PPT
Mychurch File Upload
DOCX
Mychurch File Upload
PDF
Mychurch File Upload
DOC
test
TXT
Mychurch File Upload
PDF
Mychurch File Upload
TXT
Mychurch File Upload
TXT
Mychurch File Upload
DOCX
Mychurch File Upload
TXT
Mychurch File Upload
TXT
Mychurch File Upload
TXT
Mychurch File Upload
TXT
Mychurch File Upload
PPT
Mychurch File Upload
TXT
Mychurch File Upload
PDF
Mychurch File Upload
PDF
Mychurch File Upload
TXT
Mychurch File Upload
Mychurch File Upload
Mychurch File Upload
Mychurch File Upload
Mychurch File Upload
Mychurch File Upload
test
Mychurch File Upload
Mychurch File Upload
Mychurch File Upload
Mychurch File Upload
Mychurch File Upload
Mychurch File Upload
Mychurch File Upload
Mychurch File Upload
Mychurch File Upload
Mychurch File Upload
Mychurch File Upload
Mychurch File Upload
Mychurch File Upload
Mychurch File Upload

Mychurch File Upload

  • 1. Introduction to Algorithms 6.046J/18.401J LECTURE 15 Dynamic Programming • Longest common subsequence • Optimal substructure • Overlapping subproblems Prof. Charles E. Leiserson November 7, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L15.1
  • 2. Dynamic programming Design technique, like divide-and-conquer. Example: Longest Common Subsequence (LCS) • Given two sequences x[1 . . m] and y[1 . . n], find a longest subsequence common to them both. November 7, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L15.2
  • 3. Dynamic programming Design technique, like divide-and-conquer. Example: Longest Common Subsequence (LCS) • Given two sequences x[1 . . m] and y[1 . . n], find a longest subsequence common to them both. “a” not “the” November 7, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L15.3
  • 4. Dynamic programming Design technique, like divide-and-conquer. Example: Longest Common Subsequence (LCS) • Given two sequences x[1 . . m] and y[1 . . n], find a longest subsequence common to them both. “a” not “the” x: A B C B D A B y: B D C A B A November 7, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L15.4
  • 5. Dynamic programming Design technique, like divide-and-conquer. Example: Longest Common Subsequence (LCS) • Given two sequences x[1 . . m] and y[1 . . n], find a longest subsequence common to them both. “a” not “the” x: A B C B D A B BCBA = LCS(x, y) y: B D C A B A functional notation, but not a function November 7, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L15.5
  • 6. Brute-force LCS algorithm Check every subsequence of x[1 . . m] to see if it is also a subsequence of y[1 . . n]. November 7, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L15.6
  • 7. Brute-force LCS algorithm Check every subsequence of x[1 . . m] to see if it is also a subsequence of y[1 . . n]. Analysis • Checking = O(n) time per subsequence. • 2m subsequences of x (each bit-vector of length m determines a distinct subsequence of x). Worst-case running time = O(n2m) = exponential time. November 7, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L15.7
  • 8. Towards a better algorithm Simplification: 1. Look at the length of a longest-common subsequence. 2. Extend the algorithm to find the LCS itself. November 7, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L15.8
  • 9. Towards a better algorithm Simplification: 1. Look at the length of a longest-common subsequence. 2. Extend the algorithm to find the LCS itself. Notation: Denote the length of a sequence s by | s |. November 7, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L15.9
  • 10. Towards a better algorithm Simplification: 1. Look at the length of a longest-common subsequence. 2. Extend the algorithm to find the LCS itself. Notation: Denote the length of a sequence s by | s |. Strategy: Consider prefixes of x and y. • Define c[i, j] = | LCS(x[1 . . i], y[1 . . j]) |. • Then, c[m, n] = | LCS(x, y) |. November 7, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L15.10
  • 11. Recursive formulation Theorem. c[i–1, j–1] + 1 if x[i] = y[j], c[i, j] = max{c[i–1, j], c[i, j–1]} otherwise. November 7, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L15.11
  • 12. Recursive formulation Theorem. c[i–1, j–1] + 1 if x[i] = y[j], c[i, j] = max{c[i–1, j], c[i, j–1]} otherwise. Proof. Case x[i] = y[ j]: 1 2 i m x: L 1 2 = j n y: L November 7, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L15.12
  • 13. Recursive formulation Theorem. c[i–1, j–1] + 1 if x[i] = y[j], c[i, j] = max{c[i–1, j], c[i, j–1]} otherwise. Proof. Case x[i] = y[ j]: 1 2 i m x: L 1 2 = j n y: L Let z[1 . . k] = LCS(x[1 . . i], y[1 . . j]), where c[i, j] = k. Then, z[k] = x[i], or else z could be extended. Thus, z[1 . . k–1] is CS of x[1 . . i–1] and y[1 . . j–1]. November 7, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L15.13
  • 14. Proof (continued) Claim: z[1 . . k–1] = LCS(x[1 . . i–1], y[1 . . j–1]). Suppose w is a longer CS of x[1 . . i–1] and y[1 . . j–1], that is, | w | > k–1. Then, cut and paste: w || z[k] (w concatenated with z[k]) is a common subsequence of x[1 . . i] and y[1 . . j] with | w || z[k] | > k. Contradiction, proving the claim. November 7, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L15.14
  • 15. Proof (continued) Claim: z[1 . . k–1] = LCS(x[1 . . i–1], y[1 . . j–1]). Suppose w is a longer CS of x[1 . . i–1] and y[1 . . j–1], that is, | w | > k–1. Then, cut and paste: w || z[k] (w concatenated with z[k]) is a common subsequence of x[1 . . i] and y[1 . . j] with | w || z[k] | > k. Contradiction, proving the claim. Thus, c[i–1, j–1] = k–1, which implies that c[i, j] = c[i–1, j–1] + 1. Other cases are similar. November 7, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L15.15
  • 16. Dynamic-programming hallmark #1 Optimal substructure An optimal solution to a problem (instance) contains optimal solutions to subproblems. November 7, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L15.16
  • 17. Dynamic-programming hallmark #1 Optimal substructure An optimal solution to a problem (instance) contains optimal solutions to subproblems. If z = LCS(x, y), then any prefix of z is an LCS of a prefix of x and a prefix of y. November 7, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L15.17
  • 18. Recursive algorithm for LCS LCS(x, y, i, j) if x[i] = y[ j] then c[i, j] ← LCS(x, y, i–1, j–1) + 1 else c[i, j] ← max{ LCS(x, y, i–1, j), LCS(x, y, i, j–1)} November 7, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L15.18
  • 19. Recursive algorithm for LCS LCS(x, y, i, j) if x[i] = y[ j] then c[i, j] ← LCS(x, y, i–1, j–1) + 1 else c[i, j] ← max{ LCS(x, y, i–1, j), LCS(x, y, i, j–1)} Worst-case: x[i] ≠ y[ j], in which case the algorithm evaluates two subproblems, each with only one parameter decremented. November 7, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L15.19
  • 20. Recursion tree m = 3, n = 4: 3,4 3,4 2,4 2,4 3,3 3,3 1,4 1,4 2,3 2,3 2,3 2,3 3,2 3,2 1,3 1,3 2,2 2,2 1,3 1,3 2,2 2,2 November 7, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L15.20
  • 21. Recursion tree m = 3, n = 4: 3,4 3,4 2,4 2,4 3,3 3,3 1,4 1,4 2,3 2,3 2,3 2,3 3,2 3,2 m+n 1,3 1,3 2,2 2,2 1,3 1,3 2,2 2,2 Height = m + n ⇒ work potentially exponential. November 7, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L15.21
  • 22. Recursion tree m = 3, n = 4: 3,4 3,4 2,4 2,4 same 3,3 3,3 subproblem 1,4 1,4 2,3 2,3 2,3 2,3 3,2 3,2 m+n 1,3 1,3 2,2 2,2 1,3 1,3 2,2 2,2 Height = m + n ⇒ work potentially exponential., but we’re solving subproblems already solved! November 7, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L15.22
  • 23. Dynamic-programming hallmark #2 Overlapping subproblems A recursive solution contains a “small” number of distinct subproblems repeated many times. November 7, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L15.23
  • 24. Dynamic-programming hallmark #2 Overlapping subproblems A recursive solution contains a “small” number of distinct subproblems repeated many times. The number of distinct LCS subproblems for two strings of lengths m and n is only mn. November 7, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L15.24
  • 25. Memoization algorithm Memoization: After computing a solution to a subproblem, store it in a table. Subsequent calls check the table to avoid redoing work. November 7, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L15.25
  • 26. Memoization algorithm Memoization: After computing a solution to a subproblem, store it in a table. Subsequent calls check the table to avoid redoing work. LCS(x, y, i, j) if c[i, j] = NIL then if x[i] = y[j] then c[i, j] ← LCS(x, y, i–1, j–1) + 1 same else c[i, j] ← max{ LCS(x, y, i–1, j), as before LCS(x, y, i, j–1)} November 7, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L15.26
  • 27. Memoization algorithm Memoization: After computing a solution to a subproblem, store it in a table. Subsequent calls check the table to avoid redoing work. LCS(x, y, i, j) if c[i, j] = NIL then if x[i] = y[j] then c[i, j] ← LCS(x, y, i–1, j–1) + 1 same else c[i, j] ← max{ LCS(x, y, i–1, j), as before LCS(x, y, i, j–1)} Time = Θ(mn) = constant work per table entry. Space = Θ(mn). November 7, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L15.27
  • 28. Dynamic-programming algorithm IDEA: A B C B D A B Compute the 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 table bottom-up. B 0 0 1 1 1 1 0 0 1 1 1 1 1 1 1 1 D 0 0 1 1 1 2 0 0 1 1 1 2 2 2 2 2 C 0 0 1 0 0 1 2 2 2 2 2 2 2 2 2 2 A 0 1 1 0 1 1 2 2 2 3 3 2 2 2 3 3 B 0 1 2 0 1 2 2 3 3 3 4 2 3 3 3 4 A 0 1 2 0 1 2 2 3 3 4 4 2 3 3 4 4 November 7, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L15.28
  • 29. Dynamic-programming algorithm IDEA: A B C B D A B Compute the 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 table bottom-up. B 0 0 1 1 1 1 0 0 1 1 1 1 1 1 1 1 Time = Θ(mn). D 0 0 1 1 1 2 2 2 0 0 1 1 1 2 2 2 C 0 0 1 0 0 1 2 2 2 2 2 2 2 2 2 2 A 0 1 1 0 1 1 2 2 2 3 3 2 2 2 3 3 B 0 1 2 0 1 2 2 3 3 3 4 2 3 3 3 4 A 0 1 2 0 1 2 2 3 3 4 4 2 3 3 4 4 November 7, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L15.29
  • 30. Dynamic-programming algorithm IDEA: A B C B D A B Compute the 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 table bottom-up. B 0 0 1 1 1 1 0 0 1 1 1 1 1 1 1 1 Time = Θ(mn). D 0 0 1 1 1 2 2 2 0 0 1 1 1 2 2 2 Reconstruct C 0 0 1 0 0 1 2 2 2 2 2 2 2 2 2 2 LCS by tracing backwards. A 0 1 1 0 1 1 2 2 2 3 3 2 2 2 3 3 B 0 1 2 0 1 2 2 3 3 3 4 2 3 3 3 4 A 0 1 2 0 1 2 2 3 3 4 4 2 3 3 4 4 November 7, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L15.30
  • 31. Dynamic-programming algorithm IDEA: A B C B D A B Compute the 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 table bottom-up. B 0 0 1 1 1 1 0 0 1 1 1 1 1 1 1 1 Time = Θ(mn). D 0 0 1 1 1 2 2 2 0 0 1 1 1 2 2 2 Reconstruct C 0 0 1 0 0 1 2 2 2 2 2 2 2 2 2 2 LCS by tracing backwards. A 0 1 1 0 1 1 2 2 2 3 3 2 2 2 3 3 Space = Θ(mn). B 0 1 2 0 1 2 2 3 3 3 4 2 3 3 3 4 Exercise: A 0 1 2 0 1 2 2 3 3 4 4 2 3 3 4 4 O(min{m, n}). November 7, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L15.31