implemented forward algorithm