Dynamic programming is a method for solving a complex problem by breaking a big problem down into several simpler smaller ones. Take this shortest path problem for example.

The goal here is to count how many paths there are from the upper left hand corner of the grid down to the bottom right. The rule is you can only continuously move down and to the right.

A dynamic programming approach to this problem would be to note that the number of paths from the start to the end can be broken down by summing the number of paths from A to the end, also with B to the end. So now we have broken down our big problem, into smaller sub problems.

That that these subproblems can then be further broken down into as we walk through the grid.

This recursion stops when we get to the end and the number new paths left is 1.

One way of translating this into a recursive algorithm would be to write something like this.

With code like this

**DynamicProgrammingRecursive.java**

public class DynamicProgrammingRecursive { private int grid[][]; public DynamicProgrammingRecursive(int[][] grid) { this.grid = grid; } public int countPaths(int row, int col) { if (!isValidSquare(row, col)) return 0; if (isAtEnd(row, col)) return 1; return countPaths(row + 1, col) + countPaths(row, col + 1); } public boolean isValidSquare(int row, int col) { return isInBounds(row, col) && !isBlocked(row, col); } public boolean isBlocked(int row, int col) { return this.grid[row][col] == 1; } public boolean isInBounds(int row, int col) { return (row < grid.length && col < grid[0].length); } public boolean isAtEnd(int row, int col) { return grid.length - 1 == row && grid[row].length - 1 == col; } }

**foo**

import junit.framework.Assert; import org.junit.Before; import org.junit.Test; public class DynamicProgrammingRecursiveTest { private DynamicProgrammingIterative empty2x2; private DynamicProgrammingIterative empty3x3; @Before public void setUp() throws Exception { int[][] paths2x2 = new int[][] { {0,0}, {0,0} }; empty2x2 = new DynamicProgrammingIterative(paths2x2); int[][] paths3x3 = new int[][] { {0,0,0}, {0,0,0}, {0,0,0} }; empty3x3 = new DynamicProgrammingIterative(paths3x3); } /* 2 1 1 x */ @Test public void TwoByTwoEmptyPathCount() throws Exception { Assert.assertEquals(2, empty2x2.countPaths(1,1)); } /* 6 3 1 3 2 1 1 1 x */ @Test public void ThreeByThreeEmptyPathCount() throws Exception { Assert.assertEquals(6, empty3x3.countPaths(0,0)); } @Test public void IsAtEnd() throws Exception { int row = 0; int column = 0; Assert.assertTrue(empty2x2.isAtEnd(row, column)); } @Test public void IsInBounds() throws Exception { Assert.assertTrue(empty2x2.isInBounds(0,0)); Assert.assertTrue(empty2x2.isInBounds(0,1)); Assert.assertTrue(empty2x2.isInBounds(1,0)); Assert.assertTrue(empty2x2.isInBounds(1,1)); Assert.assertFalse(empty2x2.isInBounds(0,2)); Assert.assertFalse(empty2x2.isInBounds(2,0)); } @Test public void IsBlocked() throws Exception { int[][] paths = new int[][] { {0,0}, {0,1} }; DynamicProgrammingRecursive brute = new DynamicProgrammingRecursive(paths); Assert.assertFalse(brute.isBlocked(0,0)); Assert.assertFalse(brute.isBlocked(0,1)); Assert.assertFalse(brute.isBlocked(1,0)); Assert.assertTrue(brute.isBlocked(1,1)); } @Test public void CountBlockedPaths() throws Exception { int[][] paths = new int[][] { {0,0,0,0,0,0,0,0}, {0,0,1,0,0,0,1,0}, {0,0,0,0,1,0,0,0}, {1,0,1,0,0,1,0,0}, {0,0,1,0,0,0,0,0}, {0,0,0,1,1,0,1,0}, {0,1,0,0,0,1,0,0}, {0,0,0,0,0,0,0,0} }; DynamicProgrammingRecursive brute = new DynamicProgrammingRecursive(paths); Assert.assertEquals(27, brute.countPaths(0,0)); } }

We just traverse down and to the right, making sure that we are not out of bounds or in a blocked off square. And when we get to the end, we just return 1, allowing the recursion of the algorithm to walk back up to the top where we started.

The validSquare method does the boundary checking as well as making sure we are not in a blocked off square. If we are blocked return 0. If we are at the end return 1. Else recurse and do the calc for the next down and right squares of the grid.

*One Tip for when coding grids*

Whenever you do matrix algorithms use row and column for your variable names rather than x and y.

The reason for this is the row and column translates into [y][x] and its a common cause for mistakes. So when dealing with matrices using row and column instead. Just easier.

**Memoization**

One thing you may have noticed in our previous examples, is that we calculate the number of paths from C to the end twice. That is something we can store for future look ups. And that’s a memoization approach.

**DynamicProgrammingMemoized.java**

public class DynamicProgrammingMemoized { private int grid[][]; private int paths[][]; public DynamicProgrammingMemoized(int[][] grid) { this.grid = grid; this.paths = new int[grid.length][grid[0].length]; // assume square } public int countPaths(int row, int col) { if (!isValidSquare(row, col)) return 0; if (isAtEnd(row, col)) return 1; if (this.paths[row][col] == 0) { this.paths[row][col] = countPaths(row + 1, col) + countPaths(row, col + 1); } return this.paths[row][col]; } public boolean isValidSquare(int row, int col) { return isInBounds(row, col) && !isBlocked(row, col); } public boolean isBlocked(int row, int col) { return this.grid[row][col] == 1; } public boolean isInBounds(int row, int col) { return (row < grid.length && col < grid[0].length); } public boolean isAtEnd(int row, int col) { return grid.length - 1 == row && grid[row].length - 1 == col; } }

**DynamicProgrammingMemoizedTest.java**

import junit.framework.Assert; import org.junit.Before; import org.junit.Test; public class DynamicProgrammingMemoizedTest { private DynamicProgrammingMemoized empty2x2; private DynamicProgrammingMemoized empty3x3; @Before public void setUp() throws Exception { int[][] paths2x2 = new int[][] { {0,0}, {0,0} }; empty2x2 = new DynamicProgrammingMemoized(paths2x2); int[][] paths3x3 = new int[][] { {0,0,0}, {0,0,0}, {0,0,0} }; empty3x3 = new DynamicProgrammingMemoized(paths3x3); } /* 2 1 1 x */ @Test public void TwoByTwoEmptyPathCount() throws Exception { Assert.assertEquals(2, empty2x2.countPaths(0,0)); } /* 6 3 1 3 2 1 1 1 x */ @Test public void ThreeByThreeEmptyPathCount() throws Exception { Assert.assertEquals(6, empty3x3.countPaths(0,0)); } @Test public void CountBlockedPaths() throws Exception { int[][] paths = new int[][] { {0,0,0,0,0,0,0,0}, {0,0,1,0,0,0,1,0}, {0,0,0,0,1,0,0,0}, {1,0,1,0,0,1,0,0}, {0,0,1,0,0,0,0,0}, {0,0,0,1,1,0,1,0}, {0,1,0,0,0,1,0,0}, {0,0,0,0,0,0,0,0} }; DynamicProgrammingRecursive brute = new DynamicProgrammingRecursive(paths); Assert.assertEquals(27, brute.countPaths(0,0)); } }

Same idea as the recursive implementation. Only here we store the results as we calculate them which speeds the algorithm and makes the calculations happen a lot faster.

**Traditional Dynamic Programming Approach**

A more traditional dynamic approach to this problem would be to start at the end and work backwards working our way up.

If we think about what the recursive approach does, the first first concrete values it gets, other than blocked paths, are these two in the bottom right. 1 and 1.

Now, we can we go from there. If we take that recursion one step up, it may be filling one of the next three cells.

If we start on the left, all of these cells have just one path to the bottom right. So all of these values are one.

And if we continue doing this for the other cells, we can continue walking up and filling in values for the other cells as follows for the entire grid.

**DynamicProgrammingIterative.java**

public class DynamicProgrammingIterative { private int grid[][]; private int paths[][]; public DynamicProgrammingIterative(int[][] grid) { this.grid = grid; this.paths = new int[grid.length][grid.length]; } public int countPaths(int row, int col) { if (!isValidSquare(row, col)) return 0; if (isAtEnd(row, col)) return 1; if (isAtBeginning(row, col)) { paths[row][col] = 1; } if (paths[row][col] == 0) { int bottomCell = 0; int rightCell = 0; if (isValidSquare(row + 1, col)) { bottomCell = paths[row + 1][col]; } if (isValidSquare(row, col + 1)) { rightCell = paths[row][col + 1]; } paths[row][col] = bottomCell + rightCell; } return countPaths(row - 1, col) + countPaths(row, col - 1); } public boolean isValidSquare(int row, int col) { if (!isInBounds(row, col)) { return false; } if (isBlocked(row, col)) { return false; } return true; } public boolean isBlocked(int row, int col) { return grid[row][col] == 1; } public boolean isInBounds(int row, int col) { if (row < 0 || col < 0) { return false; } return (row < grid.length && col < grid[0].length); } public boolean isAtEnd(int row, int col) { return row == 0 && col == 0; } public boolean isAtBeginning(int row, int col) { return row == grid.length - 1 && col == grid[0].length - 1; } public void print2x2() { System.out.println(paths[0][0] + " " + paths[0][1]); System.out.println(paths[1][0] + " " + paths[1][1]); } public void print3x2() { System.out.println(paths[0][0] + " " + paths[0][1] + " " + paths[0][2]); System.out.println(paths[1][0] + " " + paths[1][1] + " " + paths[1][2]); System.out.println(paths[2][0] + " " + paths[2][1] + " " + paths[2][2]); } public void print8x8() { System.out.println(paths[0][0] + " " + paths[0][1] + " " + paths[0][2] + paths[0][3] + " " + paths[0][4] + " " + paths[0][5] + paths[0][6] + " " + paths[0][7]); System.out.println(paths[1][0] + " " + paths[1][1] + " " + paths[1][2] + paths[1][3] + " " + paths[1][4] + " " + paths[1][5] + paths[1][6] + " " + paths[1][7]); System.out.println(paths[2][0] + " " + paths[2][1] + " " + paths[2][2] + paths[2][3] + " " + paths[2][4] + " " + paths[2][5] + paths[2][6] + " " + paths[2][7]); System.out.println(paths[3][0] + " " + paths[3][1] + " " + paths[3][2] + paths[3][3] + " " + paths[3][4] + " " + paths[3][5] + paths[3][6] + " " + paths[3][7]); System.out.println(paths[4][0] + " " + paths[4][1] + " " + paths[4][2] + paths[4][3] + " " + paths[4][4] + " " + paths[4][5] + paths[4][6] + " " + paths[4][7]); System.out.println(paths[5][0] + " " + paths[5][1] + " " + paths[5][2] + paths[5][3] + " " + paths[5][4] + " " + paths[5][5] + paths[5][6] + " " + paths[5][7]); System.out.println(paths[6][0] + " " + paths[6][1] + " " + paths[6][2] + paths[6][3] + " " + paths[6][4] + " " + paths[6][5] + paths[6][6] + " " + paths[6][7]); System.out.println(paths[7][0] + " " + paths[0][1] + " " + paths[7][2] + paths[7][3] + " " + paths[0][4] + " " + paths[7][5] + paths[7][6] + " " + paths[7][7]); } }

**DynamicProgrammingIterativeTest.java**

import junit.framework.Assert; import org.junit.Before; import org.junit.Test; public class DynamicProgrammingIterativeTest { /* 2 1 1 x */ @Test public void TwoByTwoEmptyPathCount() throws Exception { int[][] paths2x2 = new int[][] { {0,0}, {0,0} }; DynamicProgrammingIterative empty2x2 = new DynamicProgrammingIterative(paths2x2); Assert.assertEquals(2, empty2x2.countPaths(1,1)); // start lower right } /* 1 x 1 1 */ @Test public void TwoByTwoOneCellBlocked() throws Exception { int[][] paths2x2 = new int[][] { {0,1}, {0,0} }; DynamicProgrammingIterative iterative = new DynamicProgrammingIterative(paths2x2); Assert.assertEquals(1, iterative.countPaths(1,1)); } /* 6 3 1 3 2 1 1 1 1 */ @Test public void ThreeByThreeEmpty() throws Exception { int[][] paths3x3 = new int[][] { {0,0,0}, {0,0,0}, {0,0,0} }; DynamicProgrammingIterative empty3x3 = new DynamicProgrammingIterative(paths3x3); Assert.assertEquals(6, empty3x3.countPaths(2,2)); } /* 3 1 0 2 1 x 1 1 1 */ @Test public void ThreeByThreeBlocked() throws Exception { int[][] paths3x3 = new int[][] { {0,0,0}, {0,0,1}, {0,0,0} }; DynamicProgrammingIterative iterative = new DynamicProgrammingIterative(paths3x3); Assert.assertEquals(3, iterative.countPaths(2,2)); } /* 3 1 0 2 1 x 1 1 1 */ @Test public void ThreeByThreeBlocked2() throws Exception { int[][] paths3x3 = new int[][] { {0,0,0}, {1,0,0}, {0,0,0} }; DynamicProgrammingIterative iterative = new DynamicProgrammingIterative(paths3x3); Assert.assertEquals(3, iterative.countPaths(2,2)); } /* 1 1 1 x x 1 1 1 1 */ @Test public void ThreeByThreeBlocked3() throws Exception { int[][] paths3x3 = new int[][] { {0,0,0}, {1,1,0}, {0,0,0} }; DynamicProgrammingIterative iterative = new DynamicProgrammingIterative(paths3x3); Assert.assertEquals(1, iterative.countPaths(2,2)); } @Test public void CountBlockedPaths() throws Exception { int[][] paths = new int[][] { {0,0,0,0,0,0,0,0}, {0,0,1,0,0,0,1,0}, {0,0,0,0,1,0,0,0}, {1,0,1,0,0,1,0,0}, {0,0,1,0,0,0,0,0}, {0,0,0,1,1,0,1,0}, {0,1,0,0,0,1,0,0}, {0,0,0,0,0,0,0,0} }; DynamicProgrammingIterative iterative = new DynamicProgrammingIterative(paths); Assert.assertEquals(27, iterative.countPaths(7,7)); } }

Now the advantage of doing things this way is that we have used slightly less memory. Runtime is still O(n^2) but we’ve reduced the actual amount of memory used slightly because we no longer have to use the call stack.

**So where does this leave us?**

The main take away of dynamic programming problems is that when you have a big problem that consists of many similarly oriented smaller ones, you can often implement them using a combination of recursion, with memoization for performance.

Not also that if you look at the tests I wrote, a good place to start with any of these problems is which something really small and simple (like a 2×2 or 3×3 matrix), write tests, and then work your way up from there.

Happy coding!

Links that help

http://www.sanfoundry.com/dynamic-programming-solutions-longest-common-substring-problem/

https://stackoverflow.com/questions/4000169/getting-the-array-length-of-a-2d-array-in-java

https://stackoverflow.com/questions/12231453/syntax-for-creating-a-two-dimensional-array

## Leave a Reply