Merge pull request #211 from KPatr1ck/dynamic_programming

dynamic programming applications in python
This commit is contained in:
wangzheng0822 2019-01-07 10:43:29 +08:00 committed by GitHub
commit 1bdf5c2870
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 130 additions and 0 deletions

View File

@ -0,0 +1,66 @@
#!/usr/bin/python
# -*- coding: UTF-8 -*-
from typing import List, Tuple
def bag(items_info: List[int], capacity: int) -> int:
"""
固定容量的背包计算能装进背包的物品组合的最大重量
:param items_info: 每个物品的重量
:param capacity: 背包容量
:return: 最大装载重量
"""
n = len(items_info)
memo = [[-1]*(capacity+1) for i in range(n)]
memo[0][0] = 1
if items_info[0] <= capacity:
memo[0][items_info[0]] = 1
for i in range(1, n):
for cur_weight in range(capacity+1):
if memo[i-1][cur_weight] != -1:
memo[i][cur_weight] = memo[i-1][cur_weight] # 不选
if cur_weight + items_info[i] <= capacity: # 选
memo[i][cur_weight + items_info[i]] = 1
for w in range(capacity, -1, -1):
if memo[-1][w] != -1:
return w
def bag_with_max_value(items_info: List[Tuple[int, int]], capacity: int) -> int:
"""
固定容量的背包计算能装进背包的物品组合的最大价值
:param items_info: 物品的重量和价值
:param capacity: 背包容量
:return: 最大装载价值
"""
n = len(items_info)
memo = [[-1]*(capacity+1) for i in range(n)]
memo[0][0] = 0
if items_info[0][0] <= capacity:
memo[0][items_info[0][0]] = items_info[0][1]
for i in range(1, n):
for cur_weight in range(capacity+1):
if memo[i-1][cur_weight] != -1:
memo[i][cur_weight] = memo[i-1][cur_weight]
if cur_weight + items_info[i][0] <= capacity:
memo[i][cur_weight + items_info[i][0]] = max(memo[i][cur_weight + items_info[i][0]],
memo[i-1][cur_weight] + items_info[i][1])
return max(memo[-1])
if __name__ == '__main__':
# [weight, ...]
items_info = [2, 2, 4, 6, 3]
capacity = 9
print(bag(items_info, capacity))
# [(weight, value), ...]
items_info = [(3, 5), (2, 2), (1, 4), (1, 2), (4, 10)]
capacity = 8
print(bag_with_max_value(items_info, capacity))

View File

@ -0,0 +1,64 @@
#!/usr/bin/python
# -*- coding: UTF-8 -*-
from typing import List
Layer_nums = List[int]
def yh_triangle(nums: List[Layer_nums]) -> int:
"""
从根节点开始向下走过程中经过的节点只需存储经过它时最小的路径和
:param nums:
:return:
"""
assert len(nums) > 0
n = len(nums) # 层数
memo = [[0]*n for i in range(n)]
memo[0][0] = nums[0][0]
for i in range(1, n):
for j in range(i+1):
# 每一层首尾两个数字,只有一条路径可以到达
if j == 0:
memo[i][j] = memo[i-1][j] + nums[i][j]
elif j == i:
memo[i][j] = memo[i-1][j-1] + nums[i][j]
else:
memo[i][j] = min(memo[i-1][j-1] + nums[i][j], memo[i-1][j] + nums[i][j])
return min(memo[n-1])
def yh_triangle_space_optimization(nums: List[Layer_nums]) -> int:
assert len(nums) > 0
n = len(nums)
memo = [0] * n
memo[0] = nums[0][0]
for i in range(1, n):
for j in range(i, -1, -1):
if j == i:
memo[j] = memo[j-1] + nums[i][j]
elif j == 0:
memo[j] = memo[j] + nums[i][j]
else:
memo[j] = min(memo[j-1] + nums[i][j], memo[j] + nums[i][j])
return min(memo)
def yh_triangle_bottom_up(nums: List[Layer_nums]) -> int:
assert len(nums) > 0
n = len(nums)
memo = nums[-1].copy()
for i in range(n-1, 0, -1):
for j in range(i):
memo[j] = min(memo[j] + nums[i-1][j], memo[j+1] + nums[i-1][j])
return memo[0]
if __name__ == '__main__':
nums = [[3], [2, 6], [5, 4, 2], [6, 0, 3, 2]]
print(yh_triangle(nums))
print(yh_triangle_space_optimization(nums))
print(yh_triangle_bottom_up(nums))