## Dynamic Programming Example

Dynamic Programming is iterative. Each major step finds the optimal solution to a subproblem (a small part of the whole problem). We then enlarge the subproblem slightly and use the optimum solutions found in the previous steps to find the optimum solution to the current subproblem. We continue enlarging subproblems until we have solved the entire problem, then trace back to find the solution.

Problems that can be solved by Dynamic Programming have these characteristics:

- problem can be divided into
,*stages* - each stage has one or more
,*states* - you make a
at each stage,*decision* - the decision you make affects the state for the next stage,
- there is a
between the value of the decision at the stage and the previously found optima.*recursive relationship*

**Example:** The number of students that fail ENG101 depends on the number of TAs assigned to each section. There are six TAs available to be assigned to the course and there are four sections. How many TAs should be assigned to each section of the course to have the fewest students fail?

The first step is to formulate the solution. We use reverse recursion.

stages:ENG101 sections (ie. 1st solve section D, 2nd solve sections C and D, 3rd solve sections B, C and D, 4th solve sections A, B, C and D)

state at a stage:number of TAs available to be assigned

decision:how many TAs to assign to sectioni

decision update to state:number of available TAs to assign is reduced according to the decision.

recursive value relationship:

xᵢ: number of TAs assigned to section i

Nᵢ(xᵢ) : number of students who fail ENG101 in sectionigivenxᵢTAs are assigned

dᵢ: state (number of TAs available at the start of stagei)

fᵢ(dᵢ) :possible solution from stagebestito the end

*fᵢ*(*dᵢ*) = min over *x _{i}*
of {

*N*(

_{i}*x*) +

_{i}*f*

_{i+1}(

*d*-

_{i}*x*)}

_{i}Notice how the recursive relationship ties together (1) the local value of a decision at the current stage *N _{i}*(

*x*) , and (2) the optimum solution for an already-solved subproblem

_{i}*f*

_{i+1}(

*d*-

_{i}*x*). This use of previously-found optima is what makes dynamic programming efficient.

_{i}