CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming...

121
CSE 202: Design and Analysis of Algorithms Lecture 6 Instructor: Kamalika Chaudhuri

Transcript of CSE 202: Design and Analysis of Algorithms · 2011. 4. 14. · Three steps of Dynamic Programming...

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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