Open
Description
Description of the problem
Using a naive approach we can find LCA between two nodes in a non cyclic graph in O(n)
time. But when there are O(n)
queries i.e we need to get LCA of O(n)
different pairs of nodes, the time complexity becomes O(n^2)
.
But by doing some pre processing we can reduce the complexity to O(nlogn)
.
There are mainly two approaches:
Binary lifting
using Segment tree and Eulars tour
Both of these can be implemented in pydatastructs/graphs/algorithms.py as classes