SMS: Algebraic dynamic programming