(leaf_a, leaf_b, path='longest')
return lca, height_of_lca
Finds the lowest common ancestor of two leaves and its height.

  The height of a node here is defined as the maximum distance between that node
  and any of the leaves it spans.

  When there are multiple paths starting from a given leaf (due to it possibly
  having multiple parents), we rely on the value of path to choose which one to
  use. By default, we use the path whose length to the root is the longest. We
  find the lowest common ancestor of the two given leaves along the longest such
  path for each. Alternatively, all paths can be used in which case the minimum
  LCA over all is returned.

  Args:
    leaf_a: A Synset.
    leaf_b: A Synset.
    path: A str. One of 'longest', or 'all'.

  Returns:
    lca: A Synset. The lowest common ancestor.
    height_of_lca: An int. The height of the lowest common ancestor.

  Raises:
    ValueError: Invalid path. Must be 'longest', or 'all'.
Finds the lowest common ancestor of two leaves and its height.
Finds the lowest common ancestor of two leaves and its height .
def get_lowest_common_ancestor(leaf_a, leaf_b, path='longest'):
  """Finds the lowest common ancestor of two leaves and its height.

  The height of a node here is defined as the maximum distance between that node
  and any of the leaves it spans.

  When there are multiple paths starting from a given leaf (due to it possibly
  having multiple parents), we rely on the value of path to choose which one to
  use. By default, we use the path whose length to the root is the longest. We
  find the lowest common ancestor of the two given leaves along the longest such
  path for each. Alternatively, all paths can be used in which case the minimum
  LCA over all is returned.

  Args:
    leaf_a: A Synset.
    leaf_b: A Synset.
    path: A str. One of 'longest', or 'all'.

  Returns:
    lca: A Synset. The lowest common ancestor.
    height_of_lca: An int. The height of the lowest common ancestor.

  Raises:
    ValueError: Invalid path. Must be 'longest', or 'all'.
  """
  if path not in ['longest', 'all']:
    raise ValueError('Invalid path. Must be "longest", or "all".')

  # A list of paths from a each leaf to the root.
  paths_a = get_upward_paths_from(leaf_a)
  paths_b = get_upward_paths_from(leaf_b)

  # Each element in paths_a is a path starting from leaf_a and ending at the
  # root (and analogously for paths_b). We pick the longest path of each list of
  # paths and find the lowest common ancestor between those two paths.
  if path == 'longest':
    path_a = paths_a[np.argmax([len(path_a) for path_a in paths_a])]
    path_b = paths_b[np.argmax([len(path_b) for path_b in paths_b])]
    lca, height_of_lca = find_lowest_common_in_paths(path_a, path_b)

  else:
    # Search for the LCA across all possible paths from the given leaves.
    lca, height_of_lca = None, None
    for path_a in paths_a:
      for path_b in paths_b:
        lca_candidate, height = find_lowest_common_in_paths(path_a, path_b)
        if height_of_lca is None or height < height_of_lca:
          lca = lca_candidate
          height_of_lca = height

  return lca, height_of_lca
def get_lowest_common_ancestor ( leaf_a , leaf_b , path = 'longest' ) : if path not in [ 'longest' , 'all' ] : raise ValueError ( 'Invalid path. Must be "longest", or "all".' ) # A list of paths from a each leaf to the root. paths_a = get_upward_paths_from ( leaf_a ) paths_b = get_upward_paths_from ( leaf_b ) # Each element in paths_a is a path starting from leaf_a and ending at the # root (and analogously for paths_b). We pick the longest path of each list of # paths and find the lowest common ancestor between those two paths. if path == 'longest' : path_a = paths_a [ np . argmax ( [ len ( path_a ) for path_a in paths_a ] ) ] path_b = paths_b [ np . argmax ( [ len ( path_b ) for path_b in paths_b ] ) ] lca , height_of_lca = find_lowest_common_in_paths ( path_a , path_b ) else : # Search for the LCA across all possible paths from the given leaves. lca , height_of_lca = None , None for path_a in paths_a : for path_b in paths_b : lca_candidate , height = find_lowest_common_in_paths ( path_a , path_b ) if height_of_lca is None or height < height_of_lca : lca = lca_candidate height_of_lca = height return lca , height_of_lca