Professional Documents
Culture Documents
Sequence Alignment
Sequence Alignment
zasha
ashes
zash__a
_ashes_
Naïve algorithm
• Try every way to put in underscores
• If it works, and is best so far, record it.
• At end, return best solution.
Naïve Algorithm – Running
Time
• Strings size M,N: ( 2 M N )
Dynamic Approach – A table
• Table(x,y): best alignment for first x letters
of string 1, and first y letters of string 2
• Decide what to do with the end of string,
then look up best alignment of remainder in
Table.
e.g. ‘a’ vs. ‘s’
• “zasha” vs. “ashes”. 2 possibilities for last
letters:
– (1) match ‘a’ with ‘_’:
• best_alignment(“zash”,”ashes”)+1
– (2) match ‘s’ with ‘_’:
• best_alignment(“zasha”,”ashe”)+1
best_alignment(“zasha”,”ashes”)
=min(best_alignment(“zash”,”ashes”)+1,
best_alignment(“zasha”,”ashe”)+1)
An example
(empty) Z A S H A
(empty)
A
S
H
E
S
Example with solution
(empty) Z A S H A
(empty) 0 1 2 3 4 5
A 1 2 1 2 3 4
S 2 3 2 1 2 3
H 3 4 3 2 1 2
E 4 5 4 3 2 3
S 5 6 5 4 3 4
zasha__
_ash_es
Pseudocode (bottom-up)
Given: Strings X,Y , Table[0..x,0..y]
For i=1 to x do
Table[i,0]=i
For j=1 to y do
Table[0,j]=i
i=1, j=1
While i<=x and j<=y
If X[x]=Y[y] Then
// matches – no underscores
Table[x,y]=Table[x-1,y-1]
Else
Table[x,y]=min(Table[x-1,y],Table[x,y-1])+1
End If
i=i+1
If i>x Then
i=1
j=j+1
End If
Pseudocode (top-down)
Given: Strings X,Y , Table[0..x,0..y]
BestAlignment (x,y)
Compute Table[x-1,y] if necessary
Compute Table[x,y-1] if necessary
Compute Table[x-1,y-1] if necessary
If X[x]=Y[y] Then
// matches – no underscores
Table[x,y]=Table[x-1,y-1]
Else
Table[x,y]=min(Table[x-1,y],Table[x,y-1])+1
End If
Running time
• Every square in table is filled in once
• Filling it in is constant time
(n2) squares
alg is (n2)
Idea of dynamic Albert Q.
Dynamic
programming at Whisler
mountain