CS6212
Youssef

May 16, 2011

 

Homework 1

Due Date: May 25, 2011

Problem 1: (20 points)

a)     Prove that 1 + 2 + 3 + ... +n = n(n+1)/2. (Hint: By induction)
b)     Prove that 1 + x +x2 + x3 + ... +xn = (xn+1 -1)/(x-1), where x is not equal to 1.
c)     Let T(n) = 2T(n/2) + cn, T(1)=a, c is a constant, and n = 2k. Prove by induction that
        T(n) = cn log n + an.
d)     Let T(n) = 4T(n/2)+ cn, T(1)=a, c is a constant, and n=2k. Prove by induction that
        T(n) = (a+c)n2-cn.

Problem 2: (20 points)

Let X[1..n] be an array of alphabetic characters. Write a recursive algorithm that determines whether or not the string ``grade'' occurs in the array X, and if so, to return the location of the leftmost occurrence of ``grade''. Analyze the time complexity of your algorithm.
(NOTE: Throughout the course, your grade depends on both the correctness and the speed of your algorithms.)

Problem 3: (20 points)

a)      Insert 9, 7, 3, 1, 12, 8, 4, 3 (as you read them) into an originally empty min-heap. Show how the heap looks like after each insertion, both in tree form and in array form.

b)      Heapsort 9, 7, 3, 1, 12, 8, 4, 3 using the heap constructed in (a). Show how the heap looks like after each deletion, both in tree form and in array form.

Problem 4: (20 points)

Insert 17, 6, 20, 15, 13, 11, 2, 3, 4, 1,25 (as you read them) into an originally empty binary search tree. Show how the tree looks like after each insertion. Perform delete (6) on this tree step by step, showing how the tree looks like after each step.

Problem 5: (20 points)

Consider the elements 1, 2, ... , 11. Perform the following sequence of Unions (U) and Finds (F) using the path compression algorithm, show how the forest looks like after each operation, and display the PARENT array alongside each snapshot of the forest:

U(1,2) U(1,3) F(3) U(6,10) U(6,5) U(1,7) U(1,6) U(8,9) U(4,8) U(8,11) U(1,8) F(9) F(11)