---
title: Viterbi algorithm
slug: viterbi-algorithm
qid: 
snapshot: enwiki-001375500441
content_hash: 
license: CC BY-SA 4.0
source: https://en.wikipedia.org/w/index.php?oldid=1375500441
tags: [10639794, 111278769, 20548265, 21493616, 22906789, 23081986, 23118434, 23603337, 24043274, 24623845, 25257581, 26709286, 27424684, 37645845]
---

# Viterbi algorithm

> [!note] Provenance
> Snapshot `enwiki-001375500441` · content root `…`
> Derived from <https://en.wikipedia.org/w/index.php?oldid=1375500441> · Licensed CC BY-SA 4.0

The Viterbi algorithm is a dynamic programming algorithm that finds the most likely sequence of hidden events that would explain a sequence of observed events. The result of the algorithm is often called the Viterbi path. It is most commonly used with hidden Markov models (HMMs). For example, if a doctor observes a patient's symptoms over several days (the observed events), the Viterbi algorithm could determine the most probable sequence of underlying health conditions (the hidden events) that caused those symptoms. The algorithm has found universal application in decoding the convolutional codes used in both CDMA and GSM digital cellular, dial-up modems, satellite, deep-space communications, and 802.11 wireless LANs. It is also commonly used in speech recognition, speech synthesis, diarization, keyword spotting, computational linguistics, and bioinformatics. For instance, in speech-to-text (speech recognition), the acoustic signal is the observed sequence, and a string of text is the "hidden cause" of that signal. The Viterbi algorithm finds the most likely string of text given the acoustic signal.

The Viterbi algorithm is a dynamic programming algorithm that finds the most likely sequence of hidden events that would explain a sequence of observed events. The result of the algorithm is often called the Viterbi path. It is most commonly used with hidden Markov models (HMMs). For example, if a doctor observes a patient's symptoms over several days (the observed events), the Viterbi algorithm could determine the most probable sequence of underlying health conditions (the hidden events) that caused those symptoms. The algorithm has found universal application in decoding the convolutional codes used in both CDMA and GSM digital cellular, dial-up modems, satellite, deep-space communications, and 802.11 wireless LANs. It is also commonly used in speech recognition, speech synthesis, diarization, keyword spotting, computational linguistics, and bioinformatics. For instance, in speech-to-text (speech recognition), the acoustic signal is the observed sequence, and a string of text is the "hidden cause" of that signal. The Viterbi algorithm finds the most likely string of text given the acoustic signal. P_{t,s} = \begin{cases} \pi_s \cdot b_{s,o_t} & \text{if } t=0, \\ \max_{r \in S} \left(P_{t-1,r} \cdot a_{r,s} \cdot b_{s,o_t} \right) & \text{if } t>0. \end{cases} The formula for Q_{t,s} is identical for t>0 , except that \max is replaced with \arg\max , and Q_{0,s} = 0 . The Viterbi path can be found by selecting the maximum of P at the final timestep, and following Q in reverse. prob[0][s] = init[s] * emit[s][obs[0 for t = 1 to T - 1 inclusive do // t = 0 has been dealt with already for each state s in states do for each state r in states do new_prob ← prob[t - 1][r] * trans[r][s] * emit[s][obs[t if new_prob > prob[t][s] then prob[t][s] ← new_prob prev[t][s] ← r path ← empty array of length T path[T - 1] ← the state s with maximum prob[T - 1][s] for t = T - 2 to 0 inclusive do path[t] ← prev[t + 1][path[t + 1 return path end The time complexity of the algorithm is O(T\times\left|{S}\right|^2) . If it is known which state transitions have non-zero probability, an improved bound can be found by iterating over only those r which link to s in the inner loop. Then using amortized analysis one can show that the complexity is O(T\times(\left|{S}\right| + \left|{E}\right|)) , where E is the number of edges in the graph, i.e. the number of non-zero entries in the transition matrix. init = {"Healthy": 0.6, "Fever": 0.4} trans = { "Healthy": {"Healthy": 0.7, "Fever": 0.3}, "Fever": {"Healthy": 0.4, "Fever": 0.6}, } emit = { "Healthy": {"normal": 0.5, "cold": 0.4, "dizzy": 0.1}, "Fever": {"normal": 0.1, "cold": 0.3, "dizzy": 0.6}, } In this code, init represents the doctor's belief about how likely the patient is to be healthy initially. Note that the particular probability distribution used here is not the equilibrium one, which would be {'Healthy': 0.57, 'Fever': 0.43} according to the transition probabilities. The transition probabilities trans represent the change of health condition in the underlying Markov chain. In this example, a patient who is healthy today has only a 30% chance of having a fever tomorrow. The emission probabilities emit represent how likely each possible observation (normal, cold, or dizzy) is, given the underlying condition (healthy or fever). A patient who is healthy has a 50% chance of feeling normal; one who has a fever has a 60% chance of feeling dizzy. A particular patient visits three days in a row, and reports feeling normal on the first day, cold on the second day, and dizzy on the third day. Firstly, the probabilities of being healthy or having a fever on the first day are calculated. The probability that a patient will be healthy on the first day and report feeling normal is 0.6 \times 0.5 = 0.3 . Similarly, the probability that a patient will have a fever on the first day and report feeling normal is 0.4 \times 0.1 = 0.04 . The probabilities for each of the following days can be calculated from the previous day directly. For example, the highest chance of being healthy on the second day and reporting to be cold, following reporting being normal on the first day, is the maximum of 0.3 \times 0.7 \times 0.4 = 0.084 and 0.04 \times 0.4 \times 0.4 = 0.0064 . This suggests it is more likely that the patient was healthy for both of those days, rather than having a fever and recovering. The rest of the probabilities are summarised in the following table: From the table, it can be seen that the patient most likely had a fever on the third day. Furthermore, there exists a sequence of states ending on "fever", of which the probability of producing the given observations is 0.01512. This sequence is precisely (healthy, healthy, fever), which can be found be tracing back which states were used when calculating the maxima (which happens to be the best guess from each day but will not always be). In other words, given the observed activities, the patient was most likely to have been healthy on the first day and on the second day (despite feeling cold that day), and only to have contracted a fever on the third day. The operation of Viterbi's algorithm can be visualized by means of a trellis diagram. The Viterbi path is essentially the shortest path through this trellis.
