Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Question 1. (15 points) Consider the following array {3 , 7 , 2 , 1 , 9 , 8 , 6 , 4}.

a) Illustrate the operation of the MAKEHEAP algorithm to build a max-heap. (show your steps)
1)

7 2

1 9 8 6

2) Floor of 8/2 = index 4, sift-down 1:


3

7 2

4 9 8 6

3) sift-down 2:

7 8

4 9 2 6

1
4) sift-down 7:

9 8

4 7 2 6

5) sift-down 3:

7 8

4 3 2 6

b) After part (a) delete the 3rd element (H[3]).


1) H = 9, 7, 8, 4, 3, 2, 6, 1. H[3] = 8.
2) We let H[3] = H[8]

7 1

4 3 2 6

3) Since H[8] < H[3], we sift down:

7 6

4 3 2 1

c) After part (b) insert an element with value of 20.


1) H[n+1] = 20:
9

7 6

4 3 2 1

20

2) Sift-up 20:

20
9 6

7 3 2 1

Question 2. (15 points) Give an algorithm to merge two heaps into one heap. What is the time
complexity of your algorithm? (Your algorithm must be O(n) where n = n1 + n2 and n1 is the size
of heap 1 and n2 is the size of heap 2)
 Input: Heap1 [1..n], Heap2 [1..n], i, n, Arr3.
 Output: heap3[1..(heap1.size+heap2.size)]
 i = 1, n = 1
 While i<=heap1.length
o Arr3[i] = heap1[i]
o i = i+1
 While n<= heap2.length
o Arr3[i] = heap2[n]
o n++
o i++
 Heap3 = makeAHeap(Arr3);
 The time complexity is O(n+m) for looping both heaps, and O(n) for makeAheap. Hence, the
time complexity is O(n)

Question 3. (10 points) Solve the following recurrence relations and express them in bigtheta-
notation:
i. f(n) = 6f(n – 1) for n >= 1; f(0) = 5;
 f(n) = 6f(n-1)
 f(n) = 6 * 6f(n-2)
 f(n) = 6 * … *6f(0)
 f(n) = 6 n∗5
 Time complexity = Big-Theta(6 n)

ii. f(n) = 5 f(n – 1) - 6 f(n – 2) for n >=2; f(0) = 1; f(1) =4


 Characteristic equation = x 2−5 x+ 6=( x−3 )( x−2 )=0
 r1 = 3, r2 = 2
 find c1 and c2:
o f(0) = c1(30 ) + c2(20) = c1 + c2 = 1
o f(1) = c1(31) + c2(21) = 3c1 + 2c2 = 4
o c1 = 2, c2 = -1
 f(n) = 2 ( 3 )n−1 ( 2 )n
 f(n) = 2( 3 )n −( 2 )n
 Time complexity = Big-theta(3n )
Question 4. (10 points) Use expansion method to solve the recurrence and express it in notation:
f(n) = 2f(n/2) + n log n for n >= 2; f(n) = 1 if n = 1;
 f(n) = 2f(n/2) + nlogn
n n n n
 f( ) = 2f( 2 ) + log ⁡( )
2 2 2 2
n n n
 f(n) = 2(2f( 2 ) + log ⁡( )) + nlogn
2 2 2
2 n
( )
 f(n) = 2 f 2 +nlog
2
n
2 ()
+ nlogn

n n n
( )
 f( 2 ) = 2f( 3 ) + 2 log 2
2 2 2
n
2

 f(n) = 2 ( 2 f ( )+ log ( ) ) +nlog ( ) +nlogn


2 n n n n
2 2 3
2 2
2 2

 f(n) = 2 f ( ) +n log ( )+ nlog ( ) +nlogn


3 n n n
2
3
2 22

 Based on this pattern, we can say:


i n
() n
 f(n) = 2 f i +i(n log i−1 )
2 2 ( )
 Since we want to reach f(1) = 1:
n
 i =1 => n=2i => logn = i
2
 Substituting “i” with logn we get:

 f(n) = 2
logn
( ( ))
f ( 1 ) +logn n log
n
logn−1
2
 f(n) = nf(1) + logn( n log ( 2 ))
 f(n) = n + log(2)nlogn
 Time complexity = Big-theta(nlogn)
Question 5. (10 points) Use Master’s theorem to solve the following recurrence relations and
express it in Big-theta-notation:
i. f(n) = 4f(n/2) + n2
 a = 4, b = 2, g(n) = n2
 n log 4 =n2
2

 Since n2 =Θ(nlog 4=n2) :


2

 f(n) = Θ ( n2 logn )

ii. f(n) = 𝑛.
 a=√ 2 ,b=2 , g n =√ n
( )
1
log √ 2
 n 2
= n2 = √ n
 Since g ( n ) is also √ n. By case 2:
 f(n) = Θ( √ n logn)

Question 6. (10 points) Illustrate the operation of Algorithm radixsort on the following sequence
of eight numbers: 4567, 2463, 6523, 7461, 4251, 3241, 6492, 7563. (show your steps)

Start Step 1 Step 2 Step 3 Step 4


4567 7461 6523 3241 2463
2463 4251 3241 4251 3241
6523 3241 4251 7461 4251
7461 6492 7461 2463 4567
4251 2463 2463 6492 6492
3241 6523 7563 6523 6523
6492 7563 4567 7563 7461
7563 4567 6492 4567 7563
Question 7. (5 points) Express the time complexity of Algorithm radixsort in terms of n when the
input consists of n positive integers in the interval
i. [1, n]
 There are n passes, and each cost Θ(n).
 Hence, TC = Θ ( n∗n )=Θ(n 2). .
ii. [1, n2]
 There are n passes, and each cost Θ(n 2).
 Hence, TC = Θ(n3 ).
iii. [1, 2n]
 There are n passes, and each cost Θ(2n ).
 Hence, TC = Θ(n 2n ).

Question 8. (5 points) Use Recursive Exponentiation


Algorithm to compute 57. (show your steps)
 x = 5, n = 7
 RecExp(5, 7) => m after this call = 3. This call returns y=125 2∗5=78125
 RecExp(5, 3) => m after this call = 1. This call returns y=5 2∗5=125
 RecExp(5, 1) => m after this call = 0. This call returns y=12∗5=5
 RecExp(5, 0). This call returns y = 1.
 Hence, 57 = 78125

Question 9. (20 points) Let A[1..n] be a sorted array of n integers, and x an integer. Design an O(n)
time recursive algorithm to determine whether there are two elements in A, if any, whose sum is
exactly x. Show that the running time of the algorithm is O(n) in the worst case.
 Input: Arr, x, startIndex = 1, finalIndex = n
 Output: true or false.

 matchX(Arr, x, startIndex, finalIndex)
 if startIndex == finalIndex
o return false
 if Arr[startIndex] + Arr[finalIndex] == x
o return true
 if Arr[startIndex] + Arr[finalIndex] > x
o return matchX(Arr, x, startIndex, finalIndex-1)
 return matchX(Arr, x, startIndex+1, finalIndex)
 The time complexity in the worst case will be when the sum is not in the array.
 The function matchX will be called n times, hence the time complexity is Θ(n).

You might also like