# What is the complexity of Viterbi algorithm ?

• Viterbi algorithm is a dynamic programming approach to find the most probable sequence of hidden states given the observed data, as modeled  by a HMM.
• Without dynamic programming, it becomes an exponential problem as there are exponential number of possible sequences for a given observation(How – explained in answer below).
• Let the transition probabilities(state transition) be denoted by , and the output probabilities be denoted by • Here let us call the most probable sequence of hidden states of length that finishes in the state and generates the output and be the probability of this sequence which can be computed  from the equation below  • Say we have states , observation sequence of length , then we need to find and and maximum in each iteration is obtained by checking for all states . This leave us with a of • Space complexity: Next question could be what is the extra storage or memory complexity of Viterbi Algorithm –With DP, we always maintain an extra array to store the intermediate computations for reuse to avoid re-work. Here, we maintain a matrix which is in size. We also maintain argmax in every iteration which is also of same size. Hence the memory complexity is *Hint: To answer such a question one must either remember the exact answer or the algorithm itself. In general, to compute complexity one can check how many times we have terms like “for all” .