Calculate Family Relationship from Genealogical Data

后端 未结 6 2114
萌比男神i
萌比男神i 2021-01-30 11:36

I would like to be able to calculate the family relationship between two individuals in a family tree, given the following data schema (simplified from my actual data schema, on

6条回答
  •  青春惊慌失措
    2021-01-30 12:22

    Below is my PHP implementation of my algorithm to calculate relationship. This is based upon the data schema I outlined in the original question. This only finds the "closest" i.e. shortest-path relationship between the two individuals, it does not resolve compound relationships such as half-siblings or double cousins.

    Note that data access functions such as get_father and get_gender are written in the style of a database abstraction layer I always use. It should be fairly straightforward to understand what is going on, basically all dbms-specific functions such as mysql_query are replaced with generalized functions such as db_query; it is not very complicated at all, especially in the examples in this code, but feel free to post questions in comments if it isn't clear.

     $c_anc) {
        $distance = $c_anc[1] + $c_anc[2];
        if ($least_distance < 0 || $least_distance > $distance) {
          $least_distance = $distance;
          $ld_index = $i;
        }
      }
    
      return $ld_index >= 0 ? $common_ancestors[$ld_index] : false;
    } //END function lowest_common_ancestor
    
    function common_ancestors($a_id, $b_id) {
      $common_ancestors = array();
    
      $a_ancestors = get_ancestors($a_id);
      $b_ancestors = get_ancestors($b_id);
    
      foreach ($a_ancestors as $a_anc) {
        foreach ($b_ancestors as $b_anc) {
          if ($a_anc[0] == $b_anc[0]) {
            $common_ancestors[] = array($a_anc[0], $a_anc[1], $b_anc[1]);
            break 1;
          }
        }
      }
    
      return $common_ancestors;
    } //END function common_ancestors
    
    function get_ancestors($id, $dist = 0)
    {
      $ancestors = array();
    
      // SELF
      $ancestors[] = array($id, $dist);
    
      // PARENTS
      $parents = get_parents($id);
      foreach ($parents as $par) {
        if ($par != 0) {
          $par_ancestors = get_ancestors($par, $dist + 1);
          foreach ($par_ancestors as $par_anc) {
            $ancestors[] = $par_anc;
          }
        }
      }
    
      return $ancestors;
    } //END function get_ancestors
    
    function get_parents($id)
    {
      return array(get_father($id), get_mother($id));
    } //END function get_parents
    
    function get_father($id)
    {
      $res = db_result(db_query("SELECT father_id FROM child WHERE child_id = %s", $id));
      return $res ? $res : 0;
    } //END function get_father
    
    function get_mother($id)
    {
      $res = db_result(db_query("SELECT mother_id FROM child WHERE child_id = %s", $id));
      return $res ? $res : 0;
    } //END function get_mother
    
    function get_gender($id)
    {
      return intval(db_result(db_query("SELECT gender FROM individual WHERE id = %s", $id)));
    }
    
    function ordinal_suffix($number, $super = false)
    {
      if ($number % 100 > 10 && $number %100 < 14) {
        $os = 'th';
      } else if ($number == 0) {
        $os = '';
      } else {
        $last = substr($number, -1, 1);
    
        switch($last) {
          case "1":
            $os = 'st';
            break;
          case "2":
            $os = 'nd';
            break;
          case "3":
            $os = 'rd';
            break;
          default:
            $os = 'th';
        }
      }
    
      $os = $super ? ''.$os.'' : $os;
    
      return $number.$os;
    } //END function ordinal_suffix
    
    function format_plural($count, $singular, $plural)
    {
      return $count.' '.($count == 1 || $count == -1 ? $singular : $plural);
    } //END function plural_format
    
    ?>
    

    As I had mentioned previously, the algorithm to determine LCA is far less than optimal. I plan to post a separate question to optimize that, and another to address the problem of calculating compound relationships such as double cousins.

    Many thanks to everyone who helped prod me in the right direction! With your tips, this turned out to be much easier than I originally thought.

提交回复
热议问题