Skip to content

Commit bb8d715

Browse files
authored
Minimum Cost Path
Initial Path
1 parent 93460d4 commit bb8d715

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

Minimum Cost Path

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Recursion
2+
3+
def min_cost(cost,m,n):
4+
if (n<0 or m<0):
5+
return sys.maxsize
6+
if (m==0 and n==0):
7+
return cost[m][n]
8+
else:
9+
return cost[m][n]+min(min_cost(cost,m-1,n-1),
10+
min_cost(cost,m-1,n),
11+
min_cost(cost,m,n-1))
12+
13+
14+
15+
#Dynamic Programming
16+
17+
def min_cost_dp(cost_mat,m,n):
18+
dp=[[0 for i in range(m+1)]
19+
for j in range(n+1)]
20+
21+
dp[0][0]=cost_mat[0][0]
22+
23+
for i in range(1,m+1):
24+
dp[i][0]=dp[i-1][0]+cost_mat[i][0]
25+
for j in range(1,n+1):
26+
dp[0][j]=dp[0][j-1]+cost_mat[0][j]
27+
28+
for i in range(1,m+1):
29+
for j in range(1,n+1):
30+
dp[i][j]=cost_mat[i][j]+min(dp[i-1][j-1],
31+
dp[i-1][j],
32+
dp[i][j-1])
33+
return dp[m][n]

0 commit comments

Comments
 (0)