CS502 - Fundamental of Algorithm Short-Question Answer: Ref: Handouts Page No. 40
CS502 - Fundamental of Algorithm Short-Question Answer: Ref: Handouts Page No. 40
CS502 - Fundamental of Algorithm Short-Question Answer: Ref: Handouts Page No. 40
QNo.2 Quick sort such that sort the array in to non-increasing order? (Mark2)
Answer:-
Quick sorting, an array A[1..n] of n numbers We are to reorder these elements into
increasing (or decreasing) order. More generally, A is an array of objects and we sort
them based on one of the attributes - the key value. The key value need not be a
number. It can be any object from a totally ordered domain. Totally ordered domain
means that for any two elements of the domain, x and y, either x < y, x = y or x > y.
Ref: Handouts Page no. 40
QNo.3 Draw the cost table for chain multiplication problem with initial states(Mark3)
Answer:-
(A1)(A2A3A4 . . .An)
or (A1A2)(A3A4 . . .An)
or (A1A2A3)(A4 . . .An)
......
or (A1A2A3A4 . . .An−1)(An)
Ref: Handouts Page no. 90
Worst case for edit distance algorithm? What is the simple change that can change the
worst case time ? 5 marks
Answer:-
Analysis of DP edit distance
There are (n ) entries in the matrix. Each entry E(i,j) takes (1) time to compute. The
2
total running is (n ) Recursion clearly leads to the same repetitive call pattern that we
2
saw in Fibonnaci sequence. To avoid this, we will use the DP approach. We will build the
solutionb bottom-up. We will use the base case E(0,j) to fill first row and the base case
E(I,0) to fill first column. We will fill the remaining E matrix row by row.
Describe an efficient algorithm to find the median of a set of 106 integers; it is known
that there are fewer than 100 distinct integers in the set
Solution:-
Step1:Start
Step2:Find the 100 distinct numbers among 10^6 integers.
Step3:Sort the 100 distinct numbers
Step4:Count the distinct numbers
Step5: if count is odd,middle number is the median
Step6:if count is even,add the middle two numbers then divide by 2,the result is the
median Step5:End
number.
we have the values of n in one column and the values of C_n in another, then to put this
formula in Excel, on the (n+1)-th row just replace C_n and n with the appropriate cells
from the previous row.
Ref: Handouts Page no. 85
Formulate problem recursively. Write down a formula for the whole problem
as a simple
combination of answers to smaller sub problems.
Build solution to recurrence from bottom up. Write an algorithm that starts
with base cases and works its way up to the final solution.
8 else V[i,w]V[i-1,w]
The time complexity is clearly O(n,W), it must be cautioned that as n and W get large,
both time and space complexity become significant.
Ref: Handouts Page no. 96
Differentiate b/w Bubble sort, insertion sort and selection sort? 3 marks
SOLUTION:
Bubble sort: scan the array. Whenever two consecutive items are found that are out of
order, swap them. Repeat until all consecutive items are in order.
Insertion sort: assume that A*1…i-1] have already been sorted. Insert A[i] into its proper
position in this sub array. Create this position by shifting all larger elements to the right.
Selection sort:
Assume that A[1..i-1] contain the i-1 smallest elements in sorted order. Find the smallest
in A*i….n+ swap it with A*i+.
Ref: Handouts Page no. 54
2012
What are the two steps generally involved while developing dynamic programming
algorithm. (2)
Solution:
Developing a dynamic programming algorithm generally involves two separate steps:
• Formulate problem recursively.
Write down a formula for the whole problem as a simple
combination of answers to smaller subproblems.
• Build solution to recurrence from bottom up.
Write an algorithm that starts with base cases and works its way up to the final solution.
Ref: Handouts Page no. 75
What are the applications of edit distance technique? Name any three (3)
Solution:
Spelling Correction
Plagiarism Detection
Computational Molecular Biology
Ref: Handouts Page no. 76
What is the worst case running time for the bucket sort? What simple change is
required in the algorithm to preserve its linear expected running time and makes it
worst case time Θ(n log n) (5)
Solution:
The worst case for bucket sort occurs when the all inputs falls into single bucket,
for example. Since we use insertion sort for sorting buckets and insertion sort
has a worst case of O(n2). the worst case runtime for bucket sort is O(n2).
By using an algorithm with worst case runtime of O(nlgn) instead of insertion sort for
sorting buckets, we can ensure that worst case is O(nlgn) without affecting the average
case behavior.
To see that the worst case is still O(nlgn), lets consider a case where n data are
distributed among two buckets, a elements in one bucket and na in the other. Since we
use O(nlgn sorting algorithm in each bucket, the run time for each sort is, kalg(a) + c2
and k(n a)lg(n a) + c2, where k; c2 are positive constants. The total runtime is
kalg(a)+k(na)lg(na)+2c2 This quantity attains its maximum value when a = 0 or a = n and
the maximum value is knlgn + 2c2. Thus total runtime is still O(nlgn). It is clear from
this that maximum running cost occurs when data are in single bucket instead
of spread in two buckets. Extending this argument, we can see that worst case
for the hash table occurs when all inputs hash into the same bucket. (We also
note that the expressions obtained are basically convex combinations of nlgn
which is a convex function and then jensen's rule can be applied to arrive at the
same conclusion).
Ref: http://classes.soe.ucsc.edu/cmps101/Spring11/hw/hw3sol.pdf
Given an unordered list of n x0, x1, x2, …, xn and elements is common, if there are
atleast n/5 copies of it.We want to identify all the common numbers in our list. Give
O(n log n) to solve the problem. (5)
Solution:
We define R n to be the set of ordered n-tuples of real numbers,
Rn := {(x1, x2 . . . xn) : x1, . . . , xn ∈ R}.The elements of Rn are called vectors. Given a
vector x = (x1, . . . , xn), the numbers x1, . . . , xn are called the components of x.
You are already quite familiar with Rnfor small values of n.
Ref: http://www.math.rice.edu/~hassett/teaching/221fall05/linalg1.pdf
Find the out cost A1=5 x 4 A2= 4 x 6 A3= 6x2 (2 marks)
Solution:-
For Instance
A1. = 5 x 4
A2 = 4 x 6
A3 = 6 X 2
And A4 = 2 x 7
Hence
A1 x (A2 A3) XA4 = ((5 X 4 X 2 ) + (4 X 6 2)) + 2 X 7 X 5
= 40 + 48 + 70
= 88 + 70
= 158
HERE OPTIMAL SEQUENCE IS A1 (A2 A3) A4. For this cost 158 which is optimal the
optimal sequence is a1x (a2 xa3) xa4
REF:: http://books.google.com.pk/books?id=9mBJ0CpKXdkC&pg=SA11-PA37&lpg=SA11-
PA37&dq=Find+the+out+cost+A1%3D5+x+4++A2%3D+4+x+6++A3%3D+6x2&source=bl&
ots=xYZLnJv6zx&sig=C_u-HLVZ-
iQCjvwNpG2hhkosCf4&hl=en&sa=X&ei=i_TEUInCMJOyhAffyoDYAw&redir_esc=y#v=one
page&q=Find%20the%20out%20cost%20A1%3D5%20x%204%20%20A2%3D%204%20x
%206%20%20A3%3D%206x2&f=false