CS502 – Fundamental of Algorithm midterm past papers

CS502 – Fundamental of Algorithm Solved Midterm Past Papers

Q. we can avoid unnecessary repetitions for recursive calls? 

Answer:-
We can avoid these unnecessary repetitions by writing down the results of recursive calls and looking them up again if we need them later. This process is called memorization.

What are Catalan numbers? Give the formula.

Answer: – Catalan numbers form a sequence of natural numbers that occur in variouscounting
problems, often involving recursively defined objects
Formula is C(n) = 2n Cn / (n+1)

Q. What is heap and what is heap order? 

Answer:-
The heap is the section of computer memory where all the variables created or initialized at runtime are stored. The heap order property: in a (min) heap, for every node X, the key in the parent is smaller than or equal to the key in X.

Q. How we build heap? 

Answer: –
We build a max heap out of the given array of numbers A[1..n]. We repeatedly extract the maximum item from the heap. Once the max item is removed, we are left with a hole at the root.

Q. 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)

Answer: –
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.

Q. What are the applications of edit distance technique? Name any three.

Answer: –
Spelling Correction
Plagiarism Detection
Computational Molecular Biology

Q-Write a pseudo code Fibonacci With memorization? 

Answer: –
MEMOFIB(n)
1 if (n < 2)
2 then return n
3 if (F[n] is undefined)
4 then F*n+ MEMOFIB(n − 1) + MEMOFIB(n − 2)
5 return F[n]

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

Answer:-
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

Step7:End number.

Q. How we build heap.

Answer: – We build a max heap out of the given array of numbers A[1..n]. We repeatedly extract
the maximum item from the heap. Once the max item is removed, we are left with a hole at the root.

Q. Quick sort such that sort the array in to non-increasing order?

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.

Q. Draw the cost table for chain multiplication problem with initial states.

Answer:-
(A1)(A2A3A4 . . .An)
or (A1A2)(A3A4 . . .An)
or (A1A2A3)(A4 . . .An)
. . . . . .
or (A1A2A3A4 . . .An−1)(An)

Q.What is the formula for generating Catalan numbers?

Answer:- 
Equation (22) is a recurrence relation.
C_(n+1) = C_n * [2(2n+1)] / (n+2)
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.

Q. Write Down the steps of Dynamic programming.

Answer: –Dynamic programming is essentially recursion without repetition. 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 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.
Dynamic programming algorithms need to store the results of intermediate sub problems. This is often but not always done with some kind of table. We will now cover a number of examples of problems in which the solution is based on dynamic programming strategy.

Q. Differentiate b/w Bubble sort, insertion sort and selection sort? 

Answer:-  

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

Q. Spelling correction in edit distance? 

Answer:-  A better way to display this editing process is to place the words above the other:
S D I M D M
M A – T H S
A – R T – S
THE FIRST WORD HAS AGAP FOR EVERY INSERTION (1)AND THE SECOND WORD HAS A
GAP FOR EVERY DELETION (D). MATHES (M) DO NOT COUNT. THE EDIT TRANSCRIPT IS
DEFINED AS A STRING OVER THE ALPHABETM,S,I,d THAT DESCRIBES A
TRANSFORMATION OF ONE STRING INTO OTHER. FOR EXAMPLE
S D I M D M
1+1+1+0+1+0+=4

Q. Suggest the criteria for measuring algorithms. Also discuss the issues need to be discussed in the algorithm design.

Answer: – In order to design good algorithms, we must first agree the criteria for measuring
algorithms. The emphasis in this course will be on the design of efficient algorithm, and hence we will measure algorithms in terms of the amount of computational resources that the algorithm requires. These resources include mostly running time and memory. Depending on the application, there may be other elements that are taken into account, such as the number disk accesses in a database program or the communication bandwidth in a networking application.

Q. Write down the steps of dynamic programming strategy. 

Answer:-  Developing a dynamic programming algorithm generally involves two separate steps:
1_formulate problem recursively.
Write down a formula for the whole problem as a simple combination of answers to smaller sub problems.
2_ Build solution to recurrence from bottom up:

Q. What are the two steps generally involved while developing dynamic programming
algorithm.

Answer:- 
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.

Q. How to construct an optimal solution for o/1 knapsack problem ?

Construction of optimal solution: Construct an optimal solution from computed information. Let us try this: If items are labelled 1, 2, . . . , n, then a subproblem would be to find an optimal solution for S k = items labelled 1, 2, . . . , k This is a valid subproblem definition. The question is: can we describe the final solution S n in terms of subproblems S k ? Unfortunately, we cannot do that. Here is why. Consider the optimal solution if we can choose items 1 through 4 only.

Answer: –
Items chosen are 1, 2, 3, 4
Total weight: 2 + 3 + 4 + 5 = 14
Total value: 3 + 4 + 5 + 8 = 20
Now consider the optimal solution when items 1 through 5 are available

Q.Effect of calling max heap

Answer: – The smallest key is in the root in a min heap; in the max heap, the largest is in the root.