PESIT Bangalore South Campus

USN
1 P E
I
S
PESIT Bangalore South Campus
Hosur road, 1km before Electronic City, Bengaluru -100
Department of Information Science and Engineering
INTERNAL ASSESSMENT TEST I
Date
: 24/02/2016
Subject & Code : Design and Analysis of Algorithms(10CS43)
Name of faculty : Prof. Sandesh.B.J, Ms.Rashma.B.M
Max Marks : 50
Section
: 4th Sem. ISE A & B
Time
: 11:30am to 1:00pm
Answer any 5 full questions
1. Suggest a general plan for analyzing the efficiency of recursive algorithms. Apply the plan to analyze the efficiency
of following algorithms:
i. Analysis of Recursive binary search using backward substitution.
ii. Analysis of merge sort by Masters theorem.
(10 Marks)
Ans:
(a) Decide on a parameter indicating an input’s size.
(b) Identify the algorithm’s basic operation.
(c) Check whether the number of times the basic op. is executed may vary on different inputs of the same size. (If it
may, the worst, average, and best cases must be investigated separately.)
(d) Set up a recurrence relation with an appropriate initial condition expressing the number of times the basic op. is
executed.
(e) Solve the recurrence (or, at the very least, establish its solution’s order of growth) by backward substitutions or
another method.
(i) Let T(N) be the time complexity of binary search on an array with N elements. In the worst
case (when the x is not found or is found at the very last step), binary search spends some constant
amount of time to compare x with the middle element, and then takes time T(N=2) to search a sub array of size N=2.
Hence, we have
Take i=k
T(N)= T(N/2k ) + K
Apply smoothness rule N=2k hence
T(N) =T(N/N) + log2 n
Time complexity is O(log2 n)
B.E 4th Semester
USN
1 P E
I
S
PESIT Bangalore South Campus
Hosur road, 1km before Electronic City, Bengaluru -100
Department of Information Science and Engineering
(ii) Recurrence relation of Merge sort is:
a=2 , b=2 , d=1
a=bd
2 = 21
Hence according masters theorem time complexity is O(N log N)
2. Consider the following algorithm:
ALGORITHM Mystery(n)
//input: A nonnegative integer n
S=0
for i =1 to n do
S = S+i*i
return S
i) What does this algorithm compute? ii) What is its basic operation? iii) How many times is the basic operation
executed? iv) Analyse the efficiency class of this algorithm?
(10 Marks)
Ans:
(i)
(ii)
(iii)
(iv)
Sum of squares of N natural numbers.
Basic operation is one addition and one multiplication.
Basic operation executes n number of times
For one operation ∑i=1 to n 1
Upperbound is n, lower bound is 1 hence:
U-L+1= n- 1+1
=n
Hence for both operation addition and multiplication n+n.
Efficiency is O(n+n)≈ O(n).
3. Explain brute force string matching algorithm for the sample
TEXT: “SHE_SELSS_SEA_SHELL_ON_SEA_SHORE” , PATTERN:”SHELL”.
(10 Marks)
Ans: outer loop runs for 15 no. of times.
Returns 15 after comparison of 15 , 16 , 17 , 18 , 19 becomes successful.
(Show detailed trace with respect to algorithm.)
Algorithm returns 15 i.e starting index of first occurrence of pattern in text.
4. i) Explain general method for divide and conquer with example.
ii)Trace the following sample using Quick sort algorithm 8,3,2,9,7,1,5,4.
Ans:
B.E 4th Semester
(7+3 Marks)
USN
1 P E
I
S
PESIT Bangalore South Campus
Hosur road, 1km before Electronic City, Bengaluru -100
Department of Information Science and Engineering
The divide-and-conquer strategy solves a problem by:
1. Breaking it into subproblems that are themselves smaller instances of the same type of
problem
2. Recursively solving these subproblems
3. Appropriately combining their answers
The real work is done piecemeal, in three different places: in the partitioning of problems
into subproblems; at the very tail end of the recursion, when the subproblems are so small
that they are solved outright; and in the gluing together of partial answers. These are held
together and coordinated by the algorithm's core recursive structure.
Any example that relates divide and conquer.
5.
Write an algorithm for fractional kanapsack problem by greedy method and explain the same for the following
sample: No. of objects N=3, capacity M=20 , profits are(p1,p2,p3)=(25,24,15), weights are (w1,w2,w3)=(18,15,10).
(10 Marks)
Ans:Explain trace in detail.
n objects, each with a weight wi > 0
a profit pi > 0
capacity of knapsack: M
Maximize
∑px
∑w x
i i
1≤i ≤ n
Subject to
i i
≤M
1≤i ≤ n
0 ≤ xi ≤ 1, 1 ≤ i ≤ n
The greedy algorithm:
Step 1: Sort pi/wi into nonincreasing order.
Step 2: Put the objects into the knapsack according
to the sorted sequence as possible as we can.
e. g.
n = 3, M = 20, (p1, p2, p3) = (25, 24, 15)
(w1, w2, w3) = (18, 15, 10)
Sol: p1/w1 = 25/18 = 1.39
p2/w2 = 24/15 = 1.6
p3/w3 = 15/10 = 1.5
Optimal solution: x1 = 0, x2 = 1, x3 = 1/2
6. Explain dijikstras algorithm with respect to this graph.
B.E 4th Semester
(10 Marks)
USN
1 P E
I
S
PESIT Bangalore South Campus
Hosur road, 1km before Electronic City, Bengaluru -100
Department of Information Science and Engineering
Edge b-e is 4, c-e=4 and e-f=2.
Show detailed trace with explanation which includes visited array, distance vector and path vector.
If a is source:
a->b cost=3
a->b->c cost=4
a->e->d cost=10
a->e cost=5
a->f cost =6
if b is source:
b->a=3
b->c=1
b->c->d=7
b->e=4
b->f=6
etc.
B.E 4th Semester