↑ ↓ to move · enter to open

021 · Expert · Graphs, topological sort

alien dictionary

An alien language writes with our letters but sorts them in an order nobody told you. All you have is a list of its words, already in dictionary order. Each pair of neighboring words gives at most one clue about which letter comes first, the clues form a directed graph, and any topological order of that graph is an alphabet that fits.

LAB 1

reading the clues

A dictionary sorts by the first letter where two words differ. So walk each neighboring pair left to right until the letters disagree. That position is the only clue the pair gives: the top word's letter comes before the bottom word's letter. Everything after it was never used to sort the pair, so it tells you nothing.

neighboring pairs
Presets

How to read this: each box holds two neighboring words. We compare them letter by letter, and the first letters that differ (gold) tell us which letter comes first in the alien alphabet.

  • same in both words
  • comparing now
  • first difference: the clue
  • struck out: after the clue, ignored
  • longer word placed first (impossible)
  • faded box: not read yet

Try this: press Step a few times and watch where the gold letter lands in each pair. In the Clues list, gold chips are new clues and dim ones are repeats.

—
Pairs checked
—
Letter comparisons
—
Distinct clues
—
Letters seen

This step0 / 0

Clues

Extract the cluesO(C)

Each letter is compared at most once, so the work is bounded by C, the total number of letters in the list. With n words there are n − 1 pairs and at most n − 1 clues.

LAB 2

from clues to alphabet

Draw each letter as a node and each clue x → y as an arrow. The badge on a node is its in-degree: how many letters still have to be placed before it. Kahn's algorithm repeatedly takes a letter whose badge reads 0, writes it down, and removes its outgoing arrows. The letters come out in gold, in order.

letter graph
Queue
Alphabet
Presets

How to read this: an arrow x → y means x comes before y. The small badge on each letter counts arrows still pointing into it; when it hits 0 the letter is free to be written next. Letters are laid out left to right by how many must come before them.

  • ready: nothing left before it (badge 0)
  • being written now
  • written; "#n" = nth letter of the alphabet
  • thick arrow: being crossed off
  • dashed arrow: already used

Try this: pick "one loose letter", then Step and watch the queue hold two letters at once.

—
Letters · arrows
—
Letters placed
—
Operations (V + E)
—
Valid alphabets

This step0 / 0

Kahn's algorithmO(V + E)

Is the alphabet unique?—

The run is the same Kahn's algorithm as in 019 · Topological sort, fed by a graph built from words. More graph algorithms live in 002 · Graphs.

LAB 3

when the dictionary is broken

Not every word list can come from a real dictionary. Two things break it: a word placed after a longer word that starts with it, and clues that chase each other in a circle. A third case looks odd but is fine: letters that appear in the words and never get a clue. Pick a preset or type your own list, then run the full check.

check
Presets

How to read this: the top row shows each neighboring pair and the clue it gives (gold), or "·" for none. Below, arrows are clues; if they chase each other in a loop, no alphabet can work.

  • red letters and arrows: a loop of clues (impossible)
  • "blocked": waits on a letter in the loop
  • dashed green ring: no clue, can go anywhere
  • red "✕" pair: longer word came first

Try this: run "three-letter cycle", then "free letters", and compare the verdicts.

—
Verdict
—
Pairs checked
—
Letters placed
—
Letters with no clue

This step0 / 0

The full check

Why these fail

Prefix second. Any real dictionary puts ab before abc, because running out of letters counts as smaller than any letter. No alphabet can put abc first, so the answer is "no order". The reverse, a prefix placed first, is normal and gives no clue.

A cycle. Clues a → b, b → c, c → a would need a before itself. Kahn's algorithm detects this without searching for it: every letter on the cycle keeps an in-degree of at least 1, the queue empties early, and fewer letters come out than went in. Letters downstream of the cycle get stuck too.

No clue. A letter with no arrows starts at in-degree 0 and can go anywhere in the alphabet. That is still a valid answer, just one of many.