(i)
What is the RAM model? What are the problems with this model? [2marks]
(ii)
Analyze the running time of the following program fragments [3marks]
assuming a RAM model of computation. State any assumptions or
definitions you make.
for i ← 0 to n-3
print i
(iii)
Find the asymptotic growth in Big O notation for the following [4marks]
functions.
a) 6n7 + nn + 3n2 + 2n + 9
b) 2logn + 2n + 3n +8
c) 4n4 + 3n + 7
d) 3log n + 2n + 12
(iv)
The following Java program does “Sequential Search”. Give the [5marks]
best, worst & average operation counts if a.length = 200. Briefly
describe how you arrive at the answer.
(v)
Calculate the number of steps line number 3 is executed in the [6marks]
following code, and give the complexity using Big O notation.
1. for i = 1 to n
2. for j = 1 to 2n
3. K[i][j] = i* j
4. end for
5. end for
1
QUESTION TWO [Recursion, Divider and Conquer method]
(i)
Following is an algorithm for INSERTION-SORT which takes an array
A as input.
[5 marks]
INSERTION-SORT (A)
1 for j ← 2 to length [A]
2 do key ← A [j]
3 insert A [j] into the sorted sequence A [1..j-1]
4 i ← j-1
5 while i>0 and A [i] > key
6 do A [i+1] ← A [i]
7 i ← i –1
8 A [i+1] ← key
Fibonacci (n) = 0 if n =0
= 1 n=1
= Fibonacci (n-1) + Fibonacci (n-2) otherwise
(iii)
Solve the following recurrence equation using iterative substitution
method [2 marks]
T(n) = T(n-1)+ 3
T(1) = 4
2
(iv)
We have two algorithms, named as X and Y. Both are doing the
same task. Their running times are as follows
Tx(n)= 3n3 +2n2 [4 marks]
Ty(n)= 8n2
Where n is the input size.
a) Sketch the graphs of the above running time functions.
b) Hence find the value of the input size where both algorithms
will take the same running time.
c) Find the range of input sizes where X is faster than Y.
(v)
What is the divide-and-conquer method? [1 mark]
(vi)
The pseudocode for merge sort is given below
[4 marks]
Procedure MERGESORT (A,p,r)
1. if p < r
2. then q ← floor( (p + r)/2 )
3. MERGESORT (A, p, q)
4. MERGESORT (A, p, q)
5. MERGE (A, p, q, r)
3
QUESTION THREE Part (a) [Heapsort Algorithm]
(i) Heap is a Complete Binary Tree with the “heap property”. [1 mark]
What do you mean by “heap property”?
(ii)
Illustrate the heapsort operation for the heap given below using the
algorithms given. [9 marks]
7 24 19 21 14 3 10 2 13 11
Procedure HEAPSORT(A)
1. BUILD_HEAP[A]
2. for i ← length[A] down to 2
3. Exchange A[1] ↔ A[i]
4. heap_size[A] ← heap_size[A]-1;
5. HEAPIFY(A,1)
Procedure BUILD_HEAP (A)
1. heap_size[A] ← length[A]
2. for i ← length[A]/2 downto 1
3. HEAPIFY(A,i)
1. l ← LEFT_CHILD (i);
2. r ← RIGHT_CHILD (i);
3. if l ≤ heap_size[A] and A[ l ] > A[ i ]
4. then largest ← l;
5. else largest ← i;
6. if r ≤ heap_size[A] and A[r] > A[largest]
7. then largest ← r;
8. if largest ≠ i
9. then exchange A[i] ↔ A[largest]
10. HEAPIFY (A,largest)
4
QUESTION THREE Part (b) [Graph Algorithms]
13
7
A C
5
2 1
D
1
E
Figure 3.1
(ii) Find the indegree of the vertices A and C of the graph in the [1 mark]
Figure 3.1
(iii) Express the graph in the Figure3 .1 using the G = ( V , E ) notation. [2 marks]
(iv)
Perform DFS on the graph shown in Figure 3.2. Show how you use
a stack for traversing the graph. Clearly show the elements being [5 marks]
popped and pushed in each step. Take starting point as node A.
B C
D
E
Figure 3.2
5
QUESTION FOUR Part (a) [Greedy Method]
(i) [1 mark]
What is a spanning tree of a graph?
(ii) [1 mark]
What are the applications of a minimum cost spanning tree (MCST)
of a graph?
(iii)
B
9
1 3
4 C
A 7
5 4 D
2
1
3
F 1 E
Figure 4 .1
Find the shortest path, from node A to D, using the greedy method [1 mark]
for the graph shown in Figure 4 .1.
(iv)
Find the MCST of the graph shown in Figure 4.1 using Kruskal’s [3marks]
algorithm.
(v)
Using the Dijkstra’s Single-source shortest path algorithm, find the
length of the shortest path from the node A to all other nodes on the [4marks]
graph shown in Figure 4 .1. Clearly show the vertex to be marked,
distance to each vertex and the unmarked vertices in each step.
6
QUESTION FOUR Part (b) [String Searching Algorithms]
(i)
Taking modulo q = 3, how many spurious hits and valid hits do the [3 marks]
Rabin-Karp matcher encounter in the text T = 553122731 when
looking for pattern P =12?
(ii)
Construct the string-matching automaton for the pattern P = abba [4 marks]
and illustrate its operation on the text string T = abbababbab. Take
the input alphabet as {a,b}
(iii)
Following is the Naïve-String-Matcher Algorithm, which is used
to find the occurrence(s) of a pattern string within another string
or body of text.
Naïve-String-Matcher (T, P)
1. n ← length[T]
2. m ← length[P]
3. for s ← 0 to n-m
4. do if P[1..m] = T[s+1..s+m]
5. then print "Pattern occurs with shift" s
Text T Pattern P
a a b b a a b b a a b b
[3 marks]
a. What will be the output from this algorithm?
b. How many comparisons would occur in this algorithm?
7
QUESTION FIVE [Data compression algorithms]
(i)
What are two methods of data compression? [1 mark]
(ii)
What is the problem with the Variable Length Code given below?
Symbol Codeword [2 marks]
a 10
b 11
c 101
d 111
(iv)
What is Entropy? Calculate the entropy for the following message.
[5 marks]
GOOGLE
(v)
Construct the codeword for the following message using
Huffman Algorithm. [Hint: You can use either probabilities
or frequencies for calculations] [6 marks]
WWW.SLIIT.LK
(vi)
Develop a codeword for following data using Shannon Fano
Algorithm.
[5 marks]
Symbol a c d f h j l
Frequency 3 5 1 4 6 2 7
8
QUESTION SIX Part (a) [Dynamic programming]
(i)
What is meant by optimal substructure? [2 marks]
(ii)
In the 0/1 knapsack problem we define P(i,k) to be the maximum
profit possible using items i…n and remaining capacity k. We
denote the weight and profit of the ith item by wi and pi .
0 i = n & wn > k
p
P (i , k ) = n
i = n & wn ≤ k
P (i + 1, k ) i < n & wi > k
max( P (i +1, k ), pi + P (i + 1, k − wi )) i < n & wi ≤ k
[6 marks]
a. Draw a table for the following 0/1 knapsack problem-using
Dynamic Programming.
n = 4, c = 5, w = [ 1, 3, 4 , 2 ] and p = [ 4, 5, 6, 3 ]
(iii)
Calculate the number of scalar multiplications that would be
required by each of
[2 marks]
((A1 × A2) × A3)
(A1 × (A2 × A3)
9
QUESTION SIX Part (b) [Backtracking, Branch and Bound]
(i)
What are the steps in the backtracking method? [2 marks]
(ii)
40
1 2
15
20
16
14
3 30 4
[8 marks]
Figure 6.1
10