Diameter of a binary tree

The diameter of a tree is the length of the longest simple path between nodes of a binary tree. How do I use Divide and Conquer to find the diameter using JAVA?Thank
[179 byte] By [LeonLLa] at [2007-10-2 3:54:07]
# 1
Tell me more about the application you are going to usethis algorithm in.
parza at 2007-7-15 23:14:58 > top of Java-index,Other Topics,Algorithms...
# 2
http://ideas.repec.org/p/nwu/cmsems/379.html
rkippena at 2007-7-15 23:14:58 > top of Java-index,Other Topics,Algorithms...
# 3
If you just want the longest path then: http://www.cs.cmu.edu/afs/cs.cmu.edu/project/phrensy/pub/papers/LeisersonM88/node17.html
rkippena at 2007-7-15 23:14:58 > top of Java-index,Other Topics,Algorithms...
# 4
Seems a bit messy. I'd have gone for the simple approach of saying that the diameter of the tree rooted at N is either the sum of the depths of the two subtrees of N or the diameter of a subtree of N.
YAT_Archivista at 2007-7-15 23:14:58 > top of Java-index,Other Topics,Algorithms...
# 5
>Seems a bit messy.Start at node n.Find farthest node m from nUse m as new starting pointFind farthest node p from m.diameter is distance from m to p.I doubt it runs in O(lgn) time. Probably O(n) time.What is messy about that?
rkippena at 2007-7-15 23:14:58 > top of Java-index,Other Topics,Algorithms...
# 6
> What is messy about that?As originally expressed, you re-root the tree. I understood that to mean applying rolls etc to put m actually at the root. That's messy.
YAT_Archivista at 2007-7-15 23:14:58 > top of Java-index,Other Topics,Algorithms...
# 7

As long as every node has a reference to its parent, then it's easy. If you have to rebuild the tree, then yes it will be a little messy.

Can you expand on your algorithm and what the runtime would be?

Here is the outline code for the algorithm mentioned in the link.

public static void farthestFrom(Node n, Object[] ret) {

Node f = n;

stack s = new stack;

s.push(n);

int dist = 0;

int maxDist = 0;

while (!s.isEmpty()) {

Node m = (Node) s.pop();

m.visited = true;

boolean b = false;

if (m.parent != null && !m.parent.visited) {

b = true;

s.push(m);

}

for each child (c) of m {

if (!c.visited) {

b = true;

s.push(c);

}

}

if (b) {

dist++;

if (dist > maxDist) {

maxDist = dist;

f = (Node) s.peek();

}

}

else {

dist--;

}

}

ret[0] = f;

ret[1] = new Integer(maxDist);

}

Tree tree = ...;

Object[] ret = new Object[2];

farthestFrom(tree.root, ret);

farthestFrom((Node) ret[0], ret);

System.out.println("diameter: " + ret[1]);

rkippena at 2007-7-15 23:14:58 > top of Java-index,Other Topics,Algorithms...
# 8
sorry that code incorrectly counts the distance
rkippena at 2007-7-15 23:14:58 > top of Java-index,Other Topics,Algorithms...
# 9

> Can you expand on your algorithm and what the runtime would be?

(int, int) findDiameterInner(Node n)

{

if (n == null) return (0, 0)

(a, b) = findDiameterInner(n.leftChild);

(c, d) = findDiameterInner(n.rightChild);

depth = max(a, c) + 1;

dia = max(b, d, a + c);

return (depth, dia);

}

int findDiameter(Node root)

{

(depth, dia) = findDiameterInner(root);

return dia;

}

Time is O(n), because findDiameterInner is called once on each node and, other than recursive calls, takes constant time.

YAT_Archivista at 2007-7-15 23:14:58 > top of Java-index,Other Topics,Algorithms...
# 10
That looks good. If it wasn't a binary tree, then the diameter computation would have to be modified to add the maximum depths I think.Maybe the algorithm that reroots the tree is used to find the actual path, not just the diameter.
rkippena at 2007-7-15 23:14:58 > top of Java-index,Other Topics,Algorithms...
# 11

> If it wasn't a binary tree, then the diameter computation would have to be modified to add the

> maximum depths I think.

I concur.

> Maybe the algorithm that reroots the tree is used to find the actual path, not just the diameter.

I could modify mine to do that by passing back more information. Precisely what information depends on whether nodes have references to their parents or not, but it would still be O(n) overall.

I do wonder whether the rerooting algorithm might work for general graphs, but I'm not going to try to prove or disprove it.

YAT_Archivista at 2007-7-15 23:14:58 > top of Java-index,Other Topics,Algorithms...