simpson’s hidden talents · simpson’s hidden talents. problem agiven two strings and find the...

47
Simpson’s Hidden Talents

Upload: others

Post on 27-Sep-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

Simpson’s Hidden Talents

Page 2: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

Problem

Given two strings and find the longest prefix of that is a suffix of

S1 S2S1S2

Page 3: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

Problem

Given two strings and find the longest prefix of that is a suffix of

S1 S2S1S2

ababcababdaba

babacbadababc

Example:S1 :S2 :

Page 4: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

Problem

Given two strings and find the longest prefix of that is a suffix of

S1 S2S1S2

ababcababdaba

babacbadababc

Example:S1 :S2 :

Page 5: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

First Idea

a b a b c a b a b d a b a

b a b a c b a d a b a b c

S1 :

S2 :

Page 6: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

First Idea

a b a b c a b a b d a b a

b a b a c b a d a b a b c

S1 :

S2 :

Page 7: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

First Idea

a b a b c a b a b d a b a

a b a c b a d a b a b c

S1 :

S2 :

Page 8: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

First Idea

a b a b c a b a b d a b a

a b a c b a d a b a b c

S1 :

S2 :

Page 9: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

First Idea

a b a b c a b a b d a b a

a b a c b a d a b a b c

S1 :

S2 :

Page 10: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

First Idea

a b a b c a b a b d a b a

a b a c b a d a b a b c

S1 :

S2 :

Page 11: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

First Idea

a b a b c a b a b d a b a

a b a c b a d a b a b c

S1 :

S2 :

Page 12: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

First Idea

a b a b c a b a b d a b a

b a c b a d a b a b c

S1 :

S2 :

Page 13: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

First Idea

a b a b c a b a b d a b a

b a c b a d a b a b c

S1 :

S2 :

Page 14: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

First Idea

a b a b c a b a b d a b a

a c b a d a b a b c

S1 :

S2 :

Page 15: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

First Idea

a b a b c a b a b d a b a

a c b a d a b a b c

S1 :

S2 :

Page 16: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

First Idea

a b a b c a b a b d a b a

a c b a d a b a b c

S1 :

S2 :

Page 17: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

First Idea

a b a b c a b a b d a b a

c b a d a b a b c

S1 :

S2 :

Page 18: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

First Idea

a b a b c a b a b d a b a

c b a d a b a b c

S1 :

S2 :

Page 19: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

First Idea

a b a b c a b a b d a b a

b a d a b a b c

S1 :

S2 :

Page 20: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

First Idea

a b a b c a b a b d a b a

b a d a b a b c

S1 :

S2 :

Page 21: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

First Idea

a b a b c a b a b d a b a

a d a b a b c

S1 :

S2 :

Page 22: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

First Idea

a b a b c a b a b d a b a

a d a b a b c

S1 :

S2 :

Page 23: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

First Idea

a b a b c a b a b d a b a

a d a b a b c

S1 :

S2 :

Page 24: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

First Idea

a b a b c a b a b d a b a

d a b a b c

S1 :

S2 :

Page 25: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

First Idea

a b a b c a b a b d a b a

d a b a b c

S1 :

S2 :

Page 26: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

First Idea

a b a b c a b a b d a b a

a b a b c

S1 :

S2 :

Page 27: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

First Idea

a b a b c a b a b d a b a

a b a b c

S1 :

S2 :

Page 28: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

First Idea

a b a b c a b a b d a b a

a b a b c

S1 :

S2 :

Page 29: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

First Idea

a b a b c a b a b d a b a

a b a b c

S1 :

S2 :

Page 30: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

First Idea

a b a b c a b a b d a b a

a b a b c

S1 :

S2 :

Page 31: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

First Idea

a b a b c a b a b d a b a

a b a b c

S1 :

S2 :

Page 32: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

First Idea

a b a b c a b a b d a b a

a b a b c

S1 :

S2 :

Page 33: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

What if …

a a a a a a a a a a a a a

a a a a a a a a a a a a z

S1 :

S2 :

Page 34: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

What if …

a a a a a a a a a a a a a

a a a a a a a a a a a a z

S1 :

S2 :

How many comparisons?

n + (n −1) + ...+ 2 +1= Θ(n2)

Page 35: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

Recall the Z algorithm*

Simplest linear string matching algorithmGiven a string , denote by the length of the longest string starting at position of S, that is a prefix of S

Note: just one stringCompute iteratively

*Algorithms on strings, trees and sequences, D.Gusfield

Z[i]S[1..n]

Z[i]

i

Page 36: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

Definitions

S1 ni

Z[i]+ i −1

Z[i]Z box at position i

Page 37: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

DefinitionsFix a position k

Sk r[k]l[k]

r[k] Rightmost end of a Z box that startsat a position 1< j ≤ k

l[k] The left end of the Z box defined byr[k]

Page 38: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

The Z Algorithm

Compute iteratively

Only last value of will be needed. Refer to them as

Three cases to consider

Z[i], l[i],r[i]

l[i],r[i]

l, r

Page 39: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

The Z AlgorithmIteration kCase 1: k > r

Skr

Compute by explicitelycomparing the characters atposition and on with thecharacters at the beginningof the string

Z[k]

k

Page 40: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

The Z AlgorithmIteration k,k ≤ rCase 2a:

αβγ γ βγ

kl rk'

Z[k'] <| β |

S α

Z[k']

Page 41: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

The Z AlgorithmIteration k,k ≤ rCase 2b:

αβ β

kl rk'

Z[k'] ≥| β |

βS

Page 42: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

The Z algorithm

Why does it run in linear time?

Page 43: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

The Contest Problem

What if we have two strings?

The solution is a simple extension of the Z algorithm

Call the longest string which starts at position of and which is also a prefix of

Y[i]i S2

S1

Page 44: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

The Contest Problem

Definition of a Y box, and are analogous, this time with respect to

l r

S2

Page 45: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

Case 1

S2kr

S1

Page 46: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

Case 2a

α βγ

kl r

S2

βγ γ

k'

S1

Z[k'] <| β |

Need Z values

for S1

Z[k']

Page 47: Simpson’s Hidden Talents · Simpson’s Hidden Talents. Problem AGiven two strings and find the longest prefix of that is a suffix of S1 S2 S1 S2. Problem AGiven two strings and

Case 2b

α β

kl rS

β

k'

βS

Z[k'] ≥| β |