Skip to content

Commit 1bdf5c2

Browse files
Merge pull request wangzheng0822#211 from KPatr1ck/dynamic_programming
dynamic programming applications in python
2 parents b9e635e + dfc4d96 commit 1bdf5c2

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/python
2+
# -*- coding: UTF-8 -*-
3+
4+
from typing import List, Tuple
5+
6+
7+
def bag(items_info: List[int], capacity: int) -> int:
8+
"""
9+
固定容量的背包,计算能装进背包的物品组合的最大重量
10+
11+
:param items_info: 每个物品的重量
12+
:param capacity: 背包容量
13+
:return: 最大装载重量
14+
"""
15+
n = len(items_info)
16+
memo = [[-1]*(capacity+1) for i in range(n)]
17+
memo[0][0] = 1
18+
if items_info[0] <= capacity:
19+
memo[0][items_info[0]] = 1
20+
21+
for i in range(1, n):
22+
for cur_weight in range(capacity+1):
23+
if memo[i-1][cur_weight] != -1:
24+
memo[i][cur_weight] = memo[i-1][cur_weight] # 不选
25+
if cur_weight + items_info[i] <= capacity: # 选
26+
memo[i][cur_weight + items_info[i]] = 1
27+
28+
for w in range(capacity, -1, -1):
29+
if memo[-1][w] != -1:
30+
return w
31+
32+
33+
def bag_with_max_value(items_info: List[Tuple[int, int]], capacity: int) -> int:
34+
"""
35+
固定容量的背包,计算能装进背包的物品组合的最大价值
36+
37+
:param items_info: 物品的重量和价值
38+
:param capacity: 背包容量
39+
:return: 最大装载价值
40+
"""
41+
n = len(items_info)
42+
memo = [[-1]*(capacity+1) for i in range(n)]
43+
memo[0][0] = 0
44+
if items_info[0][0] <= capacity:
45+
memo[0][items_info[0][0]] = items_info[0][1]
46+
47+
for i in range(1, n):
48+
for cur_weight in range(capacity+1):
49+
if memo[i-1][cur_weight] != -1:
50+
memo[i][cur_weight] = memo[i-1][cur_weight]
51+
if cur_weight + items_info[i][0] <= capacity:
52+
memo[i][cur_weight + items_info[i][0]] = max(memo[i][cur_weight + items_info[i][0]],
53+
memo[i-1][cur_weight] + items_info[i][1])
54+
return max(memo[-1])
55+
56+
57+
if __name__ == '__main__':
58+
# [weight, ...]
59+
items_info = [2, 2, 4, 6, 3]
60+
capacity = 9
61+
print(bag(items_info, capacity))
62+
63+
# [(weight, value), ...]
64+
items_info = [(3, 5), (2, 2), (1, 4), (1, 2), (4, 10)]
65+
capacity = 8
66+
print(bag_with_max_value(items_info, capacity))
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/python
2+
# -*- coding: UTF-8 -*-
3+
4+
from typing import List
5+
6+
Layer_nums = List[int]
7+
8+
9+
def yh_triangle(nums: List[Layer_nums]) -> int:
10+
"""
11+
从根节点开始向下走,过程中经过的节点,只需存储经过它时最小的路径和
12+
:param nums:
13+
:return:
14+
"""
15+
assert len(nums) > 0
16+
n = len(nums) # 层数
17+
memo = [[0]*n for i in range(n)]
18+
memo[0][0] = nums[0][0]
19+
20+
for i in range(1, n):
21+
for j in range(i+1):
22+
# 每一层首尾两个数字,只有一条路径可以到达
23+
if j == 0:
24+
memo[i][j] = memo[i-1][j] + nums[i][j]
25+
elif j == i:
26+
memo[i][j] = memo[i-1][j-1] + nums[i][j]
27+
else:
28+
memo[i][j] = min(memo[i-1][j-1] + nums[i][j], memo[i-1][j] + nums[i][j])
29+
return min(memo[n-1])
30+
31+
32+
def yh_triangle_space_optimization(nums: List[Layer_nums]) -> int:
33+
assert len(nums) > 0
34+
n = len(nums)
35+
memo = [0] * n
36+
memo[0] = nums[0][0]
37+
38+
for i in range(1, n):
39+
for j in range(i, -1, -1):
40+
if j == i:
41+
memo[j] = memo[j-1] + nums[i][j]
42+
elif j == 0:
43+
memo[j] = memo[j] + nums[i][j]
44+
else:
45+
memo[j] = min(memo[j-1] + nums[i][j], memo[j] + nums[i][j])
46+
return min(memo)
47+
48+
49+
def yh_triangle_bottom_up(nums: List[Layer_nums]) -> int:
50+
assert len(nums) > 0
51+
n = len(nums)
52+
memo = nums[-1].copy()
53+
54+
for i in range(n-1, 0, -1):
55+
for j in range(i):
56+
memo[j] = min(memo[j] + nums[i-1][j], memo[j+1] + nums[i-1][j])
57+
return memo[0]
58+
59+
60+
if __name__ == '__main__':
61+
nums = [[3], [2, 6], [5, 4, 2], [6, 0, 3, 2]]
62+
print(yh_triangle(nums))
63+
print(yh_triangle_space_optimization(nums))
64+
print(yh_triangle_bottom_up(nums))

0 commit comments

Comments
 (0)