
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
C Program for Minimum Cost Path
Here, we will solve the minimum cost path problem in C. The implication is done on a 2D-matrix where each cell has a cost to travel. We have to find a path from the left top corner to the bottom right corner with minimum travel cost. You can only traverse down and right lower cells from a given cell.
To solve this particular problem dynamic programming is much better to approach than recursion.
Given cost matrix cost[ ][ ] and a position (m,n), we have to write a function that returns cost of minimum path to reach (m,n) from (0,0).The total cost of a path to reach (m, n) is the sum of all the costs on that path (including both source and destination).
Assumption − All costs are positive. Negative cost cycles do not exist in the input matrix
Example
Find the minimum cost path to (2,2)
The costs are given within the image itself. The path will be (0, 0) ⇒ (0, 1) ⇒ (1, 2) ⇒ (2, 2). The value of the path is eight (1 +2+2+ 3).
Approach − Create an answer matrix of a similar size as the given matrix.
Fill this matrix in a bottom-up manner.
Given − arrA[ ][ ]. At each cell, we’ve got 2 options(go right or down) and that we can select the minimum of those 2, for any i,j cell.
solution[i][j]=A[0][j] if i=0, 1st row =A[i][0] if j=0, 1st column =A[i][j]+Min(solution[i=1],[j],solution[i][j-1]) if i>0 && j>0
The approach followed in the algorithmic answers can be used to efficiently solve this problem by applying dynamic programming. Create a minimum cost path table of size m,n and define−
minimumCostPath[i][j] = minimum value to achieve (i, j) from (0, 0)
Clearly,
minimumCostPath[0][0] = costMatrix[0][0] minimumCostPath[i][0] = minimumCostPath[i - 1][0] + costMatrix[i][0], for all values of i > zero minimumCostPath[0][j] = minimumCostPath[0][j - 1] + costMatrix[0][j], for all values of j >zero
Next, we will fill the minimum cost path matrix by applying a similar formula that we applied in the algorithm. Since all the previous values will already have been calculated within the minimum cost path matrix, we will not recalculate these as we did in the algorithmic answer.
minimumCostPath[i][j] = costMatrix[i][j] +minimum(minimumCostPath[i - 1][j - 1],minimumCostPath[i - 1][j],minimumCostPath[i][j - 1])
Here, for calculating minimumCostPath[i][j] we tend to use minimumCostPath[i - 1][j - 1], minimumCostPath[i - 1][j] and minimumCostPath[i][j - 1] as a result, these are the sole permissible cells from which we will reach minimumCostPath[i][j].Finally, we return minimumCostPath[m][n].
The time complexity of the dynamic programming algorithm is O(mn).
Example
#include <iostream> using namespace std; int min_(int a, int b, int c){ if (a < b) return (a < c) ? a : c; else return (b < c) ? b : c; } int min_cost(int cost[4][4], int m, int n){ int i, j; int tot_cost[4][4]; tot_cost[0][0] = cost[0][0]; for (i = 1; i <= m; i++) tot_cost[i][0] = tot_cost[i - 1][0] + cost[i][0]; for (j = 1; j <= n; j++) tot_cost[0][j] = tot_cost[0][j - 1] + cost[0][j]; for (i = 1; i <= m; i++) for (j = 1; j <= n; j++) tot_cost[i][j] = min_(tot_cost[i - 1][j - 1], tot_cost[i - 1][j], tot_cost[i][j - 1]) + cost[i][j]; return tot_cost[m][n]; } int main(){ int cost[4][4] = { { 9, 9, 4 }, { 8, 0, 9 }, {1, 2, 8} }; cout<<" The minimum cost is "<<min_cost(cost, 2, 2); return 0; }
Output
The minimum cost is 17
- Related Articles
- Python Program for Min Cost Path
- Minimum Cost For Tickets in C++
- Minimum Cost Path with Left, Right, Bottom and Up moves allowed in C++
- Program to find minimum cost for painting houses in Python
- C++ program to find out the shortest cost path in a given graph for q queries
- Minimum Path Sum in C++
- Program to find minimum total cost for equalizing list elements in Python
- Min Cost Path
- C++ Program for Dijkstra’s shortest path algorithm?
- Minimum Falling Path Sum in C++
- Program to find out the minimum cost path between the lowest value vertex to the highest value vertex (Python)
- Program to find minimum cost to connect each Cartesian coordinates in C++
- Minimum Falling Path Sum II in C++
- Minimum Cost to Connect Sticks in C++
- Minimum Cost to Merge Stones in C++
