13

Given a tree, how to find the centre node in the tree so that the distance from the central node to other nodes is minimum(assuming each edge has unit weight)? I am trying to use DFS but is it possible to do it in linear time?

justin waugh
  • 885
  • 3
  • 12
  • 22
  • So each node has a parent attribute which allows it to point back to its predecessor? – Argote Feb 20 '11 at 10:22
  • Also, how do you calculate this distance? Is it the sum of the distance from central node C to every other node in the tree? Or are you looking to minimize the distance from the central node C to whichever node is furthest away? – Argote Feb 20 '11 at 10:28
  • @Argote - if it's the first, then you can always output the root, can't you? Or any node for that matter. – IVlad Feb 20 '11 at 11:24

2 Answers2

23

Keep removing leaf nodes from your tree until you are left with a single node (if left with two nodes, remove any one of them). That node minimizes the maximum distance from it to every other node.

Example:

   *                 *              
  / \                 \
 *   *                 *              *
      \                 \              \
       *      =>         *     =>       *    =>   *
        \                 \                     
         *                 *
          \
           *

To implement this in linear time, insert all initial leaf nodes in a FIFO queue. For each node, also store the number of its children. When removing an element from your queue, decrease its parent's number of children. If this number becomes zero, insert the parent into the queue.

IVlad
  • 43,099
  • 13
  • 111
  • 179
  • 1
    @IVlad- Do you have a proof that this works correctly? Doesn't this depend on where you root the tree? I'd be thrilled if this works, but right now I have no reason to believe that it does. – templatetypedef Feb 20 '11 at 11:36
  • 5
    @templatetypedef - the question (admittedly in my interpretation, because it is kind of badly worded) asks for the center of a tree. This is a classic algorithm. The tree need not be rooted. Just consider it a graph where you keep eliminating vertices of degree 1 and decrease their parent's degree after removal. Consider the diameter (longest path) of the tree. Then it's obvious that the center of tree is the median element of this path. Since this path is between two leafs, by continuing to remove leaf nodes, we will eventually be left with this median element. – IVlad Feb 20 '11 at 11:47
  • Also see this: http://books.google.com/books?id=7XUSn0IKQEgC&pg=PA518&lpg=PA518&dq=center+of+tree+algorithm&source=bl&ots=z6eKMOIR-f&sig=R1UvBCV-gAm1fdcBfjwzKz4qC_0&hl=en&ei=mv9gTeq5N8f1sgbYi_W1CA&sa=X&oi=book_result&ct=result&resnum=1&ved=0CBQQ6AEwADgU#v=onepage&q=center%20of%20tree%20algorithm&f=false - an excerpt from The algorithm design manual that proposes the same algorithm. – IVlad Feb 20 '11 at 11:49
  • Agree with IVlad. This is a classic and leaf removal is one of the well known algorithms for finding the center of the tree. –  Feb 20 '11 at 16:11
  • @IVlad thanks, actually the centre is one of the node of the tree. Actually its a k-centre problem where k=1 in case of a tree. – justin waugh Feb 21 '11 at 04:08
  • @Argote actually the centre is one of the node of the tree such that the distance between any two nodes is minimized. Actually its a k-centre problem where k=1 in tree. – justin waugh Feb 21 '11 at 04:10
  • 1
    @user1071138 - yes, use a min-heap to always remove the right leaf node. – IVlad Nov 24 '13 at 19:44
  • @user1071138 - you repeat this until you have one node left in the tree, so `O(n log n)`. – IVlad Nov 24 '13 at 20:22
14

Here is another approach that also runs in O(V).

Select any vertex v1 on your tree. Run BFS from this vertex. The last vertex (v2) you will proceed will be the furthest vertex from v1. Now run another BFS, this time from vertex v2 and get the last vertex v3.

The path from v2 to v3 is the diameter of the tree and your center lies somewhere on it. More precisely it lies in the middle of it. If the path has 2n + 1 points, there will be only 1 center (in the position n + 1). If the path has 2n points, there will be two centers at the positions n and n + 1.

You only use 2 BFS calls which runs in 2 * O(V) time.

Salvador Dali
  • 214,103
  • 147
  • 703
  • 753