Professional Documents
Culture Documents
1. Define Algorithm.
An algorithm is a sequence of unambiguous instructions for solving a problem, which
means to obtain a required output for any legitimate input in a finite amount of time.
12. What are the fundamental steps involved in algorithmic problem solving?
1) Constant: 1
2) Logarithmic: log n
3) Linear: n
4) N-log-n: nlogn
5) Quadratic: n2
6) Cubic: n3
7) Exponential: 2n
8) Factorial: n!
37. What are the Best, Worst and Average Case complexity of Linear Search?
· Best Case – O (1)
· Average Case – O (N)
· Worst Case – O (N)
For non-negative functions, f(n) and g(n), f(n) is little omega of g(n) if and only if f(n) = _(g(n)),
but f(n) ≠ Θ(g(n)). This is denoted as "f (n) = ω (g (n))".
42. Give the general plan for analyzing non recursive algorithm.
· Decide a parameter indicating an Input’s Size.
· Identify the algorithms Basic Operation
· Check whether the number of times the basic operation is executed only on the size of
an input. If it also depends on some additional property, the worst case, average case, and
if necessary, best case efficiencies have to be investigated separately.
· Using standard formulas and rules of sum manipulation either find a closed formula for
the count or, at the very least, establish its order of growth.
44. What are all the methods available for solving recurrence relations?
· Forward Substitution
· Backward Substitution
· Smoothness Rule
· Master Theorem
4. Find the number of comparisons made by the sequential search in the worst case and the
best case.
The numbers of comparisons made by the sequential search in the worst and best case are
one and n key comparisons respectively.
11. What is the time complexity of straight forward with D & C of finding Min-max?
· Best case occurs when the elements are in increasing order, so the number of element
comparisons is n
· Worst case occurs when the elements are in decreasing order, so the number of element
comparisons is 2(n-1).
· Average case occurs when a[i] is greater than max half the time.
19. How the choices are made in each step in greedy method:
· Choices of solution made at each step of problem solving in greedy approach is as
follows...
· Feasible-it should the problem's constraints.
· Locally optimal-among all feasible solutions the best choice is to be made Irrevocable-
once the particular choice is made then it should not get changed on subsequent steps.
2. What is the difference between dynamic programming and divide and conquer?
We can either build up solutions of sub-problems from small to large (bottom up) or we
can save results of solutions of sub-problems in a table (top down + memorization).This forms
the table structure.
The vertex s in V1 is called the source; the vertex t in VK is called the sink. G is usually
assumed to be a weighted graph. The cost of a path from node v to node w is sum of the costs of
edges in the path. The "multistage graph problem" is to find the minimum cost path from s to t.
Each set Vi is called a stage in the graph.
9. Give the formula for cost (i,j) of multistage graph in both forward and backward
approach.
Forward approach
Cost(i,j)=min{c(i,j)+cost(i+1,l)}
Backward approach
Bcost(i,j)=min{bcost(i-1,l)+c(l,j)}
l Є vi-1 , <l,j>Є E
since,bcost(2,j)=c(1,j) if <1,j> Є E and
bcost(2,j)= ∞ if<1,j> !Є E
Given a weighted connected graph (undirected or directed), the all pairs shortest paths
problem asks to find the distances (the lengths of the shortest path) from each vertex to all other
vertices.
13. Derive the formula for the Floyd’s algorithm and running time of the algorithm.
for k<--1 to n do
for i<--1 to n do
for j<--1 to n do
D[i,j]=min{[i,j],D[i,k]+D[k,j]}
The transitive closure of a directed graph with n vertices can be defined as the n by n
Boolean matrix T = {ti,}, in which the element in the i th row (1<i<n) and the jth column (l<j<n) is
1 if there exists a non trivial directed path from the ith vertex to jth vertex ; otherwise , tij is 0.
Warshall's algorithm constructs the transitive closure of a given digraph with n vertices
through a series of n by n Boolean matrices R(0), ………, R(k-l)R(k),…….., R(n)
Each of these matrices provides certain information about directed paths in the digraph.
One of the principal applications of Binary Search Tree is to implement the operation of
searching. If probabilities of searching for elements of a set are known, it is natural to pose a
question about an optimal binary search tree for which the average number of comparisons in a
search is the smallest possible.
20. What is the running time of traveling salesman problem and 0/1 knapsack problem?
TSP: 0(n2 * 2n)
0/1 Knapsack: 0(nw)
The Memory Function technique seeks to combine strengths of the top down and
bottom-up approaches to solving problems with overlapping subproblems. It does this by
solving, in the top-down fashion but only once, just necessary sub problems of a given problem
and recording their solutions in a table.
UNIT 4
3. Define Backtracking.
Backtracking is to build up the solution vector one component at a time and to use
modified criterion function Pi(x1,..,xi) (sometimes called bounding function) to test whether the
vector being formed has any chance of success. Desired solution expressed as an ntuple (x1,x2,
…,xn) where xi are chosen from some set Si.
· If |Si|=mi, m=m1m2..mn candidates are possible
· Yielding the same answer with far fewer than m trials
· Advantage : if it is realized that the partial vector (x 1,..,xi) can in no way lead to an
optimum solution, then mi+1…mn possible test vectors can be ignored entirely.”
Explicit constraints are rules that restrict each xi to take on values only from a given set.
· Explicit constraints depend on the particular instance I of problem being solved
· All tuples that satisfy the explicit constraints define a possible solution space for I
· Examples of explicit constraints
ALGORITHM Backtrack(X[1..i])
//Gives a template of a generic backtracking algorithm
//Input X[1..i] specifies the first I promising components of a solution
//Output All the tuples representing the problem’s solution
if X[1..i] is a solution write X[1..i]
else
for each element xÎSi+1 consistent with X[1..i] and the constraints do
X[i+1] Å x
Backtrack(X[1..i+1])
14. What are the tricks used to reduce the size of the state-space tree?
The various tricks are ._Exploit the symmetry often present in combinatorial problems. So
some solutions can be obtained by the reflection of others. This cuts the size of the tree by about
half.
Preassign values to one or more components of a solution
Rearranging the data of a given instance.
17. Mention two reasons to terminate a search path at the current node in a state-space
tree of a branch and bound algorithm.
The value of the node's bound is not better than the value of the best solution seen so far.
The node represents no feasible solutions because the constraints of the problem are already
violated.
21. What is the method used to find the solution in n-queen problem by symmetry?
The board of the n-queens problem has several symmetries so that some solutions can be
obtained by other reflections. Placements in the last n/2 columns need not be considered, because
any solution with the first queen in square (1, i), n/2£ i£ n can be obtained by reflection from a
solution with the first queen in square (1, n-i+1)
22. How can you represent the solution for 2 queen’s problem?
There is no solution for 2 Queen’s problem since however the queens are arranged both
queens would be in same diagonal or column.
23. How can you represent the solution for 8 queen’s problem?
All solutions represented as 8-tuples (x1, x2,…, x8) where xi is the column on which
queen “i” is placed.
Constraints are,
o Explicit constraints
Si={1,2,…,8}
o Implicit constraints
No two xi’s can be the same column or row (By this, solution space
reduced from 88 to 8!)
No two queens can be on the same diagonal
Answer state:
· Answer states are that solution states s for which the path from root node to s defines a
tuple that is a member of the set of solutions
· These states satisfy implicit constraints
Moreover, if the last vertex of the path duplicates the first one without ever repeating any
other vertex in between, then the path is called a Hamiltonian circuit
37. State the equation which is used to represent 2 queens are in same diagonal.
Let the diagonals be (i,j) and (k,l)
The equation to represent 2 queens are in same diagonal is
i+j=k+l j-l=k-I or
i-j=k-l j-l=i-k
UNIT 5
Let G=(V, E) be a graph and G1=(V1, E1)..., Gm=(Vm, Em) be its connected components.
Every vertex is in exactly in one connected component, that is, the components partition
(1) V. formally, for all i ≠ j, Vi∩ Vj=ø. Further, V=V1∪...∪ Vm and E=E1∪...∪ Em.
13. How do identify connected component using breath first search?
· BFS can be used to determine whether G is connected
· Using BFS all newly visited vertices are put on the list.
· Sub graph formed by the vertices on this list make up a connected component.
A minimum spanning tree of a weighted connected graph is its spanning tree of the
smallest weight, where the weight of a tree is defined as the sum of the weights on all its edges.
Kruskal's algorithm looks at a minimum spanning tree for a weighted connected graph G
=(V,E) as an acyclic subgraph with | V| - 1 edges for which the sum of the edge weights is the
smallest.
24. List down the bi connected components for the following connected graph.
Connected graph is
27. What are the types search strategies in branch and bound?
· FIFO search
· LIFO search
· LC search
38. What are the two classes of non polynomial time problems?
· NP- hard
· NP-complete