implement Viterbi algorithm