当前位置:网站首页>【学习笔记】子集和问题
【学习笔记】子集和问题
2022-06-29 02:00:00 【OneInDark】
零、问题引入
子集和问题 Subset-sum Problem (SSP) \text{Subset-sum Problem (SSP)} Subset-sum Problem (SSP),其意在求解关于 ξ ( S ) = { ∑ i ∈ T w i ∣ T ⊆ S } \xi(S)=\{\sum_{i\in T}w_i\;|\;T\subseteq S\} ξ(S)={ ∑i∈Twi∣T⊆S} 的信息。
如果求解的是 ξ ( S ) \xi(S) ξ(S) 某个值附近的信息,根据背包过程,我们可以总在该值附近浮动。这被称为 balancing \textit{balancing} balancing 。
利用它,我们可以 O ( n W ) \mathcal O(nW) O(nW) 求解某个值的最近邻信息,比如 max { i ∣ i ∈ ξ ( S ) , i ⩽ C } \max\{i\;|\;i\in\xi(S),\;i\leqslant C\} max{ i∣i∈ξ(S),i⩽C},其中 W = max { ∣ w i ∣ } W=\max\{|w_i|\} W=max{ ∣wi∣} 。
壹、平衡
Theorem. 可以在 O ( n ) \mathcal O(n) O(n) 时间内得到 C ∈ [ 0 , W ) C\in[0,W) C∈[0,W) 的问题。
Proof. 若 C < 0 C<0 C<0 则所有数取反(我们仍求出了 C C C 的最近邻信息);不妨设 C ⩾ 0 C\geqslant 0 C⩾0 。在 w i > 0 w_i>0 wi>0 上,贪心取极大子集 S S S 满足 ∑ i ∈ S w i ⩽ C \sum_{i\in S}w_i\leqslant C ∑i∈Swi⩽C,将 S S S 内元素取反、将 C C C 变为 C − ∑ i ∈ S w i C-\sum_{i\in S}w_i C−∑i∈Swi 。根据流程,若有 i ∉ S i\notin S i∈/S 满足 w i > 0 w_i>0 wi>0 则 C < w i ⩽ W C<w_i\leqslant W C<wi⩽W,否则 ∑ i ∈ S w i \sum_{i\in S}w_i ∑i∈Swi 就是 C C C 的最近邻。 ■ \blacksquare ■
下文可能将 x x x 视作解向量。
Definition 1. 平衡填充( balanced filling \textit{balanced filling} balanced filling) x x x 是从 x j = 0 ( j = 1 , … , n ) x_j=0\;(j=1,\dots,n) xj=0(j=1,…,n) 开始,进行若干 平衡调整 得到的,具体而言:
- x j = 0 ( j = 1 , … , n ) x_j=0\;(j=1,\dots,n) xj=0(j=1,…,n) 是平衡填充。
- 平衡加. 对于平衡填充 x x x 满足 w ⋅ x ⩽ C w\cdot x\leqslant C w⋅x⩽C,取 x t = 0 ( w t ⩾ 0 ) x_t=0\;(w_t\geqslant 0) xt=0(wt⩾0),令 x t = 1 x_t=1 xt=1 得到的 x ′ x' x′ 是平衡填充。
- 平衡减. 对于平衡填充 x x x 满足 w ⋅ x > C w\cdot x>C w⋅x>C,取 x s = 0 ( w s < 0 ) x_s=0\;(w_s<0) xs=0(ws<0),令 x s = 1 x_s=1 xs=1 得到的 x ′ x' x′ 是平衡填充。
Proposition. 对于 C C C 的(任意一侧)最近邻,存在解 x x x 是平衡填充。
Proof. 显然。以负方向最近邻为例。从 x j = 0 ( j = 1 , … , n ) x_j=0\;(j=1,\dots,n) xj=0(j=1,…,n) 开始,不断平衡加,总重超过 C C C 就平衡减。 ■ \blacksquare ■
Corollary. 平衡填充总满足 C − W < w ⋅ x ⩽ C + W C-W<w\cdot x\leqslant C+W C−W<w⋅x⩽C+W 。
贰、平衡算法
设可重集 U = { w i } \Bbb U=\{w_i\} U={ wi},记 A = [ − W , 0 ] ∩ U , B = U ∖ A A=[-W,0]\cap\Bbb U,\;B=\Bbb U\setminus A A=[−W,0]∩U,B=U∖A 。设 A = { a 1 , … , a ∣ A ∣ } , B = { b 1 , … , b ∣ B ∣ } A=\{a_1,\dots,a_{|A|}\},\;B=\{b_1,\dots,b_{|B|}\} A={ a1,…,a∣A∣},B={ b1,…,b∣B∣} 。
Definition 2. 对于 s ∈ [ 0 , ∣ A ∣ ] , t ∈ [ 0 , ∣ B ∣ ] s\in[0,|A|],\;t\in[0,|B|] s∈[0,∣A∣],t∈[0,∣B∣] 和 μ ∈ ( C − W , C + W ] \mu\in(C-W,\;C+W] μ∈(C−W,C+W],定义
f s , t ( μ ) = [ ∃ S , ∑ i ∈ S w i = μ ] s.t. S ⊆ { a 1 , … , a s } ∪ { b 1 , … , b s } S forms a balanced filling f_{s,t}(\mu)=\left[\exist S,\;\sum_{i\in S}w_i=\mu\right]\\ \begin{aligned} \text{s.t.}\quad&S\subseteq\{a_1,\dots,a_s\}\cup\{b_1,\dots,b_s\} \\ &S\text{ forms a balanced filling} \end{aligned} fs,t(μ)=[∃S,i∈S∑wi=μ]s.t.S⊆{ a1,…,as}∪{ b1,…,bs}S forms a balanced filling
其中 balanced filling \text{balanced filling} balanced filling 是平衡填充。虽然前面是用 “向量” 的口吻定义的,换成集合想必也不会引起误会。
显然只需考虑 f s , t ( μ ) = 1 f_{s,t}(\mu)=1 fs,t(μ)=1 的三元组 ( s , t , μ ) (s,t,\mu) (s,t,μ) 。注意到 t , μ t,\mu t,μ 固定时 s s s 越小越好。
Definition 3. 对于 t ∈ [ 0 , ∣ B ∣ ] t\in[0,|B|] t∈[0,∣B∣] 和 μ ∈ ( C − W , C + W ] \mu\in(C-W,\;C+W] μ∈(C−W,C+W],定义
s t ( μ ) = min { s : f s , t ( μ ) = 1 } s_t(\mu)=\min\{s: f_{s,t}(\mu)=1\} st(μ)=min{ s:fs,t(μ)=1}
对其进行 d p \tt dp dp 。按照 t t t 从小到大的顺序,每次考虑是否进行平衡加和平衡减。
定义 s t ( μ ) = ∣ A ∣ + 1 s_t(\mu)=|A|+1 st(μ)=∣A∣+1 若不存在对应的 s s s 。于是有如下伪代码:
Algorithm balsub 1 for μ ← C − W + 1 to C + W do 2 s 0 ( μ ) ← ∣ A ∣ + 1 3 s 0 ( 0 ) ← 0 4 for t ← 1 to ∣ B ∣ do 5 for μ ← C − W + 1 to C + W do 6 s t ( μ ) ← s t − 1 ( μ ) 7 for μ ← C − W + 1 to C do 8 μ ′ ← μ + b t 9 s t ( μ ′ ) ← min { s t ( μ ′ ) , s t − 1 ( μ ) } 10 for μ ← C + W downto C + 1 do 11 for j ← s t ( μ ) + 1 to s t − 1 ( μ ) do 12 μ ′ ← μ + a j 13 s t ( μ ′ ) ← min { s t ( μ ′ ) , j } \begin{array}{r|l} &\textrm{Algorithm }\texttt{balsub}\\\hline 1&\textbf{for }\mu\gets C-W+1\textbf{ to }C+W\textbf{ do }\\ 2&\quad s_{0}(\mu)\gets |A|+1\\ 3&s_0(0)\gets 0\\ 4&\textbf{for }t\gets 1\textbf{ to }|B|\textbf{ do}\\ 5&\quad\textbf{for }\mu\gets C-W+1\textbf{ to }C+W\textbf{ do}\\ 6&\quad\quad s_t(\mu)\gets s_{t-1}(\mu)\\ 7&\quad\textbf{for }\mu\gets C-W+1\textbf{ to }C\textbf{ do }\\ 8&\quad\quad\mu'\gets\mu+b_t\\ 9&\quad\quad s_t(\mu')\gets\min\{s_t(\mu'),\;s_{t-1}(\mu) \}\\ 10&\quad\textbf{for }\mu\gets C+W\textbf{ downto }C+1\textbf{ do}\\ 11&\quad\quad\textbf{for }j\gets s_t(\mu)+1\textbf{ to }s_{t-1}(\mu)\textbf{ do}\\ 12&\quad\quad\quad\mu'\gets\mu+a_j\\ 13&\quad\quad\quad s_t(\mu')\gets\min\{s_t(\mu'),\;j\} \end{array} 12345678910111213Algorithm balsubfor μ←C−W+1 to C+W do s0(μ)←∣A∣+1s0(0)←0for t←1 to ∣B∣ dofor μ←C−W+1 to C+W dost(μ)←st−1(μ)for μ←C−W+1 to C do μ′←μ+btst(μ′)←min{ st(μ′),st−1(μ)}for μ←C+W downto C+1 dofor j←st(μ)+1 to st−1(μ) doμ′←μ+ajst(μ′)←min{ st(μ′),j}
第 5 – 6 5–6 5–6 行是不进行平衡加。第 7 – 11 7–11 7–11 行进行平衡加。第 12 – 13 12–13 12–13 行在 s t ( μ ) s_t(\mu) st(μ) 对应的方案上做了平衡减。注意 11 11 11 行处 j = ∣ A ∣ + 1 j=|A|+1 j=∣A∣+1 可能出现,此时转移应当被忽略。
最关键处在于第 11 11 11 行的上界 s t − 1 ( μ ) s_{t-1}(\mu) st−1(μ) 。这是因为 j > s t − 1 ( μ ) j>s_{t-1}(\mu) j>st−1(μ) 时,与之等效的转移可以在 s t − 1 ( μ ) s_{t-1}(\mu) st−1(μ) 上完成。这直接使得第 11 11 11 行的循环次数,对于每个 μ \mu μ,是 ∑ t s t − 1 ( μ ) − s t ( μ ) = s 0 ( μ ) − s ∣ B ∣ ( μ ) ⩽ ∣ A ∣ \sum_{t}s_{t-1}(\mu)-s_{t}(\mu)=s_0(\mu)-s_{|B|}(\mu)\leqslant |A| ∑tst−1(μ)−st(μ)=s0(μ)−s∣B∣(μ)⩽∣A∣ 。因此总复杂度 O ( n W ) \mathcal O(nW) O(nW) 。
通过该数组,我们可直接求出 max { i : i ⩽ C , i ∈ ξ ( U ) } = max { z : z ⩽ C , s ∣ B ∣ ( μ ) ≠ ∣ A ∣ + 1 } \max\{i:i\leqslant C,\;i\in\xi(\Bbb U)\}=\max\{z:z\leqslant C,\;s_{|B|}(\mu)\ne |A|+1\;\} max{ i:i⩽C,i∈ξ(U)}=max{ z:z⩽C,s∣B∣(μ)=∣A∣+1},或者 min { ∣ i − C ∣ : i ∈ ξ ( U ) } = min { i : s ∣ B ∣ ( C ± i ) ≠ ∣ A ∣ + 1 } \min\{|i-C|:i\in\xi(\Bbb U)\}=\min\{i:s_{|B|}(C\pm i)\ne|A|+1\} min{ ∣i−C∣:i∈ξ(U)}=min{ i:s∣B∣(C±i)=∣A∣+1} 。
叁、引用
D. Pisinger, Linear time algorithms for knapsack problems with bounded weights, Journal of Algorithms. 33 (1999) 1–14 10.1006/jagm.1999.1034.
Chao Xu, Subset sum through balancing, personal blog.
边栏推荐
- How to manage device authorization
- 如何成为一名高级数字 IC 设计工程师(3-5)工具篇:SpyGlass 技术
- Analysis of sending principle of OData metadata request for SAP ui5 application
- ASP. Design and implementation of net+sql online alumni list
- Using autogluon to forecast house price
- Which is the best billing method for okcc call center
- Stm32l4xx serial port log configuration analysis
- Use kubernetes resource lock to complete your own ha application
- 我把整个研发中台拆分过程的一些心得总结
- Uniapp notes
猜你喜欢

ASP. Net based on LAN
![[redis] key hierarchy](/img/ab/a5d3bb61b4571966d0f47037af4f41.png)
[redis] key hierarchy

Introduction to UE gameplay 44 (animation import FBX and production standard)

Finally got the byte offer. The 25-year-old inexperienced experience in software testing is written to you who are still confused

Business system anti-virus
![[redis] get to know redis for the first time](/img/02/3c6a7f6ea8c563386a4cd458024728.png)
[redis] get to know redis for the first time

OculusRiftS与Unity.UI的交互(1)-总览

SystemVerilog-结构体(一)

Typescript (4) interface

SAP ui5 beginner tutorial Part 23 - sorting sort and grouping Group trial version of list control
随机推荐
How to become a senior digital IC Design Engineer (6-5) digital IC Verification: coverage collection
[apprendre la programmation FPGA - 49 à partir de zéro]: vision - Comment la puce a - t - elle été conçue?
Server antivirus
数字 IC 设计、FPGA 设计秋招笔试题目、答案、解析(1)2022 紫光展锐(上)
Smart world 2030
[redis] key hierarchy
TiFlash 面向编译器的自动向量化加速
Typescript (4) interface
Niuke.com Huawei question bank (41~50)
[TS] type alias
How to prevent virus
In MySQL database, the two data written when creating tables with foreign keys are the same. Do I copy them or fail to display them
Digital IC design, FPGA design written examination questions, answers and analysis of autumn move (1) 2022 Ziguang zhanrui (Part 1)
如何成为一名高级数字 IC 设计工程师(4-2)脚本篇:Verilog HDL 代码实现的文件读写操作
[high concurrency, high performance and high availability of massive data MySQL practice-10] - Implementation of mvcc in InnoDB
okcc呼叫中心的计费方式哪个最好
大三下期末考试
Redis data migration (III)
SAP ui5 beginner tutorial Part 23 - sorting sort and grouping Group trial version of list control
Pat grade a real problem 1165