CSE 202: Design and Analysis of Algorithms
Lecture 5
Instructor: Kamalika Chaudhuri
Announcement
• Homework 1 is due Wed April 13 in class
• A hint on Problem 3 is up on the class website
• Midterm is on May 4th, in class
Last Class: Review
• Greedy Algorithm
• Divide and Conquer
• Integer multiplication
• Strassen’s algorithm for matrix multiplication
• Closest pair of points on the plane
Closest Pair of Points on a Plane
Given a set P of n points on the plane, find the two points p and q in P s.t d(p, q) is minimum
p q
Closest Pair of Points on a Plane
Given a set P of n points on the plane, find the two points p and q in P s.t d(p, q) is minimum
Naive solution = O(n2)
p q
What about one dimension?
Given a set P of n points on the line, find the two points p and q in P s.t d(p, q) is minimum
p q
What about one dimension?
Given a set P of n points on the line, find the two points p and q in P s.t d(p, q) is minimum
p q
Property: The closest points are adjacent in sorted order
What about one dimension?
Given a set P of n points on the line, find the two points p and q in P s.t d(p, q) is minimum
Algorithm 11. Sort the points 2. Find a point pi in the sorted set s.t d(pi, pi+1) is minimum
p q
Property: The closest points are adjacent in sorted order
What about one dimension?
Given a set P of n points on the line, find the two points p and q in P s.t d(p, q) is minimum
Algorithm 11. Sort the points 2. Find a point pi in the sorted set s.t d(pi, pi+1) is minimum
p q
Property: The closest points are adjacent in sorted order
Running Time = O(n log n)
Does this work in 2D?
p
q
a
b (a, b) : closest in x-coordinate
(a, q) : closest in y-coordinate
(p, q) : closest
Sorting the points by x or y coordinate, and looking at adjacent pairs does not work!
A Divide and Conquer Approach
Find-Closest-Pair(Q):
1. Find the median x coordinate lx2. L = Points to the left of lx3. R = Rest of the points4. Find-closest-pair(L)5. Find-closest-pair(R)6. Combine the results
How to combine?
lx
RL
Problem Case: p in L, q in R (or vice versa)
A Divide and Conquer Approach
Find-Closest-Pair(Q):
1. Find the median x coordinate lx2. L = Points to the left of lx3. R = Rest of the points4. Find-closest-pair(L)5. Find-closest-pair(R)6. Combine the results
How to combine?
1. Naive combination
2. Can we do better?
Time = O(n2)
lx
RL
A Property of Closest Points
Let: t1 = min x, y in L d(x, y)t2 = min x, y in R d(x, y)t = min (t1, t2)
If d(a, b) < t, where a is in L, b in R, then, a and b lie within distance t of lx
RL
lx
A Property of Closest Points
Let: t1 = min x, y in L d(x, y)t2 = min x, y in R d(x, y)t = min (t1, t2)
If d(a, b) < t, where a is in L, b in R, then, a and b lie within distance t of lx
lx
RL>ta
A Property of Closest Points
Let: t1 = min x, y in L d(x, y)t2 = min x, y in R d(x, y)t = min (t1, t2)
If d(a, b) < t, where a is in L, b in R, then, a and b lie within distance t of lx
lx
RL>t
>t
a
b
A Property of Closest Points
Let: t1 = min x, y in L d(x, y)t2 = min x, y in R d(x, y)t = min (t1, t2)
If d(a, b) < t, where a is in L, b in R, then, a and b lie within distance t of lx
lx
RL>t
>t
a
b
t tWe can safely rule out all points in the grey regions!
Another Property
Let: t1 = min x, y in L d(x, y)t2 = min x, y in R d(x, y)t = min (t1, t2)Sy = all points within distance t of lx in sorted order of y coords
If d(a, b) < t, a in L, b in R, then a and b occur within 15 positions of each other in Sy
lx
t/2
t/2
t
t1L
lx
t2 R
If d(a,b) < t, a in L, b in R, a, b are > 15 positions apart
Fact: Each box in figure can contain at most one point.
then, there are >= 3 rows between a and b
Boxes
Thus, d(a, b) >= 3t/2. Contradiction!
a
b
A Divide and Conquer Approach
Find-Closest-Pair(Q):
1. Find the median x coordinate lx2. L = Points to the left of lx3. R = Rest of the points4. (a1, b1, t1) = Find-closest-pair(L)5. (a2, b2, t2) = Find-closest-pair(R)6. t = min(t1, t2)7. Combine the results
How to combine?
lx
RLS = points in Q
within distance t of Ix
Sy = sort S by y coordsFor p in Sy
Find distance from pto next 15 pointsLet (a, b) be the pair withmin such distanceIf d(a, b) < t:
Return (a, b, d(a, b))Else if t1 < t2:
Return (a1, b1, t1)Else:
Return (a2, b2, t2)
Property: If a in L, b in R, and if d(a, b) < t, then a and b occur within 15 positions of each other in Sy
A Divide and Conquer Approach
Find-Closest-Pair(Qx, Qy):
1. Find the median x coordinate lx2. L = Points to the left of lx3. R = Rest of the points4. (a1, b1, t1) = Find-closest-pair(Lx, Ly)5. (a2, b2, t2) = Find-closest-pair(Rx, Ry)6. t = min(t1, t2)7. Combine the results
How to combine?
S = points in Q within distance t of Ix
Sy = sort S by y coordsFor p in Sy
Find distance from pto next 15 pointsLet (a, b) be the pair withmin such distanceIf d(a, b) < t:
Return (a, b, d(a, b))Else if t1 < t2:
Return (a1, b1, t1)Else:
Return (a2, b2, t2)
Property: If a in L, b in R, and if d(a, b) < t, then a and b occur within 15 positions of each other in Sy
Implementing Divide + Combine:1. Maintain sorted lists of the input points:
Px = sorted by x, Py = sorted by y2. Computing Lx,Ly,Rx, Ry from Qx, Qy: O(|Q|) time2. Computing Sy from Qy : O(|S|) time3. Combination procedure: O(|Sy|) time
A Divide and Conquer Approach
Find-Closest-Pair(Qx, Qy):
1. Find the median x coordinate lx2. L = Points to the left of lx3. R = Rest of the points4. (a1, b1, t1) = Find-closest-pair(Lx, Ly)5. (a2, b2, t2) = Find-closest-pair(Rx, Ry)6. t = min(t1, t2)7. Combine the results
How to combine?
S = points in Q within distance t of Ix
Sy = sort S by y coordsFor p in Sy
Find distance from pto next 15 pointsLet (a, b) be the pair withmin such distanceIf d(a, b) < t:
Return (a, b, d(a, b))Else if t1 < t2:
Return (a1, b1, t1)Else:
Return (a2, b2, t2)
Property: If a in L, b in R, and if d(a, b) < t, then a and b occur within 15 positions of each other in Sy
Implementing Divide + Combine:
T(n) = 2 T(n/2) + cnT(n) = (n log n)Θ
Summary: Closest Pair of Points
Given a set P of n points on the plane, find the two points p and q in P s.t d(p, q) is minimum
Find-Closest-Pair(Qx,Qy):
1. Find the median x coordinate lx2. L = Points to the left of lx3. R = Rest of the points4. (a1, b1, t1) = Find-closest-pair(Lx, Ly)5. (a2, b2, t2) = Find-closest-pair(Rx, Ry)6. t = min(t1, t2)7. Let Sy = points within distance t of Ix sorted by y 8. For each p in Sy
Find distance from p to next 15 points in Sy
Let (a, b) be the closest such pair9. Report the closest pair out of:
(a, b), (a1, b1), (a2, b2)
Recurrence:
T(n) = 2T(n/2) + cnT(n) = O(n log n)
What is a base case?
Algorithm Design Paradigms
• Exhaustive Search
• Greedy Algorithms: Build a solution incrementally piece by piece
• Divide and Conquer: Divide into parts, solve each part, combine results
• Dynamic Programming: Divide into subtasks, perform subtask by size. Combine smaller subtasks to larger ones
• Hill-climbing: Start with a solution, improve it
function Fib1(n)if n = 1 return 1if n = 2 return 1return Fib1(n-1) + Fib1(n-2)
Dynamic Programming (DP): A Simple Example
Problem: Compute the n-th Fibonacci number
Recursive Solution Running Time:
T(n) = T(n-1) + T(n-2) + 1
function Fib1(n)if n = 1 return 1if n = 2 return 1return Fib1(n-1) + Fib1(n-2)
Dynamic Programming (DP): A Simple Example
Problem: Compute the n-th Fibonacci number
Recursive Solution Running Time:
T(n) = T(n-1) + T(n-2) + 1
Running time: O(cn)
T(n) = O(cn)
function Fib1(n)if n = 1 return 1if n = 2 return 1return Fib1(n-1) + Fib1(n-2)
Dynamic Programming (DP): A Simple Example
function Fib2(n)Create an array fib[1..n]fib[1] = 1fib[2] = 1for i = 3 to n: fib[i] = fib[i-1] + fib[i-2]return fib[n]
Problem: Compute the n-th Fibonacci number
Recursive Solution
Dynamic Programming Solution
Running Time:
T(n) = T(n-1) + T(n-2) + 1
Running time: O(cn)
T(n) = O(cn)
function Fib1(n)if n = 1 return 1if n = 2 return 1return Fib1(n-1) + Fib1(n-2)
Dynamic Programming (DP): A Simple Example
function Fib2(n)Create an array fib[1..n]fib[1] = 1fib[2] = 1for i = 3 to n: fib[i] = fib[i-1] + fib[i-2]return fib[n]
Problem: Compute the n-th Fibonacci number
Recursive Solution
Dynamic Programming Solution
Running Time:
T(n) = T(n-1) + T(n-2) + 1
Running time: O(cn)
T(n) = O(n)
Running Time:
Running time: O(n)
T(n) = O(cn)
function Fib1(n)if n = 1 return 1if n = 2 return 1return Fib1(n-1) + Fib1(n-2)
Why does DP do better?
function Fib2(n)Create an array fib[1..n]fib[1] = 1fib[2] = 1for i = 3 to n: fib[i] = fib[i-1] + fib[i-2]return fib[n]
Problem: Compute the n-th Fibonacci number
Recursive Solution
Dynamic Programming Solution
Running time: O(cn)
Running time: O(n)
F(5)
F(4)
F(2)F(3)
F(2) F(1)
F(3)
F(1)F(2)
Recursion Tree
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!)
Dynamic Programming
Main Steps:
1. Divide the problem into subtasks: compute fib[i]
2. Define the subtasks recursively (express larger subtasks in terms of smaller ones)
3. Find the right order for solving the subtasks (i = 1,..,n)
function Fib2(n)Create an array fib[1..n]fib[1] = 1fib[2] = 1for i = 3 to n: fib[i] = fib[i-1] + fib[i-2]return fib[n]
Dynamic Programming Solution
Running time: O(n)
Dynamic Programming
• String Reconstruction
• ...
String reconstruction
Given: document x[1..n] : an array of charactersdictionary function dict(w): returns true if w is a valid word
Is x a sequence of valid words ?
Example: x = anonymousarrayofletters : Truex = anhuymousarrayofhetters : False
String reconstruction
Given: document x[1..n] : an array of charactersdictionary function dict(w): returns true if w is a valid word
Is x a sequence of valid words ?
Structure:If x[1...i] is a valid sequence of words, then there is some j<i such that
x[1..j] is a valid sequence, and x[j+1..i] is a valid word
Example: x = anonymousarrayofletters : Truex = anhuymousarrayofhetters : False
Example: x = anonymousarrayofletters anonymousarrayof: Valid sequence of wordsletters: Valid word
String reconstruction
Given: document x[1..n] : an array of charactersdictionary function dict(w): returns true if w is a valid word
Is x a sequence of valid words ?
Example: x = anonymousarrayofletters : Truex = anhuymousarrayofhetters : False
STEP 1: Define subtaskS(k) = True if x[1..k] is a valid sequence of words = False otherwiseOutput of algorithm = S(n)
String reconstruction
Given: document x[1..n] : an array of charactersdictionary function dict(w): returns true if w is a valid word
Is x a sequence of valid words ?
Example: x = anonymousarrayofletters : Truex = anhuymousarrayofhetters : False
STEP 1: Define subtaskS(k) = True if x[1..k] is a valid sequence of words = False otherwiseOutput of algorithm = S(n)
A N O N Y M O U S A R R A Y O F L E T T E R S0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23k
x
S
String reconstruction
A N O N Y M O U S A R R A Y O F L E T T E R S0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23k
x
S
Given: document x[1..n] : an array of charactersdictionary function dict(w): returns true if w is a valid word
Is x a sequence of valid words ?
STEP 1: Define SubtaskS(k) = True if x[1..k] is a valid sequence of words False otherwise
String reconstruction
A N O N Y M O U S A R R A Y O F L E T T E R S0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23k
x
S
Given: document x[1..n] : an array of charactersdictionary function dict(w): returns true if w is a valid word
Is x a sequence of valid words ?
STEP 1: Define SubtaskS(k) = True if x[1..k] is a valid sequence of words = False otherwise
STEP 2: Express RecursivelyS(k) = True iff j < k s.t. S(j) is True, and x[j+1..k] is a valid word∃
String reconstruction
A N O N Y M O U S A R R A Y O F L E T T E R S0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23k
x
S
Given: document x[1..n] : an array of charactersdictionary function dict(w): returns true if w is a valid word
Is x a sequence of valid words ?
STEP 1: Define SubtaskS(k) = True if x[1..k] is a valid sequence of words = False otherwise
STEP 2: Express RecursivelyS(k) = True iff j < k s.t. S(j) is True, and x[j+1..k] is a valid word∃
STEP 3: Order of SubtasksS(1), S(2), S(3), ..., S(n) [ Do not solve recursively! ]
String reconstruction
A N O N Y M O U S A R R A Y O F L E T T E R S0
T1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23k
x
S
STEP 2: Express RecursivelyS(k) = True iff j < k s.t. S(j) is True, and x[j+1..k] is a valid word∃
Given: document x[1..n] : an array of charactersdictionary function dict(w): returns true if w is a valid word
Is x a sequence of valid words ?
STEP 3: Order of SubtasksS(1), S(2), S(3), ..., S(n) [ Do not solve recursively! ]
STEP 1: Define SubtaskS(k) = True if x[1..k] is a valid sequence of words = False otherwise
String reconstruction
A N O N Y M O U S A R R A Y O F L E T T E R S0
T1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23k
x
S T
STEP 2: Express RecursivelyS(k) = True iff j < k s.t. S(j) is True, and x[j+1..k] is a valid word∃
Given: document x[1..n] : an array of charactersdictionary function dict(w): returns true if w is a valid word
Is x a sequence of valid words ?
STEP 3: Order of SubtasksS(1), S(2), S(3), ..., S(n) [ Do not solve recursively! ]
STEP 1: Define SubtaskS(k) = True if x[1..k] is a valid sequence of words = False otherwise
String reconstruction
A N O N Y M O U S A R R A Y O F L E T T E R S0
T1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23k
x
S T
STEP 2: Express RecursivelyS(k) = True iff j < k s.t. S(j) is True, and x[j+1..k] is a valid word∃
Given: document x[1..n] : an array of charactersdictionary function dict(w): returns true if w is a valid word
Is x a sequence of valid words ?
STEP 3: Order of SubtasksS(1), S(2), S(3), ..., S(n) [ Do not solve recursively! ]
T
STEP 1: Define SubtaskS(k) = True if x[1..k] is a valid sequence of words = False otherwise
String reconstruction
A N O N Y M O U S A R R A Y O F L E T T E R S0
T1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23k
x
S T
STEP 2: Express RecursivelyS(k) = True iff j < k s.t. S(j) is True, and x[j+1..k] is a valid word∃
Given: document x[1..n] : an array of charactersdictionary function dict(w): returns true if w is a valid word
Is x a sequence of valid words ?
STEP 3: Order of SubtasksS(1), S(2), S(3), ..., S(n) [ Do not solve recursively! ]
T T
STEP 1: Define SubtaskS(k) = True if x[1..k] is a valid sequence of words = False otherwise
String reconstruction
A N O N Y M O U S A R R A Y O F L E T T E R S0
T1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23k
x
S T
STEP 2: Express RecursivelyS(k) = True iff j < k s.t. S(j) is True, and x[j+1..k] is a valid word∃
Given: document x[1..n] : an array of charactersdictionary function dict(w): returns true if w is a valid word
Is x a sequence of valid words ?
STEP 3: Order of SubtasksS(1), S(2), S(3), ..., S(n) [ Do not solve recursively! ]
T T F
STEP 1: Define SubtaskS(k) = True if x[1..k] is a valid sequence of words = False otherwise
String reconstruction
A N O N Y M O U S A R R A Y O F L E T T E R S0
T1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23k
x
S T
STEP 2: Express RecursivelyS(k) = True iff j < k s.t. S(j) is True, and x[j+1..k] is a valid word∃
Given: document x[1..n] : an array of charactersdictionary function dict(w): returns true if w is a valid word
Is x a sequence of valid words ?
STEP 3: Order of SubtasksS(1), S(2), S(3), ..., S(n) [ Do not solve recursively! ]
T T F F
STEP 1: Define SubtaskS(k) = True if x[1..k] is a valid sequence of words = False otherwise
String reconstruction
A N O N Y M O U S A R R A Y O F L E T T E R S0
T1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23k
x
S T
STEP 2: Express RecursivelyS(k) = True iff j < k s.t. S(j) is True, and x[j+1..k] is a valid word∃
Given: document x[1..n] : an array of charactersdictionary function dict(w): returns true if w is a valid word
Is x a sequence of valid words ?
STEP 3: Order of SubtasksS(1), S(2), S(3), ..., S(n) [ Do not solve recursively! ]
T T F F F
858-459-0501
STEP 1: Define SubtaskS(k) = True if x[1..k] is a valid sequence of words = False otherwise
String reconstruction
A N O N Y M O U S A R R A Y O F L E T T E R S0
T1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23k
x
S T
STEP 2: Express RecursivelyS(k) = True iff j < k s.t. S(j) is True, and x[j+1..k] is a valid word∃
Given: document x[1..n] : an array of charactersdictionary function dict(w): returns true if w is a valid word
Is x a sequence of valid words ?
STEP 3: Order of SubtasksS(1), S(2), S(3), ..., S(n) [ Do not solve recursively! ]
T T F F F F
STEP 1: Define SubtaskS(k) = True if x[1..k] is a valid sequence of words = False otherwise
String reconstruction
A N O N Y M O U S A R R A Y O F L E T T E R S0
T1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23k
x
S T
STEP 2: Express RecursivelyS(k) = True iff j < k s.t. S(j) is True, and x[j+1..k] is a valid word∃
Given: document x[1..n] : an array of charactersdictionary function dict(w): returns true if w is a valid word
Is x a sequence of valid words ?
STEP 3: Order of SubtasksS(1), S(2), S(3), ..., S(n) [ Do not solve recursively! ]
T T F F F F T
STEP 1: Define SubtaskS(k) = True if x[1..k] is a valid sequence of words = False otherwise
String reconstruction
A N O N Y M O U S A R R A Y O F L E T T E R S0
T1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23k
x
S T
STEP 2: Express RecursivelyS(k) = True iff j < k s.t. S(j) is True, and x[j+1..k] is a valid word∃
Given: document x[1..n] : an array of charactersdictionary function dict(w): returns true if w is a valid word
Is x a sequence of valid words ?
STEP 3: Order of SubtasksS(1), S(2), S(3), ..., S(n) [ Do not solve recursively! ]
T T F F F F T T
STEP 1: Define SubtaskS(k) = True if x[1..k] is a valid sequence of words = False otherwise
String reconstruction
A N O N Y M O U S A R R A Y O F L E T T E R S0
T1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23k
x
S T
STEP 2: Express RecursivelyS(k) = True iff j < k s.t. S(j) is True, and x[j+1..k] is a valid word∃
Given: document x[1..n] : an array of charactersdictionary function dict(w): returns true if w is a valid word
Is x a sequence of valid words ?
STEP 3: Order of SubtasksS(1), S(2), S(3), ..., S(n) [ Do not solve recursively! ]
T T F F F F T T F
STEP 1: Define SubtaskS(k) = True if x[1..k] is a valid sequence of words = False otherwise
String reconstruction
A N O N Y M O U S A R R A Y O F L E T T E R S0
T1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23k
x
S T
STEP 2: Express RecursivelyS(k) = True iff j < k s.t. S(j) is True, and x[j+1..k] is a valid word∃
Given: document x[1..n] : an array of charactersdictionary function dict(w): returns true if w is a valid word
Is x a sequence of valid words ?
STEP 3: Order of SubtasksS(1), S(2), S(3), ..., S(n) [ Do not solve recursively! ]
T T F F F F T T F F
STEP 1: Define SubtaskS(k) = True if x[1..k] is a valid sequence of words = False otherwise
String reconstruction
A N O N Y M O U S A R R A Y O F L E T T E R S0
T1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23k
x
S T
STEP 2: Express RecursivelyS(k) = True iff j < k s.t. S(j) is True, and x[j+1..k] is a valid word∃
Given: document x[1..n] : an array of charactersdictionary function dict(w): returns true if w is a valid word
Is x a sequence of valid words ?
STEP 3: Order of SubtasksS(1), S(2), S(3), ..., S(n) [ Do not solve recursively! ]
T T F F F F T T F F F
STEP 1: Define SubtaskS(k) = True if x[1..k] is a valid sequence of words = False otherwise
String reconstruction
A N O N Y M O U S A R R A Y O F L E T T E R S0
T1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23k
x
S T
STEP 2: Express RecursivelyS(k) = True iff j < k s.t. S(j) is True, and x[j+1..k] is a valid word∃
Given: document x[1..n] : an array of charactersdictionary function dict(w): returns true if w is a valid word
Is x a sequence of valid words ?
STEP 3: Order of SubtasksS(1), S(2), S(3), ..., S(n) [ Do not solve recursively! ]
T T F F F F T T F F F T
STEP 1: Define SubtaskS(k) = True if x[1..k] is a valid sequence of words = False otherwise
String reconstruction
A N O N Y M O U S A R R A Y O F L E T T E R S0
T1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23k
x
S T
STEP 2: Express RecursivelyS(k) = True iff j < k s.t. S(j) is True, and x[j+1..k] is a valid word∃
Given: document x[1..n] : an array of charactersdictionary function dict(w): returns true if w is a valid word
Is x a sequence of valid words ?
STEP 3: Order of SubtasksS(1), S(2), S(3), ..., S(n) [ Do not solve recursively! ]
T T F F F F T T F F F T F
STEP 1: Define SubtaskS(k) = True if x[1..k] is a valid sequence of words = False otherwise
String reconstruction
A N O N Y M O U S A R R A Y O F L E T T E R S0
T1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23k
x
S T
STEP 2: Express RecursivelyS(k) = True iff j < k s.t. S(j) is True, and x[j+1..k] is a valid word∃
Given: document x[1..n] : an array of charactersdictionary function dict(w): returns true if w is a valid word
Is x a sequence of valid words ?
STEP 3: Order of SubtasksS(1), S(2), S(3), ..., S(n) [ Do not solve recursively! ]
T T F F F F T T F F F T F T
STEP 1: Define SubtaskS(k) = True if x[1..k] is a valid sequence of words = False otherwise
String reconstruction
A N O N Y M O U S A R R A Y O F L E T T E R S0
T1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23k
x
S T
STEP 2: Express RecursivelyS(k) = True iff j < k s.t. S(j) is True, and x[j+1..k] is a valid word∃
Given: document x[1..n] : an array of charactersdictionary function dict(w): returns true if w is a valid word
Is x a sequence of valid words ?
STEP 3: Order of SubtasksS(1), S(2), S(3), ..., S(n) [ Do not solve recursively! ]
T T F F F F T T F F F T F T F
STEP 1: Define SubtaskS(k) = True if x[1..k] is a valid sequence of words = False otherwise
String reconstruction
A N O N Y M O U S A R R A Y O F L E T T E R S0
T1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23k
x
S T
STEP 2: Express RecursivelyS(k) = True iff j < k s.t. S(j) is True, and x[j+1..k] is a valid word∃
Given: document x[1..n] : an array of charactersdictionary function dict(w): returns true if w is a valid word
Is x a sequence of valid words ?
STEP 3: Order of SubtasksS(1), S(2), S(3), ..., S(n) [ Do not solve recursively! ]
T T F F F F T T F F F T F T F F
STEP 1: Define SubtaskS(k) = True if x[1..k] is a valid sequence of words = False otherwise
String reconstruction
A N O N Y M O U S A R R A Y O F L E T T E R S0
T1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23k
x
S T
STEP 2: Express RecursivelyS(k) = True iff j < k s.t. S(j) is True, and x[j+1..k] is a valid word∃
Given: document x[1..n] : an array of charactersdictionary function dict(w): returns true if w is a valid word
Is x a sequence of valid words ?
STEP 3: Order of SubtasksS(1), S(2), S(3), ..., S(n) [ Do not solve recursively! ]
T T F F F F T T F F F T F T F F T
STEP 1: Define SubtaskS(k) = True if x[1..k] is a valid sequence of words = False otherwise
String reconstruction
A N O N Y M O U S A R R A Y O F L E T T E R S0
T1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23k
x
S T
STEP 2: Express RecursivelyS(k) = True iff j < k s.t. S(j) is True, and x[j+1..k] is a valid word∃
Given: document x[1..n] : an array of charactersdictionary function dict(w): returns true if w is a valid word
Is x a sequence of valid words ?
STEP 3: Order of SubtasksS(1), S(2), S(3), ..., S(n) [ Do not solve recursively! ]
T T F F F F T T F F F T F T F F T F
STEP 1: Define SubtaskS(k) = True if x[1..k] is a valid sequence of words = False otherwise
String reconstruction
A N O N Y M O U S A R R A Y O F L E T T E R S0
T1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23k
x
S T
STEP 2: Express RecursivelyS(k) = True iff j < k s.t. S(j) is True, and x[j+1..k] is a valid word∃
Given: document x[1..n] : an array of charactersdictionary function dict(w): returns true if w is a valid word
Is x a sequence of valid words ?
STEP 3: Order of SubtasksS(1), S(2), S(3), ..., S(n) [ Do not solve recursively! ]
T T F F F F T T F F F T F T F F T F F
STEP 1: Define SubtaskS(k) = True if x[1..k] is a valid sequence of words = False otherwise
String reconstruction
A N O N Y M O U S A R R A Y O F L E T T E R S0
T1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23k
x
S T
STEP 2: Express RecursivelyS(k) = True iff j < k s.t. S(j) is True, and x[j+1..k] is a valid word∃
Given: document x[1..n] : an array of charactersdictionary function dict(w): returns true if w is a valid word
Is x a sequence of valid words ?
STEP 3: Order of SubtasksS(1), S(2), S(3), ..., S(n) [ Do not solve recursively! ]
T T F F F F T T F F F T F T F F T F F T
STEP 1: Define SubtaskS(k) = True if x[1..k] is a valid sequence of words = False otherwise
String reconstruction
A N O N Y M O U S A R R A Y O F L E T T E R S0
T1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23k
x
S T
STEP 2: Express RecursivelyS(k) = True iff j < k s.t. S(j) is True, and x[j+1..k] is a valid word∃
Given: document x[1..n] : an array of charactersdictionary function dict(w): returns true if w is a valid word
Is x a sequence of valid words ?
STEP 3: Order of SubtasksS(1), S(2), S(3), ..., S(n) [ Do not solve recursively! ]
T T F F F F T T F F F T F T F F T F F T T
STEP 1: Define SubtaskS(k) = True if x[1..k] is a valid sequence of words = False otherwise
String reconstruction
STEP 2: Express RecursivelyS(k) = True iff j < k s.t. S(j) is True, and x[j+1..k] is a valid word
∃
Given: document x[1..n] : an array of charactersdictionary function dict(w): returns true if w is a valid word
Is x a sequence of valid words ?
STEP 1: Define SubtaskS(k) = True if x[1..k] is a valid
sequence of words = False otherwise
STEP 3: Order of SubtasksS(1), S(2), S(3), ..., S(n)
Algorithm:S[0] = truefor k = 1 to n: S[k] = false for j = 1 to k: if S[j-1] and dict(x[j..k]) S[k] = true
Define array D(1,..n):If S(k) = true, then D(k) = starting position of the word that ends at x[k]
Reconstruct text by following these pointers.
Reconstructing Document:
String reconstruction
STEP 2: Express Recursively
S(k) = True iff there is j < k s.t. S(j) is True, and x[j+1..k] is a valid word
Given: document x[1..n] : an array of charactersdictionary function dict(w): returns true if w is a valid word
Is x a sequence of valid words ?
STEP 1: Define Subtask
S(k) = True if x[1..k] is a valid sequence of words
= False otherwise
STEP 3: Order of Subtasks
S(1), S(2), S(3), ..., S(n)
Define array D(1,..n):If S(k) = True, then D(k) = starting position of the word that ends at x[k]
Reconstruct text by following these pointers.
Reconstructing Document:
A N O N Y M O U S A R R A Y O F L E T T E R S0
T1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23k
x
S T T T F F F F T T F F F T F T F F T F F T T- - - - - - - - - - - -D 1
String reconstruction
STEP 2: Express Recursively
S(k) = True iff there is j < k s.t. S(j) is True, and x[j+1..k] is a valid word
Given: document x[1..n] : an array of charactersdictionary function dict(w): returns true if w is a valid word
Is x a sequence of valid words ?
STEP 1: Define Subtask
S(k) = True if x[1..k] is a valid sequence of words
= False otherwise
STEP 3: Order of Subtasks
S(1), S(2), S(3), ..., S(n)
Define array D(1,..n):If S(k) = True, then D(k) = starting position of the word that ends at x[k]
Reconstruct text by following these pointers.
Reconstructing Document:
A N O N Y M O U S A R R A Y O F L E T T E R S0
T1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23k
x
S T T T F F F F T T F F F T F T F F T F F T T- - - - - - - - - - - -D 11
String reconstruction
STEP 2: Express Recursively
S(k) = True iff there is j < k s.t. S(j) is True, and x[j+1..k] is a valid word
Given: document x[1..n] : an array of charactersdictionary function dict(w): returns true if w is a valid word
Is x a sequence of valid words ?
STEP 1: Define Subtask
S(k) = True if x[1..k] is a valid sequence of words
= False otherwise
STEP 3: Order of Subtasks
S(1), S(2), S(3), ..., S(n)
Define array D(1,..n):If S(k) = True, then D(k) = starting position of the word that ends at x[k]
Reconstruct text by following these pointers.
Reconstructing Document:
A N O N Y M O U S A R R A Y O F L E T T E R S0
T1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23k
x
S T T T F F F F T T F F F T F T F F T F F T T- - - - - - - - - - - -D 211
String reconstruction
STEP 2: Express Recursively
S(k) = True iff there is j < k s.t. S(j) is True, and x[j+1..k] is a valid word
Given: document x[1..n] : an array of charactersdictionary function dict(w): returns true if w is a valid word
Is x a sequence of valid words ?
STEP 1: Define Subtask
S(k) = True if x[1..k] is a valid sequence of words
= False otherwise
STEP 3: Order of Subtasks
S(1), S(2), S(3), ..., S(n)
Define array D(1,..n):If S(k) = True, then D(k) = starting position of the word that ends at x[k]
Reconstruct text by following these pointers.
Reconstructing Document:
A N O N Y M O U S A R R A Y O F L E T T E R S0
T1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23k
x
S T T T F F F F T T F F F T F T F F T F F T T- - - - - - - - - - - -D 211 3
String reconstruction
STEP 2: Express Recursively
S(k) = True iff there is j < k s.t. S(j) is True, and x[j+1..k] is a valid word
Given: document x[1..n] : an array of charactersdictionary function dict(w): returns true if w is a valid word
Is x a sequence of valid words ?
STEP 1: Define Subtask
S(k) = True if x[1..k] is a valid sequence of words
= False otherwise
STEP 3: Order of Subtasks
S(1), S(2), S(3), ..., S(n)
Define array D(1,..n):If S(k) = True, then D(k) = starting position of the word that ends at x[k]
Reconstruct text by following these pointers.
Reconstructing Document:
A N O N Y M O U S A R R A Y O F L E T T E R S0
T1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23k
x
S T T T F F F F T T F F F T F T F F T F F T T- - - - - - - - - - - -D 211 3 1
String reconstruction
STEP 2: Express Recursively
S(k) = True iff there is j < k s.t. S(j) is True, and x[j+1..k] is a valid word
Given: document x[1..n] : an array of charactersdictionary function dict(w): returns true if w is a valid word
Is x a sequence of valid words ?
STEP 1: Define Subtask
S(k) = True if x[1..k] is a valid sequence of words
= False otherwise
STEP 3: Order of Subtasks
S(1), S(2), S(3), ..., S(n)
Define array D(1,..n):If S(k) = True, then D(k) = starting position of the word that ends at x[k]
Reconstruct text by following these pointers.
Reconstructing Document:
A N O N Y M O U S A R R A Y O F L E T T E R S0
T1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23k
x
S T T T F F F F T T F F F T F T F F T F F T T- - - - - - - - - - - -D 211 3 1 10
String reconstruction
STEP 2: Express Recursively
S(k) = True iff there is j < k s.t. S(j) is True, and x[j+1..k] is a valid word
Given: document x[1..n] : an array of charactersdictionary function dict(w): returns true if w is a valid word
Is x a sequence of valid words ?
STEP 1: Define Subtask
S(k) = True if x[1..k] is a valid sequence of words
= False otherwise
STEP 3: Order of Subtasks
S(1), S(2), S(3), ..., S(n)
Define array D(1,..n):If S(k) = True, then D(k) = starting position of the word that ends at x[k]
Reconstruct text by following these pointers.
Reconstructing Document:
A N O N Y M O U S A R R A Y O F L E T T E R S0
T1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23k
x
S T T T F F F F T T F F F T F T F F T F F T T- - - - - - - - - - - -D 211 3 1 10 10
String reconstruction
STEP 2: Express Recursively
S(k) = True iff there is j < k s.t. S(j) is True, and x[j+1..k] is a valid word
Given: document x[1..n] : an array of charactersdictionary function dict(w): returns true if w is a valid word
Is x a sequence of valid words ?
STEP 1: Define Subtask
S(k) = True if x[1..k] is a valid sequence of words
= False otherwise
STEP 3: Order of Subtasks
S(1), S(2), S(3), ..., S(n)
Define array D(1,..n):If S(k) = True, then D(k) = starting position of the word that ends at x[k]
Reconstruct text by following these pointers.
Reconstructing Document:
A N O N Y M O U S A R R A Y O F L E T T E R S0
T1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23k
x
S T T T F F F F T T F F F T F T F F T F F T T- - - - - - - - - - - -D 211 3 1 10 10 15
String reconstruction
STEP 2: Express Recursively
S(k) = True iff there is j < k s.t. S(j) is True, and x[j+1..k] is a valid word
Given: document x[1..n] : an array of charactersdictionary function dict(w): returns true if w is a valid word
Is x a sequence of valid words ?
STEP 1: Define Subtask
S(k) = True if x[1..k] is a valid sequence of words
= False otherwise
STEP 3: Order of Subtasks
S(1), S(2), S(3), ..., S(n)
Define array D(1,..n):If S(k) = True, then D(k) = starting position of the word that ends at x[k]
Reconstruct text by following these pointers.
Reconstructing Document:
A N O N Y M O U S A R R A Y O F L E T T E R S0
T1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23k
x
S T T T F F F F T T F F F T F T F F T F F T T- - - - - - - - - - - -D 211 3 1 10 10 15 17
String reconstruction
STEP 2: Express Recursively
S(k) = True iff there is j < k s.t. S(j) is True, and x[j+1..k] is a valid word
Given: document x[1..n] : an array of charactersdictionary function dict(w): returns true if w is a valid word
Is x a sequence of valid words ?
STEP 1: Define Subtask
S(k) = True if x[1..k] is a valid sequence of words
= False otherwise
STEP 3: Order of Subtasks
S(1), S(2), S(3), ..., S(n)
Define array D(1,..n):If S(k) = True, then D(k) = starting position of the word that ends at x[k]
Reconstruct text by following these pointers.
Reconstructing Document:
A N O N Y M O U S A R R A Y O F L E T T E R S0
T1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23k
x
S T T T F F F F T T F F F T F T F F T F F T T- - - - - - - - - - - -D 211 3 1 10 10 15 17 17
String reconstruction
STEP 2: Express Recursively
S(k) = True iff there is j < k s.t. S(j) is True, and x[j+1..k] is a valid word
Given: document x[1..n] : an array of charactersdictionary function dict(w): returns true if w is a valid word
Is x a sequence of valid words ?
STEP 1: Define Subtask
S(k) = True if x[1..k] is a valid sequence of words
= False otherwise
STEP 3: Order of Subtasks
S(1), S(2), S(3), ..., S(n)
Define array D(1,..n):If S(k) = True, then D(k) = starting position of the word that ends at x[k]
Reconstruct text by following these pointers.
Reconstructing Document:
A N O N Y M O U S A R R A Y O F L E T T E R S0
T1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23k
x
S T T T F F F F T T F F F T F T F F T F F T T- - - - - - - - - - - -D 211 3 1 10 10 15 17 17 17
String reconstruction
STEP 2: Express Recursively
S(k) = True iff there is j < k s.t. S(j) is True, and x[j+1..k] is a valid word
Given: document x[1..n] : an array of charactersdictionary function dict(w): returns true if w is a valid word
Is x a sequence of valid words ?
STEP 1: Define Subtask
S(k) = True if x[1..k] is a valid sequence of words
= False otherwise
STEP 3: Order of Subtasks
S(1), S(2), S(3), ..., S(n)
Define array D(1,..n):If S(k) = True, then D(k) = starting position of the word that ends at x[k]
Reconstruct text by following these pointers.
Reconstructing Document:
A N O N Y M O U S A R R A Y O F L E T T E R S0
T1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23k
x
S T T T F F F F T T F F F T F T F F T F F T T- - - - - - - - - - - - 17D 171715101013211
Dynamic Programming
• String Reconstruction
• Longest Common Subsequence
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
Top Related