to implement viterbi alg