





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






Answer all Six questions. You have 90 minutes to complete the exam. The formulas you need are:
iโ=n
i=
i = n(n + 1)/ 2 . iโ=n
i=
i^2 = n(n + 1)(2n + 1)/ 6
. iโ=n
i=
ai^ = (1 โ an+1)/(1 โ a), where a 6 = 1
.
(a) Write a recursive algorithm (C++ like program) to find the largest integer less than equal to log 2 (n) of a given positive integer. For example if n = 3, the output should be 1, if n = 16, the output should be 4 and if n = 69, the output should be 6. Hint: For n > 1, the value of this function for n is one greater than for n/2.) What is the running time of your algorithm in O notation. [5 points] Solution using namespace std; #include
if (h > 0) { int x; x = (b+t)/2; mark(x,h); rulermark(b,x,h-1); rulermark(x,t,h-1); } } Show the output on the diagram below when rulermark(0,8,3) is invoked. How many times rulermark is invoked? Solution 15 times
(a) Take the following list of function and arrange them in ascending order of growth.[5points] f 1 (n) = n^2.^5 f 2 (n) =
2 n f 3 (n) = n + 10 f 4 (n) = 10n f 5 (n) = 100n f 6 (n) = N 2 log(n) Solution f2, f3, f6, f1, f4, f (b) If T (n) = log(log(n)) and f (n) = log(n)^2. prove which one of the following is true. T (n) = O(f (n)) or T (n) = ฮฉ(f (n)) or T (n) = ฮ(f (n)) [5points] Solution T (n) = O(f (n)) as log log(log(n()n 2 )) goes to zero as n goes to โ.
(a) Solve the following recurrence equation
T (n) = T (n/2) + log(n)
in terms of explicit functions of n and T (1). Assume n = 2k. [5 points] Solution T (2k) = T (2kโ^1 ) + k T (2kโ^1 ) = T (2kโ^2 ) + k โ 1 T (2^2 ) = T (1) + 2 SUmming on both sides and cancelling common terms we get T (2k) = T (1) + k(K + 1)/ 2 โ 1 which is T (n) = T (1) โ 1 + log(n) โ (log(n) + 1)/2. (b) Find the values of a and b and prove by induction hypothesis the answer. you got. iโ=n
i=
(2i โ 1)^2 = an(n^2 โ 1)/3 + bn
. [5 points] Solution
a = 4 and b = 1. Base case n=1, lhs 1. rhs =1. Assume that the Inductive hypothesis is true upto n = k; when n = K+1, the lhs is (2k+1)^2 + 4k(k^2-1)/3 + k 4k^2+4k+1 + 4k(k^2-1)/3 + k = (12k^2+12k + 4k^3-4k)/3 + K+ =(4k^3+12k^2+8k)/3 + k+1 = 4(k+1)((K+1)^2-1)/3+k+
After the numbers are inserted
10 11 8 7
4
6
Binary Searcch tree
(a) For the following Binary Search Tree, where numbers are inserted one at a time from an empty tree. Determine which of the numbers could have been inserted at the end (last step) and which of the numbers are inserted at a step before the last step. [ 5 points] Solution
int longestpathaux( BinaryNode *t) { int x, y; if (t == NULL) return 0; else x = longestpathaux(t->left); y = longestpathaux(t->right); if ( x>y) return x+(t->element); else return y + (t->element);; }