Questions tagged [lowest-common-ancestor]

54 questions
31
votes
10 answers

Algorithm to find lowest common ancestor in directed acyclic graph?

Imagine a directed acyclic graph as follows, where: "A" is the root (there is always exactly one root) each node knows its parent(s) the node names are arbitrary - nothing can be inferred from them we know from another source that the nodes were…
5
votes
4 answers

How to implement lowest common ancestor for a binary tree using Java?

I encountered the following implementation and spent some time, but still can't grasp the idea. Could someone please explain line by line what it is doing? I just don't understand at what point it can decide a node is an ancestor. Thank you public…
user2426823
5
votes
2 answers

Find multiple LCAs in unrooted tree

I am trying to implement LCA for unrooted tree. I have given a tree (a connected undirected graph without cycles) and a sequence of queries about LCA for some root and two vertices. Every particular query can have different root, so I cannot use…
4
votes
3 answers

What is the fastest way to find the positions of the first common entry in two vectors in c++?

I have two vectors u = {32, 25, 13, 42, 55, 33} and v = {18, 72, 53, 39, 13, 12, 28} for which I would like to determine the position of their first common entry, 13. For these example vectors, these positions are 3 and 5. What is the fastest way to…
4
votes
2 answers

How does one find the lowest ancestor with a descendant leaf node with some label?

The problem Given a rooted tree with n nodes, where all leaves are labelled from a set of labels. Build a datastructure which, given a leaf node, a and a label l, can find the lowest ancestor, u, of a, where u has at least one descendant with label…
4
votes
2 answers

LCA algorithm in java (working with non binary trees)

I have written a very simple tree class using arrays. This class needs to represent data that are linked together but they can have a different number of connections (i.e. one path could have only 3 nodes and another could have 10). Said that, I…
Giovanni
  • 121
  • 1
  • 1
  • 9
3
votes
1 answer

Name for algorithm reconstructing tree from lowest common ancestor?

I'd like to write a tool that works on some tree-structured data. (In fact it will work on a tree-like subset of a git revision DAG, but that's not important for this question). In particular I want an algorithm that reconstructs a subset of the…
Tom Ellis
  • 8,007
  • 21
  • 45
3
votes
1 answer

Find Lowest Common Ancestor in a Nested Set

I am looking for a way to find the Lowest Common Ancestor within a Nested Set can be found using a single equation. For example, from the image at: https://commons.wikimedia.org/wiki/File:Clothing-hierarchy-traversal.svg The LCA between Suits and…
Flosculus
  • 6,660
  • 3
  • 15
  • 39
2
votes
1 answer

Getting SIGSEGV (segmentation error) for the given problem. (Finding LCA of a generic tree)

So, I was trying to solve the below problem using the most basic method i.e. storing the paths and finding LCA. My code is working fine on VSCode and giving the right output. But when submitting on SPOJ, it gives runtime error (SIGSEGV). Problem…
2
votes
2 answers

Palindromes in a tree

I am looking at this challenge: Given a tree with N nodes and N-1 edges. Each edge on the tree is labelled by a string of lowercase letters from the Latin alphabet. Given Q queries, consisting of two nodes u and v, check if it is possible to make a…
2
votes
1 answer

Queries on Tree Path with Modifications

Question: You are given a Tree with n Nodes(can be upto 10^5) and n-1 bidirectional edges. And lets say each node contains two values: It's index(Just a unique number for node), lets say it will be from 1 to n. And It's value Vi, which can vary…
2
votes
1 answer

All lowest common ancestor with NetworkX

I want to get all LCA nodes from node"a" and node"o". In this DiGraph, node"l" and node"m" are LCA nodes. The following is the code. import networkx as nx def calc_length(Graph, node1, node2, elem): length1 = nx.shortest_path_length(Graph,…
2
votes
2 answers

Lowest common ancestor in python's NetworkX

Using NetworkX I want to get lowest common ancestor from node1 and node11 in DiGraph. The following is the code. import networkx as nx G = nx.DiGraph() #Directed…
2
votes
1 answer

Lowest common ancestor, how to build the tree from command line input?

# Python program to find LCA of n1 and n2 using one # traversal of Binary tree # def build_graph(): # n = input() # ex1, ex2 = raw_input(), raw_input() # d = {} # for i in xrange(n-1): # e1, e2 = map(str,…
Maverick
  • 3,299
  • 22
  • 82
  • 144
2
votes
1 answer

Given a tree, find the kth node in the path from node 'a' to node 'b' in Log(n)?

Given a tree, I need to find the 'k'th node in the path from 'a' to 'b'. That means that I need to find the node at the 'kth' position in the path from node 'a' to node 'b'. I was thinking on the lines of Lowest-common-ancestor and/or heavy-light…
bholagabbar
  • 3,337
  • 2
  • 22
  • 46
1
2 3 4