Dynamic Programming

Comp 122, Fall 2004 Review: the previous lecture • Principles of dynamic programming: optimization problems, optimal substructure property, overlapping subproblems, trade space for time, implementation via bottom-up/memoization.

• Example of Match Game.

• Example of Fibinacci: from Divide and Conquer (exponential time complexity) to Dynamic Programming (O(n) time and space) , then to more efficient dynamic programming (constant space, O(logn) time)

• Example of LCS in O(m n) time and space. Today we will show how to reduce the space complexity of LCS to O(n) and in future lectures we will show how to reduce the time complexity of LCS.

Comp 122, Spring 2004 Longest Common Subsequence

• Problem: Given 2 sequences, X = x1,...,xm and Y = y1,...,yn, find a common subsequence whose length is maximum.

X: springtime

Y: printing LCS(X,Y): printi Comp 122, Spring 2004 0 if  empty or  empty,  c[,  ]  c[ prefix, prefix ]1 if end()  end( ),  max(c[ prefix,  ],c[, prefix ]) if end()  end( ).

p r i n t i n g •Keep track of c[,] in a table of nm 0 0 0 0 0 0 0 0 0 entries: s 0 0 0 0 0 0 0 0 0 •top/down: increasing row order p 0 1 1 1 1 1 1 1 1 r 0 1 2 2 •within each row left-to-right: increasing column order i n g t i m Comp 122,e Spring 2004 0 if  empty or  empty,  c[,  ]  c[ prefix, prefix ]1 if end()  end( ),  max(c[ prefix,  ],c[, prefix ]) if end()  end( ).

Time Complexity: O(nm). p r i n t i n g Space Complexity: O(nm). 0 0 0 0 0 0 0 0 0 Can the space complexity be s 0 0 0 0 0 0 0 0 0 improved if we just compute the p 0 1 1 1 1 1 1 1 1 length of the LCS and do not need to recover an actual LCS? r 0 1 2 2 In this case we only need to compute i the alignment score. n Can the space complexity be g improved and still allow the t recovery of an actual LCS? i Yes, but for this we will have to m address LCS as a . Comp 122,e Spring 2004 Other sequence questions

• Edit : Given 2 sequences, X = x1,...,xm and Y = y1,...,yn, what is the minimum number of deletions, insertions, and changes that you must do to change one to another? • Protein sequence alignment: Given a score matrix on amino acid pairs, s(a,b) for a,b{}A, m and 2 amino acid sequences, X = x1,...,xmA n and Y = y1,...,ynA , find the alignment with lowest score…

Comp 122, Spring 2004 Outline

• DNA Sequence Comparison: Biological background • Sequence Alignment • First Biological Success Stories • More Grid Graphs: Manhattan Tourist Problem • Longest Paths in Grid Graphs • Back to Longest Common Subsequence Problem • Review: divide and conquer paradigm • Reducing the space requirement of LCS Dynamic Programming: String Editing The Central Dogma of Molecular Biology

DNA RNA PROTEIN Gene Function

> DNA sequence > Protein sequence AATTCATGAAAATCGTATACTGGTCTGGTACCGG MKIVYWSGTGNTEKMAELIAKGIIES CAACACTGAGAAAATGGCAGAGCTCATCGCTAAA GKDVNTINVSDVNIDELLNEDILILGC GGTATCATCGAATCTGGTAAAGACGTCAACACCA SAMGDEVLEESEFEPFIEEISTKISG TCAACGTGTCTGACGTTAACATCGATGAACTGCT KKVALFGSYGWGDGKWMRDFEER GAACGAAGATATCCTGATCCTGGGTTGCTCTGCC MNGYGCVVVETPLIVQNEPDEAEQD ATGGGCGATGAAGTTCTCGAGGAAAGCGAATTTG CIEFGKKIANI The Central Dogma of Molecular Biology

Genome: The digital backbone of molecular biology

Transcripts from Gene(DNA subsequence) to Proten sequence: Perform functions encoded in the genome The Sequence Alignment Problem

The Scoring Matrix  - a c g t A = c t a c g a g a c - -1 -1 -1 -1 a -1 1 -1 -1 -1 c -1 -1 1 -1 -1 B = a a c g a c g a t g -1 -1 -1 1 -1 t -1 -1 -1 -1 1

Compare two strings A and B and measure their similarity by finding the optimal alignment between them.

The alignment is classically based on the transformation of one sequence into the other, via operations of substitutions, insertions, and deletions (indels).

13 Two Sequence Alignment Problems Global Alignment. A = c t a c g a g a c

B = a a c g a c g a t

Local Alignment.

A = c t a c g a g a c

B = a a c g a c g a t

14 Two Sequence Alignment Problems

Global Alignment. The Scoring Matrix  A = c t a c g a g a c - a c g t - -1 -1 -1 -1 a -1 1 -1 -1 -1 B = a a c g a c g a t c -1 -1 1 -1 -1 g -1 -1 -1 1 -1 t -1 -1 -1 -1 1

Local Alignment.

A = c t a c g a g a c

B = a a c g a c g a t

15 Two Sequence Alignment Problems

Global Alignment. The Scoring Matrix  A = c t a c g a g a c - a c g t - -1 -1 -1 -1 a -1 1 -1 -1 -1 B = a a c g a c g a t c -1 -1 1 -1 -1 g -1 -1 -1 1 -1 Value: 2 t -1 -1 -1 -1 1

Local Alignment.

A = c t a c g a g a c

B = a a c g a c g a t

16 Two Sequence Alignment Problems

Global Alignment. The Scoring Matrix  A = c t a c g a g a c - a c g t - -1 -1 -1 -1 a -1 1 -1 -1 -1 B = a a c g a c g a t c -1 -1 1 -1 -1 g -1 -1 -1 1 -1 Value: 2 t --1 -1 -1 -1 1

Local Alignment.

A = c t a c g a g a c

B = a a c g a c g a t

Value: 5 17 The O(n 2 ) time, Classical Dynamic Programming Algorithm The Alignment Graph |B|= n The Scoring Matrix  a a c g a c g a t - a c g t 0 1 2 3 4 5 6 7 8 9 - -1 -1 -1 -1 c a -1 1 -1 -1 -1 1 c -1 -1 1 -1 -1 t g -1 -1 -1 1 -1 2 a t -1 -1 -1 -1 1 3 |A|= n c 4 g 5 a 6 g 7 a c8 9 0 1 2 3 4 5 6 7 8 9 18 Computing the Optimal Global Alignment Value |B|= n a c a a 0 1 a 2 3 g 4 5 c 6 g 7 8 t 9 Score of = 1 c Score of = -1 1 t 2 a 3 |A|= n c 4 g 5 a 6 g 7 a c8

9 0 1 2 3 4 5 6 7 8 9 Classical Dynamic Programming: O(n 2 ) 19 Computing an Optimal Local Alignment Value |B|= n a a c a a t 0 2 g4 5 c 6 g 8 1 3 7 9 Score of = 1 c Score of = -1 1 t 2 a 3 |A|= n c 4 g 5 a 6 g 7 a 8 c 9 0 1 2 3 4 5 6 7 8 9 Classical Dynamic Programming: O(n 2 ) 20 DNA Sequence Comparison: First Success Story

• Finding sequence similarities with genes of known function is a common approach to infer a newly sequenced gene’s function

• In 1984 Russell Doolittle and colleagues found similarities between cancer-causing gene and normal growth factor (PDGF) gene Cystic Fibrosis

• Cystic fibrosis (CF) is a chronic and frequently fatal genetic disease of the body's mucus glands (abnormally high level of mucus in glands). CF primarily affects the respiratory systems in children. • Mucus is a slimy material that coats many epithelial surfaces and is secreted into fluids such as saliva Cystic Fibrosis: Inheritance

• In early 1980s biologists hypothesized that CF is an autosomal recessive disorder caused by mutations in a gene that remained unknown till 1989

• Heterozygous carriers are asymptomatic

• Must be homozygously recessive in this gene in order to be diagnosed with CF Cystic Fibrosis: Finding the Gene Cystic Fibrosis and the CFTR Protein

•CFTR (Cystic Fibrosis Transmembrane Regulator) protein is acting in the cell membrane of epithelial cells that secrete mucus •These cells line the airways of the nose, lungs, the stomach wall, etc. Mechanism of Cystic Fibrosis

• The CFTR protein (1480 amino acids) regulates a chloride ion channel • Adjusts the “wateriness” of fluids secreted by the cell • Those with cystic fibrosis are missing one single amino acid in their CFTR • Mucus ends up being too thick, affecting many organs Cystic Fibrosis and the CFTR Protein

•CFTR (Cystic Fibrosis Transmembrane conductance Regulator) protein is acting in the cell membrane of epithelial cells that secrete mucus •These cells line the airways of the nose, lungs, the stomach wall, etc. Finding Similarities between the Cystic Fibrosis Gene and ATP binding proteins

• ATP binding proteins are present on cell membrane and act as transport channel

• In 1989 biologists found similarity between the cystic fibrosis gene and ATP binding proteins

• A plausible function for cystic fibrosis gene, given the fact that CF involves sweet secretion with abnormally high sodium level Cystic Fibrosis: Mutation Analysis

If a high % of cystic fibrosis (CF) patients have a certain mutation in the gene and the normal patients don’t, then that could be an indicator of a mutation that is related to CF

A certain mutation was found in 70% of CF patients, convincing evidence that it is a predominant genetic diagnostics marker for CF Outline

• DNA Sequence Comparison: Biological background • The Sequence Alignment problem • First Biological Success Stories • Grid Graphs: Manhattan Tourist Problem • Longest Paths in Grid Graphs • Back to Longest Common Subsequence Problem • Review: divide and conquer paradigm • Reducing the space requirement of LCS Manhattan Tourist Problem (MTP)

Imagine seeking a path (from source to Source sink) to travel (only * * eastward and * * * southward) with the * most number of * * attractions (*) in the * * * * Manhattan grid Sink Manhattan Tourist Problem (MTP)

Imagine seeking a path (from source to Source sink) to travel (only * * eastward and * * * southward) with the * most number of * * attractions (*) in the * * * * Manhattan grid Sink Manhattan Tourist Problem: Formulation

Goal: Find the longest (highest scoring) path in a weighted grid.

Input: A weighted grid G with two distinct vertices, one labeled “source” and the other labeled “sink”

Output: A longest path in G from “source” to “sink” MTP: An Example 0 1 2 3 4 j coordinate source 3 2 4 0 0 0 3 5 9

1 0 2 4 3

3 2 4 2 1 13

1 4 6 5 2

0 7 3 4 2 15 19

4 4 5 2

i coordinate i 1 3 3 0 2 3 20

5 6 8 5 3

1 3 2 2 4 23 sink MTP: Greedy Algorithm Is Not Optimal

source 1 2 5

5 3 10 5

2 1 5

3 5 3 1

2 3 4

promising start, 0 0 5 2 but leads to bad 22 0 0 0 choices! sink 18 MTP: Dynamic Programming j 0 1 source

1 0 1

i 5 S0,1 = 1

1 5

S1,0 = 5

• Calculate optimal path score for each vertex in the graph • Each vertex’s score is the maximum of the prior vertices score plus the weight of the respective edge in between MTP: Dynamic Programming (cont’d) j 0 1 2 source

1 2 0 1 3

i 5 3 S0,2 = 3

-5 1 5 4

3 S1,1 = 4

2 8

S2,0 = 8 MTP: Dynamic Programming (cont’d) j 0 1 2 3 source

1 2 5 0 1 3 8

i 5 3 10 S3,0 = 8

-5 1 1 5 4 13

3 5 S1,2 = 13

-5 2 8 9

0 S2,1 = 9

3 8

S3,0 = 8 MTP: Dynamic Programming (cont’d) j 0 1 2 3 source

1 2 5 0 1 3 8

i 5 3 10 -5

-5 1 -5 1 5 4 13 8

3 5 -3 S1,3 = 8

-5 3 2 8 9 12

0 0 S2,2 = 12

0 3 8 9 S = 9 greedy alg. fails! 3,1 MTP: Dynamic Programming (cont’d) j 0 1 2 3 source

1 2 5 0 1 3 8

i 5 3 10 -5

-5 1 -5 1 5 4 13 8

3 5 -3 2

-5 3 3 2 8 9 12 15

0 0 -5 S2,3 = 15

0 0 3 8 9 9 S3,2 = 9 MTP: Dynamic Programming (cont’d) j 0 1 2 3 source

1 2 5 0 1 3 8 Done! i 5 3 10 -5

-5 1 -5 1 5 4 13 8

3 5 -3 2 (showing all back-traces)

-5 3 3 2 8 9 12 15

0 0 -5 1

0 0 0 3 8 9 9 16 S3,3 = 16 MTP: Recurrence

Computing the score for a point (i,j) by the recurrence relation:

s + weight of the edge between (i-1, j) and (i, j) max i-1, j si, j = si, j-1 + weight of the edge between (i, j-1) and (i, j)

The running time is n x m for a n by m grid (n = # of rows, m = # of columns) Adding Diagonal Edges to the Grid

A2 A3

What about diagonals? A1 B

• The score at point B is given by:

sA1 + weight of the edge (A1, B) max of sB = sA2 + weight of the edge (A2, B)

sA3 + weight of the edge (A3, B) Adding Diagonal Edges to the Grid

More generally, computing the score for point x is given by the recurrence relation:

max s + weight of vertex (y, x) where sx = y of y є Predecessors(x)

• Predecessors (x) – set of vertices that have edges leading to x Traveling in the Grid •The only hitch is that one must decide on the order in which visit the vertices

•By the time the vertex x is analyzed, the values sy for all its predecessors y should be computed – otherwise we are in trouble. •We need to traverse the vertices in some order •Try to find such order for a directed acyclic grid graph ??? Traversing the Manhattan Grid

a) b) • 3 different strategies: • a) Column by column • b) Row by row • c) Along diagonals

c) Outline

• DNA Sequence Comparison: Biological background • The Sequence Alignment problem • First Biological Success Stories • Grid Graphs: Manhattan Tourist Problem • Generalizing to Longest Paths in Grid Graphs • Back to Longest Common Subsequence Problem • Review: divide and conquer paradigm • Reducing the space requirement of LCS Generalizing to computing longest paths in DAGs: Directed Acyclic Graphs • The Manhattan grid is a DAG • Exemplify Topological Ordering: DAG for Dressing in the morning problem Topological Ordering

• A numbering of vertices of the graph is called topological ordering of the DAG if every edge of the DAG connects a vertex with a smaller label to a vertex with a larger label • In other words, if vertices are positioned on a line in an increasing order of labels then all edges go from left to right. Topological ordering • 2 different topological orderings of the DAG Longest Path in DAG Problem

• Goal: Find a longest path between two vertices in a weighted DAG

• Input: A weighted DAG G with source and sink vertices

• Output: A longest path in G from source to sink Longest Path in DAG: Dynamic Programming

• Suppose vertex v has indegree k and

predecessors {u1, u2 … uk} • Longest path to v from source is:

su1 + weight of edge from u1 to v max sv = su2 + weight of edge from u2 to v of . . . suk + weight of edge from u3 to v In General, compute by increasing Topological Order:

sv = max (su + weight of edge from u to v) u є Predecessors(v) Generalizing to Directed Acylclic Grid Graphs Computing the score for point x is given by the recurrence relation:

max s + weight of vertex (u, v) where sv = u of u є Predecessors(v)

• Predecessors (v) – set of vertices that have edges leading to v

•What is the running time for a DAG G(V, E) , where V is the set of all vertices and E is the set of all edges?

Answer: O(E) since each edge is evaluated once Outline

• DNA Sequence Comparison: Biological background • The Sequence Alignment problem • First Biological Success Stories • Grid Graphs: Manhattan Tourist Problem • Longest Paths in Grid Graphs • Back to Longest Common Subsequence Problem • Review: divide and conquer paradigm • Reducing the space requirement of LCS Edit Graph for LCS Problem

j A T C T G A T C 0 1 2 3 4 5 6 7 8 i 0 Every path is a common T 1 subsequence.

G 2 Every diagonal edge adds an extra C 3 element to common A 4 subsequence

T 5 LCS Problem: Find a path with A 6 maximum number of diagonal edges C 7 Alignment: Dynamic Programming

si,j = si-1, j-1+1 if vi = wj

max si-1, j+0

si, j-1+0

This recurrence corresponds to the Manhattan Tourist problem (three incoming edges into a vertex) with all horizontal and vertical edges weighted by zero and diagonal edges weighted by one. Alignment as a Path in the Edit Graph

Every path in the edit graph corresponds to an alignment: Alignment as a Path in the Edit Graph

Old Alignment 0122345677 v= AT_GTTAT_ w= ATCGT_A_C 0123455667

New Alignment 0122345677 v= AT_GTTAT_ w= ATCG_TA_C 0123445667 Alignment as a Path in the Edit Graph

0122345677 v= AT_GTTAT_ w= ATCGT_A_C 0123455667

(0,0) , (1,1) , (2,2), (2,3), (3,4), (4,5), (5,5), (6,6), (7,6), (7,7) Alignment: Dynamic Programming

si,j = si-1, j-1+1 if vi = wj

max si-1, j

si, j-1 Dynamic Programming Example

Initialize 1st row and 1st column to be all zeroes. Or, to be more precise, initialize 0th row and 0th column to be all zeroes. Dynamic Programming Example

Si,j = Si-1, j-1 value from NW +1, if vi = wj max Si-1, j  value from North (top)  Si, j-1 value from West (left) Dynamic Programming Example

A T C G T A C

A

Si,j = Si-1, j-1 value from NW +1, if v = w T i j max Si-1, j  value from North (top)  value from West (left) G Si, j-1

T T A T Alignment: Backtracking

Arrows show where the score originated from. if from the top if from the left

if vi = wj Alignment: Dynamic Programming

si,j = si-1, j-1+1 if vi = wj

max si-1, j

si, j-1 LCS Algorithm

1. LCS(v,w) 2. for i  1 to n 3. si,0  0 4. for j  1 to m 5. s0,j  0 6. for i  1 to n 7. for j  1 to m 8. si-1,j 9. si,j  max si,j-1 10. si-1,j-1 + 1, if vi = wj

11. “ “ if si,j = si-1,j • bi,j  “ “ if si,j = si,j-1 • “ “ if si,j = si-1,j-1 + 1 • • return (sn,m, b) Now What?

• LCS(v,w) created the alignment grid

• Now we need a way to read the best alignment of v and w

• Follow the arrows backwards from sink Printing LCS: Backtracking

1. PrintLCS(b,v,i,j) 2. if i = 0 or j = 0 3. return

4. if bi,j = “ “ 5. PrintLCS(b,v,i-1,j-1)

6. print vi 7. else

8. if bi,j = “ “ 9. PrintLCS(b,v,i-1,j) 10. else 11. PrintLCS(b,v,i,j-1) LCS Runtime

• It takes O(nm) time to fill in the nxm dynamic programming matrix.

• Why O(nm)? The pseudocode consists of a nested “for” loop inside of another “for” loop to set up a nxm matrix.

• Memory? Naively O(nm) – we next show how to reduce it to O(n) by combining divide and conquer with dynamic programming. Outline

• DNA Sequence Comparison: Biological background • The Sequence Alignment problem • First Biological Success Stories • Grid Graphs: Manhattan Tourist Problem • Longest Paths in Grid Graphs • Back to Longest Common Subsequence Problem • Review: divide and conquer paradigm • Reducing the space requirement of LCS Divide & Conquer Algorithms Divide and Conquer Algorithms

– Divide problem into sub-problems – Conquer by solving sub-problems recursively. If the sub-problems are small enough, solve them in brute force fashion – Combine the solutions of sub-problems into a solution of the original problem (tricky part) Sorting Problem Revisited

• Given: an unsorted array 5 2 4 7 1 3 2 6

• Goal: sort it 1 2 2 3 4 5 6 7 Mergesort: Divide Step

Step 1 – Divide 5 2 4 7 1 3 2 6

5 2 4 7 1 3 2 6

5 2 4 7 1 3 2 6

5 2 4 7 1 3 2 6 log(n) divisions to split an array of size n into single elements Mergesort: Conquer Step

Step 2 – Conquer 5 2 4 7 1 3 2 6 O(n)

2 5 4 7 1 3 2 6 O(n)

2 4 5 7 1 2 3 6 O(n)

1 2 2 3 4 5 6 7 O(n) logn iterations, each iteration takes O(n) time. Total Time: O(n logn) Mergesort: Combine Step Step 3 – Combine

5 2 2 5 • 2 arrays of size 1 can be easily merged to form a sorted array of size 2 • 2 sorted arrays of size n and m can be merged in O(n+m) time to form a sorted array of size n+m Mergesort: Combine Step

Combining 2 arrays of size 4

2 4 5 7 2 4 5 7 1 1 2 1 2 3 6 2 3 6

4 5 7 4 5 7 1 2 2 1 2 2 3 2 3 6 3 6

4 5 7 Etcetera… 1 2 2 3 4 6 1 2 2 3 4 5 6 7 Merge Algorithm

1. Merge(a,b) 2. n1  size of array a 3. n2  size of array b

4. an1+1   5. an2+1   6. i  1 7. j  1 8. for k  1 to n1 + n2

9. if ai < bj

10. ck  ai 11. i  i +1 12. else

13. ck  bj 14. j j+1 15. return c Mergesort: Example

20 4 7 6 1 3 9 5

Divide 20 4 7 6 1 3 9 5

20 4 7 6 1 3 9 5

20 4 7 6 1 3 9 5

4 20 6 7 1 3 5 9 Conquer 4 6 7 20 1 3 5 9

1 3 4 5 6 7 9 20 MergeSort Algorithm

1. MergeSort(c) 2. n  size of array c 3. if n = 1 4. return c 5. left  list of first n/2 elements of c 6. right  list of last n-n/2 elements of c 7. sortedLeft  MergeSort(left) 8. sortedRight  MergeSort(right) 9. sortedList  Merge(sortedLeft,sortedRight) 10. return sortedList MergeSort: Running Time

• The problem is simplified to baby steps – for the i’th merging iteration, the complexity of the problem is O(n) – number of iterations is O(log n) – running time: O(n logn) Divide and Conquer Approach to LCS

Path(source, sink) • if(source & sink are in consecutive columns) • output the longest path from source to sink • else • middle ← middle vertex between source & sink • Path(source, middle) • Path(middle, sink) Divide and Conquer Approach to LCS

Path(source, sink) • if(source & sink are in consecutive columns) • output the longest path from source to sink • else • middle ← middle vertex between source & sink • Path(source, middle) • Path(middle, sink)

The only problem left is how to find this “middle vertex”! Computing Alignment Path Requires Quadratic Memory

Alignment Path m • Space complexity for computing alignment path for sequences of length n and m is O(nm) n • We need to keep all backtracking references in memory to reconstruct the path (backtracking) • Note that the longest path itself is linear in size. Crossing the Middle Line We want to calculate the longest m/2 m path from (0,0) to (n,m) that passes through (i,m/2) where i ranges from 0 to n and represents the i-th row Define

Prefix(i) length(i) Suffix(i) as the length of the longest path n from (0,0) to (n,m) that passes through vertex (i, m/2) Crossing the Middle Line m/2 m

Prefix(i)

Suffix(i) n

Define (mid,m/2) as the vertex where the longest path crosses the middle column.

length(mid, m/2) = optimal length = max0i n length(i) Computing Prefix(i) • prefix(i) is the length of the longest path from (0,0) to (i,m/2) • Compute prefix(i) by dynamic programming in the left half of the matrix

store prefix(i) column

0 m/2 m Computing Suffix(i) • suffix(i) is the length of the longest path from (i,m/2) to (n,m) • suffix(i) is the length of the longest path from (n,m) to (i,m/2) with all edges reversed • Compute suffix(i) by dynamic programming in the right half of the “reversed” matrix

store suffix(i) column

0 m/2 m Length(i) = Prefix(i) + Suffix(i) • Add prefix(i) and suffix(i) to compute length(i): • length(i)=prefix(i) + suffix(i) • You now have a middle vertex of the maximum path (i,m/2) as maximum of length(i)

0 middle point found

i

0 m/2 m Finding the Middle Point 0 m/4 m/2 3m/4 m Finding the Middle Point again 0 m/4 m/2 3m/4 m And Again 0 m/8 m/4 3m/8 m/2 5m/8 3m/4 7m/8 m Time = Area: First Pass

• On first pass, the algorithm covers the entire area

Area = nm Time = Area: First Pass

• On first pass, the algorithm covers the entire area

Area = nm Computing Computing But according to this figure this computation seems to prefix(i) suffix(i) require O(m n) space!

However, note that in each such DP step we only compute the Scores, note the paths… Computing Alignment Score with Linear Memory

Alignment Score • Space complexity of 2 computing just the score itself is O(n) • We only need the previous nn column to calculate the current column, and we can then throw away that previous column once we’re done using it Computing Alignment Score: Recycling Columns

Only two columns of scores are saved at any given time

memory for column 1 memory for column is used to calculate 2 is used to calculate column 3 column 4 Time = Area: First Pass

• On first pass, the algorithm covers the entire area Area = nm Space requirement: O(n)? Computing Computing (In addition to the two prefix(i) suffix(i) recycled columns used for the rectangle DP, we maintain an O(n) sized vector to store the accumulated mid points). Time = Area: Second Pass

• On second pass, the algorithm covers only 1/2 of the area

Area/2 Time = Area: Third Pass

• On third pass, only 1/4th is covered.

Area/4 Geometric Reduction At Each Iteration 1 + ½ + ¼ + ... + (½)k ≤ 2

• Runtime: O(Area) = O(nm) 5th pass: 1/16

3rd pass: 1/4

first pass: 1 4th pass: 1/8

2nd pass: 1/2