function [P, alpha] = hmmforward(a, pi, b, o)

spi = size(pi');
so = size(o');

N = spi(1);
T = so(1);

for i = 1:N
	alpha(1,i) = pi(i)*b(i,o(1));
end

for t = 1:(T-1)
	for j = 1:N
		pref = 0;
		for i = 1:N
			pref = pref + (alpha(t, i)*a(i,j));
		end
		alpha(t+1, j) = pref*b(j,o(t+1));
	end
end

P = 0;
for i = 1:N
	P = P + alpha(T, i);
end
