-
Notifications
You must be signed in to change notification settings - Fork 0
/
min_cost_path.py
32 lines (24 loc) · 1.07 KB
/
min_cost_path.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# https://www.geeksforgeeks.org/dynamic-programming-set-6-min-cost-path/
cost_matrix = [[1, 2, 3],
[4, 8, 2],
[1, 5, 3]]
def find_min_cost_matrix(cost_matrix):
row_size = len(cost_matrix)
col_size = len(cost_matrix[0])
min_cost_matrix = [[0 for x in range(col_size)] for x in range(row_size)]
min_cost_matrix[0][0] = cost_matrix[0][0]
for col in range(1, row_size):
min_cost_matrix[0][col] = (min_cost_matrix[0][col - 1] +
cost_matrix[0][col])
for row in range(1, col_size):
min_cost_matrix[row][0] = (min_cost_matrix[row - 1][0] +
cost_matrix[row][0])
for row in range(1, col_size):
for col in range(1, row_size):
min_cost_matrix[row][col] = (min(
min_cost_matrix[row - 1][col],
min_cost_matrix[row][col - 1],
min_cost_matrix[row - 1][col - 1]) +
cost_matrix[row][col])
print(min_cost_matrix)
find_min_cost_matrix(cost_matrix)