ADA Solved

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

The Analysis and Design of Algorithm

Important Questions
Section A
1. List and define the two kinds of algorithm efficiency.
→ Time efficiency : A measure of amount of time for an algorithm to execute.
→ Space efficiency : A measure of amount of memory needed for an algorithm to
execute.

2. List any four algorithms of the brute force approach.


→Sequential search.
→String matching algorithm.
→Bubble sort.
→Exhaustive search.

3. Write Euclid’s algorithm for computing gcd(m,n).


→ Step1: If n=0,return the value of m as the answer and stop, otherwise proceed step
Step2:Divide m by n and assign the value of the remainder to r.
Step3:Assign the value of n to m and the value of r to n , go to Step 1.

4. Write 3 principle cases that may arise in comparing order of growth.


→1.Best case.
2.Average case.
3.Worst case.

5. With an example list the major variations of decrease-and-conquer.


→1.Decrease by a Constant.
2.Decrease by a Constant Factor.
3.Variable Size Decrease.

6. Write the diagrammatic representation of Decrease-by-a-constant-factor.


7. Differentiate between DFS & BFS.

BFS DFS
BFS stands for “ breadth first search” DFS stands for “Depth first search”
Traverses the tree level wise. Traverses the tree depth wise.
Implemented using Queue which is Implemented using Stack which is LIFO
FIFO list. list.
BFS requires more memory compare to DFS requires less memory compare to
DFS BFS

8. Define Decrease and Conquer technique.


→The Decrease and Conquer method is a problem-solving technique that involves
breaking down a problem into smaller subproblems, solving each subproblem
independently, and then combining the solutions to the subproblems to solve the
original problem. The name “decrease and conquer” comes from the fact that this
technique involves reducing the size of the problem at each step until it becomes small
enough to solve directly.

9. Define Input enhancement approach and list algorithms based on it.


→”Precomputing” or “Input Enhancement” is a strategy in algorithm design where
certain calculations are performed in advance, before the main execution of the
algorithm.
Algorithms→
• Counting Methods for sorting.
• Boyer-Moore Algorithm for String Matching and its simplified version
suggested by Horspool.

10. What is Rehashing?


→The elements of the previous hash table will be scanned one by one and calculate the
hash key with new hash function and insert them into the new hash table. This technique
is called rehashing.

11. Define Strassen’s algorithm.


→Strassen's Matrix Multiplication Algorithm is an algorithm used to multiply two
matrices, resulting in a third matrix that contains the product of the two input matrices.
The algorithm is used in a variety of scientific and engineering applications, including
computer vision, machine learning, and numerical simulations.

12. Define Prestructuring and list methods/approach based on it.


→Prestructuring is a method used in algorithm design that organizes data in a specific
way in advance to facilitate faster or more flexible access to the data during the algorithms
execution.
Methods/Approach based on it →
• Hashing.

13. Write the worst-case analysis of merge sort.


→ The Worst case of merge sort is when the left and right array has alternating elements
which results in maximum number of comparison and the time complexity stays at O(n
log n).

14. Define Distribution counting.


→Distribution counting is a variation of the counting sort algorithm that can be used to
sort an array of integers with a known range of values. The basic idea behind
distribution counting is to compute the frequency of each value in the input array and
use this information to determine the position of each element in the sorted array.

Section B

15. With an example, explain Step counts w.r.t to computing the time efficiency of
algorithm.
→In step count , we attempts to find the time spent in all the parts of program and
finally sum up the steps value which is the final value of efficiency of the algorithm.
Example: x=a+b; →1 step*1=1

for(i=1;i<=n;i++)

X=a+b; - →1step*n=n

for(i=1; i<=n; i++)


for(j=1; j<=n; j++)
X=a+b; -→1step*n^2=n^2

16. Discuss the sequences of steps in designing and analysing an algorithm.

17. Explain Worst-case, Best-case, Average-case with respect to Sequential search.


• Best Case: The best case occurs when the element to be searched is found at
the first location. In that case, the algorithm make only one comparison.
Therefore, the number of comparisons=1
Tbest(n)=1
• Worst Case: The worst case occurs when the element to be searched is found
at the last position or element to be searched is not found at any location. In
both the cases, the number of comparisons required is n.
Tworst(n)=n
• Average case: On average, the item will be found about halfway into the list.
In other words, the search key is equally likely to be anywhere in the array, so
on average ‘n/2 comparisons will be needed. Therefore, the average case time
complexity is O(n).
Tavg(n)=n

18. Write an algorithm for selection sort.

19. Explain the basic steps to be followed in analysing the efficiency of recursive
algorithm.
→ Basic steps to be followed in analysing the efficiency of recursive algorithm:
1.Selection of a parameter Indicating Input Size : the initial step involves the
determination of a parameter that indicates the size of the input. The initial parameter
serves as a measure of the algorithms efficiency. For example, in sorting algorithms,
the parameter could be the size of the list(n) that is to be sorted.
2. Identification of the Algorithm's Basic Operation : The basic operation of an
algorithm is typically the operation that is most frequently executed. More often this
operation is located in the innermost loop of the algorithm. For example, in the
context of a sorting algorithm, the basic operation might be the comparison of two
elements.
3. Reason for Basic Operation to be Executed more than Once : Find out the
reasons why the basic operations to be executed more than once.
4. Setting up a recurrence relation, with an appropriate initial condition, for
expressing the number of times the basic operation is executed.
5. Solving the recurrence relation for finding the complex function and order of
growth.

20. Write Johnson-Trotter algorithm for generating permutations.


21. Write a note on Russian peasant multiplication.
→ Russian Peasant Multiplication, also known as the ancient Egyptian multiplication
or binary multiplication algorithm, is a simple method to multiply two numbers. The
algorithm works by repeatedly dividing one of the numbers by 2 and doubling the other
number until the first number becomes 1. The final result is the sum of the second
number in each step where the first number is odd.

22. Write a note on Fake coin problem.


→ The fake coin problem is a classic problem in computer science that involves
identifying a fake coin among a set of identical-looking coins. The problem assumes
that the fake coin is known to be lighter than the genuine coins. The goal is to identify
the fake coin using a balance scale. The balance scale can compare two sets of coins
and tells us whether the sets weigh the same or which set is lighter
One way to solve the fake coin problem is to use the decrease-by-constant-factor
technique. The basic idea is to divide the set of coins into smaller and smaller subsets,
and to use the balance scale to compare the weights of these subsets. To optimize the
number of weighings, we can divide them into three groups instead of dividing the coins
into two groups (like in a Binary Search). This way, we decrease the problem size by a
factor of 3 in each step.

23. Write a Program to find value of a^n using brute force and divide and conquer method.
→ def bpower(a, n):
pow = 1
for i in range(n):
pow = pow * a
return pow

def dpower(x, y):


if (y == 0):
return 1
elif (int(y % 2) == 0):
return (dpower(x, int(y / 2)) *
dpower(x, int(y / 2)))
else:
return (x * dpower(x, int(y / 2)) *
dpower(x, int(y / 2)))

# Main block
a=int(input("Enter a :"))
n=int(input("Enter n :"))
print("Brute Force method a^n : ",bpower(a,n))
print("Divide and Conquer a^n : ",dpower(a,n)

24. Sort the list [ 21, 21, 23, 21, 22, 23, 22, 23 ] using distribution counting.

Section C

25. Explain in detail the mathematical analysis of Factorial of a number.


26. Explain in detail the mathematical analysis of algorithm in Checking the uniqueness
in an array of n elements.
27. Write the BFS traversal for the given graph
28. With an example explain Strassen’s Matrix Multiplication.
29. Explain hashing and different techniques to choose a hash function with minimum
collisions with an example.
→Hashing is the process of mapping keys to their appropriate locations in the hash
table. It is the most effective technique of searching the values in an array or in a hash
table. Hashing allows to update and retrieve any data entry in a constant time O(1).
Different techniques to choose a hash function with minimum collisions:
1.Division Method : It is the most simple method of hashing an integer x. This method
divides x by M and then uses the remainder obtained. In this case, the hash function can
be given as : h(x)=x mod M.
Example: h(x)=x mod M
‘M’ is a prime number. Table size=100

2.Mid-Square Method : In this method, we square the key first. Then we take digits
from the middle of this number as the generating address.
The hash function is : H(K)=m.

3.Folding Method : This is the easiest way of computing the key where the key is
broken into pieces and then adding all of them to get the hash address.
The hash function is : H(K) = K1 + K2 +……+Kn.
4.Mixed Method : It is the combination of division method and folding method.

30. Apply Horspool’s algorithm to search for the pattern GREAT in the text
SAURAVISREALLYGREAT

You might also like