Viterbi Algorithm
A twenty-word sentence with forty-five possible tags per word has more candidate tag sequences than there are atoms in your coffee. The Viterbi algorithm finds the single best one without ever listing them — which is why sequence models of the pre-neural era were usable at all.
One column at a time
It is dynamic programming over a lattice: positions in the sequence along one axis, possible states along the other. Sweeping left to right, it stores for each state the probability of the best path ending there together with a back-pointer to where that path came from. Because the model is Markovian — the next state depends only on the current one — a path that is already losing at a state can never win later, so it is discarded on the spot. The cost drops from exponential to roughly length times the square of the state count, and a backward walk along the pointers recovers the winning sequence.
Where you meet it
Anywhere a model scores whole sequences of labels:
- decoding the tag sequence of an HMM part-of-speech tagger, and the identical recursion over scores rather than probabilities in a linear-chain CRF, including the neural ones in spaCy and Flair;
- span extraction under BIO or BILOU tagging, where the transition scores forbid illegal moves such as an I-PER tag straight after an O;
- word segmentation and unigram subword tokenisation, where the lattice holds candidate pieces instead of tags.
What it does and does not give you
It returns the one most probable path, exactly — unlike beam search, which keeps only the top few partial paths and can lose the winner. It does not give per-token probabilities; those come from the forward-backward algorithm, which is what training and confidence estimates use. And it inherits the model's assumptions: a tag that depends on evidence three words ahead is beyond a first-order chain, however exact the decoding.
The Viterbi algorithm is a dynamic-programming decoder that finds the single most probable sequence of hidden states behind an observed sequence. Scanning left to right, it keeps only the best path reaching each state, plus a back-pointer, reducing an exponential search to time proportional to sequence length times the square of the state count. It decodes HMM taggers and linear-chain CRFs, enforces legal BIO transitions, and segments text in unigram tokenizers.
Picture driving across a city where every junction offers several turns. You want the quickest route from one side to the other, and checking every possible route would take all day. Here is the trick: at each junction, keep only the fastest way of getting there and forget the rest — a slower arrival can never turn into the best route later on.
The Viterbi algorithm does precisely this with words. When software has to decide which word in a sentence is a person's name and which is an ordinary verb, it treats each word as a junction and each possible label as a turn. It then finds the best reading of the whole sentence at once, quickly, without ever listing the millions of alternatives.