Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.68 KB | None | 0 0
  1. """
  2.  
  3. Suppose we have some input data describing a graph of relationships between parents and children over multiple generations. The data is formatted as a list of (parent, child) pairs, where each individual is assigned a unique integer identifier.
  4.  
  5. For example, in this diagram, 3 is a child of 1 and 2, and 5 is a child of 4:
  6.  
  7.  
  8. 1   2   4
  9. \ /   / \
  10.  3   5   8
  11.   \ / \  \
  12.    6   7   10
  13.  
  14. Write a function that, for two given individuals in our dataset, returns true if and only if they share at least one ancestor.
  15.  
  16.  
  17. parentChildPairs, 3, 8 => false
  18. parentChildPairs, 5, 8 => true
  19. parentChildPairs, 6, 8 => true
  20.  
  21.  
  22. """
  23.  
  24. parent_child_pairs = [
  25.     (1, 3), (2, 3), (3, 6), (5, 6),
  26.     (5, 7), (4, 5), (4, 8), (8, 10)
  27. ]
  28.  
  29.  
  30. def ancestor_comparison(p_c_p, node_a, node_b):
  31.     a_ancestor = set()
  32.     b_ancestor = set()
  33.     for pair in p_c_p:
  34.         child = pair[1]
  35.         if child == node_a:
  36.             a_ancestor.add(pair[0])
  37.         if child == node_b:
  38.             b_ancestor.add(pair[0])  
  39.     a_list = list(a_ancestor)
  40.     for ancestor in a_list:
  41.         # for pair in p_c_p:
  42.         #     child = pair[1]
  43.         #     if child == ancestor:
  44.         #         a_ancestor.add(pair[0])
  45.         check_parent(p_c_p, ancestor, a_ancestor)
  46.     # print(a_ancestor)
  47.     b_list = list(b_ancestor)
  48.     for ancestor in b_list:
  49.         # for pair in p_c_p:
  50.         #     child = pair[1]
  51.         #     if child == ancestor:
  52.         #         b_ancestor.add(pair[0])
  53.         check_parent(p_c_p, ancestor, b_ancestor)
  54.     # print(b_ancestor)
  55.  
  56.     for ancestor in a_ancestor:
  57.         if ancestor in b_ancestor:
  58.             return True
  59.     for ancestor in b_ancestor:
  60.         if ancestor in a_ancestor:
  61.             return True
  62.        
  63.     return False
  64.        
  65. def check_parent(p_c_p, node, ancestors):
  66.     for pair in p_c_p:
  67.         if pair[1] == node:
  68.             ancestors.add(pair[0])
  69.             check_parent(p_c_p, pair[0], ancestors)
  70.    
  71.    
  72.    
  73.  
  74. print(ancestor_comparison(parent_child_pairs, 4, 8))
  75.    
  76.    
  77.  
  78. def graph_relationship(p_c_p):
  79.     parent_dic = {}
  80.     zero_parent = set()
  81.     one_parent = set()
  82.     for pair in p_c_p: # O(n) n number of pairs in p_c_p
  83.         child = pair[1]
  84.         if child in parent_dic:
  85.             parent_dic[child] += 1
  86.         else:
  87.             parent_dic[child] = 1
  88.     for pair in p_c_p: # O(n)
  89.         child = pair[1]
  90.         parent = pair[0]
  91.         if child in parent_dic and parent_dic[child] == 1:
  92.             one_parent.add(child)
  93.         if parent not in parent_dic:
  94.             zero_parent.add(parent)
  95.     return [zero_parent, one_parent]
  96.            
  97. # print(graph_relationship(parent_child_pairs))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement