## Min cost Path in 2-D array

Dynamic programming can be applied in minimum cost path problems. In this article, we are going to discuss two common minimum cost path problems.

Problem 1:

Given a matrix of size MxN where every cell has a cost associated with it. We need to find the minimum cost to reach the last cell (m,n) from (0,0). You can move only one unit down or one unit right from any cell i.e. from a cell (i,j) you can move to (i+1,j) or (i,j+1).

Example:

 1 2 3 4 8 2 1 5 3

Answer: The path with minimum cost is (0,0) -> (0,1) -> (0,2) -> (1,1) -> (1,2). The cost of this path is 1+2+3+2+3 = 11.

We are going to use a recursion for solving this problem. This problem has optimal substructure. Thus, we can break this problem into smaller and simple subproblems. We need to find the min(path going down, the path going right). We can recursively define this problem as:

Cost to reach cell (m,n) = cost[m][n] + min(cost to reach(m,n-1) , cost to reach(m-1,n)

You can check the code below.

O/P:
The minimum cost path is 11

The time complexity of this solution is going to be (2n) or exponential. This solution is going to compute the same subproblems multiple times. You can see in the recursion tree that most nodes are repeating. Thus, this solution is actually very slow. Recursion tree for minCostPath(2):

(2,2)                                          /      \                                             (1,2)     (2,1)                               /     \     /     \                                (0,2)  (1,1) (1,1)  (2,0)

This problem has both optimal substructure and overlapping subproblems. Thus, we can use the DP approach for solving this problem. We can store the result for subproblem. This will ensure that subproblems won’t be recomputed.

At every cell, we can either go down or right. We will always move towards the minimum of these two cells. For any i,j cell:

dp[i][j] = dp[i][j] + min(dp[i-1][j],dp[i][j-1]) for all i>0&&j>0

You can check the code below.

O/P:
The minimum cost path is 11

O/P: The minimum cost path is 11

The time complexity and space complexity of this solution is O(nxm).

Problem 2: Given a matrix of size MxN where every cell has a cost associated with it. We need to find the minimum cost to reach the last cell (m,n) from (0,0). You can move only one unit down, one unit right, and to diagonally lower cells from any cell i.e. from a cell (i,j) you can move to (i+1,j),(i,j+1), and (i+1,j+1).

Example:

 1 9 4 4 7 5 2 9 1

Answer: The path with minimum cost is (0,0) -> (1,1) -> (2,2). The cost of this path is 1+7+1 = 9.

This problem is very similar to the problem that we have just discussed. However, we can move to three different cells this time. We can reach (m,n) only through these cells: (m,n-1) or (m-1,n) or (m-1,n-1). We need to find the minimum of these 3 cells and add them to the cost of (m,n) cell. Thus, the recurrence relation is:

Cost to reach cell (m,n) = cost[m][n] + min(cost to reach(m,n-1) , cost to reach(m-1,n),cost to reach(m-1,n-1))

You can check the code below.

O/P: The minimum cost path is 9

This problem also has both optimal substructure and overlapping subproblems. Thus, we can use the DP approach for solving this problem. We will use a dp[][] array for storing subproblem results. You can check the code below.

O/P
: The minimum cost path is 9

The time and space complexity of this solution is also O(n*m).

Note:

You can also use Dijkstra’s shortest path algorithm for solving this problem.

Practice Problems:

Happy Coding 😊

By Programmers Army
{"ft_lang_label":"__label__en","ft_lang_prob":0.89783907,"math_prob":0.98259485,"size":3238,"snap":"2022-05-2022-21","text_gpt3_token_len":941,"char_repetition_ratio":0.14347558,"word_repetition_ratio":0.2857143,"special_character_ratio":0.30759728,"punctuation_ratio":0.14785992,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99950206,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-05-23T11:52:19Z\",\"WARC-Record-ID\":\"<urn:uuid:16f3bfb8-7732-4d65-9eab-595da29b530c>\",\"Content-Length\":\"19472\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:0959ce08-f7e2-4f96-b44f-8900bdda863d>\",\"WARC-Concurrent-To\":\"<urn:uuid:77dd4778-f134-4b39-b6a0-1466bdb0e41e>\",\"WARC-IP-Address\":\"185.199.111.153\",\"WARC-Target-URI\":\"https://programmersarmy.com/DP/Min-Cost.html\",\"WARC-Payload-Digest\":\"sha1:6MCP7ZMCMAV44GRIZEAYTHS7I3BWGU7D\",\"WARC-Block-Digest\":\"sha1:GG637SSJPTIRUZPZC2YWTFXNSHSECHRR\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-21/CC-MAIN-2022-21_segments_1652662558015.52_warc_CC-MAIN-20220523101705-20220523131705-00000.warc.gz\"}"}