Implemented Viterbi algorithm.