2

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 decomposition but I'm not sure if that's the way to do it. Any guidance in the right direction is appreciated.

Details:

  • The tree is NOT a binary tree. It's an undirected graph having n-1 edges, n vertices and no cycles. Just your regular tree
  • The tree has vertices numbered from 1 to n. There are n-1 undirected edges connecting n-1 pairs of these vertices
  • 'a' and 'b' are any 2 vertices numbered from 1 to n in the tree. We are to find the 'k'th node in the path from 'a' to 'b'. It is guaranteed that the value of 'k' is <= number of nodes in the path from 'a' to 'b'

A BFS applied on every query (kth node from 'a' to 'b') is not an option as the number of queries are large.

bholagabbar
  • 3,337
  • 2
  • 22
  • 46
  • Seems really trivial so probably it's not what the problem is intended to be, but, if the path is given, can't you just keep track of the nodes encountered starting from 'a' and counting them till the kth? – Matteo Di Napoli Jan 25 '16 at 14:56
  • O(log(n)) query? FYI, path is not 'given'. In a tree, there is only 1 path between any 2 pairs of vertices. – bholagabbar Jan 25 '16 at 14:57
  • Your question begins with "given a path". And yes, I'd say in a tree the path between two nodes is unique, otherwise it's not a tree. – Matteo Di Napoli Jan 25 '16 at 15:02
  • Fixed. It's tree and n-1 edges with vertices are given – bholagabbar Jan 25 '16 at 15:06
  • 1
    Is it a binary tree, a binary search tree, or a general purpose tree? Do you know where `a` and `b` are in the three? Is the tree node already defined, or do you have liberty to change it? Can you provide a sample input, and the expected output? – Fede Jan 25 '16 at 15:14
  • I've updated the question. Is it more clear? – bholagabbar Jan 25 '16 at 15:34
  • Is the question that unclear? I need to find the node at the 'kth' position in the path from node 'a' to node 'b' – bholagabbar Jan 25 '16 at 15:40
  • I just don't understand the emphasis on the kth node. In my understanding, finding the path from a to b is the difficult problem (but at the same time a much more common problem), and once you have that path, getting the kth element is rather trivial, isn't it? – tobias_k Jan 25 '16 at 15:42
  • Well that's the thing. I don't have the 'path'. Also, i'm not sure if a dfs/bfs everytime would pass since the constraints are pretty tight. – bholagabbar Jan 25 '16 at 15:43
  • The answer was what I was looking for. Also, I mentioned log(n) per query in the first comment. Thanks anyways – bholagabbar Jan 25 '16 at 15:48

1 Answers1

2

Do the lowest-common-ancestor, and keep for every node it's depth (distance to the root).

Next figure out if the k'th node is on the a to lca or lca to b part. The difference in depth is the number of nodes between them, so if depth[a] - depth[lca] > k then the node is on lca-b part.

If the node is on the a to lca part, just find the k'th ancestor of a. Should be log(N) using the LCA data you precalculated.

If the node is on the b to lca part, then you can compute k' which is the distance of the kth node from b (k' = 1 + depth[a] + depth[b] - 2*depth[lca] - k)) and then get k' ancestor of b (again easy with the the LCA data).

Overall all the lookups are logN and the other steps are constant so you do O(LogN) per query.

Sorin
  • 11,270
  • 17
  • 23