views:

196

answers:

5

Please suggest some algorithm to find the node in a tree whose distance to its farthest node is minimum among all the nodes.

Its is not a graph and it is not weighted.

+1  A: 

You could use Dijkstra's algorithm (http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm) on each node in turn, to find all the distances from that node to every other node; scan the resulting list to get the distance to the farthest node. Once you've Dijkstra'd every node, another scan will give you the minimum of that maximal distances.

Dijkstra is usually regarded as having runtime O(v^2), where v is the number of nodes; you'd be running it once per node, which will increase the time to O(v^3) in a naive implementation. You may be able to make gains by storing the results of earlier nodes' Dijkstra runs and using them as known values in later runs.

Chris
Floyd-Warshall is much simpler to code than Dijkstra's, but yes, it has the same complexity.
Peter Alexander
+1  A: 

You can use Johnson's algorithm for sparse graphs, but otherwise use the Floyd-Warshall algorithm simply because it is trivial to implement.

Essentially you want to find the distance from every node to every other node, and then just trivially search for the property you want.

Peter Alexander
Those algorithms are new to me, and do indeed appear simpler to code than Djikstra.
Chris
Note that FW is O(n^3) which may not be ideal.
dirkgently
Same as repeated Djikstra, for this application.
Chris
+4  A: 

Remove leaves. If more than 2 nodes left, repeat. The node (or 2 nodes) left will be the node you are looking for.

Why this works:

The node(s) are in the middle of the longest path P in the tree. Their max distance to any node is at most half of the length of the path (otherwise it would not be the longest one). Any other node on P will obviously have greater distance to the further end of P than the found node(s). Any node n not on P will have its furthest node at least (distance from n to the closest node on P, say c) + (distance from c to the further end of P), so again more than the node(s) found by the algorithm.

Rafał Dowgird
I dont know what is the root node so no idea what is the leaf node.
Sandeep
A leaf node will have exactly one edge.
Chris
this will always find the root node (asuming it has more than 1 edge, otherwise it will find the lowest branch above the root)
jk
@jk: As I understand, the edges are undirected, so there is no 'root'. This always finds the middle of the longest path in the graph. A node that has only 1 edge is a leaf, so it will be removed in the first iteration.
Rafał Dowgird
@Rafał, that should be an edit, not a comment ;-)
Pavel Shved
Yes you are right, was thrown by all the tree stuff
jk
+7  A: 

Choose an arbitrary node v in the tree T. Run BFS making v as the root of T. BFS outputs the distances from v to all the other nodes of T.

Now choose a node u that is farthest from v. Run again BFS making u as the root. On the new distance output, find a node w that is farthest from u.

Consider the path between u and w. This is the longest path in the tree T. The node in the midle of the path is the center of the tree T.

Note that there may exist two centers in a tree. If so, they are neighbours.

Performance: O(n), where n is the number of nodes of T.

Proof

Claim: a leaf (u) that is furthest from some node v lies on the longest path.

If we prove it, then the algorithm is correct, since it first finds u, and, since it's one end of the longest path, uses DFS to find this path itself.

Proof of the claim: Let's use retucto ad absurdum. Assume u---r is the longest path in the tree; and for some node v neither v---u, nor v---r is the longest path from v. Instead, the longest path is v---k. We have two cases:

a) u---r and v--k have a common node o. Then v--o--u and v--o--r are shorter than u---o---k. Then o---r is shorter than o---k. Then u---o---r is not the longest path in graph, because u---o---k is longer. It contradicts our assumption.

b) u---r and v--k don't have common nodes. But since the graph is connected, there are nodes o1 and o2 on each of these paths, such that the path between them o1--o2 doesn't contain any other nodes on these two paths. The contradiction to the assumption is the same as in point a), but with o1--o2 instead of mere o (in fact, point a is just a special case of b, where o1=o2).

This proves the claim and hence the correctness of the algorithm.

(this is proof written by Pavel Shved, and the original author might have a shorter one).

Slava
Could you divulge some reasoning for your method of finding the longest path?
Svante
A: 

As others have said in comments: A tree is a graph - an undirected connected acyclic graph to be exact - see "Tree" (Graph theory).

MadKeithV
A tree cannot be undirected, can it?
Thomas Padron-McCarthy
A tree has an implicit direction. Each node has a well-defined distance to the root node (tree not forest) and two connected nodes have distances to the root that differ by one. The node with the smaller distance to the root node is the parent; the node with the larger distance is the child. This parent-child relation in tree-shaped graphs is thus defined in pure graph terms. Furthermore, trees are acyclic graphs, so there is no other edge betwee parents and their childs. Hence, the parent-child relation of connected nodes acts as a direction of the edge.
MSalters