2D pattern search algorithms
I need to learn about 2D pattern searching algorithms. Tips and links greatly appreciated.
More to the point:
Given a matrix of M[m,n] with values in K
example000000000000
000001000000 0101
00010010 = M, K = {0, 1}
0101
00010001
10111
1010111
and a matrix L[i, j] with values in K + {X} representing a "shape"
example, the shape of letter "L"1
X
1
X = L
11
What algoritms can answer to the following questions:
- Can L be found in M ?
- How many times L can be found in M (disjunctive L's, no common pieces (1's or 0's))
- How many times L can be found in M (can have common pieces 开发者_JS百科(1's or 0's))
- How many times L, and K (K is defined similarly like L, K != L) can be found in M (disjoint) etc.
The language of implementation is to be JavaScript, but any other will do.
EDIT Also found this PDF.
Take a look at this presentation it should give you a basic knowledge.
The X symbol can be treated like wildcard so it will always give a match.
Don't know what exactly you mean by
How many times L, and K (K is defined like L) can be found in M (disjoint) etc.
K stands for alphabet or shape (like L)?
Problem of determining maximum number of disjoint matches will be harder. The approach would be following:
- find all possible matches
- create graph where node represents matches and edge represents that two matches have common field.
- now you have to find maximum independent set in this graph (wiki) - set of vertices in a graph, no two of which are adjacent so problem constraints are not violated.
EDIT: If your shape is L the match can be computed efficently. Create tables for columns and rows and for each cell check if there is a match upwards and to the right.
精彩评论