You are on page 1of 3

CSC 413 Mid Semester Marking Scheme

1a. List the complexity classes in ascending order. 2marks



T(n) Name Problems
O(1)
O(logn)
O(n)
O(nlogn)
O(n
2
)
O(n
3
)
constant
logarithmic
linear
Linear-logarithmic
quadratic
cubic
Easy-solved
O(2n)
O(n!)
exponential
factorial
Hard-solved

1b. Give the formal definition for the five asymptotic notations and summarize these
asymptotic notations in a Venn diagram. 6marks
O-notation (upper bounds):
O(g(n))= { f(n) : there exist constants c>0, n
0
>0 such that 0 f(n) cg(n)for all nn
0
}
-notation (lower bounds)
(g(n))= { f(n) :there exist constants c>0, n
0
>0 such that 0 cg(n) f(n) for all nn
0
}
-notation (tight bounds)
(g(n)) = O(g(n)) (g(n))
-notation
(g(n))= { f(n) :for any constant c>0, there is a constant n
0
>0 such that 0 f(n)<cg(n)
for all nn
0
}
-notation
(g(n ))= { f(n) :for any constant c> 0, there is a constant n
0
>0 such that 0 cg(n)<f(n)
for all nn
0
}

1c. Match the following: 3marks

Worst Case Tight bounds Big
Best Case Upper bounds Big
Average case Lower bounds Big O

2a. Analyze the time complexity of the algorithm below; 3marks

sum=0; c1
for(i=0;i<N;i++) c2
for(j=N/2;j>0;j--) c3
sum++; c4

c1 + c2 x (N+1) + c3 x N x (

) + c3 x (N * N/2)

Time Complexity = N * N/2 = N
2
/2
O(N
2
)

2b. Write the recurrence equation for the Divide and Conquer algorithm below. 3marks
Hence, find its running time.
Convex Hull Algorithm
Hull(S) : Given an input of size n
(1) If |S| <= 3, then compute the convex hull by brute force in O(1)time and return.
(2) Otherwise, partition the point set S into two sets A and B, where A consists of half the points
with the lowest x coordinates and B consists of half of the points with the highest x coordinates.
(3) Recursively compute HA = Hull(A) and HB = Hull(B).
(4) Merge the two hulls into a common convex hull, H, by computing the upper and lower tangents
for HA and HB and discarding all the points lying between these two tangents.
Hint: the time to compute the upper and lower tangents is linear.

The recursive relation is
T(n) = 2T(n/2) + M(n), where M(n) is linear in n.

Using Master's Theorem

Solution: a = 2, b = 2, c = 1 a = b
c
Case 2
Hence T(n) O(n log n)

2c. Analyze the time complexity of the Tower of Hanoi algorithm given below: 3marks
Tower_Of_Hanoi(N, source, destination, helper):
If N==1:
Move disk from source to destination
Else:
Tower_Of_Hanoi (N-1, source, destination, helper)
Move disk from source to destination
Tower_Of_Hanoi (N-1, helper, destination, source)

The recursive relation is
T(n) = a if n = 1
T(n) = 2T(n - 1) + b if n > 1

= 2[2T(n 2) + b] + b = 2
2
T(n 2) + 2b + b by substituting T(n 1)
= 2
2
[2T(n 3) + b] + 2b + b = 2
3
T(n 3) + 2
2
b + 2b + b by substituting T(n-2)
= 2
3
[2T(n 4) + b] + 2
2
b + 2b + b = 2
4
T(n 4) + 2
3
b + 2
2
b + 2
1
b + 2
0
b by substituting
T(n 3) in (2)
=
= 2
k
T(n k) + b[2
k- 1
+ 2
k 2
+ . . . 2
1
+ 2
0
]

The base case is reached when n k = 1 k = n 1, we then have:

Hence T(n) O(2
n
)

You might also like