Anda di halaman 1dari 10

QUESTION ONE [Big O Notation]

(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.

Public Static int SeqSearch (object [] a, object x)


{
int i;
for (i = 0, i< a.length && !x.equals (a[i]), i++)
if (i == a.length) return –1;
else return i;
}

(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

Illustrate the operation of INSERTION-SORT on the array


A = {6,3,5,7,8}

(ii) What is a recurrence equation? [1 mark]

Fibonacci number series is 0,1, 1,2,3,5,8,13,….


The recursive definition for Fibonacci numbers is [3 marks]

Fibonacci (n) = 0 if n =0
= 1 n=1
= Fibonacci (n-1) + Fibonacci (n-2) otherwise

Write a recursive pseudocode “fib”, that gives Fibonacci value of n


as output, when n is given as the input.

(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)

Procedure MERGE (A,l,q,r)


1. i ← l
2. j ← q+1
3. k ← 0
4. while (i ≤ q) and ( j ≤ r) do
5. k ← k+1
6. if A[i] ≤ A[j] then
7. TEMP [k] ← A [i]
8. i ← i +1
9. else
10. TEMP [k] ← A [j]
11. j ← j +1
12. if j > r then
13. for t ←0 to q – i do
14. A [r-t] ← A [q-t]
15. for t ← 0 to k-1 do
16. A [l +t] ← TEMP [t +1]

(a) What is the purpose of the Temp array in the MERGE


algorithm?
(b) Why does it execute line numbers 13 and 14?

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)

Procedure 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]

(i) Represent the following graph by using an Adjacency matrix. [2 marks]

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

Given the text and pattern as follows

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

(iii) How do you overcome the problem in (ii) above? [1 mark]

(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 ]

b. Fill the table using above equations.

c. Using above table find the optimum solution.

(iii)
Calculate the number of scalar multiplications that would be
required by each of
[2 marks]
((A1 × A2) × A3)
(A1 × (A2 × A3)

Where A1 is a 4× 10 matrix, A2 is a 10 ×5 matrix and A3 is a 5 × 8


matrix.

Which is the preferred bracketing?

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

a. Draw the solution space tree for the traveling salesman


problem shown in the Figure 6.1 taking node 1 as the start
node.

b. Trace the working of a backtracking algorithm on this tree.


Clearly label the nodes in the order in which the
backtracking algorithm first reaches them. Identify the nodes
that do not get reached.

10

Anda mungkin juga menyukai