Professional Documents
Culture Documents
) + 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
)