In computer science Dynamic Programming is a powerful algorithmic paradigm that can help solve seemingly intractable problems in viable ways.

The rather confusing name ‘Dynamic Programming’ originates from quite an amusing story.

First of all the word ‘Programming’ comes from a time when programmers weren’t programming computers, but were rather planners for logistic purposes (and such).

It is said that Richard Bellman invented the term as some sort of euphemism to avoid pissing off his military boss who hated the word ‘research’ and would turn violent if anyone used it in his presence. So to hide the mathematical character of his work he needed a term that wouldn’t get him into trouble for working on the planning of optimal multi-stage processes, so ‘Dynamic Programming’ it was.

In many algorithmic problems it is common to split a problem down into smaller sub-problems, solve each sub-problem and then re-combine solutions to a full solution for the original problem. The paradigm I just described is commonly referred to as **Divide-and-Conquer**. The methods to achieve this can vary, but very often some naive form of recursion can be thought out with relative ease. Unfortunately such solutions may lead to an exponential explosion of sub-problems if you allow the recursion to branch.

Certain problems however have the property that generated sub-problems begin to repeat themselves in an overlapping manner. This means the recursive solution does a lot of unnecessary work since it would be possible to avoid re-solving the same problem over and over again if the solution was saved the first time it was encountered and subsequently looked up in a table.

Dynamic Programming algorithms exploit this overlapping property in the way described above to create more efficient solutions.

For these reasons Dynamic Programming solutions can often bring down the runtime of a naive exponential time algorithm to polynomial time. For all practical purposes this can mean the difference between usable and not usable.

A basic example is that of computing the term of the Fibonacci sequence.

**Problem 1 (Computing The Fibonacci Sequence)**

The Fibonacci Sequence is defined via the recursion where .

The recursive algorithm is straightforward and presented below in pseudo language.

1. **If**

2.

3. **Else**

4.

As you can see there are going to be many overlapping subproblems when the recursion tree is expanded.

If we let represent the number of additions we have to perform to compute we get that . This is a so called non-homogeneous linear difference equation which can be solved for using methods I outlined in this post (note that so could if we wanted to).

Most importantly however is the realization that grows exponentially, which can readily be seen from the picture above.

If we save solutions to old subproblems as opposed to recomputing them, it seems like we should be able to bring down the number of additions drastically. Therefore instead of recursively finding a solution top down, we construct a solution **bottom up**, referring back to smaller sub-problems we solved along the way.

The bottom up approach is generally what characterizes Dynamic Programming algorithms.

Here is pseudo code for a Dynamic Programming solution to the Fibonacci-Recurrence

1.

2.

3. **For** **to**

4.

5.

Now our sub-problem graph looks much smaller:

In fact it is easy to see that only number of additions are necessary using this algorithm.

Worth noting is that it would have also been possible to pass up solutions to smaller sub-problems in a

**top down** recursion. This technique is commonly known as **memoization** (no spelling error).

Each recursive call leaves a ‘memo’ for its parent caller with intermediate results so it can avoid expanding unnecessary branches. The main difference is that memoization is lazy which roughly means it won’t do any evaluation until it must. This can be problematic in practice because of stack depth limits. Dynamic Programming gives you a little bit more freedom as to which order sub-problems should be computed although memoization often feels more “natural” to people which is why some people prefer it.

One of the hallmark applications of Dynamic Programming is in solving optimization problems.

The problem should, apart from having **overlapping sub-problems** also be re-combinable. That is to say, optimal solutions to smaller problems make up optimal solutions to bigger problems. An optimization problem with this property is said to exhibit **optimal substructure**. These two properties together are pre-requisites for Dynamic Programming solutions.

**Problem 2 (Longest and Shortest Paths)**

Not all problems exhibit optimal substructure.

Consider the problem of finding the longest simple path in a directed graph with positive weights.

On the other hand the shortest simple path problem in a directed graph with positive weights has got optimal substructure.

The reason this argument doesn’t work for the Longest Simple Path problem is that concatenating two longest sub-paths may result in loops which are not allowed since paths have to be simple. On the other hand a path that has a loop can never be shortest (removing the loop yields a shorter path).

There are many algorithms for computing shortest paths in a directed graph.

Perhaps among the most well-known is Dijkstra’s Algorithm which only works for graphs with positive weights.

This algorithm exploits the Greedy Choice Property of the problem (which is for a different discussion).

The Dynamic Programming solution works by solving sub-problems of finding the lengths of all shortest paths from a source to any given node using at most intermediate nodes.

The algorithm can also be easily modified to output the actual path by maintaining a successor for every node.

This is what the Dynamic Programming algorithm looks like in pseudo language:

1. **ForEach**

2.

3.

4.

5. **For** **to**

6. **ForEach**

7.

8. **ForEach**

9.

10.

11.

It’s runtime is and its memory requirements .

There is an improved version of the above code that’s called Bellman-Ford algorithm.

The worst-case runtime complexity of this algorithm is still but runs substantially faster in practice. Its memory requirement is reduced to .

Like the above algorithm it can also handle negative weights as long as there is no negative cycle in the graph.

Also worth mentioning is the **Floyd-Warshall algorithm** which computes the length of the shortest path between *every* pair of nodes in a graph. This is also a Dynamic Programming algorithm which successively computes the length of the shortest path between nodes and given that the path only uses nodes from .

The input is a weighted adjacency matrix .

1.

2. **For** **to**

3. **For** **to**

4.

5.

6. **For** **to**

7. **For** **to**

8. **For** **to**

9.

10.

11.

Both runtime and memory are clearly above.

However memory can be reduced to by removing the dimension for from (why?)

The algorithm can also be modified to output the shortest paths and not just their length (how?)

In the remainder of this post we will look at various other instances of Dynamic Programming problems.

**Problem 3 ( The Hiring Problem )**

*Suppose you are a company manager and have just received an augmented budget to recruit new people into your team to compensate for the increased work load.*

The company you work for is popular so there are an abundant number of people applying with all kinds of backgrounds and experience. It is expected that candidates with certain types of profile will provide more ‘value’ (a subjective integer metric) than others, but at the same time eat up more/less of the budget.

You have different types of profile categories, an array of the expected value they will provide and an array of how much they typically cost (in thousands of dollars).

Your additional budget is thousand dollars and you may choose any number of candidates from each profile category (as long as budget is not breached).

You are tasked to come up with a set of new hires that will provide the most value to the team.

*Give an algorithm to compute the optimal hires and their total value added.*

Note: This problem is more commonly referred to as the **Integer Knapsack Problem** with the popular metaphor being what value items a burglar should put in his knapsack if he is only capable of carrying a certain maximum weight.

**Solution**

The mathematical formulation of this problem can be expressed via the following Integer Program

**Integer Knapsack Problem**

Maximize

subject to:

Let us check first whether this problem even needs a Dynamic Programming solution.

Couldn’t we just be greedy and choose the profiles that maximize value per cost?

The answer is no in general:

What about a recursive solution?

1.

This algorithm has exponential worst case runtime complexity!

Let’s investigate a Dynamic Programming solution.

What are the sub-problems?

- Define Maximum value given budget

Clearly if the optimal solution to involves hiring a profile candidate then removing this hire leaves us with the problem of finding the optimal solution to .

Since the optimal hire has to be one of the profiles which can yet fit into the budget we should look for similarly to how we did for the recursive solution.

However this time we also remember the optimal solutions to the smaller sub-problems.

In a sense if we look at the budgets as nodes and let the costs describe transitions between the nodes weighted by the profile value then this problem is equivalent to finding the longest path in a directed graph with positive weights.

But did we not say earlier that this problem hasn’t got the optimal substructure property?

The difference here is that the graph is acyclic and this eliminates the problem described earlier with concatenation of longest simple sub-paths.

Hence when we are dealing with directed acyclic graphs(DAGs) we do in fact always have optimal sub-structure.

A lot (if not all) Dynamic Programming problems related to optimization can be reduced to the problem of finding the longest/shortest path in a DAG so it is well worth remembering how to solve this problem.

Finally we can present the Dynamic Programming algorithm for solving our problem:

1.

2. **For** **to**

3.

4.

To output a team that achieves this maximum you can modify the algorithm as follows:

1. **For** **to**

2.

3. **For** **to**

4. **If** **and**

5.

6.

7.

8.

9.

Then you can follow pointers back and unfold the optimal solution via .

The algorithm has runtime complexity and memory requirements.

Is that really good?

Well it is better than exponential, but the runtime is unfortunately dependent on the value of the input rather than just the size of it. For that reason we say that the complexity is ** pseudo-polynomial**.

For general this problem is actually NP-hard.

Now let’s look at a slightly trickier problem.

**Problem 4 (The Firing Problem)**

*This time you are also managing a team but the company you are working for is not doing very well so they need you to fire people in order to maintain a tighter budget for your team.*

Given are people in your team with values and costs .

Your new budget is thousand dollars.

*Give an algorithm for computing the set of people you should keep in order to maximize value while not breaching budget.*

Note: This problem is more commonly referred to as the **Binary Knapsack Problem**

**Solution**

This problem looks awfully similar to the previous problem.

There is however one important difference.

You can only keep or fire a person once!

The corresponding integer program is given by

**Binary Knapsack Problem**

Maximize

subject to:

In the previous problem we were able to define sub-problems as

- Maximum value given budget

Here this doesn’t work, how do we know if we have already kept an employee or not?

If we have, we cannot keep the employee twice so it seems we need to enlarge our state space to take this into account.

We can make the following new definition:

- Maximum value given budget using only employees from

When we increment the second parameter we have the value of the optimal team using one less employee for the same budget. The new employee either is, or is not part of an optimal team with the given budget.

Whichever it is this employee must be added to a team which is optimal with respect to the other employees before it.

So with a budget of we can either gain or not gain by adding employee to the optimal solution for over not adding employee to the optimal team using only employees in .

We thus get the following expression for the optimal value of a subproblem:

Hence we can construct the following Dynamic Programming algorithm:

1. **For** **to**

2.

3. **For** **to**

4.

5.

6. **For** **to**

7. **For** **to**

8.

9. **If** **and**

10.

11.

12.

13. **Else**

14.

15.

16.

The optimal team is then found by iterating over firing every employee such that and keeping the rest (hopefully there are no bugs).

The algorithm has runtime complexity and memory requirements.

The time management problem where tasks are given taking hours to complete and have value for is also equivalent to the Binary Knapsack problem. The knapsack capacity is given by a global deadline and the problem is to pick out the tasks that will maximize value before hitting deadline. In general any problem which fits the integer program specification can be solved using the same algorithm.

The final problem along with its many variations is a very intriguing problem which occurs naturally in many real-world situations. Unfortunately its general formulation is very hard to solve (NP-hard in fact).

However if we make some simplifying assumptions it can be solved optimally in polynomial time using a Dynamic Programming approach (though still very unpractical for larger inputs).

**Problem 5 (The Parallel Task Scheduling Problem)**

*Given are tasks of different types with multiplicities and processing times for *

You have a requirement that all tasks need to finish before time .

To achieve this the tasks need to be scheduled to run in parallel on a number of identical machines.

*Give an algorithm for computing the minimum number of machines needed.*

**Solution**

This problem is a restricted version of the general **1-dimensional Bin Packing Problem**.

In the bin-packing problem the assumption that there can only exist different item-types is relaxed to allow for any number of item sizes.

This problem can be specified via the following integer program:

**Restricted Bin Packing Problem**

Minimize

subject to:

Here if task is scheduled for machine and if machine is used.

The first constraint says that the processing time for all tasks scheduled on machine should not exceed and the third constraint that each task is scheduled on exactly one machine.

It is quite clear that a naive brute-force solution to this problem is intractable.

Effectively we would have to enumerate all partitions of tasks (of which there are exponentially many), check that none of the subsets of the partition exceed the time limit (in linear time) and then take the minimum over all such feasible partitions.

What about a Dynamic Programming solution?

A natural choice for our sub-problems are

- Minimal number of machines needed with tasks of type

The tricky part is how we should transition from smaller to larger problem instances.

We cannot simply remove a task and ask what the optimal solution is to the sub-problem since the number of required machines could either increase or remain the same through some potential rearrangement, so there is no obvious way to build on top of a smaller optimal solution with a single new task.

The easiest way to get to a bigger instance from a smaller one is by stepping one machine at a time i.e if we know all possible ways of combining tasks into a single machine then the optimal solution to is obtained from the optimal solution to by considering all possible task tuples s.t fitting into one machine.

So an optimal solution can be obtained by stepping forward with one machine from some optimal solution with one less machine whichever way it may be, we must consider all tuples that could possibly be assigned to that machine since any of the extensions could be optimal.

Define the set of all ways of filling one machine with tasks satisfying the constraint . Then has size at most

We can compute in time via the following algorithm (which if implemented properly should rather be recursively defined with a parameter k)

1.

2. **For** **to**

3. **For** **to**

4. …

5. **For** **to**

6. **If**

7.

8.

9.

10.

We can now compute the minimum number of required machines via:

1.

2.

3. **For** **to**

4. **For** **to**

5. …

6. **For** **to**

7.

8.

For each of the at most sub-problems we do at most work by computing the min over all base configurations. Hence worst-case runtime complexity is i.e polynomial in the size of the task input for fixed . Space requirement is .

The bin packing problem arises naturally in many logistics problems such as the problem of finding the minimum number of lorries required to deliver goods of different sizes or the minimum number of shelves needed in a storage room. It also arises in advertisement problems, where you for instance want to fit a certain number of commercials of different length in a minimum series of 3 minute television breaks. As we have seen above it can also arise in scheduling problems. Wikipedia mentions another interesting variant of the Bin Packing problem where items could occupy less space in comparison to the sum of their individual sizes when packed together. This arises in the problem of minimizing physical servers hosting virtual machines (VMs), where resource sharing may occur on the host. For that reason this variation of the problem is referred to as VM packing.

Needless to say the Bin Packing problem is very important.

Many other nice variations of this problem also exists. For example one may be interested in looking at bins of multiple capacities, higher dimensional extensions of the bins/items, different geometrical shapes of the bins/items or looking at online versions of these problems (i.e where items arrive incrementally) and etc…

We saw that the Bin Packing problem admitted a polynomial time solution when restricted to distinct types of items. The performance however is still rather dreadful in practice. Often when dealing with difficult problems such as the Bin Packing problem one has to resort to heuristics and approximation algorithms.

One simple such heuristic is the **Next-Fit** packing algorithm which traverses the items once and checks whether the next item fits in the last bin. If it does, then it puts it there, if it doesn’t then it opens a new bin. This may seem like a rather crude way of going about it, but the algorithm has certain advantages.

For instance it is only linear time, works online and outputs at most twice the optimal number of bins [Exercise 1].

Yet another simple heuristic which is slightly slower [ or less depending on implementation] but gives better results is the **First-Fit** packing algorithm. This algorithm repeatedly traverses the list of items and inserts into the last bin the first item that fits into it. If no item fits, it opens a new bin and goes through the list again until all items are gone.

……

This concludes our discussion about dynamic programming.

There are many other interesting problems which fit this topic. You will see two of them in the exercises below, but I recommend signing up to places like TopCoder and check out their practice rooms for a large variety of other Dynamic Programming problems of varying difficulty.

If you are interested in algorithms in general you may also find it worthwhile to check out Wikipedia’s list of algorithms.

The standard book carried around by all computer scientists you can find here.

**Exercise 1 (Next-Fit Bin Packing – Easy):**

*Prove that the Next-Fit bin packing algorithm never does worse than two times the number of bins in the optimal solution.*

**Exercise 2 (Maximum Sum Contiguous Subsequence – Easy):**

*Give an algorithm for finding the value of the maximum sum contiguous subsequence in an array of integers.*

[Think carefully about what the sub-problems should be in a Dynamic Programming solution and how you would transition between them]

**Exercise 3 ( Which Meetings To Attend? – Intermediate):**

*Your day is very busy and your calendar is packed with meetings and various other commitments (all here referred to as “meetings”), some possibly overlapping.*

You are given an array of starting times for each meeting, an array of corresponding finishing times and an array of corresponding importance of the meetings.

Since you can only attend one meeting at a time you wish to pick out a subset of non-overlapping meetings which maximizes the total importance.

*Give a Dynamic Programming algorithm for computing the optimal meeting schedule.*

(Hint: You may wish to do some pre-processing to compute an array which gives you the latest finishing meeting not overlapping with meeting )

Example: