You are on page 1of 16

Dynamic Programming

Popescu Ion-Adrian

Dynamic Programming History


` `

Bellman. [1950s] Pioneered the systematic study of dynamic programming. Etymology.


` ` `

Dynamic programming = planning over time. Secretary of Defense was hostile to mathematical research. Bellman sought an impressive name to avoid confrontation.

` ` `

"it's impossible to use dynamic in a pejorative sense" "something not even a Congressman could object to Reference: Bellman, R. E. Eye of the Hurricane, An Autobiography.

Dynamic Programming Applications


`

Areas.
` ` ` ` `

Bioinformatics. Control theory. Information theory. Operations research. Computer science: theory, graphics, AI, compilers, systems, . Unix diff for comparing two files. Viterbi for hidden Markov models / decoding convolutional codes Smith-Waterman for genetic sequence alignment. Bellman-Ford for shortest path routing in networks. Cocke-Kasami-Younger for parsing context free grammars.

Some famous dynamic programming algorithms.


` ` ` ` `

Case Study: Fibonacci Sequence


`

Sequence definition:
a1 = 1 a2 = 1 an = an-1 + an-2

Recursive algorithm

Fib(n)
if n=1 or n=2 then return 1 else a = Fib(n-1) b = Fib(n-2) return a+b

` `

To illustrate the sequence:


1, 1, 3, 5, 8, 13, 21, 34,

How should you compute the Fibonacci sequence?


But what about running time?

Computing The Fibonacci Sequence Faster


` `

The current implementation provides lots of redundancy. The recursive algorithm only solves n-1 different subproblems. Memoization = A procedure through which we store the values returned by recursive calls in order to cache future calls. This implementation runs in linear time, provided that integer operations take constant time

Resulting Algorithm

Fib(n)
if n=1 or n=2 then return 1 else f[1]=1; f[2]=1 for i:= 3,n f[i] := f[i-1] + f[i-2] return f[n]

Computing Fibonacci Sequence Faster


`

Weighted Interval Scheduling


`

Unweighted Interval Scheduling Review


` `

Such types of problems could be solved using the Greedy algorithm but only if all weights are 1. A possible two-stepped approach would be to:
` `

Consider jobs in ascending order of finish time. Add job to subset if it is compatible with previously chosen jobs.

Typically, Greedy algorithm have a tendency to fail when applying for problems using weights.

Weighted Interval Scheduling


`

Dynamic Programming: Binary Choice


`

Weighted Interval Scheduling: Brute Force


`

Weighted Interval Scheduling: Brute Force


`

The previously implemented recursive algorithm fails spectacularly because of the number of redundant subproblems it solves, leading to it becoming an example of exponential algorithm. A valid take-away is that the number of recursive calls for any given family of "layered" instances grows much like the Fibonacci sequence.

Weighted Interval Scheduling: Memoization


`

Weighted Interval Running Time


`

Finding a Solution
`

References
` ` ` `

CSE 565: Algorithm Design and Analysis Prof. Adam Smith, Pennsylvania State University CS 473: Algorithms Prof. Chandra Chekuri, University of Illinois http://xlinux.nist.gov/dads/HTML/memoize.html http://codebetter.com/matthewpodwysocki/2008/08/01/re cursing-into-recursion-memoization/

You might also like