Recursion, Dynamic Programming, Divide & Conquer: Sequence Alignment, Quicksort
Recursion, Dynamic Programming, Divide & Conquer: Sequence Alignment, Quicksort
Veronica
Gaspes
[email protected]
www.hh.se/staff/vero/itads
lecture 4
p.1/36
lecture 4
Tower of Hanoi
p.2/36
Tower of Hanoi
lecture 4
p.3/36
lecture 4
p.4/36
Recursion
Recursion
lecture 4
p.5/36
lecture 4
Fibonacci numbers
Consider the following sequence of numbers
1 1 1+1 2 1+2 3 2+3 5 3+5 8
p.6/36
Fibonacci numbers
5+8 13
8+13 2
!
1
f ib(n) =
f ib(n 1) + f ib(n 2)
lecture 4
if n = 0 or n = 1
if n 2
p.7/36
lecture 4
p.8/36
Fibonacci numbers
Nice, all the rules are followed (base cases, progression,
belief!)
lecture 4
lecture 4
p.10/36
Quicksort
13 81 92 43 31 65 57 26 75 0
13 81 92 43 31 65 57 26 75 0
Pivot?
Partition
13 0 26 43 57 31
65
92 75 81
0 13 26 31 43 57
65
75 81 92
p.11/36
lecture 4
p.12/36
Quicksort
Quicksort
Auxiliary methods
partition
quicksort( a, low, i - 1 );
// Pivot at i
quicksort( a, i + 1, high );
2. Partition. All smaller than the pivot to the left, all larger
to the right.
Loop through the array from low upwards and from
high downwards.
Stop on elements that are on the wrong half.
Exchange elements when needed and continue
looping until all elements are in the proper half.
lecture 4
}
}
p.13/36
lecture 4
p.14/36
Quicksort
Quicksort
Small array
lecture 4
p.15/36
lecture 4
p.16/36
Quicksort - analysis
Quicksort
Partition
// Place pivot at position high - 1
swapReferences( a, middle, high - 1 );
T pivot = a[ high - 1 ];
// Begin partitioning
int i, j;
for( i = low, j = high - 1; ; ){
while( a[ ++i ].compareTo( pivot ) < 0 );
while( pivot.compareTo( a[ --j ] ) < 0 );
if( i >= j ) break;
swapReferences( a, i, j );
}
// Restore pivot
swapReferences( a, i, high - 1 );
lecture 4
Divide it into two halves takes O(c) to pick the pivot and
O(N ) to partition. So division is O(N ).
p.17/36
lecture 4
Quicksort - analysis
Fibonaccis problem
Look once more at the definition of f ib(n):
!
1
if n = 0 or n = 1
f ib(n) =
f ib(n 1) + f ib(n 2) if n 2
lecture 4
p.18/36
p.19/36
lecture 4
p.20/36
The Problem
fib(8)
fib(7)
Memoaization
Whenever we have to compute a value, check in a memo
whether we already have computed it!
fib(6)
fib(6)
fib(5)
fib(5)
fib(4)
fib(3)
fib(3)
fib(2)
fib(4)
BigInteger [] memo;
fib(3)
fib(3)
fib(2)
fib(2)
fib(1)
fib(5)
fib(4)
fib(4)
fib(4)
fib(3)
fib(2)
fib(3)
fib(3)
fib(2)
fib(1)
fib(2)
fib(2)
fib(1)
fib(1)
fib(0)
lecture 4
p.21/36
lecture 4
p.22/36
Sequence Comparison
It is easy to realize that we can fill the array from the base
cases and forward! And that we only need 2 values any
point!
(according to peptide
CNGTKESITKLVSDLNSATLEAD__VDVVVAPPFVYIDQVKSSLTGRVEISA
CNGTTDQVDKIVKILNEGQIASTDVVEVVVSPPYVFLPVVKSQLRPEIQVAA
p.23/36
lecture 4
p.24/36
p.25/36
lecture 4
p.26/36
First attempt
Second attempt
Based on the observation that Any prefix of the optimal
alignment is an optimal alignment of prefixes use the
recursion
for i = 0
j
for j = 0
i
(i, j) =
(i 1, j) + 1
min (i, j 1) + 1
(i 1, j 1) + (a , b )
i1 j1
lecture 4
where
p.27/36
lecture 4
p.28/36
Dynamic Programming
The matrix can be filled in different ways so that the values
needed in the computation are available:
0
m 1
a 2
r 3
t 4
i 5
n 6
v
_
ve
__
v
1
1
2
3
e
2
2
2
3
r
3
3
3
2
o n i c
4 5 6 7
a
8
Row by row
Column by column
vero
____
veron
_____
lecture 4
p.29/36
veroni
veronic
...
Antidiagonal by antidiagonal
?
ver
___
...
lecture 4
p.30/36
veronica
Dynamic Programming
(i 1, j) + 1
min (i, j 1) + 1
(i 1, j 1) + (a , b )
i1 j1
0
m 1
a 2
r
v
1
1
2
e
2
2
2
r
3
3
3
3 3 3
o n i c a
4 5 6 7 8
lecture 4
p.32/36
p.33/36
lecture 4
Score matrices
p.34/36
p.35/36
lecture 4
BLAST
Original sources:
A general method appplicable to search for similarities
in the amino acid sequence of two proteins by
Needleman and Wunch, JBL 1970.
Identification of common molecular subsequences by
Smith and Waterman, JBL 1981.
Basic Local Alignment Search Tool by Altschul et al.,
JBL 1990.
p.36/36