Here's an implementation of the Viterbi algorithm that I "discovered" recently. The purpose here is to decide the optimal distribution of frame types in video encoding. Viterbi itself is a bit hard to understand sometimes, so I think the best method is via actual example.
In this example, Max Consecutive B-frames is 2. All paths must end with a P-frame.
Path length of 1 gives us P as our best path, since all paths must end on a P-frame, there's no other choice.
Path length of 2 gives us BP and _P. "_" is the best path of length 1.
This gives us BP and PP. Now, we calculate the actual costs.
Let's say, for the sake of this example, that BP is best.
Path length of 3 gives us BBP and _BP and __P. "__" is the best path of length 2.
This gives us BBP and PBP and BPP. Now, we calculate the actual costs.
Let's say, for the sake of this example, that BBP is best.
Path length of 4 gives us _BBP and __BP and ___P. "___" is the best path of length 3.
This gives us PBBP and BPBP and BBPP. Now, we calculate the actual costs.
Let's say, for the sake of this example, that BPBP is best.
Path length of 4 gives us __BBP and ___BP and ____P. "____" is the best path of length 4.
This gives us BPBBP and BBPBP and BPBPP.
Now--wait a minute--all of the paths agree that the first frame is a B! So the first frame is a B.
Process is repeated until they agree on which frame is the first P-frame, and then encoding starts.
This algorithm can be adapted to a huge variety of problems in many fields; its also the same algorithm I referred to in this post.