0% found this document useful (0 votes)
25 views

Lec 10

This document discusses an algorithm for finding the lowest common ancestor (LCA) of two nodes in a tree. It presents an approach that first computes the Euler tour of the tree, finds the leftmost and rightmost occurrences of each vertex, and constructs an Euler array. It then checks conditions on the left and right indices of the two nodes to determine if one is an ancestor of the other. If not, it finds the lowest-level node between their rightmost-leftmost ranges as their LCA. The algorithm reduces the problem to a range minimum query which can be solved using precomputed prefix and suffix minima.

Uploaded by

2021 03031
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Lec 10

This document discusses an algorithm for finding the lowest common ancestor (LCA) of two nodes in a tree. It presents an approach that first computes the Euler tour of the tree, finds the leftmost and rightmost occurrences of each vertex, and constructs an Euler array. It then checks conditions on the left and right indices of the two nodes to determine if one is an ancestor of the other. If not, it finds the lowest-level node between their rightmost-leftmost ranges as their LCA. The algorithm reduces the problem to a range minimum query which can be solved using precomputed prefix and suffix minima.

Uploaded by

2021 03031
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

MC224 Parallel and Distributed Algorithms 31st Jan, 2023

Lec. 10: Lowest Common Ancestor


Lecturer: Anuj Tawari Scribe: 202103027, 202103031

10.1 Introduction
This method finds the common ancestor between two nodes (u,v).

10.2 Approach
• Input: Two nodes u,v of a tree.

• Output: The ancestor of u,v is farthest from the root.

• Solution:

– Compute the Euler tour.


– Decide if v is the ancestor of u.
– Find the leftmost and rightmost occurrence of every vertex in the Euler path.
– Construct an Euler array, when we visit (u,v) in the Euler tour write v in the Euler
array (ordering according to visiting).
l(u) : rank of (p(u),u) in the Euler tour.
r(u) : rank of (u,p(u)) in the Euler tour.
– Now we have l(u),r(u),l(v),r(v).
– v is the ancestor of u if and only if:
l(v) < l(u) < r(v)
– Either
r(v) < l(u)
r(u) < l(v)
if this condition is satisfied then u,v don’t have an ancestor-descendant relationship.
– In short, we are traversing through all nodes between r(v) and l(u) and the lowest level
node will be the ancestor.
– Construction:
∗ Construct Euler tour, find the leftmost and the rightmost occurrence of every vertex.
∗ Case-1: r(v) < l(v).
find the vertex of the lowest level between r(v) and l(u).
∗ For that we need the level of every vertex and the occurrence of every vertex.

10-1
– Find LCA(g,j):

Here we label each node in the euler tour in terms of (index,level) form.

10-2
Now the l(g) is 12 and l(j) is 15. Therefore, r(g) = 12 and r(j) = 15. Here r(g) < l(j)

10-3
Hence the LCA(g,j) is C.

10-4
• Range Minima Problem:

• Store prefix minima, suffix minima


Prefix minima : minimum of first i many numbers for every i.
Suffix minima : minimum of last i many numbers for every i.

• Store the prefix and suffix minima



 at every step. Suffix will store : min l(j),....,n
Prefix will store min 1,....,r(g)

• Compute prefix and suffix minima for every prefix and suffix to combine the range.

10-5

You might also like