You are on page 1of 48

DESIGN ANALYSIS AND ALGORITHM (DAA)

UNIT 1 QUESTION BANK ANSWERS

1)Explain Asymptotic Notations with examples?

Ans) Asymptotic Notations are the expressions that are used to represent the complexity of an

algorithm.

Asymptotic notations are the mathematical notations used to describe the running time of an

algorithm when the input are given.

For example: In bubble sort, when the input array is already sorted, the time taken by the

algorithm is linear i.e. the best case.

But, when the input array is in reverse condition, the algorithm takes the maximum time

(quadratic) to sort the elements i.e. the worst case.

When the input array is neither sorted nor in reverse order, then it takes average time. These

durations are denoted using asymptotic notations.

There are three types of analysis that we perform on a particular algorithm:

(a) Best Case: In which we analyse the performance of an algorithm for the input, for which the

algorithm takes less time or space.

(b) Worst Case: In which we analyse the performance of an algorithm for the input, for which the

algorithm takes more time or space.

(c) Average Case: In which we analyse the performance of an algorithm for the input, for which

the algorithm takes time or space that lies between best and worst case.

Types of Asymptotic Notation:

(a) Big-O Notation (Ο) – Big O notation specifically describes worst case scenario.

 It represents the upper bound running time complexity of an algorithm.

 Eg: f(n)=3n+2 , g(n)=n

3n+2 ≤ 4*n for all n ≥ 2

f(n)=O(g(n))

f(n)= O(n)

O(g(n)) = { f(n): there exist positive constants c and n 0


such that 0 ≤ f(n) ≤ cg(n) for all n ≥ n 0 }
(b) Omega Notation (Ω) – Omega(Ω) notation specifically describes best case scenario.

 It represents the lower bound running time complexity of an algorithm.

Eg: f(n)=3n+2

3n+2 ≥ 3*n for all n ≥1

f(n)=Ω(g(n))

f(n)=Ω(n)

Ω(g(n)) = { f(n): there exist positive constants c and n 0


such that 0 ≤ cg(n) ≤ f(n) for all n ≥ n 0 }

(c) Theta Notation (θ) – This notation represents the average complexity of an algorithm.

 This notation describes both upper bound and lower bound running time complexity of an
algorithm.

Eg: f(n)=3n+2

3n+2 ≥ 3*n for all n ≥ 1

f(n)=Ɵ(g(n))

3n+2=Ɵ(n)

f(n)=3n+2

3n+2 ≤ 4*n for all n ≥ 2

f(n)=Ɵ(g(n))

3n+2=Ɵ(n)

therefore.., 3*n ≤ 3n+2 ≤ 4*n n>=2

f(n)=Ɵ(n)

Θ(g(n)) = { f(n): there exist positive constants c1, c2 and n0


such that 0 ≤ c 1g(n) ≤ f(n) ≤ c2g(n) for all n ≥ n0 }

f (n)
Little oh (o): The function f(n)=o g(n) [“read as f of n is little oh of g of n “ ] if and only lim =0
n→∞ g (n )
Little omega (ω): The function f(n)= ω g(n) [“read as f of n is little omega of g of n “ ] if and only
g (n )
lim =0
n→∞ f (n)
2) Explain Binary Search Algorithm and its time complexity with an example?

Ans) Binary search algorithm works on the principle of divide and conquer.

For this algorithm to work properly, the data collection should be in the sorted form.

Steps to solve Binary search:

Binary search looks for a particular item by comparing the middle most item of the collection. If a

match occurs, then the index of item is returned.

if(a[mid]=X)

return mid;

If the middle item is greater than the item, then the item is searched in the sub-array to the left of

the middle item.

if(a[mid]>X)

return Binsea(a, lb, mid-1)

Otherwise, the item is searched for in the sub-array to the right of the middle item.

if(a[mid]<X)

return Binsea(a, mid+1, ub)

This process continues on the sub-array as well until the size of the subarray reduces to zero.

Algorithm:
Working Procedure:
3) Explain Merge Sort Algorithm and its time complexity with an example?
Ans) It works on the principle of Divide and Conquer.
Merge sort repeatedly breaks down a list into sub lists until each sub list consists of a single
element and merging those sub lists in a manner that results into a sorted list.
 Algorithm:
 Working Procedure:
4)Explain Quick Sort algorithm and its time complexity with an example?
Ans) Quick sort is a highly efficient sorting algorithm and is based on partitioning of array of data into
smaller arrays.
It is also called partition-exchange sort.
Pivot element can be any element from the array, it can be the first element, the last element or
any random element.
Algorithm:
Working Procedure:
Time Complexity:

5)Explain the general method of divide and conquer. Also explain the performance analysis?
Ans) In divide and conquer approach, a problem is divided into smaller problems, then the smaller
problems are solved independently, and finally the solutions of smaller problems are combined into
a solution for the large problem.
Generally, divide-and-conquer algorithms have three parts −
Divide: This involves dividing the problem into some sub problem.
Conquer: Sub problem by calling recursively until sub problem solved.
Combine: The Sub problem Solved so that we will get find problem solution. 
 
Algorithm:

Performance Analysis of Divide and Conquer algorithm:  


Quicksort is a sorting algorithm. The algorithm picks a pivot element, rearranges the array
elements in such a way that all elements smaller than the picked pivot element move to left side
of pivot, and all greater elements move to right side. Finally, the algorithm recursively sorts the
subarrays on left and right of pivot element.
Merge Sort is also a sorting algorithm. The algorithm divides the array in two halves, recursively
sorts them and finally merges the two sorted halves.
Strassen’s Algorithm is an efficient algorithm to multiply two matrices. A simple method to
multiply two matrices need 3 nested loops and is O(n^3). Strassen’s algorithm multiplies two
matrices in O(n^2.8974) time.

6) Explain the algorithm for finding maximum and minimum and its time complexity with an
example?
Ans) Algorithm for min and max is used to find the maximum and minimum elements in a set of n
elements.
Algorithm:
 Time Complexity:

7) Explain Stressen’s Matrix Multiplication and its time complexity with an example?
Ans)
8) Explain time and space complexity with an example?
Ans)
DESIGN ANALYSIS AND ALGORITHM (DAA)
UNIT 2 QUESTION BANK ANSWERS

1)Explain the general method of greedy method. Define feasible solution and optimal solution?
Ans)

 Algorithm:

2) Solve the job sequencing problem with number of jobs n = 4. Their profits are (p1,p2,p3,p4,p5) =
(100,10,15,27) and deadlines are (d1,d2,d3,d4) = ( 2,1,2,1). Write an algorithm for the same.
Ans)
Time complexity is Ɵ(n2 )
Best case is O(n)

3) Consider the weights and profits given and find the optimal solution where Max Capacity of
Knapsack, M =15, (p1,p2,p3,p4,p5,p6,p7) = (10,5,15,7,6,18,3) and (w1,w2,w3,w,4,w5,w6,w7) =
(2,3,5,7,1,4,1). Write an algorithm for the same.
Ans)
Time Complexity=O(n)
4) Explain optimal storage on tapes with an example?
Ans)
5) Explain Prim’s and Kruskal’s algorithm with an example?
Ans)

Prim’s Algorithm Kruskal’s Algorithm

It starts to build the Minimum It starts to build the Minimum Spanning


Spanning Tree from any vertex in the Tree from the vertex carrying minimum
graph. weight in the graph.

It traverses one node more than one


time to get the minimum distance. It traverses one node only once.

Prim’s algorithm has a time complexity


of O(V2), V being the number of
vertices and can be improved up to O(E Kruskal’s algorithm’s time complexity is O(E
+ log V) using Fibonacci heaps. log V), V being the number of vertices.

Kruskal’s algorithm can generate


Prim’s algorithm gives connected forest(disconnected components) at any
component as well as it works only on instant as well as it can work on
connected graph. disconnected components
6) Explain single source shortest path problem with an example?
Ans)
DESIGN ANALYSIS AND ALGORITHM (DAA)
UNIT 3 QUESTION BANK ANSWERS

1) Explain Multistage Graph with an example?


Ans)
3)Explain the travelling salesperson problem for the following graph using dynamic approach?
Ans)
4) Explain all pairs shortest path problem with an example?
Ans) The all-pairs shortest path problem is the determination of the shortest graph
distances between every pair of vertices in a given graph.
The all pair shortest path algorithm is also known as Floyd-Warshall algorithm is used to
find all pair shortest path problem from a given weighted graph. As a result of this algorithm,
it will generate a matrix, which will represent the minimum distance from any node to all
other nodes in the graph.
5) Consider n=3 and keys(k1,k2,k3)=(25,26,27),the frequency of the keys are 4,3,2. Construct
an optimal binary search tree?
Ans)

6) Explain OBST algorithm with an example?


Ans) A Binary Search Tree is a tree where the key values are stored in the internal nodes.
The external nodes are null nodes.
For each internal node all the keys in the left sub-tree are less than the keys in the node,
and all the keys in the right sub-tree are greater the keys in the node.
7) Explain the general method of dynamic programming?
Ans)

You might also like