Proofs: Bird-Baker algorithm
These are the human proofs of Bird-Baker algorithm, at full length.
Bird-Baker outputs exactly the occurrences of the patterns full
This proof shows: correctness, termination.
Claim. For every rectangular text with rows and columns and every list of rectangular patterns , BIRD-BAKER stops and outputs each triple such that occurs with its top-left corner at exactly once, and no other triple.
Notation. For a set of words, its trie has a node for each prefix of a word of ; is the prefix of node , and is its length. The failure link of a node root is the node of the longest proper suffix of that is a node. The step from by a symbol follows failure links from until a node with a child by is found, and goes to that child, or to the root if no such node exists. The output list of is the list of indices of the words equal to , followed by the output list of .
Lemma 1 (Aho-Corasick). Let be a sequence and the state after the steps for , from the root. Then is the longest suffix of that is a prefix of a word of , and the output list of holds each index with a suffix of exactly once.
Proof. First, the nodes on the failure chain spell exactly the suffixes of that are nodes, in decreasing length: each link goes to the longest shorter one, so none is skipped. Now use induction on ; for empty the state is the root. Let satisfy the claim for , and read . A suffix of that is a node and is not empty has the form , where is a suffix of and a node, so and is a suffix of , so is on the failure chain of . The step walks this chain in decreasing length and stops at the first with a child by , which gives the longest ; if there is none, the answer is the empty suffix, the root. For the output, a word that is a suffix of is a node and a suffix of , so it is a suffix of and is spelled by exactly one node of the failure chain of . The output list of is the concatenation of the lists of the words equal to the strings of these distinct nodes, so it holds exactly once. Conversely each index on it is of a word that is a suffix of , hence of .
Fix the width of one pass, and let for be the nonempty patterns of width . Let be the set of their rows, all of length , with distinct names, and ⊥ a symbol that is not a name.
Lemma 2 (row names). After line L7 for row , for each with , is the name of if this string is in , and ⊥ otherwise.
Proof. By Lemma 1 for , the state after spells the longest suffix of that is a prefix of a row in , and . If is in , then is such a suffix of length , so , the state has depth , and line L7 writes the name of . If the state has depth , then is a prefix of length of a row of length , so is in , and because . So line L7 writes a name exactly when , and then the name of .
Lemma 3 (reduction). For and , , the pattern occurs at if and only if, with , the names written for the rows spell .
Proof. occurs at if and only if for each . Since , Lemma 2 shows that this holds if and only if the name written at is the name of : if the strings are equal, that name is written; if the name of is written, the text string is the row with that name, which is because different rows have different names.
Lemma 4 (columns). In the pass for width , line L10 at row and column outputs exactly for the such that is a suffix of the column string , where is the name written at , and each such once.
Proof. Line L5 starts at the root, and line L9 feeds it in order, one symbol per row, since column is visited at every row. A ⊥ is in no , so the step sends the state to the root, which is right by Lemma 1 because no nonempty suffix of a sequence ending in ⊥ is a prefix of some . Apply Lemma 1 with .
Theorem. The claim holds.
Proof. Termination: every loop runs over a finite range, and each step of an automaton follows finitely many failure links because each one lowers the depth. Line L1 outputs, for each pattern with no rows or with empty rows, exactly the triples of its occurrences given in the problem statement, once each, and such patterns take part in no pass. Take a nonempty pattern of width and a corner . A triple can be output only in the pass for width , at row and column , since line L10 determines and from , and . At that row and column, by Lemma 4 it is output once if is a suffix of , that is, if the rows of column spell (which needs ), and not otherwise. By Lemma 3 this happens exactly when occurs at . A corner with or never arises, since and . So every occurrence is output once and nothing else is output.