Docsity

Dynamic Programming - Introduction to Algorithms - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download Dynamic Programming - Introduction to Algorithms - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Algorithms

Dynamic Programming

Review: Amortized Analysis

● To illustrate amortized analysis we examined

dynamic tables

  1. Init table size m = 1
  2. Insert elements until number n > m
  3. Generate new table of size 2 m
  4. Reinsert old elements into new table
  5. (back to step 2)

● What is the worst-case cost of an insert?

● What is the amortized cost of an insert?

Review: Aggregate Analysis

● n Insert() operations cost

● Average cost of operation

= (total cost)/(# operations) < 3

● Asymptotically, then, a dynamic table costs the

same as a fixed-size table

■ Both O(1) per Insert operation

c n n n n

n

j

j

n

i

i^2 (^21 )^3

lg

1 0

∑ ≤^ + ∑ = + −^ <

= =

Review: Accounting Analysis

● Charge each operation $3 amortized cost

■ Use $1 to perform immediate Insert()

■ Store $

● When table doubles

■ $1 reinserts old item, $1 reinserts another old item

■ We’ve paid these costs up front with the last n /

Insert()s

● Upshot: O(1) amortized cost per operation

Dynamic Programming

● Another strategy for designing algorithms is

dynamic programming

■ A metatechnique, not an algorithm

(like divide & conquer)

■ The word “programming” is historical and

predates computer programming

● Use when problem breaks down into recurring

small subproblems

Dynamic Programming Example:

Longest Common Subsequence

● Longest common subsequence ( LCS ) problem:

■ Given two sequences x[1..m] and y[1..n], find the

longest subsequence which occurs in both

■ Ex: x = {A B C B D A B }, y = {B D C A B A}

■ {B C} and {A A} are both subsequences of both

○ What is the LCS?

■ Brute-force algorithm: For every subsequence of x,

check if it’s a subsequence of y

○ How many subsequences of x are there?

○ What will be the running time of the brute-force alg?

Finding LCS Length

● Define c[ i,j ] to be the length of the LCS of

x[1.. i ] and y[1.. j ]

■ What is the length of LCS of x and y?

● Theorem:

● What is this really saying?

max( [ , 1 ], [ 1 , ]) otherwise

[ 1 , 1 ] 1 if [ ] [ ], [ , ] c i j c i j

c i j x i y j c i j

The End