Combinatorics · Lesson 6
Generating Bordered Words
The shortest border is unbordered
A word can have many borders, but its shortest border is special.
If a word is bordered and is its shortest border, then is unbordered and .
Unbordered. Any border of would be a prefix of and a suffix of (since is both), hence a border of shorter than — impossible.
At most half. If , the length- prefix of matches the corresponding suffix, giving a border of shorter than — again impossible. So .
Every bordered word is
The two observations above collapse into a clean characterization.
Let . A word of length is bordered if and only if for a unique unbordered word with and some word of length .
The word is the shortest border of ; the middle is the unique segment . Uniqueness is exactly why classifying words by their shortest border is the right way to count and to generate.
There are 10 bordered binary words of length 4.
0000 = 0 ·00· 0
Shortest border of length 1
Shortest border of length 2
Generated by GenB: every bordered word is x z x, where x is its shortest border (an unbordered word generated by GenU) and z is arbitrary. The words are grouped by the length of x.
Why the decomposition works
Only bordered. If is unbordered and , then is a prefix and a suffix of , so the word is bordered and has a proper border of length .
All, with the shortest. Any border of of length would be a prefix of and a suffix of , i.e. a border of . Since is unbordered, no such border exists. So the decomposition recovers exactly the shortest border — every bordered word is produced, each exactly once.
Counting sanity check. This is why alone could not count bordered words: the number of bordered words of length is
which equals .
Efficiency
The GenB algorithm loops over the shortest-border length , runs
GenU to produce the unbordered words of length
, and for each runs an odometer over the
choices of
.
Every recursive step of GenU and every odometer increment produces at least one output
word, and there are at least as many outputs as calls to GenU overall (each
yields at least one ). Excluding the cost of writing
each word out, GenB runs in constant-amortized time.
Practice
Which words are bordered?
Which of these words are bordered? Select all that apply.
Which of these words are bordered? Select all that apply.
Practice this topic →Every quiz from this lesson, at four difficulties.