{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

midsol - Analysis of Algorithms Midterm(Solutions K...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Analysis of Algorithms - Midterm (Solutions) K. Subramani LCSEE, West Virginia University, Morgantown, WV { [email protected] } 1 Problems 1. Recurrences: Solve the following recurrences exactly or asymototically. You may assume any convenient form for n . (a) T (1) = 1 T ( n ) = T ( 3 n ) + 1 , n > 1 (b) T (1) = 0 T ( n ) = 4 T ( n 2 ) + n 2 · log n, n > 1 Solution: (a) Put n = 3 k . Accordingly, the recurrence can be restated as: T (3 0 ) = 1 T (3 k ) = T (3 k 3 ) + 1 , k > 0 Let G ( k ) denote T (3 k ) . Accordingly, the above recurrence can be represented as: G (0) = 1 G ( k ) = G ( k 3 ) + 1 , k > 0 Using one of the many techniques discussed in class (expansion, induction, the Master Theorem), it is easily seen that G ( k ) = log 3 n , from which it follows that T ( n ) = log 3 log 3 n . (b) We use the Master Theorem to solve this recurrence. As per the pattern discussed in class, a = 4 , b = 2 and f ( n ) = n 2 log n . It is clear that f ( n ) Θ( n log 2 4 log 1 n ) , from which it follows that T ( n ) Θ( n 2 log 2 n ) . 2. Binary Trees: Let T denote a proper binary tree with n internal nodes. We define E ( T ) to be the sum of the depths of all the external nodes of T ; likewise, I ( T ) is defined to be the sum of the depths of all the internal nodes of T . Prove that E ( T ) = I ( T ) + 2 · n . Solution: We use induction on the number of internal nodes in T . 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Base case: n = 1 . In this case, T consists of a root node with a left child and right child node. The root node is the only internal node and hence I ( T ) is 0 ; its two children are the only external nodes and hence E ( T ) is 1 + 1 = 2 . Since E ( T ) = I ( T ) + 2 · 1 , the conjecture is proven in the base case. Assume that if T is a proper binary tree with i internal nodes, where i k then E ( T ) = I ( T ) + 2 · i . Now consider a proper binary tree T having exactly k +1 internal nodes. Let h denote the height of this tree. Since T is proper, there are at least two external nodes, which are children of the same internal node. Splice out these external nodes to get a new tree proper binary tree T having k internal nodes (since a node that was internal in T has now become external). As per the inductive hypothesis, we must have E ( T ) = I ( T ) + 2 · k . Observe that in T two external nodes at depth h in T have been removed and one node which was internal in T at depth h - 1 has been added; hence, E ( T ) = E ( T ) - 2 · h + ( h - 1) .
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}