69
Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05 Shift-And und Karp- Rabin Seminar „Fortgeschrittene algorithmische Bioinformatik“ SS 2005 Wadim Bayerbach

Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Embed Size (px)

Citation preview

Page 1: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Shift-And und Karp-Rabin

Seminar„Fortgeschrittene algorithmische Bioinformatik“

SS 2005

Wadim Bayerbach

Page 2: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Gegenstand: Exaktes StringmatchingWelche Algorithmen kennen wir bereits?

• Z-Box-Algorithmus• Boyer-Moore-Algorithmus• Knuth-Morris-Prath-Algorithmus

Einführung

1

Page 3: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Gegenstand: Exaktes StringmatchingWelche Algorithmen kennen wir bereits?

• Z-Box-Algorithmus• Boyer-Moore-Algorithmus• Knuth-Morris-Prath-Algorithmus

Warum noch ein Algorithmus?.. alle bisherigen Algorithmen sind vergleichs-basiert

(comparison-based) es gibt aber noch Verfahren, die auf Bit-Operationen

basieren

Einführung

1

Page 4: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

1. Karp-Rabin-fingerprint-Methode• Grundbegriffe: H(P), H(Tr )• Fingerprints von P und T: Hp(P), Hp(Tr), • die Wahl von p als Primzahl• Hauptsatz von Karp-Rabin• randomisierter fingerprint-Algorithmus• Erweiterungen

2. Shift-And-Algorithmus• Grundbegriffe: Matrix M, Bit-Shift-Vektor, Vektor U(x)• Laufzeitanalyse und Platzverbrauch• agrep: Erweiterung auf nicht-exaktes Stringmatching

Unser Fahrplan

2

Page 5: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Zunächst einige Konventionen:• P – Pattern der Länge n • T – Template der Länge m

Konventionen

3

Page 6: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

von R.Karp und M.Rabin 1987 publiziert ursprünglicher Name: Randomisierte Fingerprint-Methode lineare Laufzeit, kaum Fehler keine Fehler, überlineare Laufzeit Sichtweise als arithmetisches Problem

Karp-Rabin-fingerprint-Methode

4

Page 7: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

von R.Karp und M.Rabin 1987 publiziert ursprünglicher Name: Randomisierte Fingerprint-Methode lineare Laufzeit, kaum Fehler keine Fehler, überlineare Laufzeit Sichtweise als arithmetisches Problem

O.B.d.A:

Karp-Rabin-fingerprint-Methode

4

{0,1}

Page 8: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Definition 1:Für einen Text-String T bezeichnen wir mit einen Substring von T der Länge n, welcher an Position r in T anfängt. Falls n aus dem Kontext bekannt, so schreibe

Arithmetik ersetzt Vergleich

5

nrT

rT

Page 9: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Definition 1:Für einen Text-String T bezeichnen wir mit einen Substring von T der Länge n, welcher an Position r in T anfängt. Falls n aus dem Kontext bekannt, so schreibe

Beispiel: 123456789012T = 011010111100 = 0111

Arithmetik ersetzt Vergleich

5

nrT

rT

46T

Page 10: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Definition 2:Für ein binäres Pattern sei ,

wobei P(i) Zeichen in P an der Stelle i ist.

Analog

Arithmetik ersetzt Vergleich

6

1

( ) : 2 ( )n

n i

i

H P P i

1

( ) : 2 ( 1)n

n ir

i

H T T r i

Page 11: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Definition 2:Für ein binäres Pattern sei ,

wobei P(i) Zeichen in P an der Stelle i ist.

Analog

Beispiel:P = 10011 n = 5,

T = 10010111010010, n = 4, r = 5

Arithmetik ersetzt Vergleich

6

1

( ) : 2 ( )n

n i

i

H P P i

1

( ) : 2 ( 1)n

n ir

i

H T T r i

4 3 2 1 0( ) 1*2 0*2 0*2 1*2 1*2 19H P

( ) 7rH T

Page 12: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Satz 1:Ein Pattern P tritt in einem Template T an der Position r genau dann auf, wenn .

Beweisidee:jede Integer-Zahl kann eindeutig als eine Summe von positiven Zweierpotenzen geschrieben werden

Wann tritt P in T auf ?

7

( ) ( )rH P H T

Page 13: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Satz 1:Ein Pattern P tritt in einem Template T an der Position r genau dann auf, wenn .

Beweisidee:jede Integer-Zahl kann eindeutig als eine Summe von positiven Zweierpotenzen geschrieben werden

Stringmatching als numerisches Problem: Vergleiche nicht zwei Zeichen, sondern zwei Zahlen

Wann tritt P in T auf ?

7

( ) ( )rH P H T

Page 14: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Problem: für große n ist die Berechnung nicht effizient.

Problem

8

Page 15: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Problem: für große n ist die Berechnung nicht effizient. für müssen wir Integers bis bewältigen können Kann man aus berechnen?...

Problem

8

| | t nt

( )rH T 1( )rH T

Page 16: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Lösung: benutze Modulo-Werte (mod p)

dadurch kleine Anzahl von Bits für Darstellung es treten Fehler auf

Fingerprints von P und T

9

Page 17: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Lösung: benutze Modulo-Werte (mod p)

dadurch kleine Anzahl von Bits für Darstellung es treten Fehler auf

Definition 3:

Für eine positive ganze Zahl p sei . D.h. der Rest von der Division von durch p.

Analog: .

Die Zahlen und nennen wir fingerprints von P und T.

Fingerprints von P und T

9

( ) : ( ) modpH P H P p( )H P

( ) : ( ) modp r rH T H T p

( )pH P ( )p rH T

Page 18: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Als Resultat: Zahlen zwischen 0 und p-1.

Aber:Um zu berechnen brauchen wir wieder ! wir sind beim Anfangsproblem

wir brauchen eine neue Herangehensweise !..

Fingerprints von P und T

10

( )pH P ( )H P

Page 19: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Ausflug in die Mathematik:Sei p(x) ein Polynom des n-ten Grades:

durch sukzessive Ausklammerung erreiche folgende Form:

Horner’s Schema

11

10 1 1

0

( ) ...n

n n n in n i

i

p x a x a x a x a a x

0 1 2 3 1 0( ) (...( ) ) )... )np x a x a x a x a a x a

Page 20: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Lemma 1:

dabei übersteigt keine Zahl während der Berechnung den Wert 2p

Fingerprints von P und T

12

( ) ... (1)*2mod (2) *2mod (3) ..( ) . mod ( ) mod]{ }[ [ ]pH P P p P p P p P n p

Page 21: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Lemma 1:

dabei übersteigt keine Zahl während der Berechnung den Wert 2p

Beispiel:P=100110 und p = 7, so ist H(P)=38,

Fingerprints von P und T

12

( ) 38mod 7 3pH P 1*2mod 7 0 2 2*2mod 7 0 4 4*2mod 7 1 2 2*2mod 7 1 5 5*2mod 7 0 3

3mod 7 3

( ) ... (1)*2mod (2) *2mod (3) ..( ) . mod ( ) mod]{ }[ [ ]pH P P p P p P p P n p

Page 22: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Wir stellen fest: Anzahl von Multiplikationen und Additionen linear Zwischenergebnisse sind klein

Fingerprints von P und T

13

Page 23: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Wir stellen fest: Anzahl von Multiplikationen und Additionen linear Zwischenergebnisse sind klein

Unser jetziges Ziel:für r >1 aus mit konstanter Operationenanzahl

Wir haben:

Fingerprints von P und T

13

( )p rH T 1( )p rH T

1( ) 2* ( ) 2 ( 1) ( 1)nr rH T H T T r T r n

( ) ( ) modp r rH T H T p

Page 24: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Beispiel:Sei n=5 und r =7 12345678901234567890 T=10110110010101100111 T6= 11001 _ 2*T6= 110010 25*T(6) 100000 = 010010 + T(11) 0 = 010010 entspricht 18Folglich:

Fingerprints von P und T

14

1( ) 2* ( ) 2 ( 1) ( 1)nr rH T H T T r T r n

7( ) 18H T

6( ) 25H T 62* ( ) 50H T

1( ) [(2* ( ) mod ) (2 mod )* ( 1) ( 1)]modnp r rH T H T p p T r T r n p

Page 25: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Außerdem:

Fazit:jeder Wert von kann für r >1 in konstanter Zeit berechnet werden.

Fingerprints von P und T

15

1( ) [(2* ) ( )* ( 1) ( 1)]mo2( ) mod dmodnrp rH T T r T rH T p n pp

12 mod 2*(2 mod ) modn np p p

( )p rH T

Page 26: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Wir wissen: P tritt in T (an r) auf

Umkehrung gilt nicht !

Wahl von p als Primzahl

16

( ) ( )p p rH P H T

Page 27: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Wir wissen: P tritt in T (an r) auf

Umkehrung gilt nicht !

Definition 4:Gilt , aber P tritt in T an r nicht auf, so sagen wir es gibt einen falschen Match an Position r.

Wunsch: p möglichst klein, aber Wahrscheinlichkeit eines falschen Matches gering

Lösung: p – Primzahl

Wahl von p als Primzahl

16

( ) ( )p p rH P H T

( ) ( )p p rH P H T

Page 28: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Definiton 5:Für eine positive Zahl u bezeichnen wir mit die Anzahl aller Primzahlen, welche kleiner oder gleich als u sind.

Wahl von p als Primzahl

17

( )u

Page 29: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Definiton 5:Für eine positive Zahl u bezeichnen wir mit die Anzahl aller Primzahlen, welche kleiner oder gleich als u sind.

Satz 2 (ohne Beweis): Es gilt:

Lemma 2:Wenn ist, so ist der Produkt aller Primzahlen, die kleiner oder gleich als u sind, größer als 2u.

Wahl von p als Primzahl

17

( )u

( ) 1.26ln( ) ln( )

u uu

u u

29u

Page 30: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Definiton 5:Für eine positive Zahl u bezeichnen wir mit die Anzahl aller Primzahlen, welche kleiner oder gleich als u sind.

Satz 2 (ohne Beweis): Es gilt:

Lemma 2:Wenn ist, so ist der Produkt aller Primzahlen, die kleiner oder gleich als u sind, größer als 2u.

Beispiel:u=29 Primzahlen: 2, 5, 7, 11, 13, 17, 19, 23, 29deren Produkt: 2,156,564,410 ;während 229 = 536,870,912

Wahl von p als Primzahl

17

( )u

( ) 1.26ln( ) ln( )

u uu

u u

29u

Page 31: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Folgerung 1:

Falls und x eine Zahl kleiner gleich 2u ist, so hat x weniger als verschiedene Primteiler.

Wahl von p als Primzahl

18

( )u29u

Page 32: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Nun können wir den Hautzsatz von Karp-Rabin formulieren:

Hauptsatz:

Seien P und T zwei Strings mit .

Sei I eine positive ganze Zahl. Wenn p eine zufällig gewählte Primzahl mit p < I ist, so ist die Wahrscheinlichkeit eines falschen Matches zwischen P und T kleiner gleich .

Abschätzung abhängig von p

unabhängig von P und T !

abhängig von Längen |P | und |T |

Hauptsatz

19

29nm

( )

( )

nm

I

Page 33: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

1. Wähle eine positive ganze Zahl I2. Wähle zufällig eine Primzahl und berechne

3. in T berechne und vergleiche mit falls gleich (wahrscheinlicher) Match auf Wunsch: prüfe explizit

Aber:Laufzeit O(m) ohne expliziten Check

Randomisierter fingerprint-Algorithmus

20

( )pH Pp Ir ( )p rH T ( )pH P

Page 34: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Die Wahl von I: große I besser, kleinere Fehler-Wahrscheinlichkeit aber mehr Aufwand bei Berechnung von ,

Die Wahl von I

21

( )pH P ( )p rH T

Page 35: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Die Wahl von I: große I besser, kleinere Fehler-Wahrscheinlichkeit aber mehr Aufwand bei Berechnung von ,

Oft nimmt man:I = nm2

Satz 3:Falls I = nm2 ist, so ist die Wahrscheinlichkeit eines falschen Matches höchstens

Beispiel:Sei n = 250 und m = 4000. Wir erhalten dann:I = nm2 =4*109 < 232. Wahrscheinlichkeit eines falschen Matches höchstens

Die Wahl von I

21

( )pH P ( )p rH T

2.53

m

32.53

0.0006325 104000

Page 36: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Nahe liegende Idee: k viel Primzahlen zu nehmen Match, falls für jedes i=1,…, k falscher Match, falls ,

aber für jedes i=1,…, k

Erweiterungen

22

1 2, ,..., kp p p( ) ( )i ip p rH P H T

( ) ( )rH P H T( ) ( )i ip p rH P H T

Page 37: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Nahe liegende Idee: k viel Primzahlen zu nehmen Match, falls für jedes i=1,…, k falscher Match, falls ,

aber für jedes i=1,…, k

Satz 4:Werden Primzahlen zwischen 1 und I gewählt und k fingerprints berechnet, so liegt die Wahrscheinlichkeit eines falschen Matches bei höchstens .

Erweiterungen

22

1 2, ,..., kp p p

( ) ( )rH P H T

( )

( )

knm

I

( ) ( )i ip p rH P H T

( ) ( )i ip p rH P H T

Page 38: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Beispiel:Sei n = 250 und m = 4000 So liegt die Wahrscheinlichkeit eines falschen Matches

bei höchstens !

Laufzeit erhöht sich lediglich um einen konstanten Faktor 4 !

Erweiterungen

23

4122.53

104000

Page 39: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Karp-Rabin-fingerprint-Methode ist sehr effizient hat sehr kleine Wahrscheinlichkeit eines Fehlerauftretens, dafür Laufzeit O(m) ist exakt, dafür Laufzeit O(mn)

Fazit

24

Page 40: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

von R.Baeza-Yates und G.Gonnet entworfen relativ einfache bit-orientierte Methode für relativ kleine Pattern sehr effizient Ursprünglicher Name: Shift-Or

Shift-And-Algorithmus

25

Page 41: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Definition 1:Sei MM ein n x m Matrix mit Einträgen aus {0, 1}, i = 1..n, j = 1..m.Ein Eintrag M(i,j)M(i,j) ist 1 dann, und nur dann, wenn die ersten i Zeichen von P exakt mit i Zeichen in T matchen, welche an Position j enden.

d.h. M(i,j) = 1 P[1..i] = T[j-i+1..j]

Shift-And-Algorithmus

26

Page 42: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

1234 iP = dach

12345678 jT = verdacht so ist M(1,4) = M(2,5) = M(3,6) = M(4,7) = 1 alle restlichen Einträge sind 0

Beispiel

27

v e r d a c h t

d 0 0 0 1 0 0 0 0

a 0 0 0 0 1 0 0 0

c 0 0 0 0 0 1 0 0

h 0 0 0 0 0 0 1 0

1 2 3 4 5 6 7 8

1

2

3

4

Page 43: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Interessant ist die letzte Zeile: Die Einträge M(n,j ) = 1 signalisieren das Vorkommen

von P in T, welches an der Position j endet Wir brauchen also eigentlich nur die letzte Zeile der

Matrix

Beispiel

28

v e r d a c h t

d 0 0 0 1 0 0 0 0

a 0 0 0 0 1 0 0 0

c 0 0 0 0 0 1 0 0

h 0 0 0 0 0 0 1 0

i

j

Page 44: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Hilfsmittel:Für jedes Zeichen unseres Alphabetes berechnen wir einen Vektor der Dimension n, welcher an allen denjenigen Stellen einen 1-Eintrag hat, an denen das Zeichen x in P vorkommt.

d.h.

Shift-And-Algorithmus

29

( )x ( )U x

( )[ ] 1U x i [ ]P i x

Page 45: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Hilfsmittel:Für jedes Zeichen unseres Alphabetes berechnen wir einen Vektor der Dimension n, welcher an allen denjenigen Stellen einen 1-Eintrag hat, an denen das Zeichen x in P vorkommt.

d.h.

Beispiel:

P = cgtcgtattca

U(c) = 10010000010

U(t) = 00100101100

Shift-And-Algorithmus

29

( )x ( )U x

( )[ ] 1U x i [ ]P i x

Page 46: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Definition 2: Ein Bit-Shift(j-1)-Vektor ist ein Vektor, der aus (j-1)-ten Spalte wie folgt entsteht:• Shifte die Spalte um eine Position nach unten• Verwerfe den Überlauf und setze den ersten Spalteneintrag gleich 1.

Shift-And-Algorithmus

30

Page 47: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Definition 2: Ein Bit-Shift(j-1)-Vektor ist ein Vektor, der aus (j-1)-ten Spalte wie folgt entsteht:• Shifte die Spalte um eine Position nach unten• Verwerfe den Überlauf und setze den ersten Spalteneintrag gleich 1.

Shift-And-Algorithmus

30

v e r d a c h t

d 0 0 0 1 0 0 0 0

a 0 0 0 0 1 0 0 0

c 0 0 0 0 0 1 0 0

h 0 0 0 0 0 0 1 0

1

0( 1)

1

0

Bit Shift j

j-1

Page 48: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

spaltenweise von links nach rechts

Konstruktion der Matrix M

31

Page 49: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

spaltenweise von links nach rechts

Schritt 1: Fülle erste Spalte von M mit Nullen, falls Andererseits setze M(1,1)=1

Konstruktion der Matrix M

31

(1) (1)T P

Page 50: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

spaltenweise von links nach rechts

Schritt 1: Fülle erste Spalte von M mit Nullen, falls Andererseits setze M(1,1)=1

Schritt 2: Für alle j > 1:M(j) = Bit-Shift(j-1) AND U(T(j)), wobei M(j) – j-te Spalte von M ist

Konstruktion der Matrix M

31

(1) (1)T P

Page 51: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Beispiel:P = acag M(j) = Bit-Shift(j-1) AND U(T(j))T = actacagacga

Konstruktion der Matrix M (2)

32

a c t a c a g a c g a

a 1

c 0

a 0

g 0

j = 1

Erster Eintrag 1, da T(1)=P(1)

Page 52: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Beispiel:P = acag M(j) = Bit-Shift(j-1) AND U(T(j))T = actacagacga

Konstruktion der Matrix M (2)

32

1

1(1)

0

0

Bit Shift

0

1( )

0

0

U c

a c t a c a g a c g a

a 1 0

c 0 1

a 0 0

g 0 0

j = 2, T(2) = c

1 0 0

1 1 1(2)

0 0 0

0 0 0

M AND

Page 53: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Beispiel:P = acag M(j) = Bit-Shift(j-1) AND U(T(j))T = actacagacga

Konstruktion der Matrix M (2)

32

1

0(2)

1

0

Bit Shift

0

0( )

0

0

U t

a c t a c a g a c g a

a 1 0 0

c 0 1 0

a 0 0 0

g 0 0 0

j = 3, T(3) = t

1 0 0

0 0 0(3)

1 0 0

0 0 0

M AND

Page 54: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Beispiel:P = acag M(j) = Bit-Shift(j-1) AND U(T(j))T = actacagacga

Konstruktion der Matrix M (2)

32

1

0(5)

1

0

Bit Shift

1

0( )

1

0

U a

a c t a c a g a c g a

a 1 0 0 1 0 1

c 0 1 0 0 1 0

a 0 0 0 0 0 1

g 0 0 0 0 0 0

j = 6, T(6) = a

1 1 1

0 0 0(6)

1 1 1

0 0 0

M AND

Page 55: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Beispiel:P = acag M(j) = Bit-Shift(j-1) AND U(T(j))T = actacagacga

Konstruktion der Matrix M (2)

32

a c t a c a g a c g a

a 1 0 0 1 0 1 0 1 0 0 1

c 0 1 0 0 1 0 0 0 1 0 0

a 0 0 0 0 0 1 0 0 0 0 1

g 0 0 0 0 0 0 1 0 0 0 0

Page 56: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Für alle i >1 gilt: M(i,j) = 1 gdw. 1. P[1..i-1] = T[j-i+1..j-1] und

Korrektheit

33

a c a g

a c a g a …t…

Page 57: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Für alle i >1 gilt: M(i,j) = 1 gdw. 1. P[1..i-1] = T[j-i+1..j-1] und 2. P[i] = T[j]

Erste Bedingung ist wahr, falls M(i-1,j-1) = 1

Korrektheit

33

a c a g

a c a g a …t…

a c t a c a g a c g a

a 1 0 0 1 0 1 0 1 0 0 1

c 0 1 0 0 1 0 0 0 1 0 0

a 0 0 0 0 0 1 0 0 0 0 1

g 0 0 0 0 0 0 1 0 0 0 0

6

Page 58: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Für alle i >1 gilt: M(i,j) = 1 gdw. 1. P[1..i-1] = T[j-i+1..j-1] und 2. P[i] = T[j]

Erste Bedingung ist wahr, falls M(i-1,j-1) = 1Zweite Bedingung ist wahr, falls U(T(j))[i] = 1

Korrektheit

33

a c a g

a c a g a …t…

a c t a c a g a c g a

a 1 0 0 1 0 1 0 1 0 0 1

c 0 1 0 0 1 0 0 0 1 0 0

a 0 0 0 0 0 1 0 0 0 0 1

g 0 0 0 0 0 0 1 0 0 0 0

T(j) = g

0

0( )

0

1

U g

1

1(6)

0

1

Bit Shift

i = 4

6

Page 59: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Für i =1: Durch Bit-Shifting erzeugte 1 ist notwendig für

UND-Verknüpfung

Insgesamt:Algorithmus erzeugt korrekte Matrix M.

Korrektheit

34

a c t a c a g a c g a

a 1 0 0 1 0 1 0 1 0 0 1

c 0 1 0 0 1 0 0 0 1 0 0

a 0 0 0 0 0 1 0 0 0 0 1

g 0 0 0 0 0 0 1 0 0 0 0

T(j) = a

1

0( )

1

0

U a

1

0(7)

0

0

Bit Shift

7

Page 60: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Laufzeit: im „worst-case“ ist die Anzahl von Bit-Operationen sehr effizient, wenn n kleiner als die Größe des Computer-Wortes U(x) und Bit-Shift(j-1) in einem Wort kodieren Bit-Shift und VerUNDung sind einfache Wort-OP‘s

Laufzeitanalyse und Platzverbrauch

35

( )mn

Page 61: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Laufzeit: im „worst-case“ ist die Anzahl von Bit-Operationen sehr effizient, wenn n kleiner als die Größe des Computer-Wortes U(x) und Bit-Shift(j-1) in einem Wort kodieren Bit-Shift und VerUNDung sind einfache Wort-OP‘s

Platzverbrauch: für jedes berechne U(x) O(n) für j-te Spalte brauchen wir neben U(x) nur (j-1)-te Spalte

Insgesamt: O(n)

Laufzeitanalyse und Platzverbrauch

35

( )mn

x

Page 62: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

sehr gut für nicht so große Pattern z.B. Wörter der deutschen oder englischen Sprache

rein theoretisch nicht linear aber ist praktisch und wird eingesetzt

Fazit

36

Page 63: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

von S.Wu und U.Manber entwickelt im Unix-Werkzeug agrep implementiert

Erweiterung des Shift-And-Algorithmus

37

Page 64: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

von S.Wu und U.Manber entwickelt im Unix-Werkzeug agrep implementiert Shift-And-Algorithmus mit „Fehlern“ d.h. wir erlauben bestimmte (kleine) Anzahl von Mismatches, Insertions oder Deletions

für eine kleine Anzahl von Fehlern und kleine Pattern ist agrep sehr effizient

Wir beschränken uns auf Mismatches

Erweiterung des Shift-And-Algorithmus

37

Page 65: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Definition 3:Seien Strings P und T gegeben mit |P| = n, |T| = m; sei MMkk eine Matrix mit Einträgen aus {0, 1}. Wir setzen M(i, j) = 1 dann und nur dann, wenn mindestens i - k Zeichen von P[1..i] mit i Zeichen von T bis zur Position j matchen.

agrep

38

Page 66: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Definition 3:Seien Strings P und T gegeben mit |P| = n, |T| = m; sei MMkk eine Matrix mit Einträgen aus {0, 1}. Wir setzen M(i, j) = 1 dann und nur dann, wenn mindestens i - k Zeichen von P[1..i] mit i Zeichen von T bis zur Position j matchen.

Erweiterung von M(i, j)-Definition, M0 M Mk(n, j) = 1 P tritt in Suffix von T[1..j] mit höchstens k Mismatches auf für k müssen M0, M1 ,…, Mk berechnet werden für k = 3, 4 extrem schnell

agrep

38

Page 67: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Sei k fixiert.Es müssen MMss für alle s=0,..,k berechnet werden. berechne M0

berechne Ms aus Ms-1 wie folgt: Ms(j) = Ms-1(j) OR [Bit-Shift(Ms(j-1)) AND U(T(j))] OR Ms-1(j-1)

Berechnung von Mk

39

Page 68: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Sei k fixiert.Es müssen MMss für alle s=0,..,k berechnet werden. berechne M0

berechne Ms aus Ms-1 wie folgt: Ms(j) = Ms-1(j) OR [Bit-Shift(Ms(j-1)) AND U(T(j))] OR Ms-1(j-1)

Denn: P[1..i] matcht mit Suffix von T[1..j] mit höchstens s-1 Missmatches ODER P[1..i-1] matcht mit Suffix von T[1..j-1] mit höchstens s Missmatches UND es gilt P[i] = T[j] ODER P[1..i-1] matcht mit Suffix von T[1..j-1] mit höchstens s-1 Missmatches

Berechnung von Mk

39

Page 69: Seminar Fortgeschrittene algorithmische Bioinformatik, SS05 Shift-And und Karp-Rabin Seminar Fortgeschrittene algorithmische Bioinformatik SS 2005 Wadim

Seminar „Fortgeschrittene algorithmische Bioinformatik“, SS05

Fragen ?

40

Vielen Dank fürs Zuhören!