Given a family tree, find out if people is blood related

Source: Internet
Author: User

Given a family tree for a few generations for the entire population and both people write a routine that'll find out if T Hey is blood related. Siblings is blood related since they has the same parents. Cousins is blood related since one of their parents has the same parents etc. Design the data structure first and then write the routine.

https://www.careercup.com/question?id=4812957531766784

Solution: G Home, we can use DFS or BFS, DFS are generally a bit easier to implement.

If They has a common ancestor.

Data Structure:graph, BECAUSD, parents. Store parents in an array or list rather than have a separate pointer for the father and mother.

Dfs/bfs Startig from 1 person is not ideal. Bfs/iterative deepening for common ancestor from the each person, as don ' t process unnecessary children.

Lots of ways to determine a common ancestor. Pure recursion, recursively compute the ancestor set of both people and check intersection, iterative deepening to build u P The sets.

Java:

For 2 persons to is blood related, perform a breadth first traversal with the person as root and the parents as child node S and store in a ArrayList.
Then for both the persons search was there is a common the parent in the ArrayList.
If Common parent found then, the persons is related, else not related.

The data structure to store the generation tree would have nodes defined Aspublic class person {String name; Arraylist<person> children = null; Person Parent1;  Person Parent2;public person (String personname, person personParent1, person personParent2) {name = Personname;children = New Arraylist<person> ();p arent1 = Personparent1;parent2 = PersonParent2;} Public Person GetParent () {return parent;}}

Java:

public class Person {person[] parents;} Naming for cousins is:n th cousin M times removed//where n was the Min generations to a common ancestor and M are the N Umber of generations difference between the 2 cousins//so that's going to be O ((2^n+m) +2) which are still more efficient  than DFS assuming the NUM generations in the population are > n+mpublic boolean bloodrelated (person P1, person p2) {// Simple search would go down P1 ' s children/grandchildren/etc and see if we find P2//Then vice versa//Then worry Abou   T cousin style relationships//Here we ' d go to the parent tree on both until we found a common node (or ran out of data)  We could take this last approach anyway and it would get us a parent-child match too set<person> p1ancestors =  New Hashset<person> ();  set<person> p2ancestors = new hashset<person> (); So ideally here we ' re going to does BFS, but we ' re going to do 2 at once to try to minimise the depth we had to go List <person&gT  p1discovered = new linkedlist<person> ();  P1discovered.add (p1);  list<person> p2discovered = new linkedlist<person> ();  P2discovered.add (p2);    while (!p1discovered.isempty () | |!p2discovered.isempty ()) {Person nextP1 = p1discovered.remove (0);      if (nextP1! = null) {if (P2ancestors.contains (nextP1)) {return true;      } for (person parent:nextP1.parents) {P1discovered.add (parent);    } p1ancestors.add (nextP1);    } Person nextP2 = P2discovered.remove (0);      if (nextP2! = null) {if (P1ancestors.contains (nextP2)) {return true;      } for (person parent:nextP2.parents) {P2discovered.add (parent);    } p2ancestors.add (nextP2); }} return false;}

  

Given a family tree, find out if people is blood related

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.