Jump to content

NP-Hard problem

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
gk_manutd

gk_manutd

    Newbie

  • Members
  • Pip
  • 1 posts
I'm given an array of 100 integers and a threshold value. I have to select numbers from the array such that they add up to the threshold value. The numbers can be selected more than once [The same number can be used over & over till the threshold is reached]. This is a minimization problem. I have to minimize the sum [and not the number of integers that I select from the array]. If the sum is not equal to (that is to say- it is greater) than the threshold, then it should be the minimum possible sum greater than or equal to the threshold. Clearly, taking the smallest number in the array and looping till the threshold is crossed will not work here.

[This is part of a larger optimization problem. I've handled the generic cases. This is a special case. Backtracking is not an option in the larger context!].

Consider the following array of 10 random integers:

[157,131,127,151,139,113,109,137,0,149]

threshold value= 1213

This threshold value is to be reached by adding up the numbers in the array (more than once if necessary).

Since, threshold is 1213.. the minimal sum HAS TO BE at least 1213. It cannot be lesser than 1213.
In the above array, the solution is adding up ALL the elements in the array (which gives 1213) OR do the following calculation:

157*4+149+109*4 (which also yields 1213).

The sum of the elements in the array is not guaranteed to be equal to the threshold (the first case is a convenient example). So, it is necessary to do something like the second case to achieve minimization.

We can exceed the threshold IF and ONLY IF no combination of elements in the array(taken once and/or taken multiple times) adds up to the threshold.

So, in absence of 149 in the above array, our calculations would become 157*4+109*4+151=1215
(as close to the threshold as possible- to ensure minimality).


SO, HOW DO YOU SOLVE THIS? I'm fresh out of ideas.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Do some research on Linear Programming. It is the branch of mathematics designed to solve this type of problem. In particular, the Simplex Method would be quite useful for this.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog