Dynamic Programming - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Dynamic Programming

Description:

Most often subproblems are recursive calls to several instances of simpler ... It works in (an) where a is golden ratio (1 sqrt(5))/2. ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 18
Provided by: IRE62
Category:

less

Transcript and Presenter's Notes

Title: Dynamic Programming


1
Dynamic Programming
  • Irena Pevac
  • CS463

2
Dynamic Programming
  • Introduced in 1950 by Bellman as a general method
    for optimizing control systems.
  • It is technique for solving problems with
    overlapping subproblems. Most often subproblems
    are recursive calls to several instances of
    simpler problem of the same type.

3
Fibonacci
  • F(0)0
  • F(1)1
  • F(n)F(n-1)F(n-2), for ngt1
  • Recursive method recomputes the same F(k) many
    times. It works in ?(an) where a is golden ratio
    (1sqrt(5))/2.
  • Dynamic programming version eliminates
    recomputing by storing previously computed
    results.

4
Fibonacci
  • F00
  • F11
  • for i 2 to n
  • FiFi-1Fi-2
  • We use one-dimmensional array, fill in the values
    of subproblems, and use those values for future
    calculations.

5
Longest Common Subsequence (LCS)
  • Let x1,x2,xm and y1,y2,yn be two lists.
  • Subsequence is sublist containing subset of
    items from the original list that appear in the
    same order, but do not have to be consecutive.
  • For example, x1,x4,x5,x8 is a subsequence of
    x1,x2,x3.x9, but x1,x5,x2 is not.
  • LCS is longest subsequence of both lists.
  • X abcabba Y cbabac One LCS is baba and
    the other is cbba.

6
Recursive solution for LCS
  • Let x1,x2,xm and y1,y2,yn be two lists.
  • If xm!yn than LCS cannot include both xm and yn.
  • So, it must be either
  • An LCS of x1,x2,xm-1 and y1,y2,yn
  • or
  • An LCS of x1,x2,xm and y1,y2,yn-1
  • If xmyn than LCS includes xm that is
    concatenated to the tail of LCS of x1,x2,xm-1
    and y1,y2,yn-1

7
Length L of LCS
  • If (mn0) // both lists are empty, LCS is ?
  • L(0,0) 0
  • Else if (m0 or n0)
  • L(m,n)0
  • Else if mgt0 and ngt0 and xm!yn
  • L(m,n)max (L(m,n-1), L(m-1,n))
  • Else if mgt0 and ngt0 and xmyn
  • L(m,n)1 L(m-,n-1)
  • This has exponential running time

8
Dynamic Programming Solution for LCS
  • If we build two-dimensional array to store L(i,j)
    for the various values of i and j then we avoid
    recomputing them. This brings time down to O(mn).
  • Let x1,x2,xm and y1,y2,yn be stored in arrays
    x and y.

9
LCS Table
  • for (j 0 jltn j)
  • L0j0
  • for (i1 iltmi)
  • Li00
  • for ( j1 jltn j)
  • if (xi ! yi)
  • if (Li-1jgtLij-1)
  • LijLi-1j
  • else
  • LijLij-1
  • else
  • Lij 1 Li-1j-1

10
Example cbabac abcabba
11
Recovery of an LCS
  • Table provides matching pairs of elements that
    form one of the LCSs.
  • We should find the path through the table,
    beginning in the upper right corner.
  • If we are in row i and column j, check if
    xiyj. If so, take xi to be part of the LCS
    ahead of all elements of LCS found so far. After
    that move to row i-1 and column j-1.
  • If xi and yj are different Lij must equal
    at least one of Li-1j or Lij-1.
  • If Lij Li-1j we shall move our path one
    row down.
  • If Lij Lij-1 we shall move our path one
    column left.
  • We stop when we arrive to the left lower corner.

12
Binomial Coefficients
  • (ab)n C(n, 0) anb0 C(n,1) an-1b1
  • C(n, 2) an-1b2 ...
  • C(n, k)an-kbk
  • C(n, n)a0bn
  • Where C(n, k) is n!/ (k! (n-k)! )

13
Properties of Binomial Coefficients
  • C(n,0)1
  • C(n,n)1
  • C(n,k)C(n-1,k-1)C(n-1,k) for ngtkgt0
  • Recurrence for C(n,k) uses overlapping
    subproblems. So, let us use dynamic programming
    technique. We record the values of binomial
    coefficients in a table of n1 rows and k1
    columns.

14
Computing C(n,k)
15
Computing C(n,k)
16
Algorithm for C(n,k)
  • for( row 0 rowltn row) // first column
    items are 1s
  • Crow01
  • for row 1 to k
  • for col1 to row-1
  • Crowcol Crow-1col-1
    Crow-1col
  • Crowrow 1 // last item in a
    row is 1
  • for row k1 to n
  • for col1 to k
  • Crowcol Crow-1col-1
    Crow-1col

17
Cost of the Dynamic Algorithm
  • Basic operation addition
  • First k1 rows 0..k form a triangle.
  • Remaining n-k rows k1..n form a rectangle
  • Number of additions to determine C(n,k) is
Write a Comment
User Comments (0)
About PowerShow.com