Given a family tree, find out if two people are blood related
Given a family tree for a few generations for the entire population and two people write a routine that will find out if they are blood related. Siblings are blood related since they have the same parents. Cousins are blood related since one of their parents have the same parents etc. Design the data structure first and then write the routine.
https://www.careercup.com/question?id=4812957531766784
解法:G家,we can use DFS or BFS, DFS is generally a bit easier to implement.
If they have a common ancestor.
Data structure: Graph, becausd two parents. Store parents in an array or list rather than having a separate pointer for the father and mother.
DFS/BFS startig from 1 person is not ideal. BFS/Iterative Deepening for common ancestor from 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 up the sets.
Java:
For 2 persons to be blood related, perform a Breadth First Traversal with the person as root and the parents as child nodes and store in a ArrayList.
Then for both the persons search is there is a common parent in the ArrayList.
If common parent found then, the persons are related, else not related.
The data structure to store the generation tree will have nodes defined as
public 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>();
		parent1 = 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 is the min generations to a common ancestor and m is the number of generations difference between the 2 cousins
// so this is going to be O((2^n+m)+2) which is still more efficient than dfs assuming the num generations in the population is > n+m
public 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 about cousin style relationships
  // here we'd go up 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 do BFS, but we're going to do 2 at once to try to minimise the depth we have to go
  List<Person> 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 two people are blood related的更多相关文章
- openerp学习笔记 context 的应用
		
1.在Action中定义,context用于传递搜索条件和分组条件,在搜索视图中默认显示: 示例代码: <record model="ir.actions.act_window&quo ...
 - 日常训练 dfs  之 拓扑排序
		
今天被拓扑排序给折磨了一天,主要就是我的一个代码有点小bug,真难找... 先来看看我今天写的题目吧! C. Fox And Names Fox Ciel is going to publish a ...
 - python 自然语言处理(四)____词典资源
		
词典或者词典资源是一个词和/或短语及其相关信息的集合,例如:词性和词意定义等相关信息.词典资源附属于文本,而且通常在文本的基础上创建和丰富.下面列举几种nltk中的词典资源. 1. 词汇列表语料库 n ...
 - [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
		
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
 - SAP CRM 树视图(TREE VIEW)
		
树视图可以用于表示数据的层次. 例如:SAP CRM中的组织结构数据可以表示为树视图. 在SAP CRM Web UI的术语当中,没有像表视图(table view)或者表单视图(form view) ...
 - 无限分级和tree结构数据增删改【提供Demo下载】
		
无限分级 很多时候我们不确定等级关系的层级,这个时候就需要用到无限分级了. 说到无限分级,又要扯到递归调用了.(据说频繁递归是很耗性能的),在此我们需要先设计好表机构,用来存储无限分级的数据.当然,以 ...
 - 2000条你应知的WPF小姿势 基础篇<45-50 Visual Tree&Logic Tree 附带两个小工具>
		
在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...
 - Leetcode 笔记 110 - Balanced Binary Tree
		
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
 - Leetcode 笔记 100 - Same Tree
		
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
 
随机推荐
- Win10 Subsystem Linux : Ubuntu 的root密码
			
安装完Ubuntu后忽然意识到没有设置root密码, 不知道密码自然就无法进入根用户下.Ubuntu的默认root密码是随机的, 即每次开机都有一个新的root密码.我们可以在终端输入命令 sudo ...
 - learning java Cloneable
			
class Address{ String Detail; public Address(String detail){ this.Detail = detail; } } class User im ...
 - 转微软最新的Web服务器Katana发布了版本3
			
作者 Jonathan Allen ,译者 邵思华 发布于 2014年8月28日 Katana是微软对OWIN(基于.NET的开放Web接口)标准自行开发的一套实现方案,它是一种相对于IIS及Syst ...
 - C程序的函数说明使用和特点说明第一节
			
一.函数的特点: 全部都是全部函数构成 面向过程的:是函数式语言 函数的调用 是按需调用 封装包含 二.程序中函数的作用: 可以使用函数使程序变的简短 和 清晰 提高代码重用性 提高开发效率 有利于程 ...
 - codevs1580单词游戏
			
题目描述中说: 单词为at,k=8则新单词为ib 推移规则是:如果k为正数则下推,否则上推,当推移超越边界时回到另一端继续推移. 但在我做题时发现: 这个描述与数据所要求的是完全相反的!!!! 样例1 ...
 - 37、数据源之通用的load和save操作
			
一.通用的load和save操作 1.概述 对于Spark SQL的DataFrame来说,无论是从什么数据源创建出来的DataFrame,都有一些共同的load和save操作. load操作主要用于 ...
 - ora-28000:the account is locked,Oracle修改密码有效期,Oracle设置密码不过期
			
查询Oracle用户是否被锁定 --例如我这里是VMCXEDDB 是否被锁定 select username,account_status,lock_date from dba_users where ...
 - jmeter之timer --笔记一
			
简介:测试过程中需要用到time进行造数据测试,需要各种年月日,或者未来时间,就像python中的time和datetime 1.jmeter中timer,使用—time()函数 1.1 timeSh ...
 - PHP-FPM远程代码执行漏洞(CVE-2019-11043)
			
0x00 简介 在长亭科技举办的 Real World CTF 中,国外安全研究员 Andrew Danau 在解决一道 CTF 题目时发现,向目标服务器 URL 发送 %0a 符号时,服务返回异常, ...
 - mysql忘记密码恢复
			
MySQL忘记密码恢复密码的实现方法 作者:mdxy-dxy 流传较广的方法,mysql中文参考手册上的,各位vps主机租用客户和服务器托管用户忘记mysql5.1管理员密码时,可以使用这种方法破解下 ...