Data Structures Question Paper
Data Structures Question Paper
(a) The Towers of Hanoi is an old puzzle. It consists of 3 pegs, A, B and C, [6 marks]
and n discs of different sizes. Each disc has a hole in its center. Initially
all discs are on peg A and in order of decreasing size. The object is to
move all discs to peg number C.
eg. n=4
A B C
| | |
111 | |
22222 | |
3333333 | |
444444444 | |
------------------------------------
The only legal move is to transfer a single (top-most) disc from one peg
to another peg. At no time may a larger disc sit on top of a smaller disc.
Write a recursive algorithm to print a sequence of moves to solve the
puzzle. Hint: in the example above, discs 1 to 3 must be "parked"
somewhere before disc 4 can be moved to peg C.
T (n) = T (2 n 3 ) + Θ(1)
Page 1 of 10
_____________________________________
Sri Lanka Institute of Information Technology
Design and Analysis of Algorithms 214 Final Examination 2006
(d) 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)
____________________________________
____________________________________
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)
_____________________________________________
Illustrate the operations of the Heap sort for the array of elements given
below. (For the illustration processes only once assign the values to the
given algorithms codes and then you can use diagrammatic way to reach
the answer.)
5 10 6 1 24 8 18 4
(e) Obtain a recurrence equation and solve it to find the running time of the [3 marks]
HEAPIFY algorithm.
[Hint: Use the master theorem to solve the above equation.]
Page 2 of 10
_____________________________________
Sri Lanka Institute of Information Technology
Design and Analysis of Algorithms 214 Final Examination 2006
(f) What is a priority queue and what are the applications of priority queue? [2 marks]
20 8 10 5 3 9 7 1
_____________________________________
Procedure HEAP_EXTRACT_MAX(A[1…n])
1.if heap_size[A] ≥ 1
2. max ← A[1]
3. A[1] ← A[heap_size[A]]
4. heap_size[A] ← heap_size[A]-1
5. HEAPIFY(A,1)
6. return max
_____________________________________________
(a) Express the following graph using the G =(V,E) notation. [2 marks]
a b c d
e f g h
Page 3 of 10
_____________________________________
Sri Lanka Institute of Information Technology
Design and Analysis of Algorithms 214 Final Examination 2006
(c) Illustrate the operation of the BFS (Breadth First Search) on the above [5 marks]
graph taking the starting vertex as vertex “b” with clearly drawing the all
the steps. (For the illustration processes assign the values only once to
the given algorithms codes and then you can use diagrammatic way to
reach the answer.)
_______________________________________________________
BFS(G,s)
1. for each vertex u ∈ V[G]-{s}
2. do color[u] ← WHITE
3. d[u] ← ∞
4. π[u] ← NIL
5. color[s] ←GRAY
6. d[s] ←0
7. π[s] ← NIL
8. Q ← {s}
9. while Q ≠ 0
10. do u← head[Q]
11. for each v∈Adj[u]
12. do if color[v] = WHITE
13. then color[v] ←GRAY
14. d[v] = d[u] + 1
15. π[v] ← u
16. ENQUEUE(Q,v)
17. DEQUEUE(Q)
18. color[u] ← BLACK
____________________________________________
(d) Show that running time of the BFS (Breadth First Search) algorithm is [2 marks]
O(V+E) where V is the number of vertices of the graph and E is the
number of edges.
Page 4 of 10
_____________________________________
Sri Lanka Institute of Information Technology
Design and Analysis of Algorithms 214 Final Examination 2006
(a) What are the applications of the Minimum Cost Spanning Tree [1 mark]
(MCST)?
(b)
2
B E
10 1
4 11
D
A G
18 7 12
8 C F
3
(c) The algorithm for the Dijkstra’s single-source shortest path is given
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 10
_____________________________________
Sri Lanka Institute of Information Technology
Design and Analysis of Algorithms 214 Final Examination 2006
_________________________________________________________
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]
_________________________________________________________
[5 marks]
Use the above Dijkstra’s Single-source shortest path algorithm; find the
length of the shortest path from the node A to all other nodes on the
graph shown below.
40
F
B
50 10
20 20
A D
E
10 10 40 10
C G
30
Page 6 of 10
_____________________________________
Sri Lanka Institute of Information Technology
Design and Analysis of Algorithms 214 Final Examination 2006
(a) (i) Taking modulo q = 15, how many spurious hits and valid hits [5 marks]
do the Rabin-Karp matcher encounter in the text
T = 25652205 when looking for pattern P = 20?
(iii) What should be the number of spurious hits and valid hits if best-
case scenario occurs in Rabin-Karp algorithm?
(iv) What should be the number of spurious hits and valid hits if
worst-case scenario occurs in Rabin-Karp algorithm?
(b) Draw the state transition diagram for a string-matching automaton for [4 marks]
the pattern P = aaba and Take the input alphabet ∑ as {a,b}
Page 7 of 10
_____________________________________
Sri Lanka Institute of Information Technology
Design and Analysis of Algorithms 214 Final Examination 2006
(b) What is “Entropy”? Write an equation to find the entropy for a given [2 marks]
message?
(d) (i) Develop a codeword for the above symbols using Shannon Fano [4 marks]
Algorithm given below.
(ii) Construct the codeword for the above data using Huffman
Algorithm given below.
[6 marks]
__________________________________________________
Huffman (C)
1 n ← |C|
2 Q ← C
3 for i ← 1 to n-1
4 do allocate a new node z
5 left[z] ← x ← EXTRACT-MIN(Q)
6 right[z] ← y ← EXTRACT-MIN(Q)
7 f[z] ← f[x] + f[y]
8 INSERT(Q,z)
9 return EXTRACT-MIN(Q)
_____________________________________________________________
(iii) If you have used the ASCII code for coding the above symbols
how many bits are need to represent these data?
(iv) Hence find the percentages of compression achieved using
Shannon Fano Algorithm and Huffman Algorithm.
Page 8 of 10
_____________________________________
Sri Lanka Institute of Information Technology
Design and Analysis of Algorithms 214 Final Examination 2006
(e) Using the Ziv –Lempel model transfer the following message into the [2 marks]
symbol stream.
“abcabcabcaabcabcabcabc”
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
,
(i) In the above recursive equation, when i < n and wi ≤ k then the
maximum profit will be given by [9 marks]
n = 4, c = 5, w = [ 2, 1, 3 , 2 ] and p = [ 5, 2, 7, 4 ]
(v) Fill all the entries of the table using above equations.
Page 9 of 10
_____________________________________
Sri Lanka Institute of Information Technology
Design and Analysis of Algorithms 214 Final Examination 2006
(c) The following algorithm will represent the matrix chain multiplication of [8 marks]
the matrices A1 , A2 ,.......... An−1 , An .
_________________________________________________________
Procedure Matrix_Chain_Order (p)
1. n ← length[p]-1
2. for i ← 1 to n
3. do m[i,i] ← 0
4. for l ← 2 to n
5. do for i ← 1 to n-l+1
6. do j ← i+l-1
7. m[i,j] ← ∞
8. for k ← i to j-1
9. do q ← m[i,k] + m[k+1,j] + pi-1pkpj
10. if q < m[i,j]
11. then m[i,j] ← q;
12. s[i,j] ← k;
13. return m and s
________________________________________________________
Page 10 of 10
_____________________________________
Sri Lanka Institute of Information Technology