Sie sind auf Seite 1von 11

Question (1) [20 marks]

(i) Analyze the running time of the following program fragment assuming the RAM
model of computation. State any assumptions you make. [2 marks]

n ← 100
while n >95 do
n ← n - 1
print n

(i). There are two sorting algorithms A and B with the time complexities O(n2) and
O(n log2 n) respectively. Can you say that the algorithm B is always better than the
algorithm A for any value of n? Justify your answer using the idea of Big O notation.
[2 marks]

n
(ii). Show that is O(n2) [2 marks]
∑ 2 i+ 3
i=1

(iii). Calculate the no of steps of the line number 3 is executed in the following code, and
give the complexity using Big O notation. [2 marks]

1. for i = 1 to n
2. for j = 1 to i
3. K[i][j] = i* j
4. end for
5. end for

(v). Following Java program does “Sequential Search”. When do the best & worst case
occur if a.length = 100. [2 marks]

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

1
(vi). Which of the followings are not heaps. Give exact reason for your conclusion. The
numbers beside the nodes are the order of filling of nodes. [2 marks]

a) b) 1 c) 1 d) 1
1 12
20
12
18 4
3 2
2 10 2 3
13
8 5 10 13
3
12

(vii). List the two methods, which are used to represent a graph?
Using any of the method you like, represent the following graph. [2 marks]

A
B
C
D

(viii). A cashier wants to give the balance using fewer no of coins. Currently he has
50cts, 20cts, 10cts, 5cts, 2cts and 1cts coins (each coin has any no of amount). What
would be the greedy method solution if he wants to give 69 cts? Does this yield the
optimal solution?
Give an example where using greedy strategy would result in a worst solution.
[2 marks]

(ix). List and briefly describe four real world applications where the String searching
and matching is used. [2 marks]

(x). List the four algorithm design methods. What are the characteristics of the problem
we have to consider, when we apply each design method? [2 marks]

Contd.

2
Question (2)[20 marks]
Q2-Part (a)

(i).Why we use recursive algorithms instead of iterative algorithms? [1 mark]

(ii).The running time of a recursive algorithm is given by T(n) = T(n/2) + c ( c is a


constant value). Draw the recursion tree for this recurrence equation. What is the
depth of this tree? Hence determine a good asymptotic upper bound on this
recurrence. (Assume that T(1) takes constant amount of time) [3 marks]

(iii).Use the iterative substitution method to solve the recurrence equation


T(n) = 2T(n-1) + Θ(1) and determine a good asymptotic upper bound on this
recurrence. [3 marks]

(iv).Following is the recursive algorithm that is used to calculate the value of the
Fibonacci number.( Fibonacci number series is 0,1, 1,2,3,5,8,13,….)

int fib(int n)
{
1. if (n <= 2) return 1
2. else return fib(n-1) + fib(n-2)
}

Obtain a recurrence equation for the time to calculate the Fibonacci value of integer n.
Assume that line 1 takes constant amount of time to execute. [3 marks]

Q2-Part (b)

(i). List and briefly describe the three steps in the Divide and conquer method [2 marks]

(ii). You are given the required algorithms for this question.
The following procedure is the implementation of QuickSort

Input: Unsorted Array (A,p,r)


Output: Sorted subarray A(1..r)

Procedure QUICKSORT (A,p,r)


1. if p < r
2. then q ← PARTITION(A,p,r)
3. QUICKSORT (A,p,q)
4. QUICKSORT (A,q+1,r)
QuickSort

3
Input : Array A(p .. r)
Output : A and q such that A[i] ≤ A[q] for all i ≤ q and A[j] > A[q] for all j > q.

Procedure PARTITION(A,p,r)
1 x ← A[p]
2 i ← p-1
3 j ← r +1
4 while TRUE
5 do repeat j ← j-1
6 until A[j] ≤ x
7 repeat i ← i + 1
8 until A[i] ≥ x
9 if i < j
10 then exchange A[i] ↔ A[j]
11 else return j
PARTITION

You are given the following array as the input to the procedure QUICKSORT

1 2 3 4 5 6 7 8 9 Indexes of
Input: A = 8 4 9 1 7 2 15 3 10 the array A

(a) Showing the values assigning to variables (i.e.p,r,x,i,j) at each step ,show the
configuration of the array after executing the line 2 of the procedure QUICKSORT.
What will be the value assigning to variable q in the procedure QUICKSORT in the
first run? [5 marks]

(b) What is the worst case running time of the QUICKSORT algorithm? [1 mark]

(iii). List two similarities and two differences of QUICKSORT algorithm with
MERGESORT algorithm.
[2 marks]

Contd.

4
Question (3) [20 marks]
Q3 - Part (a)

(i). What is the height of a heap with n nodes? [1 mark]

(ii). You are given the required algorithms for this question

Input: An array A and index i to the array. Subtrees rooted at LEFT_CHILD(i)


and RIGHT_CHILD(i) are heaps
Output: The elements of array A forming subtree rooted at i satisfy the heap
property.

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

(a) Obtain a recurrence equation and solve it to find the running time of the HEAPIFY
algorithm. Assume that the steps 1..9 takes constant amount of time. [2 marks]

(b) Check whether it is possible to execute HEAPIFY(A,1),if the array A is:

14 15 12 2 7 15 2 18

If possible illustrate the HEAPIFY algorithm on this array. [2 marks]

(iii). You are required to sort the following unsorted array using the HEAPSORT algorithm.
4 1 12 6 7 15 2 10
The required algorithms are given in the next page.

(a) Clearly showing all the steps, draw the configuration of the elements in the array
after executing the step 1(i.e.BUILD_HEAP) of the HEAPSORT algorithm [3 marks]

5
(b) Clearly show the step-by-step configuration of the array after executing steps 2- 5 of
the HEAPSORT algorithm. [2 marks]

Input : Array A[1…n], n = length[A] Input : An array A of size n = length [A], heap_size[A]
Output : Sorted array A[1…n] Output : A heap of size n

Procedure HEAPSORT(A) Procedure BUILD_HEAP (A)


1. BUILD_HEAP[A] 1. heap_size[A] ← length[A]
2. for i ← length[A] downto 2 2. for i ← length[A]/2 downto 1
3. Exchange A[1] ↔ A[i] 3. do HEAPIFY(A, i)
4. heap_size[A] ← heap_size[A]-1
5. HEAPIFY(A,1)
HEAPSORT BUILD_HEAP

Q3 - Part (b)

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

A
B

(ii). Do the BFS for the graph shown in Figure 3.1. Show how you use queue for
traversing the graph. Clearly show the elements enquing and dequing in each step.
Take starting point as node A. [4 marks]

B D
C

E
F G

Figure 3.1

(iii). Do the DFS for the graph shown in Figure 3.1. Show how you use stack for
traversing the graph. Clearly show the elements popping and pushing in each step.
Take starting point as node A. [4 marks]
Contd.

6
Question (4) [20 marks]
Q4 - Part (a)

(i).Find the MCST of the graph shown in Figure 4.1 using:

(a) Kruskal’s algorithm. Clearly show the disjoint sets in each step. [3 marks]

(b) Prim’s algorithm. Choosing the vertex C as start vertex, clearly show how the
MCST is building step by step. [3 marks]

2 C

4 B 2
3
8
9
A F
7
1
5
E D
6

Figure 4.1

(ii). 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 graph shown in Figure 4.2.
Clearly show the Vertex to be marked, Distance to each vertex and the unmarked
vertices in each step [4 marks]

3
B G
13 3
2
7
A C
3
5 4
2
1

F 1 E

Figure 4.2

7
Q4 - Part (b)

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

Naïve-String-Matcher

Given the text and pattern as follows

text T pattern P
a a b c a a b c a a b c

(a) What will be the output from this algorithm? [1 mark]

(b) How many comparisons would occur in this algorithm? [2 marks]

(ii). Taking modulo q = 13, how many spurious hits does the Rabin-Karp matcher
encounter in the text T = 3279275358158 when looking for pattern P =275?
[3 marks]

(iii). Construct the string-matching automaton for the pattern P = acba and illustrate its
operation on the text string T = aabbacbacbabba. Take the input alphabet as {a,b,c}
[4 marks]

Contd.

8
Question (5) [20 marks]

(i) What is data compression? [1 mark]

(ii) List and briefly describe the areas where data compression is used? [1 mark]

(iii) What are the two methods of data compression? Explain. [2 marks]

(iv) What is Entropy? Calculate the entropy for the following message. [3 marks]

SLIIT.LK

(v) (a) Develop a codeword for following data using Shannon Fano Algorithm.
[3 marks]

Symbol A T H S E Z B
Frequency 12 8 5 14 5 1 2

(b) If you use the Fixed Length Coding, how many bits are need to represent
these data? Hence find the percentage of compression achieved using
Shannon Fano Algorithm. [2 marks]

(vi) Construct the codeword for the following message using Huffman
Algorithm. [Hint: You can use either probabilities or frequencies for
calculations]
[4 marks]

DAA@SLIIT.LK

(vii) Using the Ziv –Lempel model transfer the following message into the symbol
stream. [4 marks]

“ZXXYTZXRYTZXR”

Letter ASCII Value


X 01111000
Y 01111001
T 01010101
Z 01111010
R 01010011

9
Question( 6) [20 marks]

Q6 - Part (a)

(i) What are the elements of dynamic programming? Explain each of them.[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. Where wi is the weight of ith item.

0 i = n & wn > k

 pn i = n & wn ≤ k
P(i, k ) = 
 P(i + 1, k ) i < n & wi > k
max(P(i +1, k ), Pi + P(i + 1, k − wi )) i < n & wi ≤ k

(a) Draw a table for the following 0/1 knapsack problem-using Dynamic
Programming. [2 marks]
n = 4 c = 4 w = [ 2, 1, 3 , 2 ] p = [ 2, 6, 5, 4 ]

(b) Fill the table using above equations. [2 marks]

(c) Using above table find the optimum solution. [2 marks]

(iv) Calculate the number of scalar multiplications that would be required by each of
((A1 × A2) × A3)
(A1 × (A2 × A3))
where A1 is a 8× 3 matrix, A2 is a 3 ×4 matrix, A3 is a 4 × 10 matrix.
Which is the preferred bracketing? [2 marks]

Contd.

10
Q6 - Part (b)

(i). What are the steps in backtracking method? [2 marks]

(ii). Compare and contrast the backtracking with branch and bound [2 marks]

(iii). 40
1 2
15
20
16
14

3 30 4

Figure 6.1

(a) Draw the solution space tree for the traveling salesman problem shown in the
Figure6.1 taking node1 as the start node. [2 mark]

(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. [3marks]

(iv). What is the data structure that is used for least cost branch-and-bound search?
[1 mark]

End of Question Paper

11

Das könnte Ihnen auch gefallen