SlideShare a Scribd company logo
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

PDF
Dynamic programming
PPTX
Dynamic programming
PPTX
dynamic programming complete by Mumtaz Ali (03154103173)
PPT
Dynamicpgmming
PPT
Dynamic programming
PPTX
Dynamic programming
PDF
Divide&Conquer & Dynamic Programming
PPTX
Introduction to dynamic programming
Dynamic programming
Dynamic programming
dynamic programming complete by Mumtaz Ali (03154103173)
Dynamicpgmming
Dynamic programming
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
AA_Unit 1_part-I.pptx
PDF
Chapter One.pdf
PDF
Alg_Wks1_2.pdflklokjbhvkv jv .v.vk.hk kv h/k
PDF
complexity analysis.pdf
PPT
Hash table
PDF
AA_Unit 1_part-II.pdf
PPTX
DAA-Unit1.pptx
PPT
lecture3.pptlecture3 data structures pptt
PDF
Lecture 3 insertion sort and complexity analysis
PPT
Matt Purkeypile's Doctoral Dissertation Defense Slides
PPTX
PPTX
Control System Homework Help
PDF
Book.pdf01_Intro.ppt algorithm for preperation stu used
PPTX
01 - Analysis of Efficiency.pptx
PPTX
Algorithms
PPT
Algorithm And analysis Lecture 03& 04-time complexity.
PPTX
algorithmanalysis and effciency.pptx
PDF
2 chapter2 algorithm_analysispart1
PPTX
Design & Analysis of Algorithm course .pptx
lecture 23
AA_Unit 1_part-I.pptx
Chapter One.pdf
Alg_Wks1_2.pdflklokjbhvkv jv .v.vk.hk kv h/k
complexity analysis.pdf
Hash table
AA_Unit 1_part-II.pdf
DAA-Unit1.pptx
lecture3.pptlecture3 data structures pptt
Lecture 3 insertion sort and complexity analysis
Matt Purkeypile's Doctoral Dissertation Defense Slides
Control System Homework Help
Book.pdf01_Intro.ppt algorithm for preperation stu used
01 - Analysis of Efficiency.pptx
Algorithms
Algorithm And analysis Lecture 03& 04-time complexity.
algorithmanalysis and effciency.pptx
2 chapter2 algorithm_analysispart1
Design & Analysis of Algorithm course .pptx

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
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
PPT
universality
Element distinctness lower bounds
Scheduling with Startup and Holding Costs
Divide and conquer surfing lower bounds
Red black tree
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
universality

Recently uploaded (20)

PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
RMMM.pdf make it easy to upload and study
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Business Ethics Teaching Materials for college
PDF
01-Introduction-to-Information-Management.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Basic Mud Logging Guide for educational purpose
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
RMMM.pdf make it easy to upload and study
VCE English Exam - Section C Student Revision Booklet
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Business Ethics Teaching Materials for college
01-Introduction-to-Information-Management.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Final Presentation General Medicine 03-08-2024.pptx
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Microbial diseases, their pathogenesis and prophylaxis
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
O7-L3 Supply Chain Operations - ICLT Program
Basic Mud Logging Guide for educational purpose
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student

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