Jump to content

Distribution of numbers - Hard one


Wizzuriz

Recommended Posts

Hello all,

 

I would like to hear if there is someone here that can help me figure out how to do this.

 

I have an array with some values in example:  array(7.50, 9, 4.50, 6.50, 3.75); now I would like to distribute them to 3 other arrays and get the values in each of the new arrays so close to the average of the first array values.

 

I get the average to 10.416666666667 so a cool result could be: array_1(9), array_2(4.5, 6.50) & array_3(7.50, 3.75).

 

I really don't know how to do this.

 

Please let me know if you have an idea on how to solve this.

 

Best regards

Wizzuriz

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/277643-distribution-of-numbers-hard-one/
Share on other sites

Looks like this is a very hard problem that short of a brute force algorithm can not be definitively solved.

http://en.wikipedia.org/wiki/3-partition_problem

Pseudocode for an approximation of the optimal solution for dividing into two arrays (the greedy algorithm) that you could adapt to an n array solution is available here. http://en.wikipedia.org/wiki/Partition_problem

Looks like this is a very hard problem that short of a brute force algorithm can not be definitively solved.

http://en.wikipedia.org/wiki/3-partition_problem

Pseudocode for an approximation of the optimal solution for dividing into two arrays (the greedy algorithm) that you could adapt to an n array solution is available here. http://en.wikipedia.org/wiki/Partition_problem

 

Thanks for the post, very nice stuff.

 

I don't need it to be definitively solved, I just need to distribute the values from the main array into 3 other arrays with a sum near the average.

Example.

 

Main_Array(7.50, 9, 4.50, 6.50, 3.75) solved into array_1(7.50,3.75) array_2(9)array_3(6.50,4.50) The sum don't need to be 100 match just evenly distributed +/- some value.  

 

If you have an example of some php code with a method to solved this please let me know.

 

Best regards

Wizzuriz

A point of clarification -

 

Is the problem to distribute 5 values over 3 arrays?

or

Is the problem more general to distribute P values over Q arrays?

The problem is to distribute P values over Q arrays.

 

Now with the numbers in the example we can't distribute them 100 evenly so the goal is to distribute them as evenly as possible.

 

* WIzzuriz

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.