Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

centre node in a tree

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?

like image 798
justin waugh Avatar asked Feb 20 '11 08:02

justin waugh


People also ask

How do you find the center node of a tree?

Eccentricity: The eccentricity of any vertex V in a given tree is the maximum distance between the given vertex V and any other vertex of the tree. Center: The center of a tree is the vertex having the minimum eccentricity. Hence, it means that in order to find the center we have to minimize this eccentricity.

What is a Centre of a tree?

The center of a tree is a vertex with minimal eccentricity. The eccentricity of a vertex X in a tree G is the maximum distance between the vertex X and any other vertex of the tree. The maximum eccentricity is the tree diameter.

How many centers are there in a tree?

A center of a graph is a vertex with minimal eccentricity. A graph can have an arbitrary number of centers. However, Jordan (1869) has proved that for trees, there are only two possibilities: The tree has precisely one center (centered trees).

What are nodes in a tree?

A tree is a collection of entities called nodes . Nodes are connected by edges . Each node contains a value or data , and it may or may not have a child node . The first node of the tree is called the root .


2 Answers

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.

like image 59
IVlad Avatar answered Sep 25 '22 10:09

IVlad


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.

like image 29
Salvador Dali Avatar answered Sep 25 '22 10:09

Salvador Dali