Docsity

Fall 2007 Data Structures and Algorithms Exam - Prof. Sanmay Das, Exams of Data Structures and Algorithms

Partial preview of the text

Download Fall 2007 Data Structures and Algorithms Exam - Prof. Sanmay Das and more Exams Data Structures and Algorithms in PDF only on Docsity!

Fall 2007, Test 1, Data Structures and Algorithms

Name:

Section:

Email id:

4th October, 2007

Answer all Six questions. You have 90 minutes to complete the exam. The formulas you need are:

1 Formulas

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

.

  1. Recursion and Analysis

(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 int log2(int n) { if (n<= 1) return 0; else return 1 + log2(n/2); } int main (void) { cout << log2(3) << endl; cout << log2(16) << endl; cout << log2(69) << endl; return 0; } The complexity is O(log(n)) (b) Let us consider the task of drawing the markings for each inch of a ruler: there is a mark at the 4โ€ point, slightly shorter marks at the 2โ€ intervals, still shorter marks at 1โ€ intervals etc. The following program is used to place suitable marks in a ruler (similar to the marks are made in a foot rule). The function mark(x,h) puts a vertical line of height h units at position x. [5 points] Solution void rulermark(int b,int t, int h) {

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

  1. Big Oh Notations

(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 โˆž.

  1. Recurrence Relation and Proof By Induction

(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

  1. Binary Search 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

  • Last insertion:6,
  • One Step before the last insertion 6,10,4,8: (b) Write a C++ program to compute the maximum among all the sums of entries from roots to leaves of a Binary tree. The input is root node of the Binary Tree (which has data, leftlink and rightlink). [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);; }