DP6 minimum Path Cost Min Cost Path @ geeksforgeeks

Source: Internet
Author: User

 

Dynamic Programming | Set 6 (Min Cost Path)

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 )?

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 ).

1) 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]

2) Overlapping Subproblems
Following is simple recursive implementation of the MCP (Minimum Cost Path) problem. The implementation simply follows the recursive structure mentioned above.


 

 

 

Package DP; public class MinCostPath {static int R = 3; static int C = 3; public static void main (String [] args) throws Exception {int [] [] cost = {1, 2, 3}, {4, 8, 2}, {1, 5, 3 }}; int m = 2, n = 2; System. out. println (minCostRec (cost, m, n); System. out. println (minCostDP (cost, m, n);}/* A Naive recursive implementation of MCP (Minimum Cost Path) problem * // * Returns cost of minimum cost path from (0, 0) to (m, n) in mat [R] [C] */public static int minCostRec (int [] [] cost, int m, int n) throws Exception {if (m <0 | n <0) {// note that INT_MAX maximum return Integer is returned here. MAX_VALUE;} else if (m = 0 & n = 0) {return cost [0] [0];} else {return min3 (minCostRec (cost m-1, n-1), minCostRec (cost, m, n-1), minCostRec (cost, M-1, n) + cost [m] [n];} public static int minCostDP (int [] [] cost, int m, int n) {// Instead of following line, we can use int dp [m + 1] [n + 1] or // dynamically allocate memoery to save space. the following line is // used to keep the program simple and make it working on all compilers.int [] [] dp = new int [R] [C]; dp [0] [0] = cost [0] [0];/* Initialize first column of total cost (tc) array */for (int I = 1; I <= m; I ++) {dp [I] [0] = dp [I-1] [0] + cost [I] [0];} /* Initialize first row of tc array */for (int j = 1; j <= n; j ++) {dp [0] [j] = dp [0] [J-1] + cost [0] [j];} /* Construct rest of the tc array */for (int I = 1; I <= m; I ++) {for (int j = 1; j <= n; j ++) {dp [I] [j] = min3 (dp [I-1] [J-1], dp [I-1] [j], dp [I] [J-1]) + cost [I] [j] ;}} return dp [m] [n];} public static int min3 (int x, int y, int z) {return Math. min (Math. min (x, y), z );}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.