Be Computer Engineering Semester 4 2019 May Analysis of Algorithms Cbcgs
Be Computer Engineering Semester 4 2019 May Analysis of Algorithms Cbcgs
(May 2019)
Q.P. Code - 55801
Q1 Solve any 4
1. Derive the complexity of quick sort for best case and worst case. 5M
Best Case:
In the best case, every time we partition the array, we divide the list into two nearly equal pieces.
Partitions are balanced and it is identical to merge sort. Hence complexity of best case will be O(n
log₂ n), Let us prove it by solving recurrence equation.
T(1) = 0
Worst Case:
Worst case for quick sort occurs when the list is already sorted. To divide the list, algorithm scans
the array and determines the correct position of the pivot, so division cost of Quick Sort is linear,
i.e θ (n). In worst case, one of the sub list has size 0. And other has size (n-1).
Thus,
T (0) = 1
T (n) = T(conquer)+T(divide)+ T (combine)
= T (n-1) + n …. (1)
MUQuestionPapers.com Page 1
T(n-1) = T (n-2) + (n-1) ….(2)
After k iterations,
T(n) = T(n-k) + (n-k+1) + (n-k+2) + … + (n-1) + n
Let k = n,
=1+2+3+…+n=∑n
= n(n + 1)/2 = (n²/2) + (n/2)
T(n) = O(n²)
2. What is asymptotic analysis? Define Big O, Omega and Theta notations. 5M
Asymptotic Notation:
Asymptotic analysis are a mathematical tool to find time or space complexity of an algorithm
without implementing it in a programming language. It is a way of describing a major component
of the cost of the entire algorithm.
- Asymptotic notation does the analysis of algorithm independent of all such parameters
Big O:
Let f(n) and g(n) are two nonnegative functions indicating running time of two algorithms. We
say, g(n) is upper bound of f (n) if there exist some positive constants c and n₀ such that 0 ≤ f(n) ≤
c . g(n) for all n ≥ n₀. It is denoted as f (n) = O(g(n)).
- The Big O notation, where O stands for ‘order of’, is concerned with what happens for very
large values of n.
- The Big O notation defines upper bound for the algorithm, it means the running time of
algorithm cannot be more than its asymptotic upper bound for any random sequence of data
Big omega:
Let f(n) and g(n) are two nonnegative functions indicating running time of two algorithms. We
say, g(n) is lower bound of function f (n) if there exist some positive constants c and n₀ such that
0 ≤ c . g(n) ≤ f(n) for all n ≥ n₀. It is denoted as f (n) = Ω(g(n)).
- This notation is denoted by ‘Ω’, and it is pronounced as “Big Omega”.
- Big Omega notation defines lower bound for the algorithm.
MUQuestionPapers.com Page 2
Big Theta:
Let f(n) and g(n) are two nonnegative functions indicating running time of two algorithms. We
say, g(n) is tight bound of function f (n) if there exist some positive constants c1, c2 and n₀ such
that 0 ≤ c1. g(n) ≤ f(n) ≤c2. g(n) for all n ≥ n₀. It is denoted as f (n) = θ(g(n)).
3. Write an algorithm to find all pairs shortest path using dynamic programming. 5M
Algorithm for all pair shortest path:
𝑇𝑗 = ∑ 𝐿𝑘
𝑘=1
Mean retrieval time of n programs. It is required to store programs in an order such that their
Mean Retrieval Time is minimum.
Optimal Storage on tape is minimization problem which,
Minimize ∑𝑛𝑖=1 ∑𝑖𝑘=1 𝐿𝑘
Subjected to ∑𝑛𝑖=1 𝐿𝑖 ≤ 𝐿
- In case we have to find the permutation of the program order which minimizes the MRT after
storing all programs on single tape only.
- Greedy algorithm stores the programs on tape in nondecreasing order of their length, which
ensures the minimum MRT.
MUQuestionPapers.com Page 3
- A magnetic tape provides only sequential access of data, In an audio tape, cassette, unlike a
CD, a fifth song from the tape cannot be just directly played. The length of the first four songs
must be traversed to play the fifth song. So in order to access certain data, head of the tape
should be positioned accordingly.
5. Define master theorem. Solve the following using master method T(n) = 8T(n/2) + n² 5M
Definition: Divide and conquer strategy uses recursion. The time complexity of the recursive
program is described using recurrence. The master theorem is often suitable to find the time
complexity of recursive problems.
- Master method is used to quickly solve the recurrence of the form T(n) = a . T(n/b) + f(n).
Master method finds the solutions without substituting the values of T(n/b). In the above
equation,
n = Size of the problem
a = Number of sub problems created in recursive solution.
n/b = Size of each sub problem
f(n) = work done outside recursive call.
This includes cost of division of problem and merging of the solution.
Example:
_____________________________________________________________________________________
Q2
A) write an algorithm for finding minimum and maximum using divide and conquer. Also derive its
complexity. 10M
Algorithm: Algorithm DC_MAXMIN (A, Low, high)
If n = = 1 then
MUQuestionPapers.com Page 4
return (A[1], A[1])
else if n = = 2 then
if A[1] < A[ 2] then
return (A[1], A[2])
else
return (A[2], A[1])
else
mid ← (low + high)/2
[LMin, LMax]= DC_MAXMIN(A, low,mid)
[RMin, RMax]= DC_MAXMIN(A, mid + 1, high)
If LMax > RMax then
Max ← LMax
Else
Max ← RMax
End
If LMin < RMin then
min← LMin
else
min←RMin
end
return (min, max)
end
Complexity of algorithm:
- DC_MAXMIN does two comparisons two determine minimum and maximum element and
creates two problems of size n/2, so the recurrence can be calculated as,
0 n=1
T(n) = 1 n=2
2T(n/2) + T(n/2) + 2 n>2
MUQuestionPapers.com Page 5
B) write Kruskal’s algorithm and show it’s working by taking suitable example of graph with 5 vertices.
10M
Algorithm KRUSKAL_MST(G)
A=∅
Foreach v ∈ G . V:
MAKE-SET (v)
A = A ∪ {(u, v)}
UNION(FIND-SET(u), FIND-SET(v))
return A
Example:
Find the cost of minimal spanning tree of the given graph by using Kruskal’s Algorithm.
Solution:
- Kruskal’s algorithm sorts the edges according to decreasing order of their weight. Sorted
edges are listed in following table.
- One by one edge is added to partial solution if it is not forming the cycle.
- Initially, set of unvisited edges 𝑈𝐸 = { <1,2>, <3,5>, <1,3>, <2,3>, <3,4>, <4,5>, <2,4>}
MUQuestionPapers.com Page 6
Partial solution Updated 𝑼𝑬
𝑈𝐸 = {<3,5>, <1,3>, <2,3>, <3,4>, <4,5>,
<2,4>}
𝑈𝐸 = {<4,5>, <2,4>}
MUQuestionPapers.com Page 7
Q3
n = 6, p= (18, 5, 9, 10, 12, 7) w = (7, 2, 3, 5, 3, 2), Max sack capacity M = 13. 10M
Solution:
Now, P1 arrives but capacity of bag is only 5 and weight is 7 so we are going to take fraction
5
Maximum Profit = 28 + * 18 = 40.852
7
B) Write an algorithm for Knuth Morris Pratt (KMP) pattern matching. 10M
i) This is first linear time algorithm for string matching. It utilizes the concept of naïve approach
in some different way. This approach keeps track of matched part of pattern.
ii) Main idea of this algorithm is to avoid computation of transition function ∂ and reducing
useless shifts performed in naive approach.
iii) This algorithm builds a prefix array. This array is also called as ∏ array.
iv) Prefix array is build using prefix and suffix information of pattern.
v) This algorithm achieves the efficiency of O(m+n) which is optimal in worst case.
MUQuestionPapers.com Page 8
q = q+1
if q = = m
Print “pattern found”
q = ∏ [q]
COMPUTE_PREFIX (P)
M = P.length
Let ∏ [1……m] be a new array
∏ [1] = 0
K=0
for k = 0 to m
while k > 0 and P[k+1] ≠ T[q]
k = ∏ [k]
if P[k+1] = = T[q]
k=k+1
∏ [q] = k
return ∏
_____________________________________________________________________________________
Q4
A) Write an algorithm to solve N Queens problem. Show its working for N = 4. 10M
Place(row, column)
{
for i row -1 do
{
if (board [i] = column) then
return 0;
else if (abs(board [i] = column)) = abs(i-row) then
return 0;
}
MUQuestionPapers.com Page 9
return 1;
}
i) n-queens problem is a problem in which n-queens are placed in n*n chess board, such that
no 2 queen should attack each other.
ii) 2 queens are attacking each other if they are in same row, column or diagonal.
iii) For simplicity, State space tree is shown below. Queen 1 is placed in a 1st column in the 1st row.
All the position is closed in which queen 1 is attacking. In next level, queen 2 is placed in 3rd
Column in row 2 and all cell are crossed which are attacked by already placed 1 and 2. As can
be seen below. No place is left to place neat queen in row 3, so queen 2 backtracks to next
possible and process continue.
iv) In a similar way, if (1, 1) position is not feasible for queen 1, then algorithm backtracks and put
the first queen cell (1, 2), and repeats the procedure. For simplicity, only a few nodes are shown
below in state space tree.
v) Complete state space tree for the 4 – queen problem is shown above. 2 – queen problem is not
feasible.
vi) This is how backtracking is used to solve n-queens problems.
MUQuestionPapers.com Page 10
B) Write an algorithm to solve sum of subset problem and solve the following problem. n =4, w = {4, 5,
8, 9}, required sum = 9. 10M
Q5
A) Prove that Vertex Cover problem is NP Complete. 10M
Given that the Independent Set (IS) decision problem is N P-complete, prove that Vertex Cover (VC) is N
P-complete.
vertex cover of G = (V, E), |VC | = k I We can check in O(|E| + |V|) that VC is a vertex cover for G.
MUQuestionPapers.com Page 11
Select a known N P-complete problem.
MUQuestionPapers.com Page 12
B) Find the longest common subsequence for the following two strings.
i) The longest common sequence is the problem of finding maximum length common
subsequence from given two string A and B.
ii) Let A and B be the two string. Then B is a subsequence of A. a string of length m has
2ᵐ subsequence.
iii) This is also one type of string-matching technique. It works on brute force approach.
iv) Time complexity = O(m*n)
Algorithm LONGEST_COMMON_SUBSEQUENCE (X, Y)
// X is string of length n and Y is string of length m
for i 1 to m do
LCS [i, 0] 0
end
for j 0 to n do
LCS [0, j] 0
end
for i 1 to m do
for j 1 to n do
if Xᵢ = = Yj then
LCS [i, j] = LCS [i-1, j-1] +
else if LCS [i-1, j] ≥ LCS [i, j-1]
LCS [i, j] = LCS [i-1, j]
else
LCS [i, j] = LCS [i, j-1]
end
end
end
end
return LCS
MUQuestionPapers.com Page 13
LCS : (A,B, C,Y)
_____________________________________________________________________________________
Q6
A) Assembly Line Scheduling 10M
- Assembly line scheduling is a manufacturing problem. In automobile industries assembly lines are used
to transfer parts from one station to another station.
- Manufacturing of large items like car, trucks etc. generally undergoes through multiple stations, where
each station is responsible for assembling particular part only. Entire product be ready after it goes
through predefined n stations in sequence.
- Manufacturing of car may be done through several stages like engine fitting, coloring, light fitting ,
fixing of controlling system, gates, seats and many other things.
- The particular task is carried out at the station dedicated to that task only. Based on the requirement
there may be more than one assembly line.
- In case of two assembly lines if the load at station j at assembly 1 is very high, then components are
transfer to station j of assembly line 2 the converse is also true. This technique helps to speed ups the
manufacturing process.
- The time to transfer partial product from one station to next station on the same assembly line is
negligible. During rush factory may transfer partially completed auto from one assembly line to another,
complete the manufacturing as quickly as possible.
MUQuestionPapers.com Page 14
B) Job Sequencing with deadlines 10M
i) Job sequencing is a problem in which n jobs are arranged in such a way that we get maximum
Profit. The job should complete their task within deadlines.
ii) It is also called as Job scheduling with deadlines.
iii) Time complexity = O(n²)
Algorithm
Step1: Sort the jobs Jᵢ into non-increasing order.
Step2: Assign Empty slot as Maximum deadline value i.e. Empty slot = Dmax.
Step3: Take i index value compute value of k
K = min (Dmax, deadline(i))
Step4: If value of K is greater and equal to one check empty slot. Empty slot = k
If empty slot is full. check previous slot if it is free assign job to that slot.
If slot is full ignore that job.
Step5: increment value of i till all the jobs are not finished. Repeat Step3.
Step6: Stop.
Example
Let n=4, (J1, J2, J3, J4)=(100, 10, 15, 27), (D1, D2, D3, D4)=(2, 1, 2, 1) find feasible solution.
With job scheduling with deadlines.
Index 1 2 3 4
Job J1 J2 J3 J4
Value 100 10 15 27
Deadlines 2 1 2 1
Arrange the jobs in non-increasing value.
Index 1 2 3 4
Job J1 J4 J3 J2
Value 100 27 15 10
Deadlines 2 1 2 1
Dmax = 2
Time slot 1 2
Status Empty Empty
MUQuestionPapers.com Page 15
i=1
K= min (Dmax, Deadline(i))
K= min (2, 2)
K=2 (k ≥ 1)
Time slot = k = 2 (empty)
Time slot 1 2
Status Empty J1
i=2
K= min (Dmax, Deadline(i))
K= min (2, 1)
K=1 (k ≥ 1)
Time slot = k = 1 (empty)
Time slot 1 2
Status J4 J1
Maximum Profit = J1 + J4
= 27 + 100
= 127
- The objective of this problem is to transform the arrangement of tiles from initial arrangement to a
goal arrangement.
MUQuestionPapers.com Page 16
- These arrangements are called different states of the puzzle.
- The initial arrangement is called as initial state and goal arrangement is called as goal state.
- The state space tree for 15 puzzle is very large because there can be 16! Different
arrangements.
- In state space tree, the nodes are numbered as per the level.
- Each next move is generated based on empty slot positions.
- Edges are label according to the direction in which the empty space moves.
- The root node becomes the E – node.
- The child node 2, 3, 4 and 5 of this E – node get generated.
- Out of which node 4 becomes an E – node. For this node the live nodes 10, 11, 12 gets
generated.
- Then the node 10 becomes the E – node for which the child nodes 22 and 23 gets generated.
- We can decide which node become an E – node based on estimation formula.
- Cost estimation formula
C (x) = f (x) + G (x)
MUQuestionPapers.com Page 17
Fig. Tile positions after subsequent moves
NP:
- Non deterministic Polynomial time solving. Problem which can't be solved in polynomial time
like TSP(travelling salesman problem) or An easy example of this is subset sum: given a set of
numbers, does there exist a subset whose sum is zero?
- But NP problems are checkable in polynomial time means that given a solution of a problem ,
MUQuestionPapers.com Page 18
NP-Complete:
- The group of problems which are both in NP and NP-hard are known as NP-Complete
problem.
NP Complete
_____________________________________________________________________________________
***********
MUQuestionPapers.com Page 19