finally successfully implemented forward and backward algorithms