Given a cost matrix cost[][] and a position (m, n) in cost[][], write a function that returns cost of minimum cost path to reach (m, n) from (0, 0). Each cell of the matrix represents a cost to traverse through that cell. Total cost of a path to reach (m, n) is sum of all the costs on that path (including both source and destination). You can only traverse down, right and diagonally lower cells from a given cell, i.e., from a given cell (i, j), cells (i+1, j), (i, j+1) and (i+1, j+1) can be traversed. You may assume that all costs are positive integers.

For example, in the following figure, what is the minimum cost path to (2, 2)?

Min Cost Path

The path with minimum cost is highlighted in the following figure. The path is (0, 0) –> (0, 1) –> (1, 2) –> (2, 2). The cost of the path is 8 (1 + 2 + 2 + 3).

Min Cost Path

  • Optimal Substructure
    The path to reach (m, n) must be through one of the 3 cells: (m-1, n-1) or (m-1, n) or (m, n-1). So minimum cost to reach (m, n) can be written as “minimum of the 3 cells plus cost[m][n]”.

minCost(m, n) = min (minCost(m-1, n-1), minCost(m-1, n), minCost(m, n-1)) + cost[m][n]

  • Overlapping Subproblems
    Following is simple recursive implementation of the MCP (Minimum Cost Path) problem. The implementation simply follows the recursive structure mentioned above.
[pastacode lang=”cpp” manual=”%2F*%20A%20Naive%20recursive%20implementation%20of%20MCP(Minimum%20Cost%20Path)%20problem%20*%2F%0A%23include%3Cstdio.h%3E%0A%23include%3Climits.h%3E%0A%23define%20R%203%0A%23define%20C%203%0A%20%0Aint%20min(int%20x%2C%20int%20y%2C%20int%20z)%3B%0A%20%0A%2F*%20Returns%20cost%20of%20minimum%20cost%20path%20from%20(0%2C0)%20to%20(m%2C%20n)%20in%20mat%5BR%5D%5BC%5D*%2F%0Aint%20minCost(int%20cost%5BR%5D%5BC%5D%2C%20int%20m%2C%20int%20n)%0A%7B%0A%20%20%20if%20(n%20%3C%200%20%7C%7C%20m%20%3C%200)%0A%20%20%20%20%20%20return%20INT_MAX%3B%0A%20%20%20else%20if%20(m%20%3D%3D%200%20%26%26%20n%20%3D%3D%200)%0A%20%20%20%20%20%20return%20cost%5Bm%5D%5Bn%5D%3B%0A%20%20%20else%0A%20%20%20%20%20%20return%20cost%5Bm%5D%5Bn%5D%20%2B%20min(%20minCost(cost%2C%20m-1%2C%20n-1)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20minCost(cost%2C%20m-1%2C%20n)%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20minCost(cost%2C%20m%2C%20n-1)%20)%3B%0A%7D%0A%20%0A%2F*%20A%20utility%20function%20that%20returns%20minimum%20of%203%20integers%20*%2F%0Aint%20min(int%20x%2C%20int%20y%2C%20int%20z)%0A%7B%0A%20%20%20if%20(x%20%3C%20y)%0A%20%20%20%20%20%20return%20(x%20%3C%20z)%3F%20x%20%3A%20z%3B%0A%20%20%20else%0A%20%20%20%20%20%20return%20(y%20%3C%20z)%3F%20y%20%3A%20z%3B%0A%7D%0A%20%0A%2F*%20Driver%20program%20to%20test%20above%20functions%20*%2F%0Aint%20main()%0A%7B%0A%20%20%20int%20cost%5BR%5D%5BC%5D%20%3D%20%7B%20%7B1%2C%202%2C%203%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B4%2C%208%2C%202%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B1%2C%205%2C%203%7D%20%7D%3B%0A%20%20%20printf(%22%20%25d%20%22%2C%20minCost(cost%2C%202%2C%202))%3B%0A%20%20%20return%200%3B%0A%7D” message=”C++” highlight=”” provider=”manual”/]

It should be noted that the above function computes the same subproblems again and again. See the following recursion tree, there are many nodes which appear more than once. Time complexity of this naive recursive solution is exponential and it is terribly slow.

[ad type=”banner”]
mC refers to minCost()
                                    mC(2, 2)
                          /            |           \
                         /             |            \             
                 mC(1, 1)           mC(1, 2)             mC(2, 1)
              /     |     \       /     |     \           /     |     \ 
             /      |      \     /      |      \         /      |       \
       mC(0,0) mC(0,1) mC(1,0) mC(0,1) mC(0,2) mC(1,1) mC(1,0) mC(1,1) mC(2,0) 

So the MCP problem has both properties (see this and this) of a dynamic programming problem. Like other typical Dynamic Programming(DP) problems, recomputations of same subproblems can be avoided by constructing a temporary array tc[][] in bottom up manner.

[pastacode lang=”python” manual=”%23%20Dynamic%20Programming%20Python%20implementation%20of%20Min%20Cost%20Path%0A%23%20problem%0AR%20%3D%203%0AC%20%3D%203%0A%20%0Adef%20minCost(cost%2C%20m%2C%20n)%3A%0A%20%0A%20%20%20%20%23%20Instead%20of%20following%20line%2C%20we%20can%20use%20int%20tc%5Bm%2B1%5D%5Bn%2B1%5D%20or%0A%20%20%20%20%23%20dynamically%20allocate%20memoery%20to%20save%20space.%20The%20following%0A%20%20%20%20%23%20line%20is%20used%20to%20keep%20te%20program%20simple%20and%20make%20it%20working%0A%20%20%20%20%23%20on%20all%20compilers.%0A%20%20%20%20tc%20%3D%20%5B%5B0%20for%20x%20in%20range(C)%5D%20for%20x%20in%20range(R)%5D%0A%20%0A%20%20%20%20tc%5B0%5D%5B0%5D%20%3D%20cost%5B0%5D%5B0%5D%0A%20%0A%20%20%20%20%23%20Initialize%20first%20column%20of%20total%20cost(tc)%20array%0A%20%20%20%20for%20i%20in%20range(1%2C%20m%2B1)%3A%0A%20%20%20%20%20%20%20%20tc%5Bi%5D%5B0%5D%20%3D%20tc%5Bi-1%5D%5B0%5D%20%2B%20cost%5Bi%5D%5B0%5D%0A%20%0A%20%20%20%20%23%20Initialize%20first%20row%20of%20tc%20array%0A%20%20%20%20for%20j%20in%20range(1%2C%20n%2B1)%3A%0A%20%20%20%20%20%20%20%20tc%5B0%5D%5Bj%5D%20%3D%20tc%5B0%5D%5Bj-1%5D%20%2B%20cost%5B0%5D%5Bj%5D%0A%20%0A%20%20%20%20%23%20Construct%20rest%20of%20the%20tc%20array%0A%20%20%20%20for%20i%20in%20range(1%2C%20m%2B1)%3A%0A%20%20%20%20%20%20%20%20for%20j%20in%20range(1%2C%20n%2B1)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20tc%5Bi%5D%5Bj%5D%20%3D%20min(tc%5Bi-1%5D%5Bj-1%5D%2C%20tc%5Bi-1%5D%5Bj%5D%2C%20tc%5Bi%5D%5Bj-1%5D)%20%2B%20cost%5Bi%5D%5Bj%5D%0A%20%0A%20%20%20%20return%20tc%5Bm%5D%5Bn%5D%0A%20%0A%23%20Driver%20program%20to%20test%20above%20functions%0Acost%20%3D%20%5B%5B1%2C%202%2C%203%5D%2C%0A%20%20%20%20%20%20%20%20%5B4%2C%208%2C%202%5D%2C%0A%20%20%20%20%20%20%20%20%5B1%2C%205%2C%203%5D%5D%0Aprint(minCost(cost%2C%202%2C%202))” message=”Python” highlight=”” provider=”manual”/]

Output:

8

Time Complexity of the DP implementation is O(mn) which is much better than Naive Recursive implementation.

[ad type=”banner”]