Google Answers Logo
View Question
 
Q: data structures and algorithms ( No Answer,   5 Comments )
Question  
Subject: data structures and algorithms
Category: Computers
Asked by: uraj-ga
List Price: $2.50
Posted: 04 Oct 2004 18:55 PDT
Expires: 03 Nov 2004 17:55 PST
Question ID: 410375
Let T be a tree with n nodes .Define the lowest common ancestor (LCA)
between two nodes v and w as the lowest node in T that has both v and
w as descendents(where we allow a node to be a descendent of
itself).Given two nodes v and w ,describe an efficient algorithm for
finding the LCA of v and w.What is the running time of your method?
Answer  
There is no answer at this time.

Comments  
Subject: Re: data structures and algorithms
From: negative1poker-ga on 07 Oct 2004 11:52 PDT
 
i know i'm not a researcher, but i have some free time...this can be
solved in O(log(n) + n) time, where n is the height of T.

First, do a search through the tree (binary search is O(log n)) for v,
and one for w.  While doing the search, add a reference of each node
visited to an array, where array[0] is the root of the tree, array [1]
is the root's child, etc.  This array business won't cost any time. 
Now you're at 2*log(n).

Now that you have the path from root to v and root to w, you just have
to compare them.  First, find which path is shorter (easy enough if
you kept track of where you're pointing in each array).  call v_len
the length of path v and w_len the length of path to w.  if v_len is
shorter, compare array_v[v_len] to array_w[v_len].  if they're equal,
good to go.  if not, just decrement v_len and compare
above...basically you're comparing, at each generation in the tree,
what node brings the path back to the root.  at the LCA, the v path
and w path will point to the same node.
Subject: Re: data structures and algorithms
From: elgogo-ga on 07 Oct 2004 14:25 PDT
 
negative1poker-ga, i do not see how you got the O(n) term...
Subject: Re: data structures and algorithms
From: negative1poker-ga on 08 Oct 2004 12:17 PDT
 
log(n) comes from the binary searches through the tree.

n comes from the linear search through the arrays.

total = O(log_n + n)
Subject: Re: data structures and algorithms
From: sebastian31415-ga on 16 Oct 2004 05:49 PDT
 
Risking that I misunderstood and restate negative1poker-ga's solution,
let me give it a try...

Consider the following algorithm (in pseudocode):

; 1. Trace u and v back to the root, recording each path in a stack.

Su := emptyStack;
while u != root(T) do
  push(Su, u);
  u := parent(u);
od;

Sv := emptyStack;
while v != root(T) do
  push(Sv, v);
  v := parent(v);
od;

; 2. Pop the stacks until the paths split.

lca := root(T);
while not empty?(Su) and not empty?(Sv) do
  x := pop(Su);
  y := pop(Sv);
  if x = y then
    lca := x;
  fi;
od;
return lca;

The running time is O(max{h(u), h(v)}), where h(x) denotes the height
of node x, i.e., the length of the path from x to the root.

The correctness of the algorithm is based on the fact that the path
from x to the root is unique: x, parent(x), parent(parent(x)), ...,
root(T). Hence, the lca(u, v) must be on both these paths, and the
tail-paths lca(u, v), parent(lca(u, v)), parent(parent(lca(u, v))),
..., root(T) are identical.

Sebastian.
Subject: Re: data structures and algorithms
From: grimmegris-ga on 19 Oct 2004 11:27 PDT
 
negative1poker suggests using binary search. Binary search assumes a
binary search tree but the question does not specify that.
Finding a node in a binary search tree would be O(log n) time only if
the tree is reasonably balanced.
And n is the number of nodes, not the height.

Sebastians algorithm seems to be correct but his runtime specification
assumes that parent() is O(1) but the question does not specify that.

The question may be too poorly specified to give a definite answer. If
T is a general tree, at least one must know which operations apply to
the tree including runtimes.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy