2
Most read
David Luebke 1
CS 332: Algorithms
Dynamic Programming
David Luebke 2
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 2m
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?
David Luebke 3
Review:
Analysis Of Dynamic Tables
● Let ci = cost of ith insert
● ci = i if i-1 is exact power of 2, 1 otherwise
● Example:
■ Operation Table Size Cost
Insert(1) 1 1 1
Insert(2) 2 1 + 1 2
Insert(3) 4 1 + 2
Insert(4) 4 1
Insert(5) 8 1 + 4
Insert(6) 8 1
Insert(7) 8 1
Insert(8) 8 1
Insert(9) 16 1 + 8
1
2
3
4
5
6
7
8
9
David Luebke 4
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
nnnnc
n
j
j
n
i
i 3)12(2
lg
01
<−+=+≤ ∑∑ ==
David Luebke 5
Review: Accounting Analysis
● Charge each operation $3 amortized cost
■ Use $1 to perform immediate Insert()
■ Store $2
● When table doubles
■ $1 reinserts old item, $1 reinserts another old item
■ We’ve paid these costs up front with the last n/2
Insert()s
● Upshot: O(1) amortized cost per operation
David Luebke 6
Review: Accounting Analysis
● Suppose must support insert & delete, table
should contract as well as expand
■ Table overflows ⇒ double it (as before)
■ Table < 1/4 full ⇒ halve it
■ Charge $3 for Insert (as before)
■ Charge $2 for Delete
○ Store extra $1 in emptied slot
○ Use later to pay to copy remaining items to new table
when shrinking table
● What if we halve size when table < 1/8 full?
David Luebke 7
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
David Luebke 8
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?
David Luebke 9
LCS Algorithm
● Brute-force algorithm: 2m
subsequences of x to
check against n elements of y: O(n 2m
)
● We can do better: for now, let’s only worry
about the problem of finding the length of LCS
■ When finished we will see how to backtrack from
this solution back to the actual LCS
● Notice LCS problem has optimal substructure
■ Subproblems: LCS of pairs of prefixes of x and y
David Luebke 10
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?



−−
=+−−
=
otherwise]),1[],1,[max(
],[][if1]1,1[
],[
jicjic
jyixjic
jic
David Luebke 11
The End

More Related Content

PPTX
Dynamic programming
PPTX
Dynamic programming
PDF
Dynamic programming
PPTX
dynamic programming complete by Mumtaz Ali (03154103173)
PPT
Dynamicpgmming
PPT
Dynamic programming
PDF
Divide&Conquer & Dynamic Programming
PPTX
Introduction to dynamic programming
Dynamic programming
Dynamic programming
Dynamic programming
dynamic programming complete by Mumtaz Ali (03154103173)
Dynamicpgmming
Dynamic programming
Divide&Conquer & Dynamic Programming
Introduction to dynamic programming

What's hot (20)

PPT
5.3 dynamic programming 03
PPTX
Dynamic programming class 16
PDF
PPT
Lecture 8 dynamic programming
PDF
Dynamic programming
PPTX
Dynamic programming - fundamentals review
PPT
dynamic programming Rod cutting class
PPT
Dynamic pgmming
PPTX
unit-4-dynamic programming
PPTX
Dynamic programming1
PPTX
Dynamic programming Basics
PPTX
Matrix chain multiplication
PPTX
Dynamic Programming
PPTX
Daa:Dynamic Programing
PPTX
Introduction to Dynamic Programming, Principle of Optimality
PPTX
PPTX
Dynamic Programming - Part 1
PPTX
daa-unit-3-greedy method
PPTX
Greedy Algorithms
PPTX
Matrix mult class-17
5.3 dynamic programming 03
Dynamic programming class 16
Lecture 8 dynamic programming
Dynamic programming
Dynamic programming - fundamentals review
dynamic programming Rod cutting class
Dynamic pgmming
unit-4-dynamic programming
Dynamic programming1
Dynamic programming Basics
Matrix chain multiplication
Dynamic Programming
Daa:Dynamic Programing
Introduction to Dynamic Programming, Principle of Optimality
Dynamic Programming - Part 1
daa-unit-3-greedy method
Greedy Algorithms
Matrix mult class-17
Ad

Viewers also liked (19)

PDF
Randomized Algorithms in Linear Algebra & the Column Subset Selection Problem
PPT
Chap08alg
PPTX
Elements of dynamic programming
PPTX
Class warshal2
PPT
Mathematical analysis of Graph and Huff amn coding
PPT
Introduction to-sql
PPTX
Dynamic programming
PDF
Solving The Shortest Path Tour Problem
PPT
Average case Analysis of Quicksort
PPT
21 backtracking
PPT
5.5 back track
PPTX
Subset sum problem Dynamic and Brute Force Approch
PPTX
Huffman codes
PPT
Covering (Rules-based) Algorithm
PPT
Backtracking
PPT
Lect6 csp
PPTX
Backtracking
PPTX
Queue- 8 Queen
PPT
(Binary tree)
Randomized Algorithms in Linear Algebra & the Column Subset Selection Problem
Chap08alg
Elements of dynamic programming
Class warshal2
Mathematical analysis of Graph and Huff amn coding
Introduction to-sql
Dynamic programming
Solving The Shortest Path Tour Problem
Average case Analysis of Quicksort
21 backtracking
5.5 back track
Subset sum problem Dynamic and Brute Force Approch
Huffman codes
Covering (Rules-based) Algorithm
Backtracking
Lect6 csp
Backtracking
Queue- 8 Queen
(Binary tree)
Ad

Similar to Dynamic programming in Algorithm Analysis (20)

PPT
lecture 23
PPTX
Advanced-Algorithmic-Analysis-CIA-II presentation.pptx
PPT
Algorithm lecture Dynamic programming
PPTX
Chapter 5.pptx
PPTX
Design and Analysis of Algorithms.pptx
PDF
Unit 4 of design and analysis of algorithms
PPTX
AA_Unit 1_part-I.pptx
PPT
Algorithm Design
PPTX
week 9 lec 15.pptx
PPTX
AoA Lec Design of algorithm spresentation
PPT
Types of Algorithms.ppt
PDF
AmortizedAnalysis
PDF
Design & Analysis Of Algorithm
PPTX
Annotaed slides for dynamic programming algorithm
PPT
dynamic-programming unit 3 power point presentation
PDF
Algorithm Design and Analysis
PPT
Algorithm Design Presentation
PPTX
problem solving and algorithm development
PPTX
Modile-1-PPT-1-BCAC0207-AlgorithmDesign.pptx
PPTX
Module 2ppt.pptx divid and conquer method
lecture 23
Advanced-Algorithmic-Analysis-CIA-II presentation.pptx
Algorithm lecture Dynamic programming
Chapter 5.pptx
Design and Analysis of Algorithms.pptx
Unit 4 of design and analysis of algorithms
AA_Unit 1_part-I.pptx
Algorithm Design
week 9 lec 15.pptx
AoA Lec Design of algorithm spresentation
Types of Algorithms.ppt
AmortizedAnalysis
Design & Analysis Of Algorithm
Annotaed slides for dynamic programming algorithm
dynamic-programming unit 3 power point presentation
Algorithm Design and Analysis
Algorithm Design Presentation
problem solving and algorithm development
Modile-1-PPT-1-BCAC0207-AlgorithmDesign.pptx
Module 2ppt.pptx divid and conquer method

More from Rajendran (20)

PPT
Element distinctness lower bounds
PPT
Scheduling with Startup and Holding Costs
PPT
Divide and conquer surfing lower bounds
PPT
Red black tree
PPT
Hash table
PPT
Medians and order statistics
PPT
Proof master theorem
PPT
Recursion tree method
PPT
Recurrence theorem
PPT
Master method
PPT
Master method theorem
PPT
Hash tables
PPT
Lower bound
PPT
Master method theorem
PPT
Greedy algorithms
PPT
Longest common subsequences in Algorithm Analysis
PPT
Np completeness
PPT
computer languages
PPT
proving non-computability
PPT
the halting_problem
Element distinctness lower bounds
Scheduling with Startup and Holding Costs
Divide and conquer surfing lower bounds
Red black tree
Hash table
Medians and order statistics
Proof master theorem
Recursion tree method
Recurrence theorem
Master method
Master method theorem
Hash tables
Lower bound
Master method theorem
Greedy algorithms
Longest common subsequences in Algorithm Analysis
Np completeness
computer languages
proving non-computability
the halting_problem

Recently uploaded (20)

PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PDF
Uderstanding digital marketing and marketing stratergie for engaging the digi...
PDF
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
Environmental Education MCQ BD2EE - Share Source.pdf
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
PDF
Trump Administration's workforce development strategy
PDF
AI-driven educational solutions for real-life interventions in the Philippine...
PDF
What if we spent less time fighting change, and more time building what’s rig...
DOCX
Cambridge-Practice-Tests-for-IELTS-12.docx
PDF
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
PDF
IGGE1 Understanding the Self1234567891011
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
PDF
Practical Manual AGRO-233 Principles and Practices of Natural Farming
PPTX
Virtual and Augmented Reality in Current Scenario
PDF
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
PPTX
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
Paper A Mock Exam 9_ Attempt review.pdf.
Uderstanding digital marketing and marketing stratergie for engaging the digi...
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Environmental Education MCQ BD2EE - Share Source.pdf
Weekly quiz Compilation Jan -July 25.pdf
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
Trump Administration's workforce development strategy
AI-driven educational solutions for real-life interventions in the Philippine...
What if we spent less time fighting change, and more time building what’s rig...
Cambridge-Practice-Tests-for-IELTS-12.docx
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
IGGE1 Understanding the Self1234567891011
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
Chinmaya Tiranga quiz Grand Finale.pdf
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
Practical Manual AGRO-233 Principles and Practices of Natural Farming
Virtual and Augmented Reality in Current Scenario
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx

Dynamic programming in Algorithm Analysis

  • 1. David Luebke 1 CS 332: Algorithms Dynamic Programming
  • 2. David Luebke 2 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 2m 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?
  • 3. David Luebke 3 Review: Analysis Of Dynamic Tables ● Let ci = cost of ith insert ● ci = i if i-1 is exact power of 2, 1 otherwise ● Example: ■ Operation Table Size Cost Insert(1) 1 1 1 Insert(2) 2 1 + 1 2 Insert(3) 4 1 + 2 Insert(4) 4 1 Insert(5) 8 1 + 4 Insert(6) 8 1 Insert(7) 8 1 Insert(8) 8 1 Insert(9) 16 1 + 8 1 2 3 4 5 6 7 8 9
  • 4. David Luebke 4 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 nnnnc n j j n i i 3)12(2 lg 01 <−+=+≤ ∑∑ ==
  • 5. David Luebke 5 Review: Accounting Analysis ● Charge each operation $3 amortized cost ■ Use $1 to perform immediate Insert() ■ Store $2 ● When table doubles ■ $1 reinserts old item, $1 reinserts another old item ■ We’ve paid these costs up front with the last n/2 Insert()s ● Upshot: O(1) amortized cost per operation
  • 6. David Luebke 6 Review: Accounting Analysis ● Suppose must support insert & delete, table should contract as well as expand ■ Table overflows ⇒ double it (as before) ■ Table < 1/4 full ⇒ halve it ■ Charge $3 for Insert (as before) ■ Charge $2 for Delete ○ Store extra $1 in emptied slot ○ Use later to pay to copy remaining items to new table when shrinking table ● What if we halve size when table < 1/8 full?
  • 7. David Luebke 7 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
  • 8. David Luebke 8 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?
  • 9. David Luebke 9 LCS Algorithm ● Brute-force algorithm: 2m subsequences of x to check against n elements of y: O(n 2m ) ● We can do better: for now, let’s only worry about the problem of finding the length of LCS ■ When finished we will see how to backtrack from this solution back to the actual LCS ● Notice LCS problem has optimal substructure ■ Subproblems: LCS of pairs of prefixes of x and y
  • 10. David Luebke 10 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?    −− =+−− = otherwise]),1[],1,[max( ],[][if1]1,1[ ],[ jicjic jyixjic jic