cse 202: design and analysis of algorithms · 2011. 4. 14. · three steps of dynamic programming...
TRANSCRIPT
![Page 1: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/1.jpg)
CSE 202: Design and Analysis of Algorithms
Lecture 6
Instructor: Kamalika Chaudhuri
![Page 2: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/2.jpg)
Announcements
• Homework due in lecture today!
• My office hours moving to Room 4110
![Page 3: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/3.jpg)
Last Class: Dynamic Programming
• String Reconstruction
• Longest Common Subsequence
![Page 4: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/4.jpg)
Three steps of Dynamic Programming
Main Steps:
1. Divide the problem into subtasks
2. Define the subtasks recursively (express larger subtasks in terms of smaller ones)
3. Find the right order for solving the subtasks (but do not solve them recursively!)
![Page 5: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/5.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
Example: x = A,C,G, T, A,G y = G,T, C,C,A,C LCS(x, y) = G,T, A
![Page 6: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/6.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
Example: x = A,C,G, T, A,G y = G,T, C,C,A,C LCS(x, y) = G,T, A
Structure: x = A,C,G, T, y = G,T,
![Page 7: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/7.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
Example: x = A,C,G, T, A,G y = G,T, C,C,A,C LCS(x, y) = G,T, A
Structure: x = A,C,G, T, y = G,T,
If x[i] = y[j], then LCS(x[1..i], y[1..j]) = LCS(x[1..i-1], y[1..j-1]) + x[i]
![Page 8: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/8.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
Example: x = A,C,G, T, A,G y = G,T, C,C,A,C LCS(x, y) = G,T, A
Structure: x = A,C,G, T, y = G,T,
If x[i] = y[j], then LCS(x[1..i], y[1..j]) = LCS(x[1..i-1], y[1..j-1]) + x[i]
x = A,C,G, T, A y = G,T,
![Page 9: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/9.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
Example: x = A,C,G, T, A,G y = G,T, C,C,A,C LCS(x, y) = G,T, A
Structure: x = A,C,G, T, y = G,T,
If x[i] = y[j], then LCS(x[1..i], y[1..j]) = LCS(x[1..i-1], y[1..j-1]) + x[i]
x = A,C,G, T, A y = G,T,
Otherwise, LCS(x[1..i], y[1..j]) = max(LCS(x[1..i-1], y[1..j]),
LCS(x[1..i], y[1..j-1]))
![Page 10: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/10.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
Example: x = A,C,G, T, A,G y = G,T, C,C,A,C LCS(x, y) = G,T, A A C T G G C T A G
GTGACAGTT
1 2 3 4 5 6 7 8 90123456789
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]Output of algorithm = S(m,n)
![Page 11: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/11.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
Example: x = A,C,G, T, A,G y = G,T, C,C,A,C LCS(x, y) = G,T, A A C T G G C T A G
GTGACAGTT
1 2 3 4 5 6 7 8 90123456789
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]Output of algorithm = S(m,n)
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
![Page 12: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/12.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
Example: x = A,C,G, T, A,G y = G,T, C,C,A,C LCS(x, y) = G,T, A A C T G G C T A G
GTGACAGTT
1 2 3 4 5 6 7 8 90123456789
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]Output of algorithm = S(m,n)
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
STEP 3: Order of subtasksRow by row, left to right
![Page 13: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/13.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
Example: x = A,C,G, T, A,G y = G,T, C,C,A,C LCS(x, y) = G,T, A A C T G G C T A G
GTGACAGTT
1 2 3 4 5 6 7 8 90123456789
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]Output of algorithm = S(m,n)
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
STEP 3: Order of subtasksRow by row, left to right Base Case: Row 0, Column 0
![Page 14: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/14.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
Example: x = A,C,G, T, A,G y = G,T, C,C,A,C LCS(x, y) = G,T, A A C T G G C T A G
GTGACAGTT
1 2 3 4 5 6 7 8 90123456789
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]Output of algorithm = S(m,n)
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
0
STEP 3: Order of subtasksRow by row, left to right Base Case: Row 0, Column 0
![Page 15: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/15.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
Example: x = A,C,G, T, A,G y = G,T, C,C,A,C LCS(x, y) = G,T, A A C T G G C T A G
GTGACAGTT
1 2 3 4 5 6 7 8 90123456789
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]Output of algorithm = S(m,n)
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
0 0 0
STEP 3: Order of subtasksRow by row, left to right Base Case: Row 0, Column 0
![Page 16: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/16.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
Example: x = A,C,G, T, A,G y = G,T, C,C,A,C LCS(x, y) = G,T, A A C T G G C T A G
GTGACAGTT
1 2 3 4 5 6 7 8 90123456789
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]Output of algorithm = S(m,n)
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
0 0 0 1
STEP 3: Order of subtasksRow by row, left to right Base Case: Row 0, Column 0
![Page 17: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/17.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
Example: x = A,C,G, T, A,G y = G,T, C,C,A,C LCS(x, y) = G,T, A A C T G G C T A G
GTGACAGTT
1 2 3 4 5 6 7 8 90123456789
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]Output of algorithm = S(m,n)
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
0 0 0 1 1 1 1 1 1
STEP 3: Order of subtasksRow by row, left to right Base Case: Row 0, Column 0
![Page 18: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/18.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
Example: x = A,C,G, T, A,G y = G,T, C,C,A,C LCS(x, y) = G,T, A A C T G G C T A G
GTGACAGTT
1 2 3 4 5 6 7 8 90123456789
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]Output of algorithm = S(m,n)
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
0 0 0 1 1 1 1 1 10 0
STEP 3: Order of subtasksRow by row, left to right Base Case: Row 0, Column 0
![Page 19: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/19.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
Example: x = A,C,G, T, A,G y = G,T, C,C,A,C LCS(x, y) = G,T, A A C T G G C T A G
GTGACAGTT
1 2 3 4 5 6 7 8 90123456789
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]Output of algorithm = S(m,n)
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
0 0 0 1 1 1 1 1 10 0 1
STEP 3: Order of subtasksRow by row, left to right Base Case: Row 0, Column 0
![Page 20: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/20.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
Example: x = A,C,G, T, A,G y = G,T, C,C,A,C LCS(x, y) = G,T, A A C T G G C T A G
GTGACAGTT
1 2 3 4 5 6 7 8 90123456789
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]Output of algorithm = S(m,n)
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
0 0 0 1 1 1 1 1 10 0 1 1
STEP 3: Order of subtasksRow by row, left to right Base Case: Row 0, Column 0
![Page 21: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/21.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
Example: x = A,C,G, T, A,G y = G,T, C,C,A,C LCS(x, y) = G,T, A A C T G G C T A G
GTGACAGTT
1 2 3 4 5 6 7 8 90123456789
0 0 0 1 1 1 1 1 10 0 1 1 1 1
STEP 3: Order of subtasksRow by row, left to right Base Case: Row 0, Column 0
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]Output of algorithm = S(m,n)
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
![Page 22: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/22.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
Example: x = A,C,G, T, A,G y = G,T, C,C,A,C LCS(x, y) = G,T, A A C T G G C T A G
GTGACAGTT
1 2 3 4 5 6 7 8 90123456789
0 0 0 1 1 1 1 1 10 0 1 1 1 1 2
STEP 3: Order of subtasksRow by row, left to right Base Case: Row 0, Column 0
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]Output of algorithm = S(m,n)
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
![Page 23: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/23.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
Example: x = A,C,G, T, A,G y = G,T, C,C,A,C LCS(x, y) = G,T, A A C T G G C T A G
GTGACAGTT
1 2 3 4 5 6 7 8 90123456789
0 0 0 1 1 1 1 1 10 0 1 1 1 1 2 2 2
STEP 3: Order of subtasksRow by row, left to right Base Case: Row 0, Column 0
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]Output of algorithm = S(m,n)
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
![Page 24: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/24.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
Example: x = A,C,G, T, A,G y = G,T, C,C,A,C LCS(x, y) = G,T, A A C T G G C T A G
GTGACAGTT
1 2 3 4 5 6 7 8 90123456789
0 0 0 1 1 1 1 1 10 0 1 1 1 1 2 2 20 0
STEP 3: Order of subtasksRow by row, left to right Base Case: Row 0, Column 0
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]Output of algorithm = S(m,n)
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
![Page 25: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/25.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
Example: x = A,C,G, T, A,G y = G,T, C,C,A,C LCS(x, y) = G,T, A A C T G G C T A G
GTGACAGTT
1 2 3 4 5 6 7 8 90123456789
0 0 0 1 1 1 1 1 10 0 1 1 1 1 2 2 20 0 1
STEP 3: Order of subtasksRow by row, left to right Base Case: Row 0, Column 0
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]Output of algorithm = S(m,n)
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
![Page 26: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/26.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
Example: x = A,C,G, T, A,G y = G,T, C,C,A,C LCS(x, y) = G,T, A A C T G G C T A G
GTGACAGTT
1 2 3 4 5 6 7 8 90123456789
0 0 0 1 1 1 1 1 10 0 1 1 1 1 2 2 20 0 1 2
STEP 3: Order of subtasksRow by row, left to right Base Case: Row 0, Column 0
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]Output of algorithm = S(m,n)
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
![Page 27: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/27.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
Example: x = A,C,G, T, A,G y = G,T, C,C,A,C LCS(x, y) = G,T, A A C T G G C T A G
GTGACAGTT
1 2 3 4 5 6 7 8 90123456789
0 0 0 1 1 1 1 1 10 0 1 1 1 1 2 2 20 0 1 2 2
STEP 3: Order of subtasksRow by row, left to right Base Case: Row 0, Column 0
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]Output of algorithm = S(m,n)
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
![Page 28: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/28.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
Example: x = A,C,G, T, A,G y = G,T, C,C,A,C LCS(x, y) = G,T, A A C T G G C T A G
GTGACAGTT
1 2 3 4 5 6 7 8 90123456789
0 0 0 1 1 1 1 1 10 0 1 1 1 1 2 2 20 0 1 2 2 2
STEP 3: Order of subtasksRow by row, left to right Base Case: Row 0, Column 0
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]Output of algorithm = S(m,n)
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
![Page 29: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/29.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
Example: x = A,C,G, T, A,G y = G,T, C,C,A,C LCS(x, y) = G,T, A A C T G G C T A G
GTGACAGTT
1 2 3 4 5 6 7 8 90123456789
0 0 0 1 1 1 1 1 10 0 1 1 1 1 2 2 20 0 1 2 2 2 2
STEP 3: Order of subtasksRow by row, left to right Base Case: Row 0, Column 0
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]Output of algorithm = S(m,n)
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
![Page 30: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/30.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
Example: x = A,C,G, T, A,G y = G,T, C,C,A,C LCS(x, y) = G,T, A A C T G G C T A G
GTGACAGTT
1 2 3 4 5 6 7 8 90123456789
0 0 0 1 1 1 1 1 10 0 1 1 1 1 2 2 20 0 1 2 2 2 2 2
STEP 3: Order of subtasksRow by row, left to right Base Case: Row 0, Column 0
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]Output of algorithm = S(m,n)
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
![Page 31: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/31.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
Example: x = A,C,G, T, A,G y = G,T, C,C,A,C LCS(x, y) = G,T, A A C T G G C T A G
GTGACAGTT
1 2 3 4 5 6 7 8 90123456789
0 0 0 1 1 1 1 1 10 0 1 1 1 1 2 2 20 0 1 2 2 2 2 2 3
STEP 3: Order of subtasksRow by row, left to right Base Case: Row 0, Column 0
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]Output of algorithm = S(m,n)
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
![Page 32: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/32.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
Example: x = A,C,G, T, A,G y = G,T, C,C,A,C LCS(x, y) = G,T, A A C T G G C T A G
GTGACAGTT
1 2 3 4 5 6 7 8 90123456789
0 0 0 1 1 1 1 1 10 0 1 1 1 1 2 2 20 0 1 2 2 2 2 2 3
STEP 3: Order of subtasksRow by row, left to right
1
Base Case: Row 0, Column 0
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]Output of algorithm = S(m,n)
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
![Page 33: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/33.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
Example: x = A,C,G, T, A,G y = G,T, C,C,A,C LCS(x, y) = G,T, A A C T G G C T A G
GTGACAGTT
1 2 3 4 5 6 7 8 90123456789
0 0 0 1 1 1 1 1 10 0 1 1 1 1 2 2 20 0 1 2 2 2 2 2 3
STEP 3: Order of subtasksRow by row, left to right
1 1
Base Case: Row 0, Column 0
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]Output of algorithm = S(m,n)
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
![Page 34: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/34.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
Example: x = A,C,G, T, A,G y = G,T, C,C,A,C LCS(x, y) = G,T, A A C T G G C T A G
GTGACAGTT
1 2 3 4 5 6 7 8 90123456789
0 0 0 1 1 1 1 1 10 0 1 1 1 1 2 2 20 0 1 2 2 2 2 2 3
STEP 3: Order of subtasksRow by row, left to right
1 1 1
Base Case: Row 0, Column 0
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]Output of algorithm = S(m,n)
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
![Page 35: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/35.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
Example: x = A,C,G, T, A,G y = G,T, C,C,A,C LCS(x, y) = G,T, A A C T G G C T A G
GTGACAGTT
1 2 3 4 5 6 7 8 90123456789
0 0 0 1 1 1 1 1 10 0 1 1 1 1 2 2 20 0 1 2 2 2 2 2 3
STEP 3: Order of subtasksRow by row, left to right
1 1 1 2
Base Case: Row 0, Column 0
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]Output of algorithm = S(m,n)
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
![Page 36: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/36.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
Example: x = A,C,G, T, A,G y = G,T, C,C,A,C LCS(x, y) = G,T, A A C T G G C T A G
GTGACAGTT
1 2 3 4 5 6 7 8 90123456789
0 0 0 1 1 1 1 1 10 0 1 1 1 1 2 2 20 0 1 2 2 2 2 2 3
STEP 3: Order of subtasksRow by row, left to right
1 1 1 2 2
Base Case: Row 0, Column 0
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]Output of algorithm = S(m,n)
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
![Page 37: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/37.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
Example: x = A,C,G, T, A,G y = G,T, C,C,A,C LCS(x, y) = G,T, A A C T G G C T A G
GTGACAGTT
1 2 3 4 5 6 7 8 90123456789
0 0 0 1 1 1 1 1 10 0 1 1 1 1 2 2 20 0 1 2 2 2 2 2 3
STEP 3: Order of subtasksRow by row, left to right
1 1 1 2 2 2
Base Case: Row 0, Column 0
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]Output of algorithm = S(m,n)
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
![Page 38: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/38.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
Example: x = A,C,G, T, A,G y = G,T, C,C,A,C LCS(x, y) = G,T, A A C T G G C T A G
GTGACAGTT
1 2 3 4 5 6 7 8 90123456789
0 0 0 1 1 1 1 1 10 0 1 1 1 1 2 2 20 0 1 2 2 2 2 2 3
STEP 3: Order of subtasksRow by row, left to right
1 1 1 2 2 2 2
Base Case: Row 0, Column 0
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]Output of algorithm = S(m,n)
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
![Page 39: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/39.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
Example: x = A,C,G, T, A,G y = G,T, C,C,A,C LCS(x, y) = G,T, A A C T G G C T A G
GTGACAGTT
1 2 3 4 5 6 7 8 90123456789
0 0 0 1 1 1 1 1 10 0 1 1 1 1 2 2 20 0 1 2 2 2 2 2 3
STEP 3: Order of subtasksRow by row, left to right
1 1 1 2 2 2 2 3
Base Case: Row 0, Column 0
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]Output of algorithm = S(m,n)
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
![Page 40: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/40.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
Example: x = A,C,G, T, A,G y = G,T, C,C,A,C LCS(x, y) = G,T, A A C T G G C T A G
GTGACAGTT
1 2 3 4 5 6 7 8 90123456789
0 0 0 1 1 1 1 1 10 0 1 1 1 1 2 2 20 0 1 2 2 2 2 2 3
STEP 3: Order of subtasksRow by row, left to right
1 1 1 2 2 2 2 3 3
Base Case: Row 0, Column 0
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]Output of algorithm = S(m,n)
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
![Page 41: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/41.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
for i = 0 to n: S[i,0] = 0for j = 0 to m: S[0,j] = 0for i = 1 to n: for j = 1 to m: if x[i] = y[j]: S[i,j] =
S[i-1,j-1] + 1 else: S[i,j] = max{
S[i-1,j], S[i,j-1]}return S[n,m]
Algorithm:STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]Output of algorithm = S(m,n)
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
STEP 3: Order of subtasksRow by row, left to right
![Page 42: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/42.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
for i = 0 to n: S[i,0] = 0for j = 0 to m: S[0,j] = 0for i = 1 to n: for j = 1 to m: if x[i] = y[j]: S[i,j] =
S[i-1,j-1] + 1 else: S[i,j] = max{
S[i-1,j], S[i,j-1]}return S[n,m]
Algorithm:
Running Time: O(mn)
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]Output of algorithm = S(m,n)
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
STEP 3: Order of subtasksRow by row, left to right
![Page 43: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/43.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
for i = 0 to n: S[i,0] = 0for j = 0 to m: S[0,j] = 0for i = 1 to n: for j = 1 to m: if x[i] = y[j]: S[i,j] =
S[i-1,j-1] + 1 else: S[i,j] = max{
S[i-1,j], S[i,j-1]}return S[n,m]
Algorithm:
Running Time: O(mn)
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]Output of algorithm = S(m,n)
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
STEP 3: Order of subtasksRow by row, left to right
How to reconstruct the actual subsequence?
![Page 44: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/44.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
To reconstruct LCS:Define L(i, j):L(i, j) = (i - 1, j - 1), if x[i] = y[j]
= (i - 1, j), ow if S(i-1,j) > S(i, j-1)= (i, j - 1), ow
Reconstruct LCS by following the L(i,j) pointers, starting with L(m,n)
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
0 0 0 1 1 1 1 1 10 0 1 1 1 1 2 2 20 0 1 2 2 2 2 2 31 1 1 2 2 2 2 3
S
L
Recall: Row 0 and column 0: Base Case
3
![Page 45: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/45.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
To reconstruct LCS:Define L(i, j):L(i, j) = (i - 1, j - 1), if x[i] = y[j]
= (i - 1, j), ow if S(i-1,j) > S(i, j-1)= (i, j - 1), ow
Reconstruct LCS by following the L(i,j) pointers, starting with L(m,n)
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
0 0 0 1 1 1 1 1 10 0 1 1 1 1 2 2 20 0 1 2 2 2 2 2 31 1 1 2 2 2 2 3
S
L
Recall: Row 0 and column 0: Base Case
3
![Page 46: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/46.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
To reconstruct LCS:Define L(i, j):L(i, j) = (i - 1, j - 1), if x[i] = y[j]
= (i - 1, j), ow if S(i-1,j) > S(i, j-1)= (i, j - 1), ow
Reconstruct LCS by following the L(i,j) pointers, starting with L(m,n)
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
0 0 0 1 1 1 1 1 10 0 1 1 1 1 2 2 20 0 1 2 2 2 2 2 31 1 1 2 2 2 2 3
S
L
Recall: Row 0 and column 0: Base Case
3
![Page 47: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/47.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
To reconstruct LCS:Define L(i, j):L(i, j) = (i - 1, j - 1), if x[i] = y[j]
= (i - 1, j), ow if S(i-1,j) > S(i, j-1)= (i, j - 1), ow
Reconstruct LCS by following the L(i,j) pointers, starting with L(m,n)
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
0 0 0 1 1 1 1 1 10 0 1 1 1 1 2 2 20 0 1 2 2 2 2 2 31 1 1 2 2 2 2 3
S
L
Recall: Row 0 and column 0: Base Case
3
![Page 48: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/48.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
To reconstruct LCS:Define L(i, j):L(i, j) = (i - 1, j - 1), if x[i] = y[j]
= (i - 1, j), ow if S(i-1,j) > S(i, j-1)= (i, j - 1), ow
Reconstruct LCS by following the L(i,j) pointers, starting with L(m,n)
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
0 0 0 1 1 1 1 1 10 0 1 1 1 1 2 2 20 0 1 2 2 2 2 2 31 1 1 2 2 2 2 3
S
L
Recall: Row 0 and column 0: Base Case
3
![Page 49: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/49.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
To reconstruct LCS:Define L(i, j):L(i, j) = (i - 1, j - 1), if x[i] = y[j]
= (i - 1, j), ow if S(i-1,j) > S(i, j-1)= (i, j - 1), ow
Reconstruct LCS by following the L(i,j) pointers, starting with L(m,n)
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
0 0 0 1 1 1 1 1 10 0 1 1 1 1 2 2 20 0 1 2 2 2 2 2 31 1 1 2 2 2 2 3
S
L
Recall: Row 0 and column 0: Base Case
3
![Page 50: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/50.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
To reconstruct LCS:Define L(i, j):L(i, j) = (i - 1, j - 1), if x[i] = y[j]
= (i - 1, j), ow if S(i-1,j) > S(i, j-1)= (i, j - 1), ow
Reconstruct LCS by following the L(i,j) pointers, starting with L(m,n)
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
0 0 0 1 1 1 1 1 10 0 1 1 1 1 2 2 20 0 1 2 2 2 2 2 31 1 1 2 2 2 2 3
S
L
Recall: Row 0 and column 0: Base Case
3
![Page 51: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/51.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
To reconstruct LCS:Define L(i, j):L(i, j) = (i - 1, j - 1), if x[i] = y[j]
= (i - 1, j), ow if S(i-1,j) > S(i, j-1)= (i, j - 1), ow
Reconstruct LCS by following the L(i,j) pointers, starting with L(m,n)
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
0 0 0 1 1 1 1 1 10 0 1 1 1 1 2 2 20 0 1 2 2 2 2 2 31 1 1 2 2 2 2 3
S
L
Recall: Row 0 and column 0: Base Case
3
![Page 52: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/52.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
To reconstruct LCS:Define L(i, j):L(i, j) = (i - 1, j - 1), if x[i] = y[j]
= (i - 1, j), ow if S(i-1,j) > S(i, j-1)= (i, j - 1), ow
Reconstruct LCS by following the L(i,j) pointers, starting with L(m,n)
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
0 0 0 1 1 1 1 1 10 0 1 1 1 1 2 2 20 0 1 2 2 2 2 2 31 1 1 2 2 2 2 3
S
L
Recall: Row 0 and column 0: Base Case
3
![Page 53: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/53.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
To reconstruct LCS:Define L(i, j):L(i, j) = (i - 1, j - 1), if x[i] = y[j]
= (i - 1, j), ow if S(i-1,j) > S(i, j-1)= (i, j - 1), ow
Reconstruct LCS by following the L(i,j) pointers, starting with L(m,n)
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
0 0 0 1 1 1 1 1 10 0 1 1 1 1 2 2 20 0 1 2 2 2 2 2 31 1 1 2 2 2 2 3
S
L
Recall: Row 0 and column 0: Base Case
3
![Page 54: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/54.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
To reconstruct LCS:Define L(i, j):L(i, j) = (i - 1, j - 1), if x[i] = y[j]
= (i - 1, j), ow if S(i-1,j) > S(i, j-1)= (i, j - 1), ow
Reconstruct LCS by following the L(i,j) pointers, starting with L(m,n)
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
0 0 0 1 1 1 1 1 10 0 1 1 1 1 2 2 20 0 1 2 2 2 2 2 31 1 1 2 2 2 2 3
S
L
Recall: Row 0 and column 0: Base Case
3
![Page 55: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/55.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
To reconstruct LCS:Define L(i, j):L(i, j) = (i - 1, j - 1), if x[i] = y[j]
= (i - 1, j), ow if S(i-1,j) > S(i, j-1)= (i, j - 1), ow
Reconstruct LCS by following the L(i,j) pointers, starting with L(m,n)
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
0 0 0 1 1 1 1 1 10 0 1 1 1 1 2 2 20 0 1 2 2 2 2 2 31 1 1 2 2 2 2 3
S
L
Recall: Row 0 and column 0: Base Case
3
![Page 56: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/56.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
To reconstruct LCS:Define L(i, j):L(i, j) = (i - 1, j - 1), if x[i] = y[j]
= (i - 1, j), ow if S(i-1,j) > S(i, j-1)= (i, j - 1), ow
Reconstruct LCS by following the L(i,j) pointers, starting with L(m,n)
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
0 0 0 1 1 1 1 1 10 0 1 1 1 1 2 2 20 0 1 2 2 2 2 2 31 1 1 2 2 2 2 3
S
L
Recall: Row 0 and column 0: Base Case
3
![Page 57: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/57.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
To reconstruct LCS:Define L(i, j):L(i, j) = (i - 1, j - 1), if x[i] = y[j]
= (i - 1, j), ow if S(i-1,j) > S(i, j-1)= (i, j - 1), ow
Reconstruct LCS by following the L(i,j) pointers, starting with L(m,n)
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
0 0 0 1 1 1 1 1 10 0 1 1 1 1 2 2 20 0 1 2 2 2 2 2 31 1 1 2 2 2 2 3
S
L
Recall: Row 0 and column 0: Base Case
3
![Page 58: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/58.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
To reconstruct LCS:Define L(i, j):L(i, j) = (i - 1, j - 1), if x[i] = y[j]
= (i - 1, j), ow if S(i-1,j) > S(i, j-1)= (i, j - 1), ow
Reconstruct LCS by following the L(i,j) pointers, starting with L(m,n)
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
0 0 0 1 1 1 1 1 10 0 1 1 1 1 2 2 20 0 1 2 2 2 2 2 31 1 1 2 2 2 2 3
S
L
Recall: Row 0 and column 0: Base Case
3
![Page 59: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/59.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
To reconstruct LCS:Define L(i, j):L(i, j) = (i - 1, j - 1), if x[i] = y[j]
= (i - 1, j), ow if S(i-1,j) > S(i, j-1)= (i, j - 1), ow
Reconstruct LCS by following the L(i,j) pointers, starting with L(m,n)
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
0 0 0 1 1 1 1 1 10 0 1 1 1 1 2 2 20 0 1 2 2 2 2 2 31 1 1 2 2 2 2 3
S
L
Recall: Row 0 and column 0: Base Case
3
![Page 60: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/60.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
To reconstruct LCS:Define L(i, j):L(i, j) = (i - 1, j - 1), if x[i] = y[j]
= (i - 1, j), ow if S(i-1,j) > S(i, j-1)= (i, j - 1), ow
Reconstruct LCS by following the L(i,j) pointers, starting with L(m,n)
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
0 0 0 1 1 1 1 1 10 0 1 1 1 1 2 2 20 0 1 2 2 2 2 2 31 1 1 2 2 2 2 3
S
L
Recall: Row 0 and column 0: Base Case
3
![Page 61: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/61.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
To reconstruct LCS:Define L(i, j):L(i, j) = (i - 1, j - 1), if x[i] = y[j]
= (i - 1, j), ow if S(i-1,j) > S(i, j-1)= (i, j - 1), ow
Reconstruct LCS by following the L(i,j) pointers, starting with L(m,n)
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
0 0 0 1 1 1 1 1 10 0 1 1 1 1 2 2 20 0 1 2 2 2 2 2 31 1 1 2 2 2 2 3
S
L
Recall: Row 0 and column 0: Base Case
3
![Page 62: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/62.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
To reconstruct LCS:Define L(i, j):L(i, j) = (i - 1, j - 1), if x[i] = y[j]
= (i - 1, j), ow if S(i-1,j) > S(i, j-1)= (i, j - 1), ow
Reconstruct LCS by following the L(i,j) pointers, starting with L(m,n)
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
0 0 0 1 1 1 1 1 10 0 1 1 1 1 2 2 20 0 1 2 2 2 2 2 31 1 1 2 2 2 2 3
S
L
Recall: Row 0 and column 0: Base Case
3
![Page 63: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/63.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
To reconstruct LCS:Define L(i, j):L(i, j) = (i - 1, j - 1), if x[i] = y[j]
= (i - 1, j), ow if S(i-1,j) > S(i, j-1)= (i, j - 1), ow
Reconstruct LCS by following the L(i,j) pointers, starting with L(m,n)
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
0 0 0 1 1 1 1 1 10 0 1 1 1 1 2 2 20 0 1 2 2 2 2 2 31 1 1 2 2 2 2 3
S
L
Recall: Row 0 and column 0: Base Case
3
![Page 64: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/64.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
To reconstruct LCS:Define L(i, j):L(i, j) = (i - 1, j - 1), if x[i] = y[j]
= (i - 1, j), ow if S(i-1,j) > S(i, j-1)= (i, j - 1), ow
Reconstruct LCS by following the L(i,j) pointers, starting with L(m,n)
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
0 0 0 1 1 1 1 1 10 0 1 1 1 1 2 2 20 0 1 2 2 2 2 2 31 1 1 2 2 2 2 3
S
L
Recall: Row 0 and column 0: Base Case
3
![Page 65: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/65.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
To reconstruct LCS:Define L(i, j):L(i, j) = (i - 1, j - 1), if x[i] = y[j]
= (i - 1, j), ow if S(i-1,j) > S(i, j-1)= (i, j - 1), ow
Reconstruct LCS by following the L(i,j) pointers, starting with L(m,n)
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
0 0 0 1 1 1 1 1 10 0 1 1 1 1 2 2 20 0 1 2 2 2 2 2 31 1 1 2 2 2 2 3
S
L
Recall: Row 0 and column 0: Base Case
3
![Page 66: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/66.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
To reconstruct LCS:Define L(i, j):L(i, j) = (i - 1, j - 1), if x[i] = y[j]
= (i - 1, j), ow if S(i-1,j) > S(i, j-1)= (i, j - 1), ow
Reconstruct LCS by following the L(i,j) pointers, starting with L(m,n)
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
0 0 0 1 1 1 1 1 10 0 1 1 1 1 2 2 20 0 1 2 2 2 2 2 31 1 1 2 2 2 2 3
S
L
Recall: Row 0 and column 0: Base Case
3
![Page 67: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/67.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
To reconstruct LCS:Define L(i, j):L(i, j) = (i - 1, j - 1), if x[i] = y[j]
= (i - 1, j), ow if S(i-1,j) > S(i, j-1)= (i, j - 1), ow
Reconstruct LCS by following the L(i,j) pointers, starting with L(m,n)
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
0 0 0 1 1 1 1 1 10 0 1 1 1 1 2 2 20 0 1 2 2 2 2 2 31 1 1 2 2 2 2 3
S
L
Recall: Row 0 and column 0: Base Case
3
![Page 68: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/68.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
To reconstruct LCS:Define L(i, j):L(i, j) = (i - 1, j - 1), if x[i] = y[j]
= (i - 1, j), ow if S(i-1,j) > S(i, j-1)= (i, j - 1), ow
Reconstruct LCS by following the L(i,j) pointers, starting with L(m,n)
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
A C T G G C T A G
GTGA
1 2 3 4 5 6 7 8 901234
0 0 0 1 1 1 1 1 10 0 1 1 1 1 2 2 20 0 1 2 2 2 2 2 31 1 1 2 2 2 2 3
S
L
Recall: Row 0 and column 0: Base Case
3
LCS = T, G, A
![Page 69: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/69.jpg)
Longest Common Subsequence (LCS)
Problem: Given two sequences x[1..m] and y[1..n], find their longest common subsequence
To reconstruct LCS:Define L(i, j):L(i, j) = (i - 1, j - 1), if x[i] = y[j]
= (i - 1, j), ow if S(i-1,j) > S(i, j-1)= (i, j - 1), ow
Reconstruct LCS by following the L(i,j) pointers, starting with L(m,n)
Running Time: O(mn)
STEP 1: Define subtasksS(i,j) = Length of LCS of x[1..i]
and y[1..j]Output of algorithm = S(m,n)
STEP 2: Express recursivelyS(i,j) = S(i-1,j-1) + 1, if x[i] = y[j] = max(S(i-1,j), S(i,j-1)), ow
STEP 3: Order of subtasksRow by row, left to right
![Page 70: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/70.jpg)
Dynamic Programming
• String Reconstruction
• Longest Common Subsequence
• ...
![Page 71: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/71.jpg)
Dynamic Programming vs Divide and Conquer
Divide-and-conquer
A problem of size n is decomposed into a few subproblems which are significantly smaller (e.g. n/2, 3n/4,...)
Therefore, size of subproblems decreases geometrically.eg. n, n/2, n/4, n/8, etc
Use a recursive algorithm.
Dynamic programming
A problem of size n is expressed in terms of subproblems that are not much smaller (e.g. n-1, n-2,...)
A recursive algorithm would take exp. time.
Saving grace: in total, there are only polynomially many subproblems.
Avoid recursion and instead solve the subproblems one-by-one, saving the answers in a table, in a clever explicit order.
![Page 72: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/72.jpg)
DP: Common Subtasks
Case 1: Input: x1, x2,..,xn Subproblem: x1, .., xi.
x1 x2 x3 x4 x5 x6 x7 x8 x9 x10
![Page 73: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/73.jpg)
DP: Common Subtasks
Case 1: Input: x1, x2,..,xn Subproblem: x1, .., xi.
Case 2: Input: x1, x2,..,xn and y1, y2,..,ym Subproblem: x1, .., xi and y1, y2,..,yj
x1 x2 x3 x4 x5 x6 x7 x8 x9 x10
x1 x2 x3 x4 x5 x6 x7 x8 x9 x10
y1 y2 y3 y4 y5 y6 y7 y8
![Page 74: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/74.jpg)
DP: Common Subtasks
Case 1: Input: x1, x2,..,xn Subproblem: x1, .., xi.
Case 2: Input: x1, x2,..,xn and y1, y2,..,ym Subproblem: x1, .., xi and y1, y2,..,yj
Case 3: Input: x1, x2,..,xn. Subproblem: xi, .., xj
x1 x2 x3 x4 x5 x6 x7 x8 x9 x10
x1 x2 x3 x4 x5 x6 x7 x8 x9 x10
y1 y2 y3 y4 y5 y6 y7 y8
x1 x2 x3 x4 x5 x6 x7 x8 x9 x10
![Page 75: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/75.jpg)
DP: Common Subtasks
Case 1: Input: x1, x2,..,xn Subproblem: x1, .., xi.
Case 2: Input: x1, x2,..,xn and y1, y2,..,ym Subproblem: x1, .., xi and y1, y2,..,yj
Case 3: Input: x1, x2,..,xn. Subproblem: xi, .., xj
Case 4: Input: a rooted tree. Subproblem: a subtree
x1 x2 x3 x4 x5 x6 x7 x8 x9 x10
x1 x2 x3 x4 x5 x6 x7 x8 x9 x10
y1 y2 y3 y4 y5 y6 y7 y8
x1 x2 x3 x4 x5 x6 x7 x8 x9 x10
![Page 76: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/76.jpg)
Dynamic Programming
• String Reconstruction
• Longest Common Subsequence
• Edit Distance
![Page 77: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/77.jpg)
Edit Distance: String Alignment
Alignment: Convert one string to another using insertions, deletions and substitutions.
S U N N - Y
S - N O W Y
Alignment 1Cost = 3
![Page 78: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/78.jpg)
Edit Distance: String Alignment
Alignment: Convert one string to another using insertions, deletions and substitutions.
S U N N - Y
S - N O W Y
S U N - - N
- S N O W -
Y
Y
Alignment 1 Alignment 2Cost = 3 Cost = 5
![Page 79: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/79.jpg)
Edit Distance: String Alignment
Alignment: Convert one string to another using insertions, deletions and substitutions.
S U N N - Y
S - N O W Y
S U N - - N
- S N O W -
Y
Y
Alignment 1 Alignment 2
Edit Distance(x, y): minimum # of insertions, deletions and substitutions required to convert x to y
Cost = 3 Cost = 5
![Page 80: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/80.jpg)
Edit Distance: String Alignment
Alignment: Convert one string to another using insertions, deletions and substitutions.
S U N N - Y
S - N O W Y
S U N - - N
- S N O W -
Y
Y
Alignment 1 Alignment 2
Edit Distance(x, y): minimum # of insertions, deletions and substitutions required to convert x to y Edit Distance(SUNNY, SNOWY) = 3
Cost = 3 Cost = 5
![Page 81: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/81.jpg)
Edit Distance: String Alignment
Alignment: Convert one string to another using insertions, deletions and substitutions.
S U N N - Y
S - N O W Y
S U N - - N
- S N O W -
Y
Y
Alignment 1 Alignment 2
Edit Distance(x, y): minimum # of insertions, deletions and substitutions required to convert x to y Edit Distance(SUNNY, SNOWY) = 3
Cost = 3 Cost = 5
Is Edit Distance(x, y) = Edit Distance(y, x)?
![Page 82: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/82.jpg)
Edit Distance
Problem: Given two strings x[1..n] and y[1..m], compute edit-distance(x, y)
S U N N - Y
S - N O W YCost = 3
Example:
Structure:Three cases in the last column of alignment between x[1..i] and y[1..j]:
-
x[i] -
y[j] y[j]
x[i]
Case 1. Align x[1..i-1] and y[1..j], delete x[i]Case 2. Align x[1..i] and y[1..j-1], insert y[j]Case 3. Align x[1..i-1] and y[1..j-1]. Substitute x[i], y[j] if different.
![Page 83: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/83.jpg)
Edit Distance
Problem: Given two strings x[1..n] and y[1..m], compute edit-distance(x, y)
STEP 1: Define subtasksE(i,j) = Edit-distance(x[1..i], y[1..j])Output of algorithm = E(n,m)
STEP 2: Express recursivelyE(i,j) = min(E(i-1,j) + 1, E(i, j-1) + 1,
E(i-1,j-1) + diff(x[i], y[j]))
STEP 3: Order of subtasksRow by row, left to right
Example: x = SUNNY y = SNOWY
Edit-distance(x, y) = 3S U N N Y
SNOWY
0 1 2 3 4 5-012345
![Page 84: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/84.jpg)
Edit Distance
Problem: Given two strings x[1..n] and y[1..m], compute edit-distance(x, y)
STEP 1: Define subtasksE(i,j) = Edit-distance(x[1..i], y[1..j])Output of algorithm = E(n,m)
STEP 3: Order of subtasksRow by row, left to right
Example: x = SUNNY y = SNOWY
Edit-distance(x, y) = 3S U N N Y
SNOWY
0 1 2 3 4 5-012345
0 1 2 3 4 512345
STEP 2: Express recursivelyE(i,j) = min(E(i-1,j) + 1, E(i, j-1) + 1,
E(i-1,j-1) + diff(x[i], y[j]))
![Page 85: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/85.jpg)
Edit Distance
Problem: Given two strings x[1..n] and y[1..m], compute edit-distance(x, y)
STEP 1: Define subtasksE(i,j) = Edit-distance(x[1..i], y[1..j])Output of algorithm = E(n,m)
STEP 3: Order of subtasksRow by row, left to right
Example: x = SUNNY y = SNOWY
Edit-distance(x, y) = 3S U N N Y
SNOWY
0 1 2 3 4 5-012345
0 1 2 3 4 512345
0
STEP 2: Express recursivelyE(i,j) = min(E(i-1,j) + 1, E(i, j-1) + 1,
E(i-1,j-1) + diff(x[i], y[j]))
![Page 86: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/86.jpg)
Edit Distance
Problem: Given two strings x[1..n] and y[1..m], compute edit-distance(x, y)
STEP 1: Define subtasksE(i,j) = Edit-distance(x[1..i], y[1..j])Output of algorithm = E(n,m)
STEP 3: Order of subtasksRow by row, left to right
Example: x = SUNNY y = SNOWY
Edit-distance(x, y) = 3S U N N Y
SNOWY
0 1 2 3 4 5-012345
0 1 2 3 4 512345
0 1
STEP 2: Express recursivelyE(i,j) = min(E(i-1,j) + 1, E(i, j-1) + 1,
E(i-1,j-1) + diff(x[i], y[j]))
![Page 87: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/87.jpg)
Edit Distance
Problem: Given two strings x[1..n] and y[1..m], compute edit-distance(x, y)
STEP 1: Define subtasksE(i,j) = Edit-distance(x[1..i], y[1..j])Output of algorithm = E(n,m)
STEP 3: Order of subtasksRow by row, left to right
Example: x = SUNNY y = SNOWY
Edit-distance(x, y) = 3S U N N Y
SNOWY
0 1 2 3 4 5-012345
0 1 2 3 4 512345
0 1 2
STEP 2: Express recursivelyE(i,j) = min(E(i-1,j) + 1, E(i, j-1) + 1,
E(i-1,j-1) + diff(x[i], y[j]))
![Page 88: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/88.jpg)
Edit Distance
Problem: Given two strings x[1..n] and y[1..m], compute edit-distance(x, y)
STEP 1: Define subtasksE(i,j) = Edit-distance(x[1..i], y[1..j])Output of algorithm = E(n,m)
STEP 3: Order of subtasksRow by row, left to right
Example: x = SUNNY y = SNOWY
Edit-distance(x, y) = 3S U N N Y
SNOWY
0 1 2 3 4 5-012345
0 1 2 3 4 512345
0 1 2 3
STEP 2: Express recursivelyE(i,j) = min(E(i-1,j) + 1, E(i, j-1) + 1,
E(i-1,j-1) + diff(x[i], y[j]))
![Page 89: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/89.jpg)
Edit Distance
Problem: Given two strings x[1..n] and y[1..m], compute edit-distance(x, y)
STEP 1: Define subtasksE(i,j) = Edit-distance(x[1..i], y[1..j])Output of algorithm = E(n,m)
STEP 3: Order of subtasksRow by row, left to right
Example: x = SUNNY y = SNOWY
Edit-distance(x, y) = 3S U N N Y
SNOWY
0 1 2 3 4 5-012345
0 1 2 3 4 512345
0 1 2 3 4
STEP 2: Express recursivelyE(i,j) = min(E(i-1,j) + 1, E(i, j-1) + 1,
E(i-1,j-1) + diff(x[i], y[j]))
![Page 90: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/90.jpg)
Edit Distance
Problem: Given two strings x[1..n] and y[1..m], compute edit-distance(x, y)
STEP 1: Define subtasksE(i,j) = Edit-distance(x[1..i], y[1..j])Output of algorithm = E(n,m)
STEP 3: Order of subtasksRow by row, left to right
Example: x = SUNNY y = SNOWY
Edit-distance(x, y) = 3S U N N Y
SNOWY
0 1 2 3 4 5-012345
0 1 2 3 4 512345
0 1 2 3 41
STEP 2: Express recursivelyE(i,j) = min(E(i-1,j) + 1, E(i, j-1) + 1,
E(i-1,j-1) + diff(x[i], y[j]))
![Page 91: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/91.jpg)
Edit Distance
Problem: Given two strings x[1..n] and y[1..m], compute edit-distance(x, y)
STEP 1: Define subtasksE(i,j) = Edit-distance(x[1..i], y[1..j])Output of algorithm = E(n,m)
STEP 3: Order of subtasksRow by row, left to right
Example: x = SUNNY y = SNOWY
Edit-distance(x, y) = 3S U N N Y
SNOWY
0 1 2 3 4 5-012345
0 1 2 3 4 512345
0 1 2 3 41 1
STEP 2: Express recursivelyE(i,j) = min(E(i-1,j) + 1, E(i, j-1) + 1,
E(i-1,j-1) + diff(x[i], y[j]))
![Page 92: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/92.jpg)
Edit Distance
Problem: Given two strings x[1..n] and y[1..m], compute edit-distance(x, y)
STEP 1: Define subtasksE(i,j) = Edit-distance(x[1..i], y[1..j])Output of algorithm = E(n,m)
STEP 3: Order of subtasksRow by row, left to right
Example: x = SUNNY y = SNOWY
Edit-distance(x, y) = 3S U N N Y
SNOWY
0 1 2 3 4 5-012345
0 1 2 3 4 512345
0 1 2 3 41 1 1
STEP 2: Express recursivelyE(i,j) = min(E(i-1,j) + 1, E(i, j-1) + 1,
E(i-1,j-1) + diff(x[i], y[j]))
![Page 93: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/93.jpg)
Edit Distance
Problem: Given two strings x[1..n] and y[1..m], compute edit-distance(x, y)
STEP 1: Define subtasksE(i,j) = Edit-distance(x[1..i], y[1..j])Output of algorithm = E(n,m)
STEP 3: Order of subtasksRow by row, left to right
Example: x = SUNNY y = SNOWY
Edit-distance(x, y) = 3S U N N Y
SNOWY
0 1 2 3 4 5-012345
0 1 2 3 4 512345
0 1 2 3 41 1 1 2
STEP 2: Express recursivelyE(i,j) = min(E(i-1,j) + 1, E(i, j-1) + 1,
E(i-1,j-1) + diff(x[i], y[j]))
![Page 94: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/94.jpg)
Edit Distance
Problem: Given two strings x[1..n] and y[1..m], compute edit-distance(x, y)
STEP 1: Define subtasksE(i,j) = Edit-distance(x[1..i], y[1..j])Output of algorithm = E(n,m)
STEP 3: Order of subtasksRow by row, left to right
Example: x = SUNNY y = SNOWY
Edit-distance(x, y) = 3S U N N Y
SNOWY
0 1 2 3 4 5-012345
0 1 2 3 4 512345
0 1 2 3 41 1 1 2 3
STEP 2: Express recursivelyE(i,j) = min(E(i-1,j) + 1, E(i, j-1) + 1,
E(i-1,j-1) + diff(x[i], y[j]))
![Page 95: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/95.jpg)
Edit Distance
Problem: Given two strings x[1..n] and y[1..m], compute edit-distance(x, y)
STEP 1: Define subtasksE(i,j) = Edit-distance(x[1..i], y[1..j])Output of algorithm = E(n,m)
STEP 3: Order of subtasksRow by row, left to right
Example: x = SUNNY y = SNOWY
Edit-distance(x, y) = 3S U N N Y
SNOWY
0 1 2 3 4 5-012345
0 1 2 3 4 512345
0 1 2 3 41 1 1 2 3
STEP 2: Express recursivelyE(i,j) = min(E(i-1,j) + 1, E(i, j-1) + 1,
E(i-1,j-1) + diff(x[i], y[j])) Running Time = O(mn)How to reconstruct thebest alignment?
![Page 96: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/96.jpg)
Dynamic Programming
• String Reconstruction
• Longest Common Subsequence
• Edit Distance
• Subset Sum
![Page 97: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/97.jpg)
Subset Sum
Problem: Given a list of positive integers a[1..n] and an integer t, is there some subset of a that sums to exactly t?
Example: a = [ 12, 1, 3, 8, 20, 50 ]
![Page 98: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/98.jpg)
Subset Sum
Problem: Given a list of positive integers a[1..n] and an integer t, is there some subset of a that sums to exactly t?
Example: a = [ 12, 1, 3, 8, 20, 50 ] t = 14 t = 44
![Page 99: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/99.jpg)
Subset Sum
Problem: Given a list of positive integers a[1..n] and an integer t, is there some subset of a that sums to exactly t?
Example: a = [ 12, 1, 3, 8, 20, 50 ] t = 14 Falset = 44 True
![Page 100: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/100.jpg)
Subset Sum
Problem: Given a list of positive integers a[1..n] and an integer t, is there some subset of a that sums to exactly t?
Example: a = [ 12, 1, 3, 8, 20, 50 ] t = 14 Falset = 44 True
Structure:
If a[1...i] has a subset T that sums to s, then:Either a[i] is not in T: a[1...i-1] has a subset that sums to sOr a[i] is in T: a[1...i-1] has a subset that sums to s - a[i]
![Page 101: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/101.jpg)
Subset Sum
Problem: Given a list of positive integers a[1..n] and an integer t, is there some subset of a that sums to exactly t?
Example: a = [ 12, 1, 3, 8, 20, 50 ]
STEP 1: Define subtasksFor i=1..n, s=1..t,S(i,s) = True, if some subset of S[1..i]
adds to s = False, ow
Output = S(n, t)
t = 14 Falset = 44 True
0 1 2 3 4 5 6 7 80123456
9
s
i
![Page 102: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/102.jpg)
Subset Sum
Problem: Given a list of positive integers a[1..n] and an integer t, is there some subset of a that sums to exactly t?
Example: a = [ 12, 1, 3, 8, 20, 50 ]
STEP 1: Define subtasksFor i=1..n, s=1..t,S(i,s) = True, if some subset of S[1..i]
adds to s = False, ow
Output = S(n, t)
≤STEP 2: Express recursivelyIf a[i] s,
S(i,s) = S(i - 1, s - a[i]) OR S(i - 1, s)Else: S(i, s) = S(i - 1, s)
t = 14 Falset = 44 True
0 1 2 3 4 5 6 7 80123456
9
s
i
![Page 103: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/103.jpg)
Subset Sum
Problem: Given a list of positive integers a[1..n] and an integer t, is there some subset of a that sums to exactly t?
Example: a = [ 12, 1, 3, 8, 20, 50 ]
STEP 1: Define subtasksFor i=1..n, s=1..t,S(i,s) = True, if some subset of S[1..i]
adds to s = False, ow
Output = S(n, t)
≤STEP 2: Express recursivelyIf a[i] s,
S(i,s) = S(i - 1, s - a[i]) OR S(i - 1, s)Else: S(i, s) = S(i - 1, s)
t = 14 Falset = 44 True
STEP 3: Order of subtasksRow by row, increasing column
0 1 2 3 4 5 6 7 80123456
9
s
i
![Page 104: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/104.jpg)
Subset Sum
Problem: Given a list of positive integers a[1..n] and an integer t, is there some subset of a that sums to exactly t?
Example: a = [ 12, 1, 3, 8, 20, 50 ]
STEP 1: Define subtasksFor i=1..n, s=1..t,S(i,s) = True, if some subset of S[1..i]
adds to s = False, ow
Output = S(n, t)
≤STEP 2: Express recursivelyIf a[i] s,
S(i,s) = S(i - 1, s - a[i]) OR S(i - 1, s)Else: S(i, s) = S(i - 1, s)
t = 14 Falset = 44 True
STEP 3: Order of subtasksRow by row, increasing column
0 1 2 3 4 5 6 7 80123456
9
s
i
Base Case:Column 0: all TrueRow 0: all False except (0, 0) entry
![Page 105: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/105.jpg)
Subset Sum
Problem: Given a list of positive integers a[1..n] and an integer t, is there some subset of a that sums to exactly t?
Example: a = [ 12, 1, 3, 8, 20, 50 ]
STEP 1: Define subtasksFor i=1..n, s=1..t,S(i,s) = True, if some subset of S[1..i]
adds to s = False, ow
Output = S(n, t)
≤STEP 2: Express recursivelyIf a[i] s,
S(i,s) = S(i - 1, s - a[i]) OR S(i - 1, s)Else: S(i, s) = S(i - 1, s)
t = 14 Falset = 44 True
STEP 3: Order of subtasksRow by row, increasing column
0 1 2 3 4 5 6 7 8012345
TTTT
6
9
s
i
F FF FFF F FF
Base Case:Column 0: all TrueRow 0: all False except (0, 0) entry
![Page 106: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/106.jpg)
Subset Sum
Problem: Given a list of positive integers a[1..n] and an integer t, is there some subset of a that sums to exactly t?
Example: a = [ 12, 1, 3, 8, 20, 50 ]
STEP 1: Define subtasksFor i=1..n, s=1..t,S(i,s) = True, if some subset of S[1..i]
adds to s = False, ow
Output = S(n, t)
≤STEP 2: Express recursivelyIf a[i] s,
S(i,s) = S(i - 1, s - a[i]) OR S(i - 1, s)Else: S(i, s) = S(i - 1, s)
t = 14 Falset = 44 True
STEP 3: Order of subtasksRow by row, increasing column
0 1 2 3 4 5 6 7 8012345
TTTT
6
9F F F F F F F F FF
s
i
Base Case:Column 0: all TrueRow 0: all False except (0, 0) entry
![Page 107: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/107.jpg)
Subset Sum
Problem: Given a list of positive integers a[1..n] and an integer t, is there some subset of a that sums to exactly t?
Example: a = [ 12, 1, 3, 8, 20, 50 ]
STEP 1: Define subtasksFor i=1..n, s=1..t,S(i,s) = True, if some subset of S[1..i]
adds to s = False, ow
Output = S(n, t)
≤STEP 2: Express recursivelyIf a[i] s,
S(i,s) = S(i - 1, s - a[i]) OR S(i - 1, s)Else: S(i, s) = S(i - 1, s)
t = 14 Falset = 44 True
STEP 3: Order of subtasksRow by row, increasing column
0 1 2 3 4 5 6 7 8012345
TTTT
6
9F F F F F F F F FF F F F F F F F F
s
i
Base Case:Column 0: all TrueRow 0: all False except (0, 0) entry
![Page 108: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/108.jpg)
Subset Sum
Problem: Given a list of positive integers a[1..n] and an integer t, is there some subset of a that sums to exactly t?
Example: a = [ 12, 1, 3, 8, 20, 50 ]
STEP 1: Define subtasksFor i=1..n, s=1..t,S(i,s) = True, if some subset of S[1..i]
adds to s = False, ow
Output = S(n, t)
≤STEP 2: Express recursivelyIf a[i] s,
S(i,s) = S(i - 1, s - a[i]) OR S(i - 1, s)Else: S(i, s) = S(i - 1, s)
t = 14 Falset = 44 True
STEP 3: Order of subtasksRow by row, increasing column
0 1 2 3 4 5 6 7 8012345
TTTT
6
9F F F F F F F F FF F F F F F F F FT
s
i
Base Case:Column 0: all TrueRow 0: all False except (0, 0) entry
![Page 109: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/109.jpg)
Subset Sum
Problem: Given a list of positive integers a[1..n] and an integer t, is there some subset of a that sums to exactly t?
Example: a = [ 12, 1, 3, 8, 20, 50 ]
STEP 1: Define subtasksFor i=1..n, s=1..t,S(i,s) = True, if some subset of S[1..i]
adds to s = False, ow
Output = S(n, t)
≤STEP 2: Express recursivelyIf a[i] s,
S(i,s) = S(i - 1, s - a[i]) OR S(i - 1, s)Else: S(i, s) = S(i - 1, s)
t = 14 Falset = 44 True
STEP 3: Order of subtasksRow by row, increasing column
0 1 2 3 4 5 6 7 8012345
TTTT
6
9F F F F F F F F FF F F F F F F F FT F F F F F F F F
s
i
Base Case:Column 0: all TrueRow 0: all False except (0, 0) entry
![Page 110: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/110.jpg)
Subset Sum
Problem: Given a list of positive integers a[1..n] and an integer t, is there some subset of a that sums to exactly t?
Example: a = [ 12, 1, 3, 8, 20, 50 ]
STEP 1: Define subtasksFor i=1..n, s=1..t,S(i,s) = True, if some subset of S[1..i]
adds to s = False, ow
Output = S(n, t)
≤STEP 2: Express recursivelyIf a[i] s,
S(i,s) = S(i - 1, s - a[i]) OR S(i - 1, s)Else: S(i, s) = S(i - 1, s)
t = 14 Falset = 44 True
STEP 3: Order of subtasksRow by row, increasing column
0 1 2 3 4 5 6 7 8012345
TTTT
6
9F F F F F F F F FF F F F F F F F FT F F F F F F F FT
s
i
Base Case:Column 0: all TrueRow 0: all False except (0, 0) entry
![Page 111: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/111.jpg)
Subset Sum
Problem: Given a list of positive integers a[1..n] and an integer t, is there some subset of a that sums to exactly t?
Example: a = [ 12, 1, 3, 8, 20, 50 ]
STEP 1: Define subtasksFor i=1..n, s=1..t,S(i,s) = True, if some subset of S[1..i]
adds to s = False, ow
Output = S(n, t)
≤STEP 2: Express recursivelyIf a[i] s,
S(i,s) = S(i - 1, s - a[i]) OR S(i - 1, s)Else: S(i, s) = S(i - 1, s)
t = 14 Falset = 44 True
STEP 3: Order of subtasksRow by row, increasing column
0 1 2 3 4 5 6 7 8012345
TTTT
6
9F F F F F F F F FF F F F F F F F FT F F F F F F F FT F
s
i
Base Case:Column 0: all TrueRow 0: all False except (0, 0) entry
![Page 112: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/112.jpg)
Subset Sum
Problem: Given a list of positive integers a[1..n] and an integer t, is there some subset of a that sums to exactly t?
Example: a = [ 12, 1, 3, 8, 20, 50 ]
STEP 1: Define subtasksFor i=1..n, s=1..t,S(i,s) = True, if some subset of S[1..i]
adds to s = False, ow
Output = S(n, t)
≤STEP 2: Express recursivelyIf a[i] s,
S(i,s) = S(i - 1, s - a[i]) OR S(i - 1, s)Else: S(i, s) = S(i - 1, s)
t = 14 Falset = 44 True
STEP 3: Order of subtasksRow by row, increasing column
0 1 2 3 4 5 6 7 8012345
TTTT
6
9F F F F F F F F FF F F F F F F F FT F F F F F F F FT F T
s
i
Base Case:Column 0: all TrueRow 0: all False except (0, 0) entry
![Page 113: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/113.jpg)
Subset Sum
Problem: Given a list of positive integers a[1..n] and an integer t, is there some subset of a that sums to exactly t?
Example: a = [ 12, 1, 3, 8, 20, 50 ]
STEP 1: Define subtasksFor i=1..n, s=1..t,S(i,s) = True, if some subset of S[1..i]
adds to s = False, ow
Output = S(n, t)
≤STEP 2: Express recursivelyIf a[i] s,
S(i,s) = S(i - 1, s - a[i]) OR S(i - 1, s)Else: S(i, s) = S(i - 1, s)
t = 14 Falset = 44 True
STEP 3: Order of subtasksRow by row, increasing column
0 1 2 3 4 5 6 7 8012345
TTTT
6
9F F F F F F F F FF F F F F F F F FT F F F F F F F FT F T T
s
i
Base Case:Column 0: all TrueRow 0: all False except (0, 0) entry
![Page 114: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/114.jpg)
Subset Sum
Problem: Given a list of positive integers a[1..n] and an integer t, is there some subset of a that sums to exactly t?
Example: a = [ 12, 1, 3, 8, 20, 50 ]
STEP 1: Define subtasksFor i=1..n, s=1..t,S(i,s) = True, if some subset of S[1..i]
adds to s = False, ow
Output = S(n, t)
≤STEP 2: Express recursivelyIf a[i] s,
S(i,s) = S(i - 1, s - a[i]) OR S(i - 1, s)Else: S(i, s) = S(i - 1, s)
t = 14 Falset = 44 True
STEP 3: Order of subtasksRow by row, increasing column
0 1 2 3 4 5 6 7 8012345
TTTT
6
9F F F F F F F F FF F F F F F F F FT F F F F F F F FT F T T F F F F F
s
i
Base Case:Column 0: all TrueRow 0: all False except (0, 0) entry
![Page 115: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/115.jpg)
Subset Sum
Problem: Given a list of positive integers a[1..n] and an integer t, is there some subset of a that sums to exactly t?
Example: a = [ 12, 1, 3, 8, 20, 50 ]
STEP 1: Define subtasksFor i=1..n, s=1..t,S(i,s) = True, if some subset of S[1..i]
adds to s = False, ow
Output = S(n, t)
≤STEP 2: Express recursivelyIf a[i] s,
S(i,s) = S(i - 1, s - a[i]) OR S(i - 1, s)Else: S(i, s) = S(i - 1, s)
t = 14 Falset = 44 True
STEP 3: Order of subtasksRow by row, increasing column
0 1 2 3 4 5 6 7 8012345
TTTT
6
9F F F F F F F F FF F F F F F F F FT F F F F F F F FT F T T F F F F F
s
i
Running Time = O(nt)How to reconstruct the subset?
![Page 116: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/116.jpg)
Subset Sum
Problem: Given a list of positive integers a[1..n] and an integer t, is there some subset of a that sums to exactly t?
STEP 1: Define subtasksFor i=1..n, s=1..t,S(i,s) = True, if some subset of S[1..i]
adds to s = False, ow
Output = S(n, t)
≤STEP 2: Express recursivelyIf a[i] s,
S(i,s) = S(i - 1, s - a[i]) OR S(i - 1, s)Else: S(i, s) = S(i - 1, s)
STEP 3: Order of subtasksRow by row, increasing column
Reconstruct the subset by following the pointers from D(n,t)
Reconstructing the subset:Define an array D(i, s). If S(i, s) = True, and S(i-1,s-a[i]) = True
D(i, s) = (i - 1, s - a[i]) = (i -1, s) ow.
Running Time = O(nt)
![Page 117: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/117.jpg)
Dynamic Programming
• String Reconstruction
• Longest Common Subsequence
• Edit Distance
• Subset Sum
• Independent Set in a Tree
![Page 118: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/118.jpg)
Independent Set
Independent Set: Given a graph G = (V, E), a subset of vertices S is an independent set if there are no edges between them
Max Independent Set Problem: Given a graph G = (V, E), find thelargest independent set in G
Max Independent Set is a notoriously hard problem!We will look at a restricted case, when G is a tree
![Page 119: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/119.jpg)
Max. Independent Set in a Tree
A set of nodes is an independent set if there are no edges between the nodes
u
Two Cases at node u:1. Don’t include u2. Include u, and don’t include its children
![Page 120: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/120.jpg)
Max. Independent Set in a Tree
A set of nodes is an independent set if there are no edges between the nodes
STEP 1: Define subtaskI(u) = size of largest independent set in subtree rooted at uWe want I(r), where r = root
STEP 3: Order of subtasksReverse order of distance from root; use BFS!
Base case: for leaf nodes, I(u) = 1
STEP 2: Express recursively
Two Cases at node u:1. Don’t include u2. Include u, and don’t include its children
l(u) =
u
![Page 121: CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming Main Steps: 1. Divide the problem into subtasks 2. Define the subtasks recursively](https://reader035.vdocument.in/reader035/viewer/2022071513/6134c04cdfd10f4dd73bee54/html5/thumbnails/121.jpg)
Max. Independent Set in a Tree
A set of nodes is an independent set if there are no edges between the nodes
STEP 1: Define subtaskI(u) = size of largest independent set in subtree rooted at uWe want I(r), where r = root
STEP 3: Order of subtasksReverse order of distance from root; use BFS!
Base case: for leaf nodes, I(u) = 1
STEP 2: Express recursively
Running Time: O(n)Edge (u, v) is examined in Step 2 at most twice:
(1) v is a child of u(2) v is a grandchild of u’s parent
There are n-1 edges in a tree on n nodes
l(u) =
u