Algorithms from sedgewick and viterabi