Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Script PHP with MAX value

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Script PHP with MAX value

Postby kronos1991 » Sat Aug 09, 2014 3:01 pm

Hi everyone,
Sorry for bad English and thanks for your help in advance :) I have to explain one kind of tricky problem I've encountered while coding. Here's the point:

I need a script that essentially do this: extract the 5 max values of 5 arrays, that are "mixed", i.e. they contain "recurrent" values. Here is an example:

array1(a, b)
array2(a, c, d, e, g)
array3(b, d, g, h)
array4(e, t, z)
array5(b, c, d, k)

The 2 essential request are the following:
1) the sum of those 5 arrays (array1+array2+array3...) MUST be the highest possible...
2) ...without repeat ANY value previously used
(e.g. if in array1 the top value was "b", this cannot be re-taken as max value in the arrays 3 or 5).

Currently I coded like this...:

Code: Select all
$group1 = array(a, b);
$group = array(a, b, c, d);

$max1a = max(group1);
$max2a = max(group2) unset($max1a);

$sum1 = $max1a + $max2a;

$max2b = max(group2);
$max1b = max(group1) unset($max2b);

$sum2 = $max1b + $max2b;

if($sum1 > $sum2) {echo $sum1}
else {echo $sum2}


... but it's kinda impossible to use this code with 5 arrays, because I should compare 5! (120 :help: ) combinations in order to find the max sum value.

I know the problem is quite difficult to explain and to solve, but I very need your help and I hope you can save me!!! :D
Cheers
kronos1991
New php-forum User
New php-forum User
 
Posts: 2
Joined: Sat Aug 09, 2014 2:49 pm

Re: Script PHP with MAX value

Postby landi » Sun Aug 10, 2014 3:20 am

Hi,

1) This sounds like a Least Cost Path problem (in your case max path) and there are solutions. However, they are not necessarily small nor simple nor cheap (in CPU time). Not my area, sorry. Try Google.

2) What answer do you want if all arrays contain the same single value?

3) Don't forget to ask if you really need the Max Cost, or just a good approximation. We often do things for intellectual stimulation that are not required by the real world. For example the least cost path for a postman is nice to calculate, but if the postman is drunk, or stops to stroke cats, the reality may differ from the theory. If you sort the arrays, and then calc the difference between elements, you might get a simple solution close to the truth.

-A
landi
New php-forum User
New php-forum User
 
Posts: 236
Joined: Thu Mar 15, 2012 3:59 pm

Re: Script PHP with MAX value

Postby kronos1991 » Sun Aug 10, 2014 4:05 am

landi wrote:Hi,

1) This sounds like a Least Cost Path problem (in your case max path) and there are solutions. However, they are not necessarily small nor simple nor cheap (in CPU time). Not my area, sorry. Try Google.

2) What answer do you want if all arrays contain the same single value?

3) Don't forget to ask if you really need the Max Cost, or just a good approximation. We often do things for intellectual stimulation that are not required by the real world. For example the least cost path for a postman is nice to calculate, but if the postman is drunk, or stops to stroke cats, the reality may differ from the theory. If you sort the arrays, and then calc the difference between elements, you might get a simple solution close to the truth.

-A


Hi and thank you for your reply.
1) Unfortunately, I can't find anything useful for me. Too bad.
2)e.g., for example...
if a>b>c>d>e
array1(a, b, c, d, e)
array2(a, b, c, d, e)
array3(a, b, c, d, e)

you mean this? In this case, I want a+b+c.

3) I know what you mean, but I really need the really true solution, and not an approximation, no matter how close it is :(
kronos1991
New php-forum User
New php-forum User
 
Posts: 2
Joined: Sat Aug 09, 2014 2:49 pm

Re: Script PHP with MAX value

Postby landi » Sun Aug 10, 2014 5:01 am

Hi,
It might help to explain WHY you need this solution, in case there are other possibilities?
-A
landi
New php-forum User
New php-forum User
 
Posts: 236
Joined: Thu Mar 15, 2012 3:59 pm


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron