Backward phase
During the backward phase, we need to compute the probability of a sequence starting at time t+1: ot+1, ot+2, ..., oSequence Length, given that the state at time t is i. Just like we have done before, we define the following probability:

The backward algorithm is very similar to the forward one, but in this case, we need to move in the opposite direction, assuming we know that the state at time t is i. The first state to consider is the last one xEnding, which is not emitting, like the initial state; therefore we have:

We terminate the recursion with the initial state:

The steps are the following ones:
- Initialization of a vector Backward with shape (N + 2, Sequence Length).
- Initialization of A (transition probability matrix) with shape (N, N). Each element is P(xi|xj).
- Initialization of B with shape (Sequence Length, N). Each element is P(oi|xj).
- For i=1 to N:
- Set Backward[xEndind, Sequence Length] = A[i, xEndind]
- For t=Sequence Length-1 to 1:
- For i=1 to N:
- Set S = 0
- For j=1 to N
- Set S = S + Backward[j, t+1] · A[j, i] · B[t+1, i]
- Set Backward[i, t] = S
- For i=1 to N:
- Set S = 0.
- For i=1 to N:
- Set S = S + Backward[i, 1] · A[0, i] · B[1, i]
- Set Backward[0, 1] = S.