Subset Sums and Knapsack

该博客探讨了一种动态规划算法用于解决物品选择问题,目标是在不超过给定重量限制的情况下最大化物品价值。算法以递归方式构建,通过比较包含和不包含当前物品的最优解来更新子集的总价值。它能在O(nW)时间内正确计算最优解,并在O(n)时间内找到最优子集。
摘要由CSDN通过智能技术生成

The Problem

问题属于dynamics programming大类。
Given n n n items { 1 , 2 , . . . , n } \{1,2,...,n\} {1,2,...,n}, and each has a given nonnegative weight w i w_i wi and value v i v_i vi. We are also given a bound W W W.
Goal: select a subset S S S of items, which maximizes ∑ i ∈ S v i \sum_{i\in S}v_i iSvi subject to ∑ i ∈ S w i ≤ W \sum_{i\in S}w_i \leq W iSwiW.

Designing the Algorithm

对于动态规划问题,一般从最后一种情况入手。
在添加最后一个element的时候,不光需要知道 OPT(n-1),也需要知道 W − w n W-w_n Wwn,即添加最后一个element时的bound是多少。

Assume that W W W is an integer, and all requests i = 1 , . . . , n i = 1, . . . , n i=1,...,n have integer weights w i w_i wi. We will have a subproblem for each$ i=0,1,…,n$ and each integer 0 ≤ w ≤ W 0≤w≤W 0wW. We will use O P T ( i , w ) OPT(i,w) OPT(i,w) to denote the value of the optimal solution using a subset of the items { 1 , . . . , i } \{1, . . . , i\} {1,...,i} with maximum allowed weight w w w, that is,
O P T ( i , w ) = max ⁡ S ∑ j ∈ S v j OPT(i,w)=\max_S \sum_{j \in S}v_j OPT(i,w)=SmaxjSvj
where S ⊆ { 1 , . . . , i } S\subseteq \{1,...,i\} S{1,...,i}

定理(6.8):if w < w i w<w_i w<wi, then O P T ( i , w ) = O P T ( i − 1 , w ) OPT(i,w)=OPT(i-1,w) OPT(i,w)=OPT(i1,w). Otherwise O P T ( i , w ) = max ⁡ ( O P T ( i − 1 , w ) , v i + O P T ( i − 1 , w − w i ) ) OPT(i,w)=\max (OPT(i-1,w), v_i + OPT(i-1,w-w_i)) OPT(i,w)=max(OPT(i1,w),vi+OPT(i1,wwi))
在这里插入图片描述

Analyzing the Algorithm

定理(6.9):The Subset-Sum( n , W n , W n,W) Algorithm correctly computes the optimal value of the problem, and runs in O ( n W ) O(nW) O(nW) time.

定理(6.10): Given a table M M M of the optimal values of the subproblems, the optimal set S S S can be found in O ( n ) O(n) O(n) time.

Reference

Algorithm Design (Jon Kleinberg)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值