Documentation

CslibFrontier.Algorithms.Lean.Palindrome.Eertree

Palindromic trees #

The eertree of a word has an imaginary root of length -1, the empty palindrome, and one vertex for each distinct nonempty palindromic factor. A letter-labelled edge extends a word at both ends, or creates a singleton from the imaginary root. The suffix link of a nonempty palindrome goes to its longest proper palindromic suffix. Both roots link to the imaginary root.

This executable representation implements the graph of Rubinchik and Shur, arXiv:1506.04862v2, §2, pp. 3–5, using full words as keys. Appending a letter retains the previous tree. A longest-first suffix scan stops at the first palindrome to identify the one possible new vertex and its link. The bound counts graph vertices. The paper's time and space bounds use a different representation and search.

Eertree vertex labels. The empty word is the root of length zero.

  • imaginary {α : Type u_1} : PalVertex α

    The formal root of length minus one, whose children are the single letters.

  • word {α : Type u_1} (letters : List α) : PalVertex α

    A word labelling a vertex, including the empty root.

Instances For
    @[instance_reducible]

    Word formed by a letter-labelled extension from a vertex.

    The text read so far and the suffix-link table indexed by complete palindromic words. The imaginary root is implicit. Each other vertex has exactly one entry.

    • text : List α

      Current input prefix.

    The empty word's eertree already contains its two roots.

    Append one letter, adding a suffix-link entry only for a previously unseen longest palindromic suffix. All earlier entries retain their links.

    Build a palindromic tree one letter at a time from left to right.

    Follow the suffix link of a vertex. An absent word has no link.

    def Cslib.Algorithms.Lean.Palindrome.Eertree.edge? {α : Type u_1} [DecidableEq α] (tree : Eertree α) (v : PalVertex α) (a : α) :

    Look up the outgoing edge labelled a. Full word keys determine extension edges.

    Building after a one-letter extension agrees with updating the previous tree.

    The word vertices are exactly the distinct palindromic factors, including the empty root.

    Counting the imaginary root as well, the eertree has at most length + 2 vertices.

    From either root or a word vertex, a letter-labelled edge exists precisely when the extended word occurs as a palindromic factor. The extended word determines the target.

    @[simp]

    The empty word has a suffix link to the imaginary root at every prefix.

    theorem Cslib.Algorithms.Lean.Palindrome.Eertree.suffixLink?_longest {α : Type u_1} [DecidableEq α] {w : List α} {a : α} {t : List α} (h : a :: t ∈ w.palindromicFactors) :
    ∃ (q : List α), (ofList w).suffixLink? (PalVertex.word (a :: t)) = some (PalVertex.word q) ∧ q <:+ a :: t ∧ q.Palindrome ∧ q.length < (a :: t).length ∧ ∀ (p : List α), p <:+ a :: t → p.Palindrome → p.length < (a :: t).length → p.length ≤ q.length

    A nonempty vertex links to the longest palindromic suffix strictly shorter than itself.