Algorithm Viva Question-Answer
Algorithm Viva Question-Answer
2. What is an algorithm?
An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In
addition, all algorithms must satisfy the following criteria/ characteristics
1) Input
2) Output
3) Definiteness
4) Finiteness
5) Effectiveness.
Analysis of algorithm refers to task of determining how much computing time and storage an
algorithm requires. It requires determining the time complexity and space complexity.
Complexity of an algorithm is how much time and space required by the algorithm. Complexity
is of two types: time and space complexity.
Worst case time complexity: worst case time complexity is the maximum time requires
executing the algorithm for any input of size n. it is denoted by big O notation.
For example: linear search has worst case time complexity O(n).
Best case time complexity: best case time complexity is the minimum time requires executing
an algorithm for any input of size n. It is denoted by Omega notation.
Average case time complexity: average case time complexity is the average time requires
executing the algorithm for all input of size n. It is denoted by Theta notation.
The upper bound of an algorithm is the function g(n) such that f(n) <= c*g(n), here g(n) is the
upper bound of an algorithm, here f(n) is complexity of the algorithm.
The lower bound of an algorithm is the function g(n) such that f(n) > c*g(n), here g(n) is the
lower bound of an algorithm, here f(n) is complexity of the algorithm.
Divide and conquer technique divide the problem into sub-problems (in most cases 2 sub-
problems). Find the solution of sub-problems and combine the solution of problem to get the
solution of original problem.
The characteristic of problem solved by divide and conquers techniques:
1. If we divide the problem into sub-problem the nature of sub-problem will be same as
original problem.
2. The sub-problem can be solved recursively using the same algorithms.
If the time for the merging operation is proportional to n, then the computing time of merge sort
is described by the recurrence relation
T(n) = a if n = 1, a a constant
2T (n/2) + n if n >1, c a constant
Recurrence relation of Quick-sort
Best Case: T(n) = a if n = 1, a a constant
=2T (n/2) + n if n >1, c a constant
Max Heap is a complete binary tree in which each node has value at least as large as the values
at their child node. It is used for sorting.
Structural properties of max heap: The value of parent node is greater than or equal to
value of their child node.
Min Heap is a complete binary tree in which each node has value at least as small as the at their
child node. It is used for implementing priority queue, used in kruskal’s algorithm.
Structural properties of max heap: The value of parent node is Less than or equal to value
of their child node.
Greedy is the most important algorithms design technique used to find the solution of
optimization problem.
In this we find the solution to the problem by selecting the objects using selection criteria based
on objective function of the problem.
In problem, given „n’ inputs, we are required to obtain the subset that satisfies some constraints.
1. Feasible solution: a subset which satisfies the constraints of the problem is called
feasible solution.
2. Optimal solution: A feasible solution that optimizes the given objective function is
called optimal solution.
A Minimum spanning tree of the connected weighted graph is a sub-graph that contains all the
vertices of the graph, which is a tree and total cost of edges in the tree is minimum,
Worst Best
1. Merge-sort O(n logn) O(n logn)
2. Quicksort O(n2) O(n logn)
3. Heapsort O(n logn) O(n logn)
4. Insertion sort O(n2) O(n)
5. Selection sort O(n2) O(n2)
Given a knapsack of capacity M and n objects are given. Each object has weight wi and profit pi
.If a fraction xi of object i is placed into knapsack then the profit pi* xi is earned. The problem is
to determine how to fill the knapsack with objects so that the profit earned is maximum.
Given a set of „n‟ jobs each job „i’ has a deadline di such that di >=0 and a profit pi such that
pi>=0. For a job „i‟ profit pi is earned iff it is completed within deadline. Each job will take only
one time to process and only one machine is available for processing the jobs. The problem is to
find the subset of jobs such the each job in the subset is completed by its deadline.
20. What is Dynamic programming?
Dynamic programming is a powerful algorithm design method in which a problem is solved by
indentifying the number of sub- problem; find the solution of smallest one, using the solution of
smallest sub-problems helps to figure out larger and larger sub problem and so on utile the whole
problem is solved. Dynamic programming works on principle of optimality.
Given a knapsack of capacity M, and n objects, Each object has weight wi and profit pi .If an
object i is placed into knapsack then the profit pi* xi is earned. The problem is to determine how
to fill the knapsack with objects so that the profit earned is maximum, where xi =0 or xi =1
Let G = (V, E) be a directed. The tour of G is a directed simple cycle that includes every vertex
in V. The cost of a tour is the sum of the cost of the edges on the tour. The problem is to find a
tour of minimum cost.
What is backtracking?
A depth first generation of problem state with bounding function is called backtracking.
In backtracking we make the state space tree and then search through it to find the solution of
the problem. If during searching, at a node if it is not satisfying the constraints (bounding
function) we backtrack to parent node and proceed with next child.
What are the requirements that are needed for performing Backtracking?
To solve any problem using backtracking, it requires that all the solutions satisfy a complex set
of constraints. They are:
i. Explicit constraints.
ii. Implicit constraints.
Solution space: All tu1ples that satisfy the explicit constraints define a possible solution
space.
Define state space of the problem. All the paths from the root of the organization tree to all the
nodes is called as state space of the problem
Define E – node.
Any live node whose children are currently being generated is called as a E – node.
n – Queens problem.
The problem is to place n queens on a n x n chessboard so that no two queen “attack” that is no
two of them are on the same row, column or on the diagonal.
Complexity of algorithm is = O(n2 2n )
The minimum number of colors required to color the graph is called chromatic number of the
graph.
A breadth first generation of problem state with bounding function is called Branch and
Bound. Branch and Bound is a state space tree search method in which all the children of a
node are generated before expanding any of its children.
Live-node: A generate node whose children have not yet been generated.
1 1 1
2 3 4 5 2 3 4 5
2 3 4 5