SlideShare a Scribd company logo
1 of 50
Download to read offline
CS 321. Algorithm Analysis & Design
Lecture 13
Dynamic Programming
Edit Distance
Knapsacks
Shortest Paths
Items with weights wi
and value vi
Knapsack
with capacity C
Items with weights wi
and value vi
w1
v1
w2
v2
wi
vi
wn-1
vn-1
wn
vn
… …
(What we want)
w1
v1
w2
v2
wi
vi
wn-1
vn-1
wn
vn
… …
(What we want)
(Build up the array bottom to top.)
w1
v1
w2
v2
wi
vi
wn-1
vn-1
wn
vn
… …
A[i] stores the best value for the first i items.
(What we want)
(Build up the array bottom to top.)
w1
v1
w2
v2
wi
vi
wn-1
vn-1
wn
vn
… …
A[i] stores the best value for the first i items.
A[i] = max(A(i-1), A[i-1] + vi)
(What we want)
(Build up the array bottom to top.)
w1
v1
w2
v2
wi
vi
wn-1
vn-1
wn
vn
… …
A[i] stores the best value for the first i items.
(What we want)
(Build up the array bottom to top.)
w1
v1
w2
v2
wi
vi
wn-1
vn-1
wn
vn
… …
(What we want)
(Build up the array bottom to top.)
w1
v1
w2
v2
wi
vi
wn-1
vn-1
wn
vn
… …
A[i,j] stores the best value for the first i items.
(What we want)
(Build up the array bottom to top.)
w1
v1
w2
v2
wi
vi
wn-1
vn-1
wn
vn
… …
that fits in a knapsack of size j.
A[i,j] stores the best value for the first i items.
A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi)
(What we want)
(Build up the array bottom to top.)
w1
v1
w2
v2
wi
vi
wn-1
vn-1
wn
vn
… …
that fits in a knapsack of size j.
1 2 3 4 5
1
2
3
4
A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi)
1 2 3 4 5
1
2
3
4
A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi)
(2,3), (2,6), (3,7), (1,4) - weight/value pairs
1 2 3 4 5
1
2
3
4
A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi)
(2,3), (2,6), (3,7), (1,4) - weight/value pairs
1 2 3 4 5
1
2
3
4
A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi)
(2,3), (2,6), (3,7), (1,4) - weight/value pairs
1 2 3 4 5
1 0 3 3 3 3
2
3
4
A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi)
(2,3), (2,6), (3,7), (1,4) - weight/value pairs
1 2 3 4 5
1 0 3 3 3 3
2
3
4
A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi)
(2,3), (2,6), (3,7), (1,4) - weight/value pairs
1 2 3 4 5
1 0 3 3 3 3
2 0 6 6 9 9
3
4
A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi)
(2,3), (2,6), (3,7), (1,4) - weight/value pairs
1 2 3 4 5
1 0 3 3 3 3
2 0 6 6 9 9
3
4
A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi)
(2,3), (2,6), (3,7), (1,4) - weight/value pairs
1 2 3 4 5
1 0 3 3 3 3
2 0 6 6 9 9
3
4
A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi)
(2,3), (2,6), (3,7), (1,4) - weight/value pairs
1 2 3 4 5
1 0 3 3 3 3
2 0 6 6 9 9
3 0 6 7 9 9
4
A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi)
(2,3), (2,6), (3,7), (1,4) - weight/value pairs
1 2 3 4 5
1 0 3 3 3 3
2 0 6 6 9 9
3 0 6 7 9 9
4
A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi)
(2,3), (2,6), (3,7), (1,4) - weight/value pairs
1 2 3 4 5
1 0 3 3 3 3
2 0 6 6 9 9
3 0 6 7 9 9
4
A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi)
(2,3), (2,6), (3,7), (1,4) - weight/value pairs
1 2 3 4 5
1 0 3 3 3 3
2 0 6 6 9 9
3 0 6 7 9 9
4 4 6 10 11 13
A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi)
(2,3), (2,6), (3,7), (1,4) - weight/value pairs
Running time = # of subproblems x time/subproblem
Running time = # of subproblems x time/subproblem
nC x O(1) = O(nC)
(n = #items, C = Capacity)
Running time = # of subproblems x time/subproblem
nC x O(1) = O(nC)
(n = #items, C = Capacity)
Is this polynomial?
Running time = # of subproblems x time/subproblem
nC x O(1) = O(nC)
(n = #items, C = Capacity)
Is this polynomial?
pseudo-polynomial
Edit Distance
Edit Distance
The cost of changing one word to another,
using insertions/deletions/changes.
Edit Distance
The cost of changing one word to another,
using insertions/deletions/changes.
Each operation can have a certain cost.
A C G T
A 0 1 1 3
C 2 0 1 1
G 1 2 0 2
T 3 2 1 0
SUNNY DEOL
SUNNY LEONE
SUNNY DEOL
SUNNY LEONE
SUNNY DEOL
SUNNY LEONE
SUNNY LEOL
SUNNY DEOL
SUNNY LEONE
SUNNY LEOL
SUNNY LEON
SUNNY DEOL
SUNNY LEONE
SUNNY LEOL
SUNNY LEON
SUNNY LEONE
Work out the solution for pairs x[i:] and y[j:].
(What we want)
(Build up the array bottom to top.)
(What we want)
(Build up the array bottom to top.)
c[i,j] stores the edit distance between
x[1…i] and y[1…j].
(What we want)
(Build up the array bottom to top.)
c[i,j] stores the edit distance between
x[1…i] and y[1…j].
c[i,j] = min(c[i-1,j] + w1, c[i,j-1] + w2, c[i-1,j-1] + w3)
(What we want)
(Build up the array bottom to top.)
c[i,j] stores the edit distance between
x[1…i] and y[1…j].
13 - 06 Feb - Dynamic Programming
13 - 06 Feb - Dynamic Programming

More Related Content

What's hot

lecture 22
lecture 22lecture 22
lecture 22
sajinsc
 
Paths and Polynomials
Paths and PolynomialsPaths and Polynomials
Paths and Polynomials
ASPAK2014
 
Specific Finite Groups(General)
Specific Finite Groups(General)Specific Finite Groups(General)
Specific Finite Groups(General)
Shane Nicklas
 
Quantum espresso G Vector distributon
Quantum espresso G Vector distributonQuantum espresso G Vector distributon
Quantum espresso G Vector distributon
Eric Pascolo
 

What's hot (20)

Scribed lec8
Scribed lec8Scribed lec8
Scribed lec8
 
Kruskal Algorithm
Kruskal AlgorithmKruskal Algorithm
Kruskal Algorithm
 
[Lecture 3] AI and Deep Learning: Logistic Regression (Coding)
[Lecture 3] AI and Deep Learning: Logistic Regression (Coding)[Lecture 3] AI and Deep Learning: Logistic Regression (Coding)
[Lecture 3] AI and Deep Learning: Logistic Regression (Coding)
 
lecture 22
lecture 22lecture 22
lecture 22
 
Paths and Polynomials
Paths and PolynomialsPaths and Polynomials
Paths and Polynomials
 
Structures
StructuresStructures
Structures
 
CrystalBall - Compute Relative Frequency in Hadoop
CrystalBall - Compute Relative Frequency in Hadoop CrystalBall - Compute Relative Frequency in Hadoop
CrystalBall - Compute Relative Frequency in Hadoop
 
Appendix of heterogeneous cellular network user distribution model
Appendix of heterogeneous cellular network user distribution modelAppendix of heterogeneous cellular network user distribution model
Appendix of heterogeneous cellular network user distribution model
 
Fun with D3.js: Data Visualization Eye Candy with Streaming JSON
Fun with D3.js: Data Visualization Eye Candy with Streaming JSONFun with D3.js: Data Visualization Eye Candy with Streaming JSON
Fun with D3.js: Data Visualization Eye Candy with Streaming JSON
 
Specific Finite Groups(General)
Specific Finite Groups(General)Specific Finite Groups(General)
Specific Finite Groups(General)
 
Appendix of downlink coverage probability in heterogeneous cellular networks ...
Appendix of downlink coverage probability in heterogeneous cellular networks ...Appendix of downlink coverage probability in heterogeneous cellular networks ...
Appendix of downlink coverage probability in heterogeneous cellular networks ...
 
Quantum espresso G Vector distributon
Quantum espresso G Vector distributonQuantum espresso G Vector distributon
Quantum espresso G Vector distributon
 
Johnson's algorithm
Johnson's algorithmJohnson's algorithm
Johnson's algorithm
 
Annals of Statistics読み回 第一回
Annals of Statistics読み回 第一回Annals of Statistics読み回 第一回
Annals of Statistics読み回 第一回
 
Freefallball
FreefallballFreefallball
Freefallball
 
Q-learning and Deep Q Network (Reinforcement Learning)
Q-learning and Deep Q Network (Reinforcement Learning)Q-learning and Deep Q Network (Reinforcement Learning)
Q-learning and Deep Q Network (Reinforcement Learning)
 
Prim algorithm
Prim algorithmPrim algorithm
Prim algorithm
 
[Paper Reading] Causal Bandits: Learning Good Interventions via Causal Inference
[Paper Reading] Causal Bandits: Learning Good Interventions via Causal Inference[Paper Reading] Causal Bandits: Learning Good Interventions via Causal Inference
[Paper Reading] Causal Bandits: Learning Good Interventions via Causal Inference
 
Hermes
HermesHermes
Hermes
 
cryptography_non_abeliean
cryptography_non_abelieancryptography_non_abeliean
cryptography_non_abeliean
 

Viewers also liked

Using Algorithms to Brute Force Algorithms...A Journey Through Time and Names...
Using Algorithms to Brute Force Algorithms...A Journey Through Time and Names...Using Algorithms to Brute Force Algorithms...A Journey Through Time and Names...
Using Algorithms to Brute Force Algorithms...A Journey Through Time and Names...
OpenDNS
 

Viewers also liked (11)

04 - 15 Jan - Heap Sort
04 - 15 Jan - Heap Sort04 - 15 Jan - Heap Sort
04 - 15 Jan - Heap Sort
 
Knapsack problem using fixed tuple
Knapsack problem using fixed tupleKnapsack problem using fixed tuple
Knapsack problem using fixed tuple
 
Heap Sort
Heap SortHeap Sort
Heap Sort
 
Heap sort
Heap sortHeap sort
Heap sort
 
05 Analysis of Algorithms: Heap and Quick Sort - Corrected
05 Analysis of Algorithms: Heap and Quick Sort - Corrected05 Analysis of Algorithms: Heap and Quick Sort - Corrected
05 Analysis of Algorithms: Heap and Quick Sort - Corrected
 
Heap sort
Heap sort Heap sort
Heap sort
 
Using Algorithms to Brute Force Algorithms...A Journey Through Time and Names...
Using Algorithms to Brute Force Algorithms...A Journey Through Time and Names...Using Algorithms to Brute Force Algorithms...A Journey Through Time and Names...
Using Algorithms to Brute Force Algorithms...A Journey Through Time and Names...
 
Bucket sort
Bucket sortBucket sort
Bucket sort
 
Heap sort
Heap sortHeap sort
Heap sort
 
Heapsort
HeapsortHeapsort
Heapsort
 
Knapsack Problem
Knapsack ProblemKnapsack Problem
Knapsack Problem
 

Similar to 13 - 06 Feb - Dynamic Programming

Dynamic1
Dynamic1Dynamic1
Dynamic1
MyAlome
 
Vectors in Two and Three.pptx
Vectors in Two and Three.pptxVectors in Two and Three.pptx
Vectors in Two and Three.pptx
gayashani2
 
Let’s Talk About Ruby
Let’s Talk About RubyLet’s Talk About Ruby
Let’s Talk About Ruby
Ian Bishop
 
Skiena algorithm 2007 lecture15 backtracing
Skiena algorithm 2007 lecture15 backtracingSkiena algorithm 2007 lecture15 backtracing
Skiena algorithm 2007 lecture15 backtracing
zukun
 
Useful javascript
Useful javascriptUseful javascript
Useful javascript
Lei Kang
 

Similar to 13 - 06 Feb - Dynamic Programming (20)

Dynamic1
Dynamic1Dynamic1
Dynamic1
 
Dynamic Programming for 4th sem cse students
Dynamic Programming for 4th sem cse studentsDynamic Programming for 4th sem cse students
Dynamic Programming for 4th sem cse students
 
Vectors in Two and Three.pptx
Vectors in Two and Three.pptxVectors in Two and Three.pptx
Vectors in Two and Three.pptx
 
4R2012 preTest9A
4R2012 preTest9A4R2012 preTest9A
4R2012 preTest9A
 
Perspective in Informatics 3 - Assignment 1 - Answer Sheet
Perspective in Informatics 3 - Assignment 1 - Answer SheetPerspective in Informatics 3 - Assignment 1 - Answer Sheet
Perspective in Informatics 3 - Assignment 1 - Answer Sheet
 
5.3 dynamic programming 03
5.3 dynamic programming 035.3 dynamic programming 03
5.3 dynamic programming 03
 
Let’s Talk About Ruby
Let’s Talk About RubyLet’s Talk About Ruby
Let’s Talk About Ruby
 
Dynamic Programing_LCS.ppt
Dynamic Programing_LCS.pptDynamic Programing_LCS.ppt
Dynamic Programing_LCS.ppt
 
Introduction to Neural Networks and Deep Learning from Scratch
Introduction to Neural Networks and Deep Learning from ScratchIntroduction to Neural Networks and Deep Learning from Scratch
Introduction to Neural Networks and Deep Learning from Scratch
 
Skiena algorithm 2007 lecture15 backtracing
Skiena algorithm 2007 lecture15 backtracingSkiena algorithm 2007 lecture15 backtracing
Skiena algorithm 2007 lecture15 backtracing
 
Useful javascript
Useful javascriptUseful javascript
Useful javascript
 
ملزمة الرياضيات للصف السادس التطبيقي الفصل الاول الاعداد المركبة 2022
 ملزمة الرياضيات للصف السادس التطبيقي الفصل الاول الاعداد المركبة 2022 ملزمة الرياضيات للصف السادس التطبيقي الفصل الاول الاعداد المركبة 2022
ملزمة الرياضيات للصف السادس التطبيقي الفصل الاول الاعداد المركبة 2022
 
ملزمة الرياضيات للصف السادس الاحيائي الفصل الاول
ملزمة الرياضيات للصف السادس الاحيائي الفصل الاولملزمة الرياضيات للصف السادس الاحيائي الفصل الاول
ملزمة الرياضيات للصف السادس الاحيائي الفصل الاول
 
Lecture#9
Lecture#9Lecture#9
Lecture#9
 
Python 101++: Let's Get Down to Business!
Python 101++: Let's Get Down to Business!Python 101++: Let's Get Down to Business!
Python 101++: Let's Get Down to Business!
 
Heuristic Algorithm for Finding Sensitivity Analysis in Interval Solid Transp...
Heuristic Algorithm for Finding Sensitivity Analysis in Interval Solid Transp...Heuristic Algorithm for Finding Sensitivity Analysis in Interval Solid Transp...
Heuristic Algorithm for Finding Sensitivity Analysis in Interval Solid Transp...
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
 
Elementary Linear Algebra 5th Edition Larson Solutions Manual
Elementary Linear Algebra 5th Edition Larson Solutions ManualElementary Linear Algebra 5th Edition Larson Solutions Manual
Elementary Linear Algebra 5th Edition Larson Solutions Manual
 
Admission in india
 Admission in india Admission in india
Admission in india
 
5.1 greedy
5.1 greedy5.1 greedy
5.1 greedy
 

More from Neeldhara Misra

More from Neeldhara Misra (15)

Erdos Posa Theorem
Erdos Posa TheoremErdos Posa Theorem
Erdos Posa Theorem
 
15 - 12 Feb - Stable Matchings
15 - 12 Feb - Stable Matchings15 - 12 Feb - Stable Matchings
15 - 12 Feb - Stable Matchings
 
07- 22 Jan - Divide and Conquer
07- 22 Jan - Divide and Conquer07- 22 Jan - Divide and Conquer
07- 22 Jan - Divide and Conquer
 
10 - 29 Jan - Recursion Part 2
10 - 29 Jan - Recursion Part 210 - 29 Jan - Recursion Part 2
10 - 29 Jan - Recursion Part 2
 
05 - 18 Jan - The Sorting Wrap-Up
05 - 18 Jan - The Sorting Wrap-Up05 - 18 Jan - The Sorting Wrap-Up
05 - 18 Jan - The Sorting Wrap-Up
 
02 - 04 Jan - Sorting (Continued)
02 - 04 Jan - Sorting (Continued)02 - 04 Jan - Sorting (Continued)
02 - 04 Jan - Sorting (Continued)
 
04 - 15 Jan - Heap Sort
04 - 15 Jan - Heap Sort04 - 15 Jan - Heap Sort
04 - 15 Jan - Heap Sort
 
08 - 25 Jan - Divide and Conquer
08 - 25 Jan - Divide and Conquer08 - 25 Jan - Divide and Conquer
08 - 25 Jan - Divide and Conquer
 
06 - 20 Jan - Divide and Conquer
06 - 20 Jan - Divide and Conquer06 - 20 Jan - Divide and Conquer
06 - 20 Jan - Divide and Conquer
 
00 - 30 Dec - Introduction
00 - 30 Dec - Introduction00 - 30 Dec - Introduction
00 - 30 Dec - Introduction
 
01 - 01 January - Sorting
01 - 01 January - Sorting01 - 01 January - Sorting
01 - 01 January - Sorting
 
14 - 08 Feb - Dynamic Programming
14 - 08 Feb - Dynamic Programming14 - 08 Feb - Dynamic Programming
14 - 08 Feb - Dynamic Programming
 
11 - 03 Feb - From Recursion to Dynamic Programming
11 - 03 Feb - From Recursion to Dynamic Programming11 - 03 Feb - From Recursion to Dynamic Programming
11 - 03 Feb - From Recursion to Dynamic Programming
 
09 - 27 Jan - Recursion Part 1
09 - 27 Jan - Recursion Part 109 - 27 Jan - Recursion Part 1
09 - 27 Jan - Recursion Part 1
 
12 - 05 Feb - Dynamic Programming
12 - 05 Feb - Dynamic Programming12 - 05 Feb - Dynamic Programming
12 - 05 Feb - Dynamic Programming
 

Recently uploaded

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
SoniaTolstoy
 

Recently uploaded (20)

Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 

13 - 06 Feb - Dynamic Programming

  • 1. CS 321. Algorithm Analysis & Design Lecture 13 Dynamic Programming Edit Distance Knapsacks Shortest Paths
  • 2.
  • 3. Items with weights wi and value vi
  • 4. Knapsack with capacity C Items with weights wi and value vi
  • 5.
  • 8. (What we want) (Build up the array bottom to top.) w1 v1 w2 v2 wi vi wn-1 vn-1 wn vn … …
  • 9. A[i] stores the best value for the first i items. (What we want) (Build up the array bottom to top.) w1 v1 w2 v2 wi vi wn-1 vn-1 wn vn … …
  • 10. A[i] stores the best value for the first i items. A[i] = max(A(i-1), A[i-1] + vi) (What we want) (Build up the array bottom to top.) w1 v1 w2 v2 wi vi wn-1 vn-1 wn vn … …
  • 11. A[i] stores the best value for the first i items. (What we want) (Build up the array bottom to top.) w1 v1 w2 v2 wi vi wn-1 vn-1 wn vn … …
  • 12. (What we want) (Build up the array bottom to top.) w1 v1 w2 v2 wi vi wn-1 vn-1 wn vn … …
  • 13. A[i,j] stores the best value for the first i items. (What we want) (Build up the array bottom to top.) w1 v1 w2 v2 wi vi wn-1 vn-1 wn vn … … that fits in a knapsack of size j.
  • 14. A[i,j] stores the best value for the first i items. A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi) (What we want) (Build up the array bottom to top.) w1 v1 w2 v2 wi vi wn-1 vn-1 wn vn … … that fits in a knapsack of size j.
  • 15. 1 2 3 4 5 1 2 3 4 A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi)
  • 16. 1 2 3 4 5 1 2 3 4 A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi) (2,3), (2,6), (3,7), (1,4) - weight/value pairs
  • 17. 1 2 3 4 5 1 2 3 4 A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi) (2,3), (2,6), (3,7), (1,4) - weight/value pairs
  • 18. 1 2 3 4 5 1 2 3 4 A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi) (2,3), (2,6), (3,7), (1,4) - weight/value pairs
  • 19. 1 2 3 4 5 1 0 3 3 3 3 2 3 4 A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi) (2,3), (2,6), (3,7), (1,4) - weight/value pairs
  • 20. 1 2 3 4 5 1 0 3 3 3 3 2 3 4 A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi) (2,3), (2,6), (3,7), (1,4) - weight/value pairs
  • 21. 1 2 3 4 5 1 0 3 3 3 3 2 0 6 6 9 9 3 4 A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi) (2,3), (2,6), (3,7), (1,4) - weight/value pairs
  • 22. 1 2 3 4 5 1 0 3 3 3 3 2 0 6 6 9 9 3 4 A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi) (2,3), (2,6), (3,7), (1,4) - weight/value pairs
  • 23. 1 2 3 4 5 1 0 3 3 3 3 2 0 6 6 9 9 3 4 A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi) (2,3), (2,6), (3,7), (1,4) - weight/value pairs
  • 24. 1 2 3 4 5 1 0 3 3 3 3 2 0 6 6 9 9 3 0 6 7 9 9 4 A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi) (2,3), (2,6), (3,7), (1,4) - weight/value pairs
  • 25. 1 2 3 4 5 1 0 3 3 3 3 2 0 6 6 9 9 3 0 6 7 9 9 4 A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi) (2,3), (2,6), (3,7), (1,4) - weight/value pairs
  • 26. 1 2 3 4 5 1 0 3 3 3 3 2 0 6 6 9 9 3 0 6 7 9 9 4 A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi) (2,3), (2,6), (3,7), (1,4) - weight/value pairs
  • 27. 1 2 3 4 5 1 0 3 3 3 3 2 0 6 6 9 9 3 0 6 7 9 9 4 4 6 10 11 13 A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi) (2,3), (2,6), (3,7), (1,4) - weight/value pairs
  • 28.
  • 29. Running time = # of subproblems x time/subproblem
  • 30. Running time = # of subproblems x time/subproblem nC x O(1) = O(nC) (n = #items, C = Capacity)
  • 31. Running time = # of subproblems x time/subproblem nC x O(1) = O(nC) (n = #items, C = Capacity) Is this polynomial?
  • 32. Running time = # of subproblems x time/subproblem nC x O(1) = O(nC) (n = #items, C = Capacity) Is this polynomial? pseudo-polynomial
  • 34. Edit Distance The cost of changing one word to another, using insertions/deletions/changes.
  • 35. Edit Distance The cost of changing one word to another, using insertions/deletions/changes. Each operation can have a certain cost.
  • 36. A C G T A 0 1 1 3 C 2 0 1 1 G 1 2 0 2 T 3 2 1 0
  • 40. SUNNY DEOL SUNNY LEONE SUNNY LEOL SUNNY LEON
  • 41. SUNNY DEOL SUNNY LEONE SUNNY LEOL SUNNY LEON SUNNY LEONE
  • 42.
  • 43.
  • 44. Work out the solution for pairs x[i:] and y[j:].
  • 45. (What we want) (Build up the array bottom to top.)
  • 46. (What we want) (Build up the array bottom to top.) c[i,j] stores the edit distance between x[1…i] and y[1…j].
  • 47. (What we want) (Build up the array bottom to top.) c[i,j] stores the edit distance between x[1…i] and y[1…j]. c[i,j] = min(c[i-1,j] + w1, c[i,j-1] + w2, c[i-1,j-1] + w3)
  • 48. (What we want) (Build up the array bottom to top.) c[i,j] stores the edit distance between x[1…i] and y[1…j].