ArticleslgStudy

mathematics

Two-dimensional pattern matching

Two-dimensional pattern matching is a mathematics topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Two-dimensional pattern matching rather than just read about it. In short: In computer science, two-dimensional pattern matching is the problem of locating occurrences of a two-dimensional matrix of characters ("the pattern") in a bigger two-dimensional matrix ("the picture", or, in analogy with string searching, "the text"). The naïve solution Assume given a pattern P [ m × m ] {\displaystyle P[m\times m]} and a text T [ n × n ] {\displaystyle T[n\times n]} , where m ≤ n {\displaystyle m\…

Key takeaways

  • Two-dimensional pattern matching belongs to mathematics; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Two-dimensional pattern matching to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Two-dimensional pattern matching from memory before moving on to harder problems.

Reference excerpt

In computer science, two-dimensional pattern matching is the problem of locating occurrences of a two-dimensional matrix of characters ("the pattern") in a bigger two-dimensional matrix ("the picture", or, in analogy with string searching, "the text").

The naïve solution Assume given a pattern P [ m × m ] {\displaystyle P[m\times m]} and a text T [ n × n ] {\displaystyle T[n\times n]} , where m ≤ n {\displaystyle m\leq n} . The simplest approach is to compare P against every sub-array of size m × m {\displaystyle m\times m} in T. This algorithm has a worst-case time of Θ ( ( n − m + 1 ) 2 m 2 ) {\displaystyle \Theta ((n-m+1)^{2}m^{2})} . It is usually assume that m ≤ n / 2 {\displaystyle m\leq n/2} , whence this can be written as Θ ( n 2 m 2 ) {\displaystyle \Theta (n^{2}m^{2})} .

An automaton-based solution We shall illustrate a more efficient solution (due essentially to Bird 1977) by means of an example. Suppose we have the following pattern and text

abaab aba baaab P = bab T = abaab aba babab ababa

We first construct a Aho-Corasick automaton to search a linear text for occurrences of the columns of P. As a biproduct we obtain an identifier for every column, so that two identical columns get the same identifier: in our example, say the first (and third) columns are idntified by 0, and the middle column by 1. Next, we run the automaton on the columns of T, obtaining (in linear time) an indication whenever one of the columns of P appears in T: in our example this will be a 3×5 matrix C as follow (a "-" marks an absence of a match):

abaab aba baaab P = bab T = abaaa C = 01--- aba babab 10--1 ababa 010-0

We now use the Knuth-Morris-Pratt Algorithm to search the rows of C for a pattern identical to P, i.e., 010. When we find one, we have identified an occurrence of P in T. Note that it is not necessary to keep all of C, or all of T, in memory; T can be read row by row, and from C one only needs to keep one row in memory---along with a row of states of the Aho-Corasick and the KMP automata. The running time of this algorithm is O ( n 2 ) {\displaystyle O(n^{2})} if one ignores the dependence on the size of the alphabet, which exists in the Aho-Corasick algorithm. Taking this into account, the complexity is O ( n 2 log ⁡ k ) {\displaystyle O(n^{2}\log k)} where k is the size of the alphabet.

More efficient solutions The dependence on the size of the alphabet was removed by an O ( n 2 ) {\displaystyle O(n^{2})} algorithm of Galil and Park.

See also string searching algorithm

Footnotes

References Apostolico, Alberto (1999). "Chapter 13: General Pattern Matching". In Atallah (ed.). Algorithms and Theory of Computation Handbook. CRC Press. pp. 13–11. ISBN 0849326494. Bird, Richard S. (1977). "Two dimensional pattern matching". Information Processing Letters. 6 (5): 168–170. Galil, Zvi; Park, Kunsoo (1996). "Alphabet-independent two-dimensional witness computation". SIAM Journal on Computing. 25 (5): 907–935.

Worked examples

Example 1 — a first encounter with Two-dimensional pattern matching

Start with the simplest possible case. Write down what Two-dimensional pattern matching claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In mathematics, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Two-dimensional pattern matching before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Two-dimensional pattern matching ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Two-dimensional pattern matching

In research
Two-dimensional pattern matching appears in mathematics research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Two-dimensional pattern matching in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Two-dimensional pattern matching is common in secondary-school and first-year university syllabi. It links to neighbouring topics Conditional constructs, Functional programming, Pattern matching, so understanding it makes those chapters shorter.
In everyday life
Look for Two-dimensional pattern matching outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Two-dimensional pattern matching in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Two-dimensional pattern matching means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Two-dimensional pattern matching out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Two-dimensional pattern matching in simple terms?

In computer science, two-dimensional pattern matching is the problem of locating occurrences of a two-dimensional matrix of characters ("the pattern") in a bigger two-dimensional matrix ("the picture", or, in analogy with string searching, "the text"). The naïve solution Assume given a pattern P […

Why does Two-dimensional pattern matching matter?

Because it connects several mathematics ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Two-dimensional pattern matching?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Two-dimensional pattern matching.

Tags

  • Conditional constructs
  • Functional programming
  • Pattern matching
  • Programming language comparisons

Keep exploring