forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
min_cost_path.py
58 lines (41 loc) · 1.44 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""
author @goswami-rahul
To find minimum cost path
from station 0 to station N-1,
where cost of moving from ith station to jth station is given as:
Matrix of size (N x N)
where Matrix[i][j] denotes the cost of moving from
station i --> station j for i < j
NOTE that values where Matrix[i][j] and i > j does not
mean anything, and hence represented by -1 or INF
For the input below (cost matrix),
Minimum cost is obtained as from { 0 --> 1 --> 3}
= cost[0][1] + cost[1][3] = 65
the Output will be:
The Minimum cost to reach station 4 is 65
Time Complexity: O(n^2)
Space Complexity: O(n)
"""
INF = float("inf")
def min_cost(cost):
"""Find minimum cost.
Keyword arguments:
cost -- matrix containing costs
"""
length = len(cost)
# dist[i] stores minimum cost from 0 --> i.
dist = [INF] * length
dist[0] = 0 # cost from 0 --> 0 is zero.
for i in range(length):
for j in range(i+1,length):
dist[j] = min(dist[j], dist[i] + cost[i][j])
return dist[length-1]
if __name__ == '__main__':
costs = [ [ 0, 15, 80, 90], # cost[i][j] is the cost of
[-1, 0, 40, 50], # going from i --> j
[-1, -1, 0, 70],
[-1, -1, -1, 0] ] # cost[i][j] = -1 for i > j
TOTAL_LEN = len(costs)
mcost = min_cost(costs)
assert mcost == 65
print(f"The minimum cost to reach station {TOTAL_LEN} is {mcost}")