将子集和的近似算法转换为php代码


translate approximate algorithm for subset sum into php code

Cross在MathExchange中发布以引发更多响应。

=============================================

我正在StackOverflow中写我最初的问题,这时我意识到这是以前问过的。

事实证明,我有一个所谓的子集和问题,所以我去了维基百科,找到了这个部分。

The algorithm for the approximate subset sum problem is as follows:
 initialize a list S to contain one element 0.
 for each i from 1 to N do
   let T be a list consisting of xi + y, for all y in S
   let U be the union of T and S
   sort U
   make S empty 
   let y be the smallest element of U 
   add y to S 
   for each element z of U in increasing order do
      //trim the list by eliminating numbers close to one another
      //and throw out elements greater than s
     if y + cs/N < z ≤ s, set y = z and add z to S 
 if S contains a number between (1 − c)s and s, output yes, otherwise no

但我在理解维基百科中编写的伪代码时遇到了问题。

例如,我认为目标是找到最接近的一组可以匹配S.的数字

但这里S是一个列表。这个包含元素0的S列表是什么

if y + cs/N < z ≤ s究竟是什么?我该如何用代码写出来

我希望有人能帮我把它翻译成php代码。

至少我更熟悉这一点。它不需要是一个完整的翻译。

只要答案让我足够理解这个近似算法,让我自己用php代码写它,那就行了。

要逐一回答您在math.stackexchange.com上发布的子问题:

S和小s之间有什么区别S是一个列表变量,它最初是列表[0],并在代码执行过程中进行修改。小s是一个保持不变的数字。具体来说,s是这个问题的数字:

给定一组整数和一个整数s,是否有任何非空子集和s

到底是什么S粗略地说,S表示我们可以使用迄今为止看到的元素进行的所有"有用"求和的集合(如果我没有错的话)。

"元素为0的列表"是否意味着包含单个数字的列表,该数字为零是的,就是这个意思。

y + cs/N < z ≤ s是什么意思表示CCD_ 11和CCD_。因此,无论何时y + c*s/N ≥ zz > s(或两者),if都将失败。

还有一些你没有问但似乎很可能会出现的问题:

什么是NN是我们给出的集合中元素的数量。

什么是xixi是我们给出的集合的第i个元素。