Two Dimensional Pattern Matching

The Knuth-Morris-Pratt algorithm [3] solves the following string matching problem: given a string of symbols p1 p2 l se pn, called the pattern, and a second string tl t2 l -e tm, called the text, to determine for each j whether or not ti,++ ! l -ti = plp2 l -p,. The KMP algorithm possesses the f4owing noteworthy properties: (1) its running time is O(n + m), where the coefficient of proportionality is small and independent of the size of the alphabet; (2) its space requirements are O(n), so no part of the text, which may be substantially longer than the pattern, need be kept in store; (3) its on-line nature, whereby the outcome for value i is determined before tj+ 1 is read. A natural generalisation of this problem is one in which the pattern is a two-dimensional array of syanbols PiI, 1 G i,] G n, and $ke text is a second array tij, 1 G i,j f m. In this case the problem is to determine where, if anywhere, the pattern occurs as a subarray of the text. Our purpose is to give an algorithm for thz! two dimensional case, one which follows the general approach of the KMP, and indeed uses the KMP as a subprogram. in particular, the algorithm has a running time of 0(n2 + m’), which is clearly optimal since both the pattern and the text have to be read and this takes O(rt’ + m2). The algorithm needs 0(n2 + m) space, and also works on-line (under the assumption that the text is read row by row). For reasons of space we assume some familiarity with