CCS 3102 Design and Analysis of Algorithms Exam
CCS 3102 Design and Analysis of Algorithms Exam
a. The development of dynamic programming algorithm can be broken into a sequence of steps.
Describe the general procedure of dynamic programming. (4marks)
b. Suppose you have two algorithms which give correct solutions to some computational problem,
what criteria will you use to select one of them. (3 marks)
d. Quicksort is claimed to have an expected running time of O(n log n), but it could at times be as
slow as O(n2). Discuss how you can fix Quicksort clearly explaining how your solution will
change the expected time from O(n2) to O(n log n). (4 marks)
h. Assuming that a tree has the preorder and inorder representations given below, reconstruct the
tree. (3 marks)
Preorder: [25, 15, 10, 4, 12, 22, 18, 24, 50, 35, 31, 44, 70, 66, 90]
Inorder: [4, 10, 12, 15, 18, 22, 24, 25, 31, 35, 44, 50, 66, 70, 90]
QUESTION TWO (20 MARKS)
a) For the following recurrence relations, give an expression for the runtime T (n) if the recurrence
can be solved with the Master Theorem. Otherwise, indicate that the Master Theorem does not
apply and justify your answer.
i. T (n) = 7T (n/2) + n/ log n (2 marks)
ii. T (n) = 0.5T (n/2) + 1/n (2 marks)
iii. Describe two limitations of recursive solutions. (2 marks)
b) When comparing algorithms we at times have to resort to empirical analysis rather than
theoretical analysis. Give three reasons why we perform empirical analysis. (3 marks)
c) A cable company wants to connect five villages to their network which currently extends to the
market town of Avonford. Using the weighted graph below compute the minimum length of
cable needed to connect all five villages. Show all working. (6 marks)
d) Given an array of 10 elements: 4, 1, 3, 2, 16, 9, 10, 14, 8, 7 build a heap structure and perform
heap sort on the tree such that it satisfies the heap property. (5 marks)
a) Suppose you are given the list of programs below each with a start time and a finish time to run
on a single processor machine with no preemption:
Program 1 2 3 4 5 6 7 8 9 10 11
Start 0 1 2 4 6 5 2 8 2 12 8
Finish 3 6 5 7 10 9 8 11 13 15 12
i. Schedule the programs using an appropriate algorithm. Clearly indicate the maximum
programs which can be successfully completed. (4 marks)
ii. State two real life applications of the algorithm used in (i) above. (2 marks)
iii. We use loop invariants to help us understand why an algorithm is correct. Explain three
things we must show about a loop invariant. (3 marks)
b) Describe the algorithm used to DELETE a node from a binary search tree. You do NOT have to
give all the details of the algorithm. Just describe the major steps in the algorithm. (4 marks)
c) Use the Hash table given below to answer the following questions.
a) Assume that we have a knapsack with max weight capacity W = 11. Our objective is to fill the
knapsack with items such that the benefit (value or profit) is maximized. The following table
contains the items along with their value and weight.
Item i 1 2 3 4 5
Value val 1 6 18 22 28
Weight wt 1 2 5 6 7
b) Suppose we want to encode a text message with the characters below occurring with the
following frequencies:
Character a e i o u s t
Frequency 10 15 12 3 4 13 1
i. Generate an optimal Huffman tree and fill the table below. (7 marks)
Character Frequency Code Code length
a 10
e 15
i 12
o 3
u 4
s 13
t 1
ii. How many bits may be required for encoding the text message? (1 mark)
iii. What makes an algorithm greedy? (2 marks)