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
Jay Nagar
 
PPTX
Dynamic programming
Melaku Bayih Demessie
 
PPTX
dynamic programming complete by Mumtaz Ali (03154103173)
Mumtaz Ali
 
PPT
Dynamicpgmming
Muhammad Wasif
 
PPT
Dynamic programming
Gopi Saiteja
 
PPTX
Dynamic programming
Yıldırım Tam
 
PDF
Divide&Conquer & Dynamic Programming
Guillaume Guérard
 
PPTX
Introduction to dynamic programming
Amisha Narsingani
 
Dynamic programming
Jay Nagar
 
Dynamic programming
Melaku Bayih Demessie
 
dynamic programming complete by Mumtaz Ali (03154103173)
Mumtaz Ali
 
Dynamicpgmming
Muhammad Wasif
 
Dynamic programming
Gopi Saiteja
 
Dynamic programming
Yıldırım Tam
 
Divide&Conquer & Dynamic Programming
Guillaume Guérard
 
Introduction to dynamic programming
Amisha Narsingani
 

What's hot (20)

PPT
5.3 dynamic programming 03
Krish_ver2
 
PPTX
Dynamic programming class 16
Kumar
 
PDF
DP
Subba Oota
 
PPT
Lecture 8 dynamic programming
Oye Tu
 
PDF
Dynamic programming
Amit Kumar Rathi
 
PPTX
Dynamic programming - fundamentals review
ElifTech
 
PPT
dynamic programming Rod cutting class
giridaroori
 
PPT
Dynamic pgmming
Dr. C.V. Suresh Babu
 
PPTX
unit-4-dynamic programming
hodcsencet
 
PPTX
Dynamic programming1
debolina13
 
PPTX
Dynamic programming Basics
Kvishnu Dahatonde
 
PPTX
Matrix chain multiplication
Respa Peter
 
PPTX
Dynamic Programming
Sahil Kumar
 
PPTX
Daa:Dynamic Programing
rupali_2bonde
 
PPTX
Introduction to Dynamic Programming, Principle of Optimality
Bhavin Darji
 
PPTX
DS ppt
kirupasuchi1996
 
PPTX
Dynamic Programming - Part 1
Amrinder Arora
 
PPTX
daa-unit-3-greedy method
hodcsencet
 
PPTX
Greedy Algorithms
Amrinder Arora
 
PPTX
Matrix mult class-17
Kumar
 
5.3 dynamic programming 03
Krish_ver2
 
Dynamic programming class 16
Kumar
 
Lecture 8 dynamic programming
Oye Tu
 
Dynamic programming
Amit Kumar Rathi
 
Dynamic programming - fundamentals review
ElifTech
 
dynamic programming Rod cutting class
giridaroori
 
Dynamic pgmming
Dr. C.V. Suresh Babu
 
unit-4-dynamic programming
hodcsencet
 
Dynamic programming1
debolina13
 
Dynamic programming Basics
Kvishnu Dahatonde
 
Matrix chain multiplication
Respa Peter
 
Dynamic Programming
Sahil Kumar
 
Daa:Dynamic Programing
rupali_2bonde
 
Introduction to Dynamic Programming, Principle of Optimality
Bhavin Darji
 
Dynamic Programming - Part 1
Amrinder Arora
 
daa-unit-3-greedy method
hodcsencet
 
Greedy Algorithms
Amrinder Arora
 
Matrix mult class-17
Kumar
 
Ad

Viewers also liked (19)

PDF
Randomized Algorithms in Linear Algebra & the Column Subset Selection Problem
Wei Xue
 
PPT
Chap08alg
Munkhchimeg
 
PPTX
Elements of dynamic programming
Tafhim Islam
 
PPTX
Class warshal2
Debarati Das
 
PPT
Mathematical analysis of Graph and Huff amn coding
Dr Anjan Krishnamurthy
 
PPT
Introduction to-sql
BG Java EE Course
 
PPTX
Dynamic programming
Maher Alshammari
 
PDF
Solving The Shortest Path Tour Problem
Nozir Shokirov
 
PPT
Average case Analysis of Quicksort
Rajendran
 
PPT
21 backtracking
Aparup Behera
 
PPT
5.5 back track
Krish_ver2
 
PPTX
Subset sum problem Dynamic and Brute Force Approch
Ijlal Ijlal
 
PPTX
Huffman codes
Nargis Ehsan
 
PPT
Covering (Rules-based) Algorithm
ZHAO Sam
 
PPT
Backtracking
Vikas Sharma
 
PPT
Lect6 csp
trivedidr
 
PPTX
Backtracking
Sally Salem
 
PPTX
Queue- 8 Queen
Ha Ninh
 
PPT
(Binary tree)
almario1988
 
Randomized Algorithms in Linear Algebra & the Column Subset Selection Problem
Wei Xue
 
Chap08alg
Munkhchimeg
 
Elements of dynamic programming
Tafhim Islam
 
Class warshal2
Debarati Das
 
Mathematical analysis of Graph and Huff amn coding
Dr Anjan Krishnamurthy
 
Introduction to-sql
BG Java EE Course
 
Dynamic programming
Maher Alshammari
 
Solving The Shortest Path Tour Problem
Nozir Shokirov
 
Average case Analysis of Quicksort
Rajendran
 
21 backtracking
Aparup Behera
 
5.5 back track
Krish_ver2
 
Subset sum problem Dynamic and Brute Force Approch
Ijlal Ijlal
 
Huffman codes
Nargis Ehsan
 
Covering (Rules-based) Algorithm
ZHAO Sam
 
Backtracking
Vikas Sharma
 
Lect6 csp
trivedidr
 
Backtracking
Sally Salem
 
Queue- 8 Queen
Ha Ninh
 
(Binary tree)
almario1988
 
Ad

Similar to Dynamic programming in Algorithm Analysis (20)

PPT
lecture 23
sajinsc
 
PPTX
AA_Unit 1_part-I.pptx
swapnilslide2019
 
PDF
Chapter One.pdf
abay golla
 
PDF
Alg_Wks1_2.pdflklokjbhvkv jv .v.vk.hk kv h/k
227567
 
PDF
complexity analysis.pdf
pasinduneshan
 
PPT
Hash table
Rajendran
 
PDF
AA_Unit 1_part-II.pdf
swapnilslide2019
 
PPTX
DAA-Unit1.pptx
NishaS88
 
PPT
lecture3.pptlecture3 data structures pptt
SyedAliShahid3
 
PDF
Lecture 3 insertion sort and complexity analysis
jayavignesh86
 
PPT
Matt Purkeypile's Doctoral Dissertation Defense Slides
mpurkeypile
 
PPTX
Lec1.pptx
AsadbekIbragimov1
 
PPTX
Control System Homework Help
Matlab Assignment Experts
 
PDF
Book.pdf01_Intro.ppt algorithm for preperation stu used
archu26
 
PPTX
01 - Analysis of Efficiency.pptx
Ishtiaq Rasool Khan
 
PPTX
Algorithms
DevMix
 
PPT
Algorithm And analysis Lecture 03& 04-time complexity.
Tariq Khan
 
PPTX
algorithmanalysis and effciency.pptx
ChSreenivasuluReddy
 
PDF
2 chapter2 algorithm_analysispart1
SSE_AndyLi
 
PPTX
Design & Analysis of Algorithm course .pptx
JeevaMCSEKIOT
 
lecture 23
sajinsc
 
AA_Unit 1_part-I.pptx
swapnilslide2019
 
Chapter One.pdf
abay golla
 
Alg_Wks1_2.pdflklokjbhvkv jv .v.vk.hk kv h/k
227567
 
complexity analysis.pdf
pasinduneshan
 
Hash table
Rajendran
 
AA_Unit 1_part-II.pdf
swapnilslide2019
 
DAA-Unit1.pptx
NishaS88
 
lecture3.pptlecture3 data structures pptt
SyedAliShahid3
 
Lecture 3 insertion sort and complexity analysis
jayavignesh86
 
Matt Purkeypile's Doctoral Dissertation Defense Slides
mpurkeypile
 
Control System Homework Help
Matlab Assignment Experts
 
Book.pdf01_Intro.ppt algorithm for preperation stu used
archu26
 
01 - Analysis of Efficiency.pptx
Ishtiaq Rasool Khan
 
Algorithms
DevMix
 
Algorithm And analysis Lecture 03& 04-time complexity.
Tariq Khan
 
algorithmanalysis and effciency.pptx
ChSreenivasuluReddy
 
2 chapter2 algorithm_analysispart1
SSE_AndyLi
 
Design & Analysis of Algorithm course .pptx
JeevaMCSEKIOT
 

More from Rajendran (20)

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

Recently uploaded (20)

PPTX
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
PPTX
CDH. pptx
AneetaSharma15
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PPTX
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
PDF
RA 12028_ARAL_Orientation_Day-2-Sessions_v2.pdf
Seven De Los Reyes
 
PPTX
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
PDF
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
PPTX
Care of patients with elImination deviation.pptx
AneetaSharma15
 
PDF
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
DOCX
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
PPTX
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
PPTX
Trends in pediatric nursing .pptx
AneetaSharma15
 
PDF
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
PPTX
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
PDF
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
PPTX
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
PPTX
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
PDF
Sunset Boulevard Student Revision Booklet
jpinnuck
 
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
CDH. pptx
AneetaSharma15
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
RA 12028_ARAL_Orientation_Day-2-Sessions_v2.pdf
Seven De Los Reyes
 
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
Care of patients with elImination deviation.pptx
AneetaSharma15
 
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 
Trends in pediatric nursing .pptx
AneetaSharma15
 
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
Sunset Boulevard Student Revision Booklet
jpinnuck
 

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