translate approximate algorithm for subset sum into php code
This is a question originally posted on stackoverflow.
I hoped to get a better response here.
================================================
I was writing my original question in StackOverflow, when I realized it
was asked before.
Turns out I have what is known as a subset sum problem, so I went to
wikipedia and found this part.
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
But I have problem understanding the pseudo code written in wikipedia.
For instance, I thought the objective is to find the closest set of
numbers that can match S.
But here S is a list. What is this S list with element 0?
And what on earth is if y + cs/N < z ¡Ü s? How do I even write this out in
code?
I was hoping someone can help me translate this into php code.
At least I am more familiar with that. It need not be a full translation.
As long as the answers make me understand this approximate algorithm
enough for me to write it in php code myself, that will do.
No comments:
Post a Comment