Download Divide and Conquer Algorithms: Mergesort and Block Alignment - Prof. Wei Wang and more Study notes Computer Science in PDF only on Docsity!
10/2/2008 Comp 590/Comp 790-90 Fall 2008 1
Lecture 12:Lecture 12:
Divide and Conquer AlgorithmsDivide and Conquer Algorithms
Study Chapter 7.1 –Study Chapter 7.1– 7.47.
Divide and 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)
10/2/2008 Comp 590/Comp 790-90 Fall 2008 3
Sorting Problem Revisited Sorting Problem Revisited
- Given: an unsorted array
- Goal: sort it
5 2 4 7 1 3 2 6
1 2 2 3 4 5 6 7
Mergesort: Divide Step 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
10/2/2008 Comp 590/Comp 790-90 Fall 2008 7
Mergesort: Merge Mergesort: Merge
Merge 2 arrays of size 4
1 2 3 6
(^2 ) 2 3 6
(^2 )
2 3 6
(^4 571 ) 3 6
(^4 )
6
(^4 571 2234) Etcetera… 1 2 2 3 4 5 6 7
Merge Algorithm Merge Algorithm
- Merge(a,b)
- n1 ← size of arraya
- n2 ← size of arrayb
- a (^) n1+1 ← ∞
- a (^) n2+1 ← ∞
- i ← 1
- j ← 1
- for k ← 1 ton1 +n
- if a (^) i <b (^) j
- c (^) k ← a (^) i
- i ← i + 1
- else
- c (^) k ← b (^) j
- j← j+ 1
- return c
10/2/2008 Comp 590/Comp 790-90 Fall 2008 9
Mergesort: Example Mergesort: Example
20 4 7 6 1 3 9 5
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
4 6 7 20 1 3 5 9
1 3 4 5 6 7 9 20
Divide
Conquer
MergeSort Algorithm MergeSortAlgorithm
- MergeSort(c)
- n ← size of arrayc
- if n = 1
- return c
- left ← list of firstn/2 elements ofc
- right ← list of lastn-n/2 elements ofc
- sortedLeft ← MergeSort(left)
- sortedRight ← MergeSort(right)
- sortedList ← Merge(sortedLeft,sortedRight)
- return sortedList
10/2/2008 Comp 590/Comp 790-90 Fall 2008 13
Computing Alignment Path Requires Computing Alignment Path Requires Quadratic MemoryQuadratic Memory
Alignment Path
- Space complexity for computing alignment path for sequences of length n and m is O( nm )
- We need to keep all backtracking references in memory to reconstruct the path (backtracking)
n
m
Computing Alignment Score with Linear Computing Alignment Score with Linear MemoryMemory
Alignment Score
- Space complexity of computing just the score itself is O( n )
- We only need the previous column to calculate the current column, and we can then throw away that previous column once we’re done using it
2
n
10/2/2008 Comp 590/Comp 790-90 Fall 2008 15
Computing Alignment Score: Recycling Columns Computing Alignment Score: Recycling Columns
memory for column 1 is used to calculate column 3
memory for column 2 is used to calculate column 4
Only two columns of scores are saved at any
given time
Computing the Alignment Path Computing the Alignment Path
n
m/2 m
Prefix(i) Suffix(i)
We want to calculate the longest 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 length ( i ) as the length of the longest path from (0,0) to ( n , m ) that passes through vertex ( i , m /2)
10/2/2008 Comp 590/Comp 790-90 Fall 2008 19
Computing Suffix( Computing Suffix( ii ))
- 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 ) in the right half of the “reversed” matrix
0 m/2 m
store suffix ( i ) column
Length(i) Length(i) == PrefixPrefix (( ii ) +) + SuffixSuffix (( ii ))
- 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)
middle point found
0 m/2 m
0
i
10/2/2008 Comp 590/Comp 790-90 Fall 2008 21
Finding the Middle Point Finding the Middle Point
0 m/4 m/2 3m/4 m
Finding the Middle Point again Finding the Middle Point again
0 m/4 m/2 3m/4 m
10/2/2008 Comp 590/Comp 790-90 Fall 2008 25
Time = Area: Second Pass Time = Area: Second Pass
- On second pass, the algorithm covers only
1/2 of the area
Area/
Time = Area: Third Pass Time = Area: Third Pass
- On third pass, only 1/4th is covered.
Area/
10/2/2008 Comp 590/Comp 790-90 Fall 2008 27
Geometric Reduction At Each Iteration Geometric Reduction At Each Iteration
1 + ½ + ¼ + ... + (½) k^ ≤ 2
- Runtime: O( Area ) = O( nm )
- Total Space: O(n) for score computation, O(n+m) to store the optimal alignment
first pass: 1
2 nd^ pass: 1/
3 rd^ pass: 1/
5 th^ pass: 1/
4 th^ pass: 1/
Is It Possible to Align Sequences in Is It Possible to Align Sequences in Subquadratic Time?SubquadraticTime?
- Dynamic Programming takes O( n^2 ) for global alignment
- Can we do better?
- Yes, use Four-Russians Speedup
10/2/2008 Comp 590/Comp 790-90 Fall 2008 31
Block Alignment Block Alignment
- Block alignment of sequences u and v:
1.An entire block in u is aligned with an entire block in v 2.An entire block is inserted 3.An entire block is deleted
- Block path : a path that traverses every t x t square through its corners
Block Alignment: Examples Block Alignment: Examples
valid invalid
10/2/2008 Comp 590/Comp 790-90 Fall 2008 33
Block Alignment Problem Block Alignment Problem
- Goal: Find the longest block path through an edit graph
- Input: Two sequences, u and v partitioned into blocks of size t. This is equivalent to an n x n edit graph partitioned into t x t subgrids
- Output: The block alignment of u and v with the maximum score (longest block path through the edit graph
Constructing Alignments within Blocks Constructing Alignments within Blocks
- To solve: compute alignment score ß i,j for each pair of blocks | u (i-1)t+1 … u it | and | v (j-1)t+1 … v jt|
- How many blocks are there per sequence?
( n / t ) blocks of size t
- How many pairs of blocks for aligning the two sequences? ( n / t ) x ( n / t )
- For each block pair, solve a mini-alignment problem of size t x t
10/2/2008 Comp 590/Comp 790-90 Fall 2008 37
Block Alignment Runtime Block Alignment Runtime
- Indices i , j range from 0 to n / t
- Running time of algorithm is
O( [ n / t ]*[ n / t ]) = O( n^2 / t^2 )
if we don’t count the time to compute each β i,j
Block Alignment Runtime Block Alignment Runtime (cont’(cont’d)d)
- Computing all β i,j requires solving ( n / t )*( n / t )
mini block alignments, each of size ( t * t )
- So computing all β i,j takes time
O([ n / t ][ n / t ] t * t ) = O( n^2 )
- This is the same as dynamic programming
- How do we speed this up?
10/2/2008 Comp 590/Comp 790-90 Fall 2008 39
Four Russians Technique Four Russians Technique
- Let t = log( n ), where t is block size, n is sequence size.
- Instead of having ( n / t )*( n / t ) mini-alignments, construct 4 t^ x 4 t^ mini-alignments for all pairs of strings of t nucleotides (huge size), and put in a lookup table.
- However, size of lookup table is not really that huge if t is small. Let t = (log n )/4. Then 4 t^ x 4 t^ = n
Look- Look-up Table for Four Russians Techniqueup Table for Four Russians Technique
Lookup table “ Score ”
AAAAAA AAAAAC AAAAAG AAAAAT AAAACA …
AAAAAAAAAAACAAAAAGAAAAATAAAACA…
each sequence has t nucleotides
size is only n , instead of ( n / t )*( n / t )