Questions tagged [lowest-common-ancestor]

56 questions
34
votes
11 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
  • 11
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
  • 9,224
  • 1
  • 29
  • 54
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,880
  • 3
  • 18
  • 42
3
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…
3
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…
gabbar0x
  • 4,046
  • 5
  • 31
  • 51
2
votes
2 answers

Queries for minimum fuel needed to travel from U to V

Question: Given a tree with N nodes. Each edges of the tree contains: D : the length of the edge T : the gold needed to pay to go through that edge (the gold should be paid before going through the edge) When moving through an edge, if you're…
unglinh279
  • 675
  • 4
  • 24
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

How to find LCA using HLD?

I have a tree. I want to answer queries such as: Add value on a path Get sum on a path I am using Heavy-Light Decomposition. There are n nodes in the tree and m queries. With HLD, when I know the Lowest Common Ancestor, I can separate any query…
Aleksandr Tukallo
  • 1,299
  • 17
  • 22
1
2 3 4