Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there any paper or an explanation on how to implement a two dimensional KMP?

I tried to solve the problem of two dimensional search using a combination of Aho-Corasick and a single dimensional KMP, however, I still need something faster.

To elaborate, I have a matrix A of characters of size n1*n2 and I wish to find all occurrences of a smaller matrix B of size m1*m2 and I want that to be in O(n1*n2+m1*m2) if possible.

For example:

A = a b c b c b
    b c a c a c
    d a b a b a
    q a s d q a

and

B = b c b
    c a c
    a b a

the algorithm should return the indexes of say, the upper left corner of the match, which in this case should return (0,1) and (0,3). notice that the occurrences may overlap.

like image 761
Ouais Alsharif Avatar asked Oct 09 '22 15:10

Ouais Alsharif


1 Answers

There is an algorithm called the Baker-Bird algorithm that I just recently encountered that appears to be a partial generalization of KMP to two dimensions. It uses two algorithms as subroutines - the Aho-Corasick algorithm (which itself is a generalization of KMP), and the KMP algorithm - to efficiently search a two-dimensional grid for a pattern.

I'm not sure if this is what you're looking for, but hopefully it helps!

like image 156
templatetypedef Avatar answered Oct 12 '22 11:10

templatetypedef