{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

MT-Sol - Midterm solutions Ques 1 There are two methods of...

This preview shows pages 1–2. Sign up to view the full content.

Sheet1 Page 1 Midterm solutions: Ques 1. ------ There are two methods of solving this problem. well as depth of a tree. Find-Dia (root r) Let the children of r be c_1, c_2, etc. Run Find-Dia 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.

{[ snackBarMessage ]}

Page1 / 2

MT-Sol - Midterm solutions Ques 1 There are two methods of...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online