https://www.wikiwand.com/en/Viterbi_algorithm
A Dynamic programming algorithm used to calculate the most likely path through a Hidden Markov model (or a Markov information source), given the hidden-state-dependent observations.
Iteration step: at point i in the sequence, and two possible states,
then we can store
by computing these recursively as
When we reach the end, the we choose the state at the end of the sequence that has the most potential likelihood, and then proceed backwards following the optimal path.