Python 初学笔记:递归解决0-1背包问题
来源:互联网 发布:软件项目需求说明书 编辑:程序博客网 时间:2024/04/29 16:59
最近看了Magnus Lie Hetland的《Python基础教程(第2版)》158页利用生成器和递归解决八皇后问题的内容,于是想用这种方法解决0/1背包问题,所以仿照基础教程写了一段代码,基本上能够解决问题,但是明显还有不足之处,先记录下,以后慢慢修改。。。
def conflict(m, w, state, pos): total = 0 for i in state: total += w[i] if total <= m and total + w[pos] > m: return True else: return Falsedef knapsacks(m, w, state=()): n = len(w) for i in range(n): if i not in state: if not conflict(m, w, state, i): for result in knapsacks(m, w, state + (i,)): yield (i,) + result else: yield ()def max_knapsack(m = 10, w = (2,2,6,5,4), p = (6,3,5,4,6)): knss = list(knapsacks(m, w)) ps = [] for kns in knss: total = 0 for k in kns: total += p[k] ps.append(total) return max(ps)def main(): m = raw_input("Input weight constraint of knapsacks: ") n = raw_input("Input the number of goods: ") w = raw_input("Input each weight of the %s goods (e.g., 2,3,5,...): " % n) p = raw_input("Input each value of the %s goods (e.g., 2,3,5,...): " % n) m = float(m) n = int(n) ws = w.split(',') ps = p.split(',') w = [] p = [] for i in range(n): w.append(float(ws[i])) p.append(float(ps[i])) print "Max value: ", max_knapsack(m, w, p)main()
从结果上明显可看出knapsacks和conflict方法还有一些不足之处。
knapsacks方法返回的可能所有满足总重量小于m的组合:
由于递归方法写得不好,导致出现多个重复的元组。
0 0
- Python 初学笔记:递归解决0-1背包问题
- Python初学笔记:递归解决汉诺塔问题
- 0-1背包问题(递归解决)
- 0/1背包问题(递归解决,递推解决)
- 数据结构之非递归解决0-1背包问题
- Python 动态规划 解决0-1背包问题
- 解决python初学问题TemplatesDoesNotExist
- python学习笔记2--初学递归函数
- 初学0/1背包
- 算法讨论(八)递归法解决0-1背包问题
- 递归求解0 1背包问题
- 0-1背包问题(递归实现)
- 递归实现0,1背包问题
- 备忘录和递归解决背包问题
- 0-1背包问题递归和非递归实现
- 0-1背包问题递归与非递归实现
- 0/1背包问题[python]
- 0-1背包问题 python
- Hive 配置安装
- win7 LoadRunner11破解“license security violation.Operation is not allowed”问题总结
- 员工离职的真正原因(送给成长中的各位)
- 约瑟夫问题
- 求帧差
- Python 初学笔记:递归解决0-1背包问题
- 《Java程序员面试笔试宝典》之Java变量命名有哪些规则
- linux下安装JDK,tomcat
- db2 创建数据库、表、存储过程等
- NFS v3 介绍
- CCSpriteBatchNode与CCTextureAtlas详解
- /lib/libc.so.6: version `GLIBC_2.7' not found
- shall编程
- PHP识别url重写请求