Sheet1
Page 1
Midterm solutions:
Ques 1.

There are two methods of solving this problem.
well as depth of a tree.
FindDia (root r)
Let the children of r be c_1, c_2, etc.
Run FindDia on c_1, c_2, etc, keeping track of the maximum diameter
found so far and the two largest depths found so far. Call these
Dia, Depth1 and Depth2.
Return depth 1+max(Depth1,Depth2)
Return diameter max(Dia,Depth1+Depth2+2)
Running time: At each node, we do a constant amount of work, therefore
total time is O(n).
Method 2:
1. Start with an arbitrary root and find the deepest node v using DFS/BSF
2. Start with v as the root and find the depth of the deepest node u.
3. Return this depth = d(u,v) as the diameter.
Running time: O(n) because each step takes O(n).
Ques 2.

MRS(1,n)
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '08
 MarvinSolomon
 Topology, Graph Theory, Data Structures, Topdown and bottomup design, Network topology, Return diameter max, deepest node

Click to edit the document details