SlideShare a Scribd company logo
CS 332: 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:  Analysis Of Dynamic Tables Let c i  = cost of  i th insert c i  =  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
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
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
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?
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?
LCS Algorithm Brute-force algorithm: 2 m  subsequences of x to check against  n  elements of y: O( n  2 m ) 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
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?
The End

More Related Content

PPT
Dynamic Programming
PPT
lecture 24
PPTX
Longest common subsequence lcs
PPT
Longest common subsequences in Algorithm Analysis
PPTX
Longest common subsequence
PPTX
Longest Common Sub-sequence (LCS)
PPTX
Longest Common Subsequence
Dynamic Programming
lecture 24
Longest common subsequence lcs
Longest common subsequences in Algorithm Analysis
Longest common subsequence
Longest Common Sub-sequence (LCS)
Longest Common Subsequence

What's hot (20)

PDF
Mychurch File Upload
PDF
Solovay Kitaev theorem
PDF
Solving the energy problem of helium final report
PDF
Fast and efficient exact synthesis of single qubit unitaries generated by cli...
PDF
Iit jam 2016 physics solutions BY Trajectoryeducation
PPT
Dynamic programming
PDF
IIT Jam math 2016 solutions BY Trajectoryeducation
PDF
Multiple Choice Questions_Successive Differentiation (CALCULUS)
PDF
1st and 2and Semester Physics Streem (2013-December) Question Papers
PDF
PDF
mathFin01
PDF
DOCX
Solution2
PDF
09 a1bs01 mathematics-1
PDF
Complex analysis notes
PDF
Mathcad explicit solution cubic equation examples
PDF
Introduction to Quantum Computing from Math perspective
PDF
Deflection 2
Mychurch File Upload
Solovay Kitaev theorem
Solving the energy problem of helium final report
Fast and efficient exact synthesis of single qubit unitaries generated by cli...
Iit jam 2016 physics solutions BY Trajectoryeducation
Dynamic programming
IIT Jam math 2016 solutions BY Trajectoryeducation
Multiple Choice Questions_Successive Differentiation (CALCULUS)
1st and 2and Semester Physics Streem (2013-December) Question Papers
mathFin01
Solution2
09 a1bs01 mathematics-1
Complex analysis notes
Mathcad explicit solution cubic equation examples
Introduction to Quantum Computing from Math perspective
Deflection 2
Ad

Similar to lecture 23 (20)

PPT
Dynamic programming in Algorithm Analysis
PPTX
Chapter 5.pptx
PPT
Amortized Analysis
PPT
Amortized analysis
PPT
Amortized analysis
PPT
Amortized analysis
PPTX
Advanced-Algorithmic-Analysis-CIA-II presentation.pptx
PPTX
AA_Unit 1_part-I.pptx
PDF
AmortizedAnalysis
PPTX
Presentation1
PDF
PPT
Introduction to Algorithms
PPTX
introduction to data structures and types
PDF
13 Amortized Analysis
PPTX
Dynamic programming class 16
PDF
Dynamic programing
DOCX
in computer data structures and algorithms
PPTX
Data structures
PPTX
week 9 lec 15.pptx
Dynamic programming in Algorithm Analysis
Chapter 5.pptx
Amortized Analysis
Amortized analysis
Amortized analysis
Amortized analysis
Advanced-Algorithmic-Analysis-CIA-II presentation.pptx
AA_Unit 1_part-I.pptx
AmortizedAnalysis
Presentation1
Introduction to Algorithms
introduction to data structures and types
13 Amortized Analysis
Dynamic programming class 16
Dynamic programing
in computer data structures and algorithms
Data structures
week 9 lec 15.pptx
Ad

More from sajinsc (20)

PPT
lecture 30
PPT
lecture 29
PPT
lecture 28
PPT
lecture 27
PPT
lecture 26
PPT
lecture 25
PPT
lecture 22
PPT
lecture 21
PPT
lecture 20
PPT
lecture 19
PPT
lecture 18
PPT
lecture 17
PPT
lecture 16
PPT
lecture 15
PPT
lecture 14
PPT
lecture 13
PPT
lecture 12
PPT
lecture 11
PPT
lecture 10
PPT
lecture 9
lecture 30
lecture 29
lecture 28
lecture 27
lecture 26
lecture 25
lecture 22
lecture 21
lecture 20
lecture 19
lecture 18
lecture 17
lecture 16
lecture 15
lecture 14
lecture 13
lecture 12
lecture 11
lecture 10
lecture 9

Recently uploaded (20)

DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
PPTX
Cardiovascular Pharmacology for pharmacy students.pptx
PDF
From loneliness to social connection charting
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
PDF
Mga Unang Hakbang Tungo Sa Tao by Joe Vibar Nero.pdf
PPTX
Revamp in MTO Odoo 18 Inventory - Odoo Slides
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
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Pre independence Education in Inndia.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Electrolyte Disturbances and Fluid Management A clinical and physiological ap...
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
PPTX
Onica Farming 24rsclub profitable farm business
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Business Ethics Teaching Materials for college
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
English Language Teaching from Post-.pdf
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
UPPER GASTRO INTESTINAL DISORDER.docx
Cardiovascular Pharmacology for pharmacy students.pptx
From loneliness to social connection charting
human mycosis Human fungal infections are called human mycosis..pptx
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
Mga Unang Hakbang Tungo Sa Tao by Joe Vibar Nero.pdf
Revamp in MTO Odoo 18 Inventory - Odoo Slides
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Abdominal Access Techniques with Prof. Dr. R K Mishra
Pre independence Education in Inndia.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Electrolyte Disturbances and Fluid Management A clinical and physiological ap...
Open Quiz Monsoon Mind Game Final Set.pptx
Onica Farming 24rsclub profitable farm business
STATICS OF THE RIGID BODIES Hibbelers.pdf
Business Ethics Teaching Materials for college
102 student loan defaulters named and shamed – Is someone you know on the list?
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
English Language Teaching from Post-.pdf
NOI Hackathon - Summer Edition - GreenThumber.pptx

lecture 23

  • 1. CS 332: Algorithms Dynamic Programming
  • 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 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?
  • 3. Review: Analysis Of Dynamic Tables Let c i = cost of i th insert c i = 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. 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
  • 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. 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. 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. 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. LCS Algorithm Brute-force algorithm: 2 m subsequences of x to check against n elements of y: O( n 2 m ) 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. 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?