Generalizing Viterbi decoding algorithm