Sei sulla pagina 1di 63

Important Questions for ADA for GTU

1. What is an algorithm? Explain various properties of an algorithm.


Definition:
A collection of unambiguous instructions occurring in some specific sequence and producing
output for a given set of inputs in a finite amount of time.
Properties:
1. Non-ambiguity: Each instruction in the algorithm should be clear and precise.
2. Range of input: The range of input should be specified for an algorithm.
3. Multiplicity: Same algorithm can be represented in several different ways.
4. Speed: Algorithms should be efficient in terms of time complexity.
5. Finiteness: After performing required operations, the algorithm should terminate.

2. Explain all asymptotic notations used in algorithm analysis.


Asymptotic Notations
1. Big Theta:

We say that g(n) is asymptotically tight bound for f(n).

2. Big O:
When we have only an asymptotic upper bound, we use O-notation. For a given
function g(n), we denote by O(g(n)) the set of functions
O(g(n)) = {f(n) : there exist positive constants c and n0 such that
0 <= f(n) <= cg(n) for all n>=n0}
3. Big Omega:

4. Little o notation:

5. Little omega:
3. Give the properties of a Heap Tree. Sort the following data with Heap Sort Method: 20,
50, 30, 75, 90, 60, 25, 10, 40.
Properties:
1. The root is max number for max-heap and min number for min-heap.
2. Parents are always greater than children for a max heap and less than children for a min
heap.
3. It is a complete binary tree.
Tracing:
4. Write selection sort algorithm and explain it with an example. Also, analyze its best case
and worst case time complexity.
Algorithm:
Selection_Sort(A)
{
//A is the array to be sorted
for i = 0 to A.length - 1
{
minIndex = i
//finding the next minimum element
for j = i + 1 to A.length
{
if A[j] < A[minIndex]
{
minIndex = j
}
}
swap(A[i], A[minIndex])
}
}

Time Complexity:
Statement Cost Frequency
for i = 0 to A.length - 1 C1 n
minIndex = i C2 n-1
for j = i + 1 to A.length C3 (n2 + n)/2
if A[j] < A[minIndex] C4 [(n2 + n)/2] - 1
minIndex = j C5 [(n2 + n)/2] - 1
swap(A[i], A[minIndex]) C6 n-1

Example:
5. Explain bubble sort and analyze it.
Algorithm:
Bubble_Sort(A)
{
//A is the array to be sorted
for i = 1 to A.length - 1
{
for j = 0 to A.length - i - 1
{
if A[j] > A[j+1]
{
swap(A[j],A[j+1])
}
}
}
}
Time Complexity:
Statement Cost Frequency
for i = 1 to A.length -1 C1 n
for j = 0 to A.length - i – 1 C2 (n2 + n)/2
if A[j] > A[j+1] C3 [(n2 + n)/2] - 1
swap(A[j],A[j+1]) C4 [(n2 + n)/2] - 1
Example:
6. Explain insertion sort and analyze it.
Algorithm:
Insertion_Sort(A)
{
//A is the array to be sorted
for i = 1 to A.length - 1
{
key = A[i]
j=i-1
while j >= 0 and A[j] > key
{
A[j+1] = A[j]
j = j -1
}
A[j+1] = key //Insert key in its rightful position
}

Worst Case Complexity


Statement Cost Frequency
for i = 1 to A.length - 1 C1 n
key = A[i] C2 n-1
j=i-1 C3 n-1
while j >= 0 and A[j] > key C4 [(n2 + n)/2] - 1
A[j+1] = A[j] C5 (n2 - n)/2
j = j -1 C6 (n2 - n)/2
A[j+1] = key C7 n-1

Best Case Complexity


Statement Cost Frequency
for i = 1 to A.length - 1 C1 n
key = A[i] C2 n-1
j=i-1 C3 n-1
while j >= 0 and A[j] > key C4 n-1
A[j+1] = A[j] C5 0
j = j -1 C6 0
A[j+1] = key C7 n-1
Example:

7. What is Divide and Conquer Technique? Give the use of it for Binary Searching Method.
Also give its Time Complexity.
A divide-and-conquer algorithm works by recursively breaking down a problem into two or
more sub-problems of the same or related type, until these become simple enough to be solved
directly.
Binary Search:
Algorithm:
Binary_Search(A, start, end, x)
{
//x is the element to be searched in array a
//start and end are start and end indices respectively
if (end = start) //Base Condition
{
if (x = A[start])
{
return start
}
else
{
return -1
}
}
else
{
mid = floor((start + end) / 2)
if (x = A[mid]) //If element to be found is A[mid]
{
return mid
}
if (x < A[mid]) //if element to be found is less than A[mid]
{
Binary_Search(A, start, mid-1, x) //Search in the left half of the array
}
if (x > A[mid]) //if element to be found is greater than A[mid]
{
Binary_Search(A, mid+1, end, x) //Search in the right half of the array
}
}
}

Example:
Time Complexity: O(logn)

8. Explain how Divide and Conquer method helps in multiplying two large integers.
Step 1: Recursively compute a * c
Step 2: Recursively compute b * d
Step 3: Recursively compute (a+b)(c+d) – a*c – b*d
Note: Step 3 finds the value of a*d + b*c using the values found in Steps 1 and 2.
Final answer is formed by 10n ac + 10n/2 (ad + bc) + bd
Example:
9. Write an algorithm for quick sort and derive its best case, worst case time complexity.
Also trace on given data (3,1,4,5,9,2,6,5)
Algorithm:
Partition(A, start, end)
{
pivot= A[end]
pIndex = start
for j = start to end - 1
{
if A[j] <= pivot
{
swap(A[pIndex], A[j])
pIndex = pIndex + 1
}
}
swap(A[pIndex], A[end])
return(pIndex)
}
Quick_Sort(A, start, end)
{
//A is the array to be sorted
//start and end are start and end indices of A respectively
if (start < end)
{
p = Partition(A, start, end)
Quick_Sort(A, start, p-1)
Quick_Sort(A, p+1, end)
}
}
Time Complexity Best Case:
Time Complexity Worst Case:
Tracing on:
3,1,4,5,9,2,6,5
10. Write the Prim’s Algorithm to find out Minimum Spanning Tree. Explain with Example.

Example:
11. Write the Kruskal’s Algorithm to find out Minimum Spanning Tree. Explain with
Example.
12. Consider Knapsack capacity W=60, w=(40,10,20,24) and v=(280,100,120,120) find the
maximum profit using greedy approach.
13. Using greedy algorithm find an optimal schedule for following jobs with n=6.Profits:
(P1,P2,P3,P4,P5,P6,P7) = (3,5,18,20,6,1,38) Deadline: (d1,d2,d3,d4,d5,d6,d7)
=(1,3,3,4,1,2,1)
14. Find an optimal Huffman code for the following set of frequency. a : 45, b:13, c:12, d:16,
e:9, f:5.
15. Explain Dijkstra's shortest path algorithm with an example.
16. Differentiate between dynamic programming and greedy algorithms and between
dynamic programming and divide and conquer strategy.
17. Find Longest Common Subsequence using Dynamic Programming Technique with
illustration X={X,Y,Z,Y,T,X,Y} Y={Y,T,Z,X,Y,X}
Answer
18. Write equation for Chained matrix multiplication using Dynamic programming.
Find out optimal sequence for multiplication: A1 [5 × 4], A2 [4 × 6], A3 [6 × 2], and
A4 [2 × 7]. Also give the optimal parenthesization of matrices.
19. Explain all pair of shortest path using Floyd’s Algorithm with algorithm.
20. Consider Knapsack capacity W=5, w = (2,3,4,5) and v=(3,4,5,6) find the maximum profit
using dynamic method.
21. Given coins of denominations 1, 4 and 6 with amount to be pay is 8. Find
optimal no. of coins and sequence of coins used to pay given amount using dynamic
method.
Ans: Coin if 4 selected twice.
22. Explain DFS with example and algorithm.
Algorithm:
Example:
23. Explain BFS with example and algorithm.
24. Find out articulation points for the following graph. Consider vertex 1 as the starting
point.
25. Explain topological sorting with an example using both DFS method and source removal
algorithm.

Using DFS
Using Source Removal Algorithm

26. Find an optimal solution to the knapsack instance n=4, M=8, (P1,P2,P3,P4)=(3,5,6,10)
and (W1,W2,W3,W4)=(2,3,4,5) using backtracking. Also draw the corresponding state
space tree.
27. What is backtracking? How is 4-Queens problem solved using backtracking?
Backtracking is a general algorithm for finding all solutions to some computational problems,
notably constraint satisfaction problems, that incrementally builds candidates to the solutions,
and abandons a candidate as soon as it determines that the candidate cannot possibly be
completed to a valid solution.
28. Explain Naive String Matching with algorithm and example. Give its time complexity.
Time Complexity: O((n-m+1)m)
29. How many spurious hits does the Rabin-Karp matcher encounter in the text T =
3141592653589793 when looking for the pattern P = 26 with working modulo q = 11?
Give the time complexity of Rabin-Karp Matcher.
Time Complexity:
Processing Time: O(m)
Matching Time: O((n-m+1)m)
29. Explain string matching with finite automata.
P = ababaca
T = abababacaba
Transition Table
Finite Automata

String Matching

30. Define P, NP, NP complete and NP-Hard problems. Give examples of each.
Class P:
Problems that can be solved in polynomial time with deterministic algorithms.
Examples: Sorting, Searching
Class NP:
Problems that cannot be solved in polynomial time with deterministic algorithms.
Examples: Travelling Salesman Problem, Knapsack Problem
NP Hard:
NP-hardness, in computational complexity theory, is the defining property of a class of
problems that are, informally, "at least as hard as the hardest problems in NP".
Example: Knapsack Problem
NP Complete:
In computational complexity theory, a problem is NP-complete when it can be solved by a
restricted class of brute force search algorithms and it can be used to simulate any other
problem with a similar algorithm.
Example: Hamilton Cycle

Potrebbero piacerti anche