Anda di halaman 1dari 10

Design and Analysis of Algorithms 214 Final Examination 2006

QUESTION ONE [24 Marks]

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

(b) The Master theorem is defined below. [3 marks]


If T (n) = aT ( n b ) + f (n) then

 Θ(n log b a ) if f (n) = O(n log b a −ε )



T (n) = Θ(n log b a lg n) if f (n) = Θ(n log b a )
 Θ( f (n)) if f (n) = Ω(n log b a +ε ) and af ( n b ) < cf (n)

Use the above theorem to solve the following recurrence equation.

T (n) = T (2 n 3 ) + Θ(1)

(c) What is a complete binary tree? [1 mark]

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]

(g) Illustrate the operation on the following array for [4 marks]


HEAP_EXTRACT_MAX (A).
(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.)

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
_____________________________________________

QUESTION TWO [11 marks]

(a) Express the following graph using the G =(V,E) notation. [2 marks]

a b c d

e f g h

(b) Represent the above graph with adjacency matrix. [2 marks]

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

QUESTION THREE [11 Marks]

(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

Find the MCST of the graph using Kruskal’s algorithm. [5 marks]

The algorithm for the Kruskal’s algorithm is given below.


Input: An undirected graph G(V,E) with a cost function c
on edges
Output: T the minimum cost spanning tree for G
_____________________________________________________________________
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)
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
_________________________________________________________

(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

_________________________________________________________

Procedure Dijkstra's Single-source shortest path_G(V,E,u)

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

QUESTION FOUR [17 marks]

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

(ii) How do you reduce the number of spurious hits in (a)?

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

(c) Following is the Naïve-String-Matcher Algorithm, which is used [8 marks]


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 a b b a a b a a b

(i) What will be the output from this algorithm?


(ii) How many comparisons would occur in this algorithm?
(iii) Show that worst-case time complexity of the above algorithm is
O(m(n − m + 1)) where n is the number of characters in the text
and m is the number of characters in the pattern.
(iv) What is the Best-case time complexity of the above algorithm?
(v) For the above given text T give one example for the pattern P,
where you get the Best-case time complexity.

Page 7 of 10
_____________________________________
Sri Lanka Institute of Information Technology
Design and Analysis of Algorithms 214 Final Examination 2006

QUESTION FIVE [19 marks]

(a) What is meant by loss less data compression? [1 mark]

(b) What is “Entropy”? Write an equation to find the entropy for a given [2 marks]
message?

(c) Symbol A B C D E F [4 mark]


Frequencies 2 5 1 8 3 4

(d) (i) Develop a codeword for the above symbols using Shannon Fano [4 marks]
Algorithm given below.

Shannon Fano Algorithm.

1. Line up the symbols by falling probability (or frequency)


of incidence.
2. Divide the symbols in two groups, so that both groups
have equal or almost equal sum of the probabilities.
3. Assign value 0 to the first group, and value 1 to the
second.
4. For each of the both groups go to step 2.

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

Letter ASCII Value


a 01000001
b 01000010
c 01000011

QUESTION SIX [18 marks]

(a) What do you mean by overlapping sub problem? [1 mark]

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

(i) In the above recursive equation, when i < n and wi ≤ k then the
maximum profit will be given by [9 marks]

P(i, k ) = max(P(i + 1, k ), pi + P(i + 1, k − w i ))


Briefly explain each component of the equation.

(ii) Why is it called the 0/1 knapsack problem?


(iii)Draw a table for the following 0/1 knapsack problem-using
Dynamic Programming.

n = 4, c = 5, w = [ 2, 1, 3 , 2 ] and p = [ 5, 2, 7, 4 ]

(iv) Find p (3,5) and p (1,4) .

(v) Fill all the entries of the table using above equations.

(vi) Briefly explain how you achieved the final solution.

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)

Input: sequence (p0,p1,…pn)


Output: an auxiliary table m[1..n,1..n] With m [i,j]
costs and another auxiliary table s[1..n,1..n] With
records of index k which achieves optimal cost in
computing m[i,j]

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
________________________________________________________

(i) Consider the following chain of matrices whose sequence of


dimensions is <4, 3, 10, 2, 5>.
(ii) How many matrices will be represented by the above sequence
of dimensions?
(iii) Write the dimensions of the above matrices.
(iv) Draw the m and s table to find the optimal parenthesizing of the
matrices for the above sequence of matrices.
(v) Hence find the optimal parenthesizing and optimal number of
scalar multiplications of the above matrices.

Page 10 of 10
_____________________________________
Sri Lanka Institute of Information Technology

Anda mungkin juga menyukai