Page **1** of **1**

### Diameter of Tree - can it be done better than in O(V*(V+E))

Posted: **Sat Nov 11, 2006 12:11 pm**

by **serur**

Hi fellows!

What's the proper approach to finding Diamter of a tree?

Obvious solution: for each vertice do bfs. Is there any other way?

I read about rooting, rootfix and all that kind of thing, but what do you say?

regards,

serur

Posted: **Sat Nov 11, 2006 12:27 pm**

by **david**

This can quite easily be done in O(V). Pick a root and start a DFS from it which returns both the diameter of the subtree and its maximum height. The diameter is the maximum of (left diameter, right diameter, left height + right height).

Posted: **Sat Nov 11, 2006 12:32 pm**

by **serur**

Is this state-of-art solution? I'll try it now...

Well, can this approach solve spoj-problem Labyrinth?...

### Problem numbers in uVa

Posted: **Sat Nov 11, 2006 7:00 pm**

by **nymo**

Can someone post problem numbers in the UVA where we need to find the diameter of the graph? ... thanks.

Posted: **Thu Nov 16, 2006 7:11 pm**

by **serur**

Hi, nymo.

There is a nice problem by Abednego somewhere in 108xx as far as I can remember - about minimum diameter spanning tree. "Labyrinth" from SPOJ is pretty straightforward one.

Well, answering the question posed here -

YES, diameter of a tree can be found in O(V+E)

### Another problem :)

Posted: **Mon Nov 20, 2006 12:45 pm**

by **nymo**

Hi Serur, a straightforward problem of finding the diameter of a tree at UVa is 10308- Roads in the North. Thanks.

Posted: **Tue Nov 21, 2006 9:58 am**

by **serur**

Hi, felows!

nymo, thank you for a nice problem. So for an arbitrary tree diameter problem can be solved in O(V+E), i.e. with constant number of bfs's. My question here is: now please you fellows comment here on minimum-diameter spanning tree problem.

EDIT: to **nymo**:

problems about diameter of tree from Sphere Online judge:

- "Labyr1"

- "Benefact"