DAA Final Examination 2005en
DAA Final Examination 2005en
DAA Final Examination 2005en
(i) Find the number of executions of line number 4 for the fragment of [2 marks]
code given below.
1. for i = 1 to n
2. for j = 1 to n2
3. for k = 1 to n3
4. k[i][j] [k] = i* j*k
5. end for
6. end for
7. end for
(ii) Analyze the running time of the following program fragment assuming a [3 marks]
RAM model of computation.
for i ← n -1 down to 0
print i
(iv) Write an recursive algorithm in psedocode that computes the number [6 marks]
of 1's in the binary representation of a non-negative integer n.
[Hint: For example if n = 15 your program should return 4 since the
binary representation of 15 is 1111.]
PART (a)
Page 1 of 9
_____________________________________
Sri Lanka Institute of Information Technology
Design and Analysis of Algorithms 214 Final Examination 2005
(iii) The equation below represent the Ackermann’s function. Where i and [5 marks]
j are integers.
2j i = 1 and j ≥ 1
A(i, j ) = A(i − 1,2) i ≥ 2 and j = 1
A(i − 1, A(i, j − 1)) i, j ≥ 2
PART (b)
(i) What are the advantages of Divide & Conquer method? [1 mark]
Page 2 of 9
_____________________________________
Sri Lanka Institute of Information Technology
Design and Analysis of Algorithms 214 Final Examination 2005
A
3 1 8 4 5 4 9
PART(c)
(i) Find the height of a complete binary tree, which contains n number of [1 mark]
nodes.
(ii) The following are the algorithms for Heap sort, Build Heap and Heapify. [5 marks]
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)
1. heap_size[A] ← length[A]
2. for i ← length[A]/2 downto 1
3. HEAPIFY(A,i)
Page 3 of 9
_____________________________________
Sri Lanka Institute of Information Technology
Design and Analysis of Algorithms 214 Final Examination 2005
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)
Illustrate the operations of the Heap sort for the heap given below.
9 8
5 4 6 7
PART (a)
(i) Draw the graph that is represented by Adjacency matrix given below. [1 mark]
0 1 0 1
0 0 1 1
1 1 0 0
1 1 1 0
Page 4 of 9
_____________________________________
Sri Lanka Institute of Information Technology
Design and Analysis of Algorithms 214 Final Examination 2005
(iv) Briefly explain with an example how the traversing is done in a graph [3 marks]
with Depth First Searching (DFS) and Breadth First Searching (BFS)
methods and what data structure should be used for each searching
methods?
PART (b)
10 D 40
A
30
25 C F
8
(iii) The algorithm for the Kruskal’s algorithm is given below. [5 marks]
1 T ← {};
2 Sort the edges of E in non decreasing order of weight c
3. for each edge in E do
4. choose first edge in E, say (v,w) // has lowest cost
5. delete (v,w) from E
6. if v and w are in disjoint sets W1 and W2 do
7. W1 = W1 ∪ W2;
8. T ← T ∪ (v,w);
9. return T
(iv) The algorithm for the Dijkstra’s single-source shortest path is given [6 marks]
below.
Input: G =(V,E), the weighted directed graph and u the source vertex
Output : for each vertex, v, d[v] is the length of the shortest path
from u to v.
Page 5 of 9
_____________________________________
Sri Lanka Institute of Information Technology
Design and Analysis of Algorithms 214 Final Examination 2005
1. mark vertex u;
2. d[u] ← 0;
3. for each unmarked vertex v ∈ V do
4. if edge (u,v) exists d [v] ← weight (u,v);
5. else d[v] ← ∞ ;
6. while there exists an unmarked vertex do
7. let v be an unmarked vertex such that d[v] is
minimal;
8. mark vertex v;
9. for all edges (v,x) such that x is unmarked do
10. if d[x] > d[v] + weight[v,x] then
11. d[x] ← d[v] + weight[v,x]
C 55
(i) a) Taking modulo q = 12, how many spurious hits and valid hits do [5 marks]
the Rabin-Karp matcher encounter in the text T = 22531491349
when looking for pattern P = 49?
c) What should be the number of spurious hits and valid hits if best-
case scenario occurs in Rabin-Karp algorithm?
Page 6 of 9
_____________________________________
Sri Lanka Institute of Information Technology
Design and Analysis of Algorithms 214 Final Examination 2005
(ii) Draw the state transition diagram for a string-matching automaton for [5 marks]
the pattern P = bbaba and illustrate its operation on the text string T =
aabbabaababba. Take the input alphabet ∑ as {a,b}
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
(ii) What is “Entropy”? Write an equation to find the entropy for a given [2 marks]
message?
(iii) Construct the codeword for the following message using Huffman [4 mark]
Algorithm.
[Hint: You may use either probabilities or frequencies for calculations]
beabebaeccdcece
Page 7 of 9
_____________________________________
Sri Lanka Institute of Information Technology
Design and Analysis of Algorithms 214 Final Examination 2005
(iv) a) Develop a codeword for following data using Shannon Fano [8 marks]
Algorithm.
Symbol A B C D E F
Frequencies 8 5 1 3 6 2
PART (a)
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
n = 4, c = 5, w = [ 1, 3, 4 , 2 ] and p = [ 4, 5, 6, 3 ]
Page 8 of 9
_____________________________________
Sri Lanka Institute of Information Technology
Design and Analysis of Algorithms 214 Final Examination 2005
0 if i = j
m[i, j ] = min
i ≤ k < j {m[i, k ] + m[k + 1, j ]} + pi −1 pk p j otherwise
Matrix Dimensions
A1 20 × 10
A2 10 × 5
A3 5× 4
A4 4×8
PART (b)
(ii) What is the difference between backtracking and branch and bound [1 mark]
method?
(iii) The following is a 0/1 Knapsack problem. [3 marks]
n = 4, c = 5, w = [ 1, 3, 4 , 2 ] and p = [ 4, 5, 6, 3 ]
Page 9 of 9
_____________________________________
Sri Lanka Institute of Information Technology