kazunori hirashima 1, hideo bannai 1, wataru matsubara 2, kazuhiko kusano 2, akira ishino 2, ayumi...

28
Bit-parallel algorithms for computing all the runs in a string Kazunori Hirashima 1 , Hideo Bannai 1 , Wataru Matsubara 2 , Kazuhiko Kusano 2 , Akira Ishino 2 , Ayumi Shinohara 2 1 Kyushu University, Japan 2 Tohoku University, Japan

Upload: sheryl-fay-floyd

Post on 02-Jan-2016

223 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Kazunori Hirashima 1, Hideo Bannai 1, Wataru Matsubara 2, Kazuhiko Kusano 2, Akira Ishino 2, Ayumi Shinohara 2 1 Kyushu University, Japan 2 Tohoku University,

Bit-parallel algorithms for computing all the runs in a

string Kazunori Hirashima1, Hideo Bannai1, Wataru Matsubara2,

Kazuhiko Kusano2, Akira Ishino2, Ayumi Shinohara2 1Kyushu University, Japan2Tohoku University, Japan

Page 2: Kazunori Hirashima 1, Hideo Bannai 1, Wataru Matsubara 2, Kazuhiko Kusano 2, Akira Ishino 2, Ayumi Shinohara 2 1 Kyushu University, Japan 2 Tohoku University,

Runs Bit-parallel algorithms for counting runs◦Counting prefix runs◦Removing duplicate runs by position◦Removing duplicates by Sieve

Computational Experiments Conclusion

Contents

Page 3: Kazunori Hirashima 1, Hideo Bannai 1, Wataru Matsubara 2, Kazuhiko Kusano 2, Akira Ishino 2, Ayumi Shinohara 2 1 Kyushu University, Japan 2 Tohoku University,

runs: occurrence of a periodic factor◦non extendable(maximal)◦exponent at least two◦primitive-rooted

example:

w = abbabbaccbcbcbc

run(w) : number of runs in string w

Runs

Page 4: Kazunori Hirashima 1, Hideo Bannai 1, Wataru Matsubara 2, Kazuhiko Kusano 2, Akira Ishino 2, Ayumi Shinohara 2 1 Kyushu University, Japan 2 Tohoku University,

Linear time algorithm [Kolpakov&Kucherov ‘99]◦ requires LZ-factorization of string

We present 3 bit-parallel algorithms to calculate run(w)◦ does not require complicated data structures◦ very efficient for short strings

Calculating run(w)

Page 5: Kazunori Hirashima 1, Hideo Bannai 1, Wataru Matsubara 2, Kazuhiko Kusano 2, Akira Ishino 2, Ayumi Shinohara 2 1 Kyushu University, Japan 2 Tohoku University,

Runs Algorithms◦Counting prefix runs◦Removing duplicate runs by position◦Removing duplicates by Sieve

Computational Experiments Discussion

Contents

Page 6: Kazunori Hirashima 1, Hideo Bannai 1, Wataru Matsubara 2, Kazuhiko Kusano 2, Akira Ishino 2, Ayumi Shinohara 2 1 Kyushu University, Japan 2 Tohoku University,

For general alphabet: Counting prefix runs

For binary alphabet: Removing duplicate runs by position Removing duplicate runs by Sieve

Bit-parallel algorithms for counting runs

Page 7: Kazunori Hirashima 1, Hideo Bannai 1, Wataru Matsubara 2, Kazuhiko Kusano 2, Akira Ishino 2, Ayumi Shinohara 2 1 Kyushu University, Japan 2 Tohoku University,

prefix repetition = a repetition that is also a prefixprefix run = a run that is also a prefix

IdeaFor each suffix:

1. detect right maximal prefix repetitions of each period2. count only repetitions with exponent at least 23. count only left maximal repetitions

Algorithm (counting prefix runs)

Page 8: Kazunori Hirashima 1, Hideo Bannai 1, Wataru Matsubara 2, Kazuhiko Kusano 2, Akira Ishino 2, Ayumi Shinohara 2 1 Kyushu University, Japan 2 Tohoku University,

example:w=aabaabaaaacaac

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14

period a a b a a b a a a a c a a c

1 1 1 0 0 0 0 0 0 0 0 0 0 0 0

2 1 1 0 0 0 0 0 0 0 0 0 0 0 0

3 1 1 1 1 1 1 1 1 0 0 0 0 0 0

4 1 1 1 1 1 0 0 0 0 0 0 0 0 0

5 1 1 1 1 1 0 0 0 0 0 0 0 0 0

6 1 1 1 1 1 1 1 1 0 0 0 0 0 0

7 1 1 1 1 1 1 1 1 1 0 0 0 0 0

period a a b a a b a a a a c a a c

1 a a - - - - - - - - - - - -

2 - - - - - - - - - - - - - -

3 a a b a a b a a - - - - - -

4 - - - - - - - - - - - - - -

5 - - - - - - - - - - - - - -

6 - - - - - - - - - - - - - -

7 - - - - - - - - - - - - - -

Algorithm(counting prefix runs)

a a b a a b a a a a c a a c

1 1 0 1 1 0 1 1 1 1 0 1 1 0

0 0 1 0 0 1 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 1 0 0 1

occ

occ[a]

occ[b]

occ[c]

Detect right maximal prefix repetitions of each period

prefix run

prefix run

w[1]=w[4],w[2]=w[4]

ActiveArea

Page 9: Kazunori Hirashima 1, Hideo Bannai 1, Wataru Matsubara 2, Kazuhiko Kusano 2, Akira Ishino 2, Ayumi Shinohara 2 1 Kyushu University, Japan 2 Tohoku University,

bitmask

1 2 3 4 5 6 7 8 9 10 11 12 13 14

Algorithm(counting prefix run)

nextChar=w[i];bitmask=((occ[nextChar] >> (Length-i)) | (~0) << i);alive=alive&bitmask;

period a a b a a b a a a a c a a c

1

2

3

4

5

6

7

example:w=aabaabaaaacaac

a 1 0 0

b 0 1 0

a 1 0 0

a 1 0 0

b 0 1 0

a 1 0 0

a 1 0 0occ a b c

・・・

1

1

1

1

1

1

1

a

1

1

1

1

1

1

1

0

0

1

1

1

1

1

a

1

1

1

1

1

1

1

b

0

0

1

1

1

1

1

a

0

0

1

1

1

1

1

0

1

1

1

1

1

1

a b a a a a c a a c

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

1 1 1 1 0 0 0 0 0 0

1 0 0 0 0 0 0 0 0 0

1 0 0 0 0 0 0 0 0 0

1 1 1 1 0 0 0 0 0 0

1 1 1 1 1 0 0 0 0 0

alivealivealivealiveL

ength - i

Detect right maximal prefix repetitions of each periodpseudo code

Page 10: Kazunori Hirashima 1, Hideo Bannai 1, Wataru Matsubara 2, Kazuhiko Kusano 2, Akira Ishino 2, Ayumi Shinohara 2 1 Kyushu University, Japan 2 Tohoku University,

Algorithm(counting prefix run)

nextChar=w[i];bitmask=((occ[nextChar] >> (Length-i)) | (~0) << i);prevAlive=alive;alive=alive&bitmask;If prevAlive ^ alive & ActiveArea≠0 then count++;

example:w=aabaabaaaacaac

1 2 3 4 5 6 7 8 9 10 11 12 13 14

period a a b a a b a a a a c a a c

1 1 1 0 0 0 0 0 0 0 0 0 0 0 0

2 1 1 0 0 0 0 0 0 0 0 0 0 0 0

3 1 1 1 1 1 1 1 1 0 0 0 0 0 0

4 1 1 1 1 1 0 0 0 0 0 0 0 0 0

5 1 1 1 1 1 0 0 0 0 0 0 0 0 0

6 1 1 1 1 1 1 1 1 0 0 0 0 0 0

7 1 1 1 1 1 1 1 1 1 0 0 0 0 0

ActibeArea

1

0

0

0

0

0

0

ActiveArea

1

1

0

0

0

0

0

prevAlive ^ alive

Count only repetitions with exponent at least 2

If i mod 2 = 1 thenactiveArea := (activeArea << 1) | 1 ;

pseudo code

Page 11: Kazunori Hirashima 1, Hideo Bannai 1, Wataru Matsubara 2, Kazuhiko Kusano 2, Akira Ishino 2, Ayumi Shinohara 2 1 Kyushu University, Japan 2 Tohoku University,

1 2 3 4 5 6 7 8 9 10 11 12 13 14

period a a b a a b a a a a c a a c

1 - - 1 0 0 0 0 0 0 0 0 0 0 0

2 - - 0 0 0 0 0 0 0 0 0 0 0 0

3 - - 0 0 0 0 0 0 0 0 0 0 0 0

4 - - 1 1 1 1 0 0 0 0 0 0 0 0

5 - - 0 0 0 0 0 0 0 0 0 0 0 0

6 - - 0 0 0 0 0 0 0 0 0 0 0 0

7 - - 0 0 0 0 0 0 0 0 0 0 0 0

period a a b a a b a a a a c a a c

1 - - 1 0 0 0 0 0 0 0 0 0 0 0

2 - - 1 1 0 0 0 0 0 0 0 0 0 0

3 - - 1 1 1 1 1 1 0 0 0 0 0 0

4 - - 1 1 1 1 0 0 0 0 0 0 0 0

5 - - 1 1 1 1 1 0 0 0 0 0 0 0

6 - - 1 1 1 1 1 1 0 0 0 0 0 0

7 - - 1 1 1 1 1 1 1 0 0 0 0 0

period a a b a a b a a a a c a a c

1 - 0 1 0 0 0 0 0 0 0 0 0 0 0

2 - 1 1 1 0 0 0 0 0 0 0 0 0 0

3 - 1 1 1 1 1 1 1 0 0 0 0 0 0

4 - 0 1 1 1 1 0 0 0 0 0 0 0 0

5 - 1 1 1 1 1 1 0 0 0 0 0 0 0

6 - 1 1 1 1 1 1 1 0 0 0 0 0 0

7 - 1 1 1 1 1 1 1 1 0 0 0 0 0

example:w=aabaabaaaacaac

a a b a a b a a a a c a a c

1 1 0 1 1 0 1 1 1 1 0 1 1 0

0 0 1 0 0 1 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 1 0 0 1

Algorithm(counting prefix run)

a a b a a b a a a a c a a c

1 1 0 1 1 0 1 1 1 1 0 1 1 0

0 0 1 0 0 1 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 1 0 0 1

occ

occ[a]

occ[b]

occ[c]

Count only left maximal repetitions

w[2]=w[2+3]w[2]=w[2+2]w[2]≠w[2+1]

w[3:8] seems to be run, but it can extend left.So w[3:8] isn’t a run.

Page 12: Kazunori Hirashima 1, Hideo Bannai 1, Wataru Matsubara 2, Kazuhiko Kusano 2, Akira Ishino 2, Ayumi Shinohara 2 1 Kyushu University, Japan 2 Tohoku University,

Idea1. detect maximal repetition for each period 1, 2 ..., |

w|/2.2. count only repetitions with exponent at least 2.3. count only repetitions of minimum period

Algorithm (binary strings)

Page 13: Kazunori Hirashima 1, Hideo Bannai 1, Wataru Matsubara 2, Kazuhiko Kusano 2, Akira Ishino 2, Ayumi Shinohara 2 1 Kyushu University, Japan 2 Tohoku University,

v= w ^ ((~w)>>p) Example   p=3

Algorithm(Efficient algorithm for binary string)

0 1 1 0 1 1 0 0 1 0 1

0 1 1 0 1 1 0 0 1 0 1

P

1 1 1 1 0 1 1 0

w XOR ~w

Detect maximal repetition for each period 1, 2 ..., |w|/2.

w =

v =

maximal repetition of period p in w stretch of 1’s in v

Page 14: Kazunori Hirashima 1, Hideo Bannai 1, Wataru Matsubara 2, Kazuhiko Kusano 2, Akira Ishino 2, Ayumi Shinohara 2 1 Kyushu University, Japan 2 Tohoku University,

5

0 1 1 0 1 1 0 0 1 0 1

0 1 1 0 1 1 0 0 1 0 1p=3

1 1 1 1 0 1 1 0

7

4=7-3

w XOR ~w

Algorithm (binary strings)

Delete repetitions with exponent less than 2.

2=5-3

Stretch of 1’s must be at least length p=3.

This is too short to be a run of period p = 3.

w =

v =

v= w ^ ((~w)>>p) Example   p=3

Page 15: Kazunori Hirashima 1, Hideo Bannai 1, Wataru Matsubara 2, Kazuhiko Kusano 2, Akira Ishino 2, Ayumi Shinohara 2 1 Kyushu University, Japan 2 Tohoku University,

0 1 1 1 1 0 0 1 0

s = v;While (p>1)

s = s & (v>>p);p--;

ENDv=s;

0 0 0 0 1 0 0 0 0

0 1 1 1 1 0 0 1 0

0 1 1 1 1 0 0 1 0

0 1 1 1 1 0 0 1 0

Algorithm(Efficient algorithm for binary string)

This calculation shortens each stretch of 1’s by p-1

Delete repetitions with exponent less than 2.

1

2

p - 1

&

&

&

Page 16: Kazunori Hirashima 1, Hideo Bannai 1, Wataru Matsubara 2, Kazuhiko Kusano 2, Akira Ishino 2, Ayumi Shinohara 2 1 Kyushu University, Japan 2 Tohoku University,

selfAND(v,p) While p>1

s = p>>1;v = v & (v>>s);p = p – s;

END0 1 1 1 1 1 1 1 1

Example  v = 00111111110010 p=7

0 1 1 1 1 1 1 1 1

p

s

0 * * 0 1 1 1 1 1

0 * 0 0 1 0 1 1 1

0 0 0 0 0 0 0 1 1

Algorithm(Efficient algorithm for binary string)

Delete repetitions with exponent less than 2.

O(p) → O(log p).

Page 17: Kazunori Hirashima 1, Hideo Bannai 1, Wataru Matsubara 2, Kazuhiko Kusano 2, Akira Ishino 2, Ayumi Shinohara 2 1 Kyushu University, Japan 2 Tohoku University,

Example   w=00110011111111, p=4◦v=w^((~w)>>p) = 000011110001111◦selfAND(v,p) = 000000010000001

run with minimum period 1

Algorithm(Efficient algorithm for binary string)

We need to remove duplicates.

2 approaches to remove repetitions of non-minimum periods:•Removing duplicate by Position•Removing duplicate by Sieve

Page 18: Kazunori Hirashima 1, Hideo Bannai 1, Wataru Matsubara 2, Kazuhiko Kusano 2, Akira Ishino 2, Ayumi Shinohara 2 1 Kyushu University, Japan 2 Tohoku University,

Algorithm(Removing duplicate by Position)

For period =1 to length/2 do

v=(w^((~w)>>1))&(1length>>period) ;

x=SelfAND(v,period);

While x ≠ 0 do

begPos=lsb(x);

y=x+(1<<begPos);

x= x & y;

y=y & (-y);

y=y << ((period – 1) << 1);

If (runEndsByBegPos[begPos] & y) = 0then

count ++;

runEndsByBegPos[begPos] = runEndsByBegPos[begPos] | y;

End

End

End 1 0 0 1 0 1 0 1 0 1 0 1 1

0 0 0 0 1 1 1 1 1 1 1 1 0

0 0 0 0 0 0 1 1 1 1 1 1 0

w=

w^((~w)>>2)=

w^((~w)>>4)=

Begin position End position

only count maximal repetitions withdifferent begin and end positions

2

4

Page 19: Kazunori Hirashima 1, Hideo Bannai 1, Wataru Matsubara 2, Kazuhiko Kusano 2, Akira Ishino 2, Ayumi Shinohara 2 1 Kyushu University, Japan 2 Tohoku University,

0 0 1 1 0 0 1 1 1 1 1

0 0 0 1 0 0 1 0 0 0 0

0 0 0 0 0 0 1 1 1 1 1

For period =1 to length/2 dopvec[period]=w^((~w)>>1) ;

End For period=1 to length/2 do

x=SelfAND(pvec[period],period);count=count+oneRuns(x);For p=2*period to length/2 do

x=x & (x >> period);If x=0 then breakpvec[p] =pvec[p] ^ (x);

End End

Algorithm(Removing duplicate by Sieve)

Example: w=11110101010

0 1 1 1 0 0 0 0 0 0 0

0 0 0 0 0 0 0 1 1 1 1

・・・・

w^((~w)>>1)

w^((~w)>>2)

w^((~w)>>3)

w^((~w)>>4)

xor

・・・・

0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 1 0 0 0 0delete runs in larger periods

Page 20: Kazunori Hirashima 1, Hideo Bannai 1, Wataru Matsubara 2, Kazuhiko Kusano 2, Akira Ishino 2, Ayumi Shinohara 2 1 Kyushu University, Japan 2 Tohoku University,

count=0;While (v ≠ 0)

v = v & ((v | (v – 1)) + 1);count++;

END

Example   v=1001110011◦ v | (v – 1) = 100111011◦ v | (v – 1) + 1 = 100111100◦ v & ((v | (v – 1))+1) = 100111000

◦ v | (v – 1) = 100111111◦ v & ((v | (v – 1))+1) = 100000000

◦ v & ((v | (v – 1))+1) = 000000000

Algorithm(Removing duplicate by Sieve)

bit operations to count the number of stretches of 1’s

Page 21: Kazunori Hirashima 1, Hideo Bannai 1, Wataru Matsubara 2, Kazuhiko Kusano 2, Akira Ishino 2, Ayumi Shinohara 2 1 Kyushu University, Japan 2 Tohoku University,

Runs Algorithms◦Counting prefix runs◦Removing duplicate runs by position◦Removing duplicates by Sieve

Computational Experiments Discussion

Contents

Page 22: Kazunori Hirashima 1, Hideo Bannai 1, Wataru Matsubara 2, Kazuhiko Kusano 2, Akira Ishino 2, Ayumi Shinohara 2 1 Kyushu University, Japan 2 Tohoku University,

Calculate run(w) for all binary strings oflength n

CPU :3.2GHz dual core Xeon GPU :Geforce 8800GT Memory :18GB OS :MacOSX 10.5 Leopard

Computational Experiments

Page 23: Kazunori Hirashima 1, Hideo Bannai 1, Wataru Matsubara 2, Kazuhiko Kusano 2, Akira Ishino 2, Ayumi Shinohara 2 1 Kyushu University, Japan 2 Tohoku University,

Computational Experiments

count=0

For period =1 to length/2 do

pvec[period] = w ^ ((~w) >> 1) ;

End

For period=1 to length/2 do

x = SelfAND(pvec[period],period);

count = count + oneRuns(x);

For p = 2 * period to length/2 do

x=x & (x >> period);

If x=0 then break

pvec[p] = pvec[p]^(~x);

End

EndGPU

MultiProcessor

MultiProcessor

StreamProcessor

・・・

Use the programming tool CUDAUse the programming tool CUDA

Page 24: Kazunori Hirashima 1, Hideo Bannai 1, Wataru Matsubara 2, Kazuhiko Kusano 2, Akira Ishino 2, Ayumi Shinohara 2 1 Kyushu University, Japan 2 Tohoku University,

Running time (seconds)for calculating run(w) for all binary strings of length n

Computational Experiments

n 20 21 22 23 24 25 26 27 28 29 30

prefix 0.32 0.69 1.49 3.13 8.02 15.6 32.4 66.4 150.2 296.5 625.4

position 0.09 0.18 0.36 0.73 1.49 3.0 6.2 12.6 25.6 52.1 106.0

sieve 0.10 0.18 0.37 0.75 1.50 3.0 6.0 11.9 23.9 48.1 96.7

GPGPU (sieve)

0.01 0.02 0.04 0.08 0.18 0.4 0.7 1.4 3.0 5.9 12.2

Page 25: Kazunori Hirashima 1, Hideo Bannai 1, Wataru Matsubara 2, Kazuhiko Kusano 2, Akira Ishino 2, Ayumi Shinohara 2 1 Kyushu University, Japan 2 Tohoku University,

n 2 3 4 5 6 7 8 9 10 11 12 13 14

p(n) 1 1 2 2 3 4 5 5 6 7 8 8 9

Computational Experiments

n 15 16 17 18 19 20 21 22 23 24 25 26 27

p(n) 10 10 12 13 14 15 15 16 17 18 19 20 21

n 28 29 30 31 32 33 34 35 36 37 38 39 40

p(n) 22 23 24 25 26 27 27 28 29 30 30 31 32

n 41 42 43 44 45 46 47

p(n) 33 35 35 36 37 38 38

New!

Kolpakov & Kucherov ’99

The maximum number of runs function ρ(n)=max { run(w) : |w| = n }

for binary strings calculated for n up to 47

Page 26: Kazunori Hirashima 1, Hideo Bannai 1, Wataru Matsubara 2, Kazuhiko Kusano 2, Akira Ishino 2, Ayumi Shinohara 2 1 Kyushu University, Japan 2 Tohoku University,

1.00n 1.05n0.95n0.90n

Lower and Upper bounds of ρ(n)

5n   [Rytter ’06]

3.48n [Puglisi et al. ’08]

3.44n  [Rytter ’07]

1.6n[Crochemore & Ilie ’08]

cn[Kolpakov & Kucherov ’99]

0 n 2n 3n 4n 5n

ρ(n)

1.029n[Crochemore et al. ’08]

0.944565n [Matsubara et al ’08]

0.94457571235n [Matsubara et al ’09][Simpson ’09]

0.927n [Franeck & Simpson ’06]

Page 27: Kazunori Hirashima 1, Hideo Bannai 1, Wataru Matsubara 2, Kazuhiko Kusano 2, Akira Ishino 2, Ayumi Shinohara 2 1 Kyushu University, Japan 2 Tohoku University,

Computational Experimentsf(n, r) : number of binary strings of length n with r runs

f(n, 3) = 2f(n – 2, 3) - f(n – 4, 3) + 234 for n 16.f(n, 2) = f(n – 2, 2) + 72 for n 9.

n

23456789

10111213141516171819202122

f(n, 1)

2 6 14 18 18 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

20

f(n, 3)

00008

38102202376596880

1220162220802598317438084502525260646930

f(n, 2)

002

14386698

138170210242282314354386426458498530570602

f(n, 4)

0000 04

34130306682

13142296373656868260

115621564220626265743359041754

n f(n, 1) f(n, 2) f(n, 3) f(n, 4)

23 20 642 7860 5118424 20 674 8842 6189825 20 714 9890 7407026 20 746 10988 87732

27 20 786 12154 103000 28 20 818 13368 119922 29 20 858 14652 138664 30 20 890 15982 159216 31 20 930 17384 181764 32 20 962 18830 206308 33 20 1002 20350 233012 34 20 1034 21912 261896 35 20 1074 23550 293138 36 20 1106 25228 326696 37 20 1146 26984 362804 38 20 1178 28778 401434 39 20 1218 30652 442762 40 20 1250 32562 486776 41 20 1290 34554 533702 42 20 1322 36580 583470

Page 28: Kazunori Hirashima 1, Hideo Bannai 1, Wataru Matsubara 2, Kazuhiko Kusano 2, Akira Ishino 2, Ayumi Shinohara 2 1 Kyushu University, Japan 2 Tohoku University,

We presented 3 bit-parallel algorithms for efficiently computing all the runs in short strings.◦O(n2) time if n = O(word size)◦ First algorithm

can be used for strings with larger alphabet size at some cost◦ Two latter algorithms

specialized for binary strings* and very efficient* We recently noticed that they can be adapted to handle larger

alphabets

Calculated ρ (n) for binary strings of length up to n=47

Conclusion