+ All Categories
Home > Documents > CSE 202: Design and Analysis of Algorithms · Summary: Closest Pair of Points Given a set P of n...

CSE 202: Design and Analysis of Algorithms · Summary: Closest Pair of Points Given a set P of n...

Date post: 03-Oct-2020
Category:
Upload: others
View: 0 times
Download: 0 times
Share this document with a friend
75
CSE 202: Design and Analysis of Algorithms Lecture 5 Instructor: Kamalika Chaudhuri
Transcript
Page 1: CSE 202: Design and Analysis of Algorithms · 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(Q

CSE 202: Design and Analysis of Algorithms

Lecture 5

Instructor: Kamalika Chaudhuri

Page 2: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 3: CSE 202: Design and Analysis of Algorithms · 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(Q

Last Class: Review

• Greedy Algorithm

• Divide and Conquer

• Integer multiplication

• Strassen’s algorithm for matrix multiplication

• Closest pair of points on the plane

Page 4: CSE 202: Design and Analysis of Algorithms · 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(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

p q

Page 5: CSE 202: Design and Analysis of Algorithms · 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(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

Page 6: CSE 202: Design and Analysis of Algorithms · 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(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

Page 7: CSE 202: Design and Analysis of Algorithms · 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(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

Page 8: CSE 202: Design and Analysis of Algorithms · 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(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

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

Page 9: CSE 202: Design and Analysis of Algorithms · 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(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

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)

Page 10: CSE 202: Design and Analysis of Algorithms · 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(Q

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!

Page 11: CSE 202: Design and Analysis of Algorithms · 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(Q

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)

Page 12: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 13: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 14: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 15: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 16: CSE 202: Design and Analysis of Algorithms · 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(Q

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!

Page 17: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 18: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 19: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 20: CSE 202: Design and Analysis of Algorithms · 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(Q

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)Θ

Page 21: CSE 202: Design and Analysis of Algorithms · 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(Q

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?

Page 22: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 23: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 24: CSE 202: Design and Analysis of Algorithms · 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(Q

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)

Page 25: CSE 202: Design and Analysis of Algorithms · 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(Q

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)

Page 26: CSE 202: Design and Analysis of Algorithms · 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(Q

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)

Page 27: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 28: CSE 202: Design and Analysis of Algorithms · 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(Q

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 29: CSE 202: Design and Analysis of Algorithms · 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(Q

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)

Page 30: CSE 202: Design and Analysis of Algorithms · 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(Q

Dynamic Programming

• String Reconstruction

• ...

Page 31: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 32: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 33: CSE 202: Design and Analysis of Algorithms · 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(Q

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)

Page 34: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 35: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 36: CSE 202: Design and Analysis of Algorithms · 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(Q

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∃

Page 37: CSE 202: Design and Analysis of Algorithms · 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(Q

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! ]

Page 38: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 39: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 40: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 41: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 42: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 43: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 44: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 45: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 46: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 47: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 48: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 49: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 50: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 51: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 52: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 53: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 54: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 55: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 56: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 57: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 58: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 59: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 60: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 61: CSE 202: Design and Analysis of Algorithms · 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(Q

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:

Page 62: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 63: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 64: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 65: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 66: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 67: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 68: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 69: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 70: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 71: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 72: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 73: CSE 202: Design and Analysis of Algorithms · 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(Q

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

Page 74: CSE 202: Design and Analysis of Algorithms · 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(Q

Dynamic Programming

• String Reconstruction

• Longest Common Subsequence

Page 75: CSE 202: Design and Analysis of Algorithms · 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(Q

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


Recommended