in reply to Re^2: Divide an array into 2 subsets to verify their sum is equal or not.
in thread Divide an array into 2 subsets to verify their sum is equal or not.

> This is an NP-complete problem http://en.wikipedia.org/wiki/Knapsack_problem

Yes, to be precise a sub class known as "Partition Problem".

See WP article for some efficient algorithms and further links.

I wonder who and why is posting well known scientific problems w/o references ...?

Cheers Rolf

( addicted to the Perl Programming Language)

  • Comment on Re^3: Divide an array into 2 subsets to verify their sum is equal or not. (Partition Problem)

Replies are listed 'Best First'.
Re^4: Divide an array into 2 subsets to verify their sum is equal or not. (Partition Problem)
by hdb (Monsignor) on May 03, 2013 at 08:14 UTC
      And if kcott or BrowserUK manage to fix their programs, there is a $1,000,000 price on it:

      No. Those prizes -- and in particular, the P v NP prize -- are not up for grabs for finding solutions to particular, restricted or bounded examples of particular NP problems. The prize is for proving that there is a whole class of problems for which there are no solutions. Quite a different matter indeed.

      Despite that the OPs problem is fairly loosely stated, it does contain the relaxation of sufficient constraints -- specifically, Subset size does not matter -- that it removes the need to find either 'all solutions' or 'the optimal solution'; and that means that the problem ceases to to be "NP-complete".

      And that brings us to a theme I've seen repeated many times in many places over my career, and frequently revisited here at the monastery.The scenario goes like this: a requirement is identified as desirable. Upon analysis, the code required to implement that requirement is identified as being some variation upon one or more of the classical NP-Complete problems; and the CS majors and half-well-reads in the room will throw their collective hands up saying, "It can't be done"!

      But, on the basis of ever fading memory and usually reliable gut feel, I'd estimate that 8 out of 10 occasions, there is scope within that "some variation upon", to allow a 'good enough' solution to be found and implemented. Often all that is required is to remove the constraint that a perfect solution must be found every time.

      Take for example the bin-packing/sheet cutting problem. On the basis of strictly correct theory, businesses who's basic operating models equate to one or the other variations of this classical problem shouldn't be able to operate; and yet we have glass-cutting services and dress makers operating in every city and small town; and the entire modern world economy would be impossible without that cargo planes and container ships and freight trains and white van men went about their daily business.

      Once you factor time into the equations and realise that it isn't necessary to find perfect or optimal solutions -- that a container with a little empty space; or few offcuts from each 7' x 10' sheet are acceptable within the operating margins of the business model -- then a whole raft of NP-complete "impossible" problems cease to be so.

      Indeed, there is a whole field (actually several whole fields), of computer science dedicated to finding good enough solutions to intractable problems. Namely Genetic Algorithms and its many offshoots. But, as with all heuristical algorithms, you need to know where the constraints can be relaxed; or the costs not exceeded to apply them. Ie. For the OPs purpose, can an exact balance of sums be overridden by a time constraint?

      As is so often the case when these problems come up, the original description is so devoid of detail, it is impossible to reach conclusions about constraints might be relaxed in order to achieve a 'good enough' solution. My tactic in these situations is to post a naive -- ie. known imperfect -- solution and then wait for the OP to come back and explain why it is not good enough.

      It is inherent in the OPs specification that not all sets are solvable -- sets with odd totals have no integer bipartition; sets like 6,4,4,4,4,4 cannot be equally partitioned -- so the OPs purpose has to accommodate these possibilities. And if his procedures and processes can accommodate the impossible conditions; who's to say that they couldn't also accommodate a slightly imperfect solution, even when a perfect solution might be possible?

      Only the OP. But, as is also often the case when these problems come up, s/he has chosen not to involve themselves further. Which is a shame, because reaching a near equal partition can be done very quickly by several of the posted attempts; and adding a time-constrained, GA giggle to that would probably be able to refine that to complete equality in 95%+ cases with a few minutes of CPU.

      But who is going to be bothered to expend such development energies when the OP doesn't seem to be interested any longer.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        Full agreement on my side. Just a couple of comments:

        • The OP did add some comment to the thread today, so at least s/he is reading it.
        • Providing a polynomial algorithm to one particular NP complete problem is proof for P=NP and the price is yours. Caveat is that most believe, but cannot prove, that P <> NP.
        • Approximate solutions are of high value, no question! A large part of our economy runs on them.
        • I have and had no intention to belittle your proposed solutions. Just the opposite: I learnt a lot from studying your code.
        • Besides the practical application of solutions produced here in the monastery, a bit of pure science should be allowed as well. Even if only for the intellectual challenge and not for any practical use. These kind of discussions seem to have a tendency to drift away from the original question.

        too long didn't read all, but

        > > And if kcott or BrowserUK manage to fix their programs, there is a $1,000,000 price on it:

        > No. Those prizes -- and in particular, the P v NP prize -- are not up for grabs for finding solutions to particular, restricted or bounded examples of particular NP problems. The prize is for proving that there is a whole class of problems for which there are no solutions. Quite a different matter indeed.

        Well not in this case!

        A consequence of this definition is that if we had a polynomial time algorithm (on a UTM, or any other Turing-equivalent abstract machine) for C, we could solve all problems in NP in polynomial time.

        So finding an algorithm for the partition problem, proven to find a solution (or it's non-existence) in polynomial time (including for the worst case) means solving P vs NP!!!

        > Upon analysis, the code required to implement that requirement is identified as being some variation upon one or more of the classical NP-Complete problems; and the CS majors and half-well-reads in the room will throw their collective hands up saying, "It can't be done"!

        Well, bad trained personnel!

        Problem classes are classified by their worst case. But normally most cases can still be solved pretty fast with already well known algorithms.

        In praxis stopping after a timeout for few remaining cases is acceptable.

        Edit

        Especially if they calculate an approximate solution.

        In many cases something like (sum @part1 -sum @part2) < min @all is already sufficient.

        Cheers Rolf

        ( addicted to the Perl Programming Language)

      I've fixed all reported bugs and updated my node.

      Now, what's all this I hear about a million bucks? :-)

      -- Ken

      Only 1e6 dollars ?

      L O L !

      Cheers Rolf

      ( addicted to the Perl Programming Language)

Re^4: Divide an array into 2 subsets to verify their sum is equal or not. (Partition Problem)
by hdb (Monsignor) on May 03, 2013 at 05:52 UTC

    There are some very talented people in this community, so perhaps if we post the one or other unsolved problem, some monk might find a solution...

      > There are some very talented people in this community

      Unlikely, and during my studies of mathematics this was considered extremely unfair!

      These are well investigated problems known to be very hard and w/o optimal solution but already good practical solutions.

      They shouldn't be disguised as a hackers question.

      Cheers Rolf

      ( addicted to the Perl Programming Language)

      TRUE