0% found this document useful (0 votes)
0 views

Algorithm Viva Question-Answer

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Algorithm Viva Question-Answer

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

1. What is Program?

A program is a set of instructions written in programming language to solve a problem on a


given input or a program is the expression of an algorithm in a programming language.

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.

3. What is recursion and recursive algorithm?


A recursion is a function that calls itself within the body of function.
An algorithm is said to be recursive if the same algorithm is invoked within its body.
An algorithm that calls itself inside it‟s body is direct recursion. An algorithm A is said to be
indirect recursion if it calls another algorithm B, which in turns calls A.

5. What is the analysis of algorithm?

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.

6. What is the asymptotic notation?


It is the mathematical notation which used to bounds the complexity of an algorithm. It is of
three types. (Big-oh, Omega and Theta Notation)

Big oh: it is used to express the upper bound of an algorithm.


The function f(n) = O(g(n)) iff there exist positive constants C and no such that f(n)<= C
* g(n) for all n, n>=n0.

Omega: it is used to express the lower bound of an algorithm.


The function f(n) =W (g(n)) iff there exist positive constant C and no such that f(n) >=C
* g(n) for all n, n> n0.

Theta: it is used to express the tight bound of an algorithm.


The function f(n) =q (g(n)) iff there exist positive constant C1, C2, and no such that
C1 g(n)<= f(n) <= C2 g(n) for all n, n >= n0.
7. What is the complexity of an algorithm?

Complexity of an algorithm is how much time and space required by the algorithm. Complexity
is of two types: time and space complexity.

Types of Complexity: Complexity is of two types: time and space complexity.

What is the space complexity?


The space complexity of an algorithm is the amount of memory the algorithm requires to
execute.

What is time complexity?


The time complexity of an algorithm is the amount of computer time algorithm requires to
execute. Time complexity is of three types.

Types of Analysis: Worst, best and Average

What is the worst case time 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).

What is the best case time complexity?

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.

For example: linear search best case time complexity O(1)

What is the average case time complexity?

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.

For example: linear search average case time complexity is O(n)


8. What is an upper bound of an algorithm?

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.

9. What is Lower bound of an 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.

10. What is a divide-and-conquer technique?

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.

The list of problem solved by divide and conquers method


 Max-min, Binary search, Merge-sort, Quick-sort, matrix multiplication,
 Complexity of:
Binary Search: O(logn), Max-min= O(3n/2)= O(n).
Merge-sort: O(n logn) – best and worst case
Quick-sort: O(n2) worst case, O(nlogn)- Best case
Heap-sort: O(nlogn)- Best and worst case

11. Describe the recurrence relation of merge sort?

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

Worst Case: T(n) = a if n = 1, a a constant


=T (n-1) + n if n >1, c a constant
12. What is Heap and Max Heap?
Heap is a complete binary tree. A complete binary tree is a binary tree in which all the level
completely filled except last level, and nodes at last levels are filled from left to right.( this is the
structural property of complete binary tree)

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.

13. What is Min Heap?

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.

14. What is greedy technique?

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.

The list of problems solved by greedy technique:


 Greedy knapsack,
 Job-sequencing,
 Minimum spanning tree
 Optimal merge pattern

15. What is minimum spanning tree of a graph?

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,

There are two algorithms:


a. Prims: time complexity: O(n2) using matrix, and O((n+e) loge) using linked
list

b. Kruskal’s: time complexity: O ((n+e) log e)


16. The complexity of various sorting algorithm:

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)

16. What is meant by feasible solution?


A subset that satisfies the given constraints of the problem is called feasible solution.

17. Define optimal solution?


A feasible solution that maximizes or minimizes the given objective function is called as
optimal solution

Write the general algorithm for Greedy method.


Algorithm Greedy (a, n)
{
solution=0;
for i=1 to n do
{
x= select(a);
if feasible(solution ,x) then
solution=Union(solution ,x);
}
return solution;
}

18. What is Greedy Knapsack problem?

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.

19. What does Job sequencing with deadlines mean?

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.

Examples: Find the nth Fibonacci number, O/1 Knapsack problem,

21. What are the features of dynamic programming?


_ Optimal solutions to sub problems are retained so as to avoid re-computing their values.
_ Decision sequences containing subsequences that are sub-optimal are not considered.
_ It definitely gives the optimal solution always.

22. What is principle of optimality in dynamic programming?


It states that an optimal sequence of decisions has the property that whenever the initial state and
decision are the remaining decisions must constitute an optimal sequence with regard to state
resulting from initial state and first decision.

Give an example of dynamic programming.

1. An example of dynamic programming is knapsack problem. The solution to the


knapsack problem can be viewed as a result of sequence of decisions. We have to decide
the value of xi for 1<i<n. First we make a decision on x1 and then on x2 and so on. An
optimal sequence of decisions maximizes the object function summation of pi xi.

2. Finding the shortest path.


One way of finding a shortest path from vertex i to j in a directed graph G is to decide
which vertex should be the second, which is the third, which is the fourth, and so on,
until vertex j is reached. An optimal sequence of decisions is one that results in a path of
least length.

Define 0/1 knapsack problem.

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

What is the formula to calculate optimal solution in 0/1 knapsack problem?


The formula to calculate optimal solution is
g(i, m) =max{g(i-1, m) , g(i-1, m-w i-1)+pi-1}.
What is traveling salesperson problem.

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.

Write some applications of traveling salesperson problem.


_ routing a postal van to pick up mail from boxes located at n different sites.
_ using a robot arm to tighten the nuts on some piece of machinery on an assembly line.
_ Production environment in which several commodities are manufactured on the same
set of machines.

Give the time complexity of traveling salesperson problem.


_ Time complexity is O (n2 2n).

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.

Define explicit constraint.


Explicit constraints are rules that restrict each Xi to take on values only from a give set Si. They
depend on the particular instance I of the problem being solved.

Solution space: All tu1ples that satisfy the explicit constraints define a possible solution
space.

Define implicit constraint.


These are rules that determine which of the tuples in the solution space of I satisfy the criteria
function. It describes the way in which the xi must relate to each other.
Define state space tree.
The tree organization of the solution space is referred to as state space tree.

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 answer states.


Answer states are that solution states S for which the path from the root to S defines a tuple that
is a member of the set of solutions of the problem.

What are static trees?


The tree organizations that are independent of the problem instance being solved are called as
static tree.

What are dynamic trees?


The tree organizations those are independent of the problem instance being solved are called as
static tree.

Define a live node.


A node which has been generated and all of whose children have not yet been generated is called
as a live node.

Define E – node.
Any live node whose children are currently being generated is called as a E – node.

Define a dead node.


Dead node is defined as a generated node, which is to be expanded further all of whose children
have been generated.

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 )

What is Sum of Subsets Problem?


Given n distinct positive numbers usually called as weights wi, the problem calls for finding all
the combinations of these numbers wi ,whose sums are m.

What Coloring problem.


Let G be a graph with n vertices and m colors. Color the vertices of the G with colors in such a
way that no two adjacent vertices have the same color.
Complexity of algorithm is = O(n mn )

Define chromatic number of the graph.

The minimum number of colors required to color the graph is called chromatic number of the
graph.

Define a planar graph.


A graph is said to be planar iff it can be drawn in such a way that no two edges cross each other.

What is Branch and Bound?

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.

It is similar to backtracking technique but uses BFS-like search.

1 1 1

2 3 4 5 2 3 4 5
2 3 4 5

Live Node: 2, 3, 4, and 6 7 8 9 8 9 6 7


5
FIFO Branch & Bound (BFS)
LIFO Branch & Bound (D-Search)
Children of E-node are
inserted in a queue. Children of E-node are inserted
in a stack.

Dead-node: A node that has been expanded


Solution-node: A which is the possible candidate for the set of solution.

You might also like