tidalik Posted October 29, 2009 Share Posted October 29, 2009 I'm trying to calculate the % influence (contribution) a certain ancestor has with in a family tree. Each generation has a % of influence which is [tex]1/x[/tex] where [tex]x[/tex] is the number of ancestors in the generation. So parents are 1/2, grandparents 1/4, greatgrandparents 1/8 etc etc. If a ancestor appears more than once, then it's influence is the sum of all instances is (such that if it's a parent and a grandparent then it's 1/2 + 1/4) I have a function that works well to tell me the number of ancestors in a generation and another that calulates the % influence. [ancestersPerGeneration() & getPercentage()] My database is structured so it is - id, name, fatherid, motherid I have a function that get's the parent's IDs and returns a 2 element array. //Set Generation number $gens = 4; $Id = 6916; //a random database member $ancestorList[0] = $Id; $ancestorInfluence[$Id]= 1; echo $ancestorList[0]." is the starter.<br>"; for ($k=1; $k < $gens; $k++){ //Work out how many entries to check this iteration $ro = ancestorsPerGeneration($k); for ($r=1; $r < $ro; $r++){ $mode = each($ancestorList); //Get Parents $parents = getParentIDs($mode[value]); //Get Contribution for each parent foreach ($parents as $parent) { $influence = getPercentage($k); $ancestorList[] = $parent; //Check if the parent is in the influence array already if (!in_array($parent, $ancestorInfluence)){ $ancestorInfluence[$parent] = $influence; } else { $ancestorInfluence[$parent] = $ancestorInfluence[$parent] + $influence; } echo "$ro Ancestors this generation. Currently at Ancestor $r.<br>"; echo "<p>$parent has $influence</p"; $r++; } } } $totalAncestors = totalAncestors($gens); echo "<h2>Expected there to be ".($totalAncestors+1)." in Ancestor array and there was ".count($ancestorList)."</h2><br>"; print_r($ancestorInfluence); There is something really *really* wrong with my logic. Output looks like this: 6916 is the starter. 2 Ancestors this generation. Currently at Ancestor 1. 8380 has 0.5 6933 has 0.5 8339 has 0.25 8614 has 0.25 9182 has 0.125 6846 has 0.125 7193 has 0.125 641 has 0.125 9608 has 0.125 2529 has 0.125 Expected there to be 31 in Ancestor array and there was 11 Array ( [6916] => 1 [8380] => 0.5 [6933] => 0.5 [8339] => 0.25 [8614] => 0.25 [9182] => 0.125 [6846] => 0.125 [7193] => 0.125 [641] => 0.125 [9608] => 0.125 [2529] => 0.125 ) I think perhaps I am going about this the wrong way. Can anyone else me straighten this out? Thanks Link to comment https://forums.phpfreaks.com/topic/179455-need-help-with-the-logic-behind-this-calculation/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.