Forward-Backward Algorithm
You can see the words of a sentence but not the grammatical tags behind them. For each position you want the probability of every possible hidden tag given the whole sentence — the evidence before it and after it alike. The forward-backward algorithm computes exactly that, without enumerating the exponentially many tag sequences.
Two passes, then a product
It is dynamic programming run twice over a hidden Markov model:
- the forward pass fills a table of alpha values — the probability of the observations up to a position together with being in a given state there;
- the backward pass fills beta values — the probability of everything after that position, given the same state;
- multiplying alpha by beta at a position and normalising gives the posterior probability of each state, and summing the final alphas gives the sentence's likelihood.
Why both passes matter
This is not the question Viterbi answers. Viterbi returns the single most likely tag path; forward-backward returns a distribution per position, which is what you need to report confidence or to hand a downstream component something soft rather than a hard guess. The two can disagree — the most likely tag at every position, taken together, need not be the most likely sequence.
Where it shows up
Its historical job is the E step of Baum-Welch, the expectation-maximisation procedure that trains an HMM tagger from unlabelled text. The same machinery reappears in modern work: the gradient of a linear-chain CRF needs exactly these marginals, and the CTC loss used in speech and handwriting recognition is a forward-backward computation under another name.
The forward-backward algorithm computes, for every position in a sequence, the probability of each hidden state given the entire observation sequence. It combines a forward pass over the prefix with a backward pass over the suffix, multiplying the two to obtain state posteriors, and also yields the likelihood of the sequence itself. It is the inference engine behind Baum-Welch training of hidden Markov models and behind the gradient of a linear-chain CRF.
Imagine working out what a friend did each day last week from their photos alone. You would not judge each day on its own — Thursday's blurry picture makes sense once you know they were travelling on Wednesday and arrived on Friday. You read the week forwards and backwards.
The forward-backward algorithm is software doing that for a sequence of words. For every position it asks how likely each hidden possibility is, using everything that came before it and everything that came after. That two-sided view is what lets a program say how confident it is about each word's role in a sentence, instead of simply committing to one answer and moving on.