Page 1 of 1

Problems using LCA form arhive.

Posted: Mon Sep 11, 2006 9:31 am
by diac_paul
Does anyone knows problemes in the arhive that use or can use LCA (Lowest common ancestor)? Or from other sites ...
Thanks

Posted: Mon Sep 11, 2006 12:17 pm
by mf

Posted: Thu Oct 19, 2006 11:49 am
by Giorgi
can anybody explain me O(log n) LCA algo or give any link?

Posted: Thu Oct 19, 2006 7:12 pm
by misof
For each vertex V and each X, compute and store the vertex (2^X) steps above the vertex V.

This can be done in O(N log N).

Think how to do it, and then how to use this information to find the LCA for a pair of vertices in O(log N) time.

Posted: Thu Oct 19, 2006 9:17 pm
by Darko
In Gusfield ("Algorithms on Strings, Trees, and Sequences"), ch.8, they describe a constant time LCA with linear preprocessing.

I tried reading it, but that just went (waaay) over my head. They map the tree to a complete binary tree in linear time in some funky way, that part I don't understand. Then finding LCA is easy (that part I kinda understand).

It is actually a good book, but is a bit hard for me to follow.

Posted: Fri Oct 20, 2006 10:35 am
by Giorgi
http://www14.in.tum.de/konferenzen/Jass ... kiefer.pdf

very interesting link :)
this algorithm finds LCA using minimizator

Posted: Sat Oct 21, 2006 5:58 am
by Cosmin.ro
You should try what misof said, it's pretty intuitive and it should be at most 30 lines of code.

Posted: Sat Oct 21, 2006 11:39 pm
by david
There is also a very easy to code algorithm due to Tarjan, in which you find the lca of several pairs of vertices during a dfs of the tree (in O(V+Q+alfa(V+Q)), where V is the number of vertices, Q the number of queries and alfa is the very slow-growing inverse of the Ackermann function). You can find it in an exercise somewhere in Cormen's book.

Posted: Thu Oct 26, 2006 8:21 pm
by Larry
Actually, you can do the precalc in O(nlogn) time by converting into an +/-1 RMQ problem in linear time, and then do the queries in O(1).

Of course, you can also precalc in O(n) and queries in O(1), and that part isn't so bad using the big/small universe paradigm. The general RMQ->LCA is probably the hardest part, but the other parts are very easy to understand.

The paper is here:
http://www.cs.sunysb.edu/~bender/pub/lca.ps