The Minimum Cost Path problem focuses on finding the lowest cost path from the top-left corner to the bottom-right corner of a grid. Each cell in the grid has an associated cost, and you can only move down, right, or diagonally (down-right) at each step. This problem is commonly encountered in various fields ranging from robotics to network routing.
Given a m x n
grid filled with non-negative integers, where each cell's value represents its cost, the goal is to find the minimum cost path from the top-left corner (0,0)
to the bottom-right corner (m-1,n-1)
.
Example:
Consider the following grid:
1 3 1
1 5 1
4 2 1
The minimum cost path from (0,0)
to (2,2)
is:
(0,0)
with cost 1
(0,1)
with additional cost 3
(1,1)
with an additional cost of 5
(2,2)
with an additional cost of 1
The total cost would be 1 + 3 + 5 + 1 = 10
.
Dynamic programming (DP) is an effective way to solve the Minimum Cost Path problem by breaking it down into smaller subproblems and reusing their results.
Define the DP Table:
We'll create a two-dimensional array dp
where dp[i][j]
will store the minimum cost to reach cell (i,j)
.
Base Case:
Set dp[0][0]
equal to the grid's starting position cost, grid[0][0]
.
State Transition: For each cell in the grid, calculate the minimum cost by taking the minimum of the costs from the top cell, left cell, and diagonal cell: [ dp[i][j] = grid[i][j] + \min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) ]
Result:
The value in the bottom-right cell dp[m-1][n-1]
will be the minimum cost to get to that position.
Here’s a simple Python implementation:
def min_cost_path(grid): if not grid or not grid[0]: return 0 m, n = len(grid), len(grid[0]) dp = [[0] * n for _ in range(m)] dp[0][0] = grid[0][0] # Initialize the first row for j in range(1, n): dp[0][j] = dp[0][j - 1] + grid[0][j] # Initialize the first column for i in range(1, m): dp[i][0] = dp[i - 1][0] + grid[i][0] # Fill up the dp table for i in range(1, m): for j in range(1, n): dp[i][j] = grid[i][j] + min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]) return dp[-1][-1]
Time Complexity: The time complexity of this approach is O(m*n)
, where m
is the number of rows and n
is the number of columns in the grid, as we iterate through every cell in the matrix once.
Space Complexity: The space complexity is also O(m*n)
due to the additional dp
table used to store the minimum costs. This can be optimized to O(n)
by only keeping track of the current and previous row.
While the above solution works for the basic problem, there are variations that can make it more complex:
Grasping the Minimum Cost Path problem is essential for excelling in dynamic programming and algorithm interviews. By applying the step-by-step method illustrated above, you can tackle this problem and its variations with confidence. Remember to practice, as familiarity with different types of grid-based problems will significantly enhance your problem-solving skills in dynamic programming.
15/11/2024 | DSA
15/11/2024 | DSA
13/10/2024 | DSA
16/11/2024 | DSA
23/09/2024 | DSA
23/09/2024 | DSA
23/09/2024 | DSA
08/12/2024 | DSA
15/11/2024 | DSA
08/12/2024 | DSA
15/11/2024 | DSA
23/09/2024 | DSA