Professional Documents
Culture Documents
UNIT I
1. Define an algorithm
A: Algorithm is defined as a collection of unambiguous instructions occurring in some
specific sequence and such an algorithm should produce output for given set of input in
finite amount of time.
2. List out the criteria that all algorithms must satisfy.
A: All algorithms must satisfy the following criteria:
Input: zero or more quantities are externally supplied.
Output: at least one quantity is produced.
Definiteness: Each instruction is clear and unambiguous
Finiteness: If we trace the instructions of an algorithm, then for all cases, the
algorithm terminates after a finite number of steps.
Effectiveness: Every instruction must be very basic and feasible.
Once an algorithm is given for a problem and decided to be correct, an important
step is to determine how much in the way of resources such as time or space, the
algorithm will require.
3. What is meant by computational procedure?
A: Algorithms that are definite (clear, unambiguous) and effective (feasible) are known as
computational procedures.
4. Define program.
A: A program is the expression of an algorithm in a programming language.
5. Define an Abstract Data Type.
A: An ADT (Abstract Data Type) is a mathematical model which gives a set of utilities
available to the user but never states the details of its implementation.
6. List out the steps in designing an algorithm.
A: The various steps in designing an algorithm are
Understanding the problem.
Ascertaining the capabilities of a computational device
Choosing between exact and approximate problem solving
Deciding on appropriate data structures
Algorithm Design techniques
Methods of specifying an algorithm
Proving an algorithms correctness
Analyzing an algorithm
Coding an Algorithm.
Performing matrix
multiplication
Actual
multiplication of the
row and column
elements in matrix
Computing GCD of
two numbers
Two numbers
Division
Then we compute total number of time taken by this basic operation. The running
time of basic operation can be obtained approximately using the following the
formula:
T(n) Cop C(n)
Where
T(n) is Running time of BASIC OPERATION
Cop is time taken by the basic operation to execute
C(n) is Number of times the operation needs to be executed.
iii.
If there is alternative ways to solve a problem, then the decision on which to use
will be based primarily on the Expected Performance difference among these
solutions.
24. Define Order of Growth.
A: Measuring the performance of an algorithm in relation with the input size n is called
order of growth. For example, the order of growth for varying input size of n is as given
below:
n
log n
n log n
n2
2n
1
0
0
1
2
2
1
2
4
4
4
2
8
16
16
8
3
24
64
256
16
4
64
256
65536
32
5
160
1024
4294967296
25. What is time space trade-off?
A: Time space trade-off is basically a situation where either space efficiency (memory
utilization) can be achieved at the cost of time or time efficiency (performance efficiency)
can be achieved at the cost of memory.
26. List out the techniques that exploits space for time trade-offs?
A: The techniques that exploits space for time trade-offs are
Input enhancement
Pre-structuring
27. Define Input enhancement.
A: It is a main technique that exploits space for time trade-offs. The idea is to preprocess
the problems input in whole or in part and store the additional information obtained in
order to accelerate solving the problem afterward. E.g. counting methods for sorting,
Boyer Moore Algorithm for string matching.
28. Define Pre-structuring.
A: It is a main technique that exploits space for time trade-offs. It uses extra space to
facilitate a faster access and or more flexible access to the data. E.g. hashing, B trees.
29. What is meant by asymptotic notations?
A: To choose the best algorithm, we need to check efficiency of each algorithm. The
efficiency can be measured by computing the time complexity of each algorithm. This is
done using notations called asymptotic notations. In short, asymptotic notation is a
shorthand way to represent the time complexity. The standard notations used are O[Big
Oh], [Omega] and [Theta].
30. Define Big Oh Notation.
A: Let f and g be two functions defined from a set of natural numbers to the set of nonnegative real numbers, that is, f, g: NR0. It is said that f(n) = O( g(n) ), if there exists
two positive constants c R and n0 N such that f(n) c g(n), for all n n 0. Here, the
function f(n) is bound above by the constant multiple of g(n).
31. Define Omega Notation.
A: Let f and g be two functions defined from a set of natural numbers to the set of nonnegative real numbers, that is, f, g: NR0. It is said that f(n) = ( g(n) ), if there exists
two positive constants c R and n0 N such that f(n) c g(n), for all n n 0. Here, the
function f(n) is bound below by the constant multiple of g(n).
32. Define Theta Notation.
A: Let f and g be two functions defined from a set of natural numbers to the set of
non-negative real numbers, that is, f, g: NR0. It is said that f(n) = ( g(n) ), if there
exists three positive constants c1, c2 R and n0 N such that c1 g(n) f(n) c2 g(n), for all
n n0. Here, the function f(n) is bound lower by the constant multiple of g(n). Here the
function f(n) is bound both above and below by another function g(n).
33. Define conditional asymptotic notation.
A: Many algorithms are easier to analyse if we impose conditions on them initially.
Imposing such conditions, when we specify asymptotic value, it is called conditional
asymptotic notation. Let f, g: NR0, it is said that f(n) = O (g(n) | A(n)), read as g(n)
when A(n), if there exists two positive constants c R and n 0 N such that A(n) [f(n)
c g(n)], for all n n0.
34. What are the properties of order of growth?
A: If f1(n) is O(g1(n)) and f2(n) is O(g2(n)), then f1(n) + f2(n) O(max(g1(n)+ g2(n))).
1. polynomials of degree m (nm) that means maximum degree is considered from
the polynomial for order of growth (nm)
2. O(1) < O (log n)< O( n) < O(n2)< O(n3)<O(2n)
3. exponential functions an have different orders of growth for different values of a.
35. Define recurrence equation.
A: It is an equation that defines a sequence recursively. It is normally in the following
form:
T(n) = T(n-1) + n
for n>0___________ 1
T(0) = 0
_________________ 2
1 is called recurrence relation
2 is called the initial condition.
The general solution to the recursive function specifies some formula.
Eg: f(n) = 2f(n-1) + 1 for n>1
f(1) = 1Then by solving this recurrence relation we get, f(n) = 2n 1.
36. What is meant by divide and conquer?
A:
A technique for designing algorithms that decompose instance into smaller
sub-instance of the same problem, solving the sub-instances independently and
combining the sub solutions to obtain the solution of original instance is called divide
and conquer. It uses the top-down approach to obtain solutions of problems.
37. Define divide and conquer algorithm.
A:
Formally, divide and conquer algorithm consists of the following major
phases: Divide: Breaking the problem( if the input size is too large) into several subproblems that are similar to the original problem but smaller in size.
Conquer: conquer recursively to solve the sub-problems.
Combine: Take the solutions of sub-problems and merge these solutions into a
solution for the original problem.
38. Give the recurrence relation for divide and conquer algorithm.
A:
T(N) = g(n)
if n is small
aT(N/b)+f(n)
Otherwise
39. Give the examples of divide and conquer algorithm.
A:
i Sorting: merge sort and quick sort
ii Binary tree traversals
iii Binary Search
iv Multiplication of large integers
40. What is meant by binary search?
A:
Binary Search is an efficient searching method. While searching the
elements using this method the most essential thing is that the elements must be a presorted one.
41. Give the divide and conquer steps of binary search.
A:
Let A[m] be the middle element of array A and K be the search key. Then
there are three conditions that needs to be tested while searching the array using this
method:
i.
If K= A[m] then desired element is found
ii.
Otherwise divide the array into two sub-arrays about half as large. If
K< A[m] then search the left sub-list
iii.
Otherwise if K> A[m] then search the right sub-list
UNIT 2
1) What is meant by greedy algorithm?
A:
Greedy algorithm is an algorithmic design technique mainly used to solve
optimization problems. They take decisions on the basis of information at hand
without worrying about the effect these decisions may have in the future. This
approach suggests constructing a solution through a sequence of steps, each
expanding a partially constructed solution obtained so far, until a complete solution to
the problem is reached. On each step, the choice made must be
Feasible: it should satisfy problems constraints
Locally optimal: Among all feasible solutions the best choice is made
Irrevocable: Once the choice is made then it should not be changed on the
Sub-sequent steps of the algorithm.
2) What is meant by feasible solution?
A:
Solutions that satisfy problems constraints
3) What is meant by locally optimal solution?
A:
The best choice among all feasible solutions is called a locally optimal
solution
4) What is meant by irrevocable solution?
A: Once the choice is made then it should not be changed on the sub-sequent steps of
the algorithm.
5) What are the functions of greedy algorithm?
A:
The greedy approach consists of 4 functions
A function that checks whether a given set of candidates give a solution.
A function that checks the feasibility of a set of candidates
A selection function which chooses some candidate which has not yet
been chosen
An objective function which gives the value of a solution.
6) Write down the advantages and disadvantages of greedy algorithm?
A: Advantage:
Greedy algorithms work fast when they work simple algorithms,easy to
implement.
Disadvantage:
Greedy algorithms dont always work.Hard to prove correct.
7) What is meant by container loading?
A:
In this problem, the ship is to be loaded with containers. All containers are
the same size. Different containers may have different weights. At each stage, each
container is loaded into the ship. The total weight of all the containers must be less
than or equal to the capacity of ship.
source the shortest path to all other vertices is obtained.The main focus is not to find
only one single path but to find the shortest paths from any vertex to all other
remaining vertices.
22.What is the use of Dijkstras algorithm?
A: It is used to solve the single source shortest paths problem:for a given vertex
called the source in a weighted connected graph,find the shortes path to all its other
vertices.
UNIT 3
1) What is meant by dynamic programming?
A:
Dynamic Programming is an algorithm design technique that can be used
when the solution to a problem can be viewed as the result of a sequence of decisions.
For example, KNAPSACK: The solution to the knapsack problem can be viewed as
the result of a sequence of decisions. We have to decide the values of x i, 1in. First,
a decision is made on x1, then on x2, then on x3 and so on. An optimal sequence of
decisions maximizes the objective function pi xi. An optimal sequence of decisions
is obtained by the principle of optimality.
2) Define the principle of optimality
A:
Dynamic Programming works on the principle of optimality. The principle
of optimality states that an optimal sequence of decisions has the property that
whatever the initial state and decision are, the remaining decisions must constitute an
optimal sequence of decisions with regard to the state resulting from the first
decision.
3) Compare and contrast between Greedy algorithm and Dynamic programming
A:
GREEDY METHOD
DYNAMIC PROGRAMMING
Greedy method is used for Dynamic programming is also used
obtaining optimum solution
for obtaining optimum solution
An optimum solution is picked There is no special set of feasible
from a set of feasible solutions
solutions in this method
The optimum selection is without Considers all possible sequences in
revising previously generated order to obtain the optimum solution
solutions.
Only one decision sequence is ever Many decision sequences may be
generated
generated
There is no as such guarantee of It is guaranteed that the dynamic
getting optimum solution
programming will generate optimum
solution using principle of optimality
A:
i Multistage graphs
ii Shortest path problems
iii Longest common sub-sequence problem
iv Knapsack Problem.
5) Compare and contrast between Divide and Conquer and Dynamic programming
A:
DIVIDE AND CONQUER
DYNAMIC PROGRAMMING
The problem is divided into small Many decision sequences are
sub-problems. These are solved generated and all the overlapping
independently. Finally all the sub-instances are considered.
solutions of the sub-problems are
combined to get the whole solution
to the given problem
Less efficient because of rework More efficient than divide and
on solution
conquer strategy.
Uses top down approach of Uses bottom up approach of problem
problem
solving(
recursive solving( iterative method)
method)
Divide and Conquer splits its input It splits its input at ever possible
at specific deterministic points
split points rather than at a particular
point. After trying all split points, it
determines which split point is
optimal
Has logarithmic time complexity
Has polynomial time complexity
6) Define multistage graphs
A: A multistage graph G = (V, E) is a directed graph in which the vertices are
partitioned into k>= 2 disjoint sets Vi, 1 i k. The objective is to find a minimum
cost path from source (s) to sink (t). Each set Vi defines a stage in graph. That is,
every path from s to t starts in stage1, goes to stage2, then to stage3 and so on until
terminating in stage k. Hence we obtain minimum path length for each vertex at each
stage.eg: draw a multistage graph
7) What is meant by all pairs shortest path problem?
A:
The all pairs shortest path problem is to determine a matrix A such that
A(i, j) is the length of a shortest path from i to j. It is computed by solving n single
source problems as follows:
Often a path originates from vertex i and goes through some intermediate
vertices and terminates at vertex j.
If k is an intermediate vertex on this shortest path, then the sub-paths from
i to k and k to j must be shortest paths from i to k and k to j, respectively
Otherwise i to j path is not of minimum length
Therefore the principle of optimality holds.
8) Give any two properties of dynamic programming approach
A:
1) Optimal substructure: The dynamic programming technique makes use
of principle of optimality to find the optimal solution from sub-problems.
2) Overlapping sub-problems: The dynamic programming is a technique
in which the problem is divided into sub-problems. The solutions of sub-problems are
shared to get the final solution to the problem. It avoids repetition of work and we can
get the solution more efficiently.
9) What does dynamic programming have in common with divide and conquer?
What is the principle difference between the two?
A:
Both divide and conquer and dynamic programming solves the problem by
breaking it into number of sub-problems. In both these methods the solutions from
sub-problems are collected to get the final solution to the problem. But in divide and
conquer the sub-problems are solved independently whereas in dynamic
programming the sub-problems share the solutions among themselves. The dynamic
programming works on principle of optimality.
10) Give the commonly used designing steps for dynamic programming algorithm.
A:
Dynamic programming design involves 4 major steps
Characterize the structure of optimal solution.
Recursively define the value of an optimal solution.
By using bottom up technique compute the value of optimal solution.
Compute an optimal solution from computed information.
11) Define OBST
A: A binary search tree is organized so as to minimize the number of nodes visited in
all searches, given that it is known how often each word occurs. To increase the search
efficiency more frequently used words are arranged nearer to the root and less frequently
used words away from the root, with the help of probability value of each word. This type
of arrangement is called optimal binary search tree.
12) Give the components of dynamic programming.
A: A dynamic programming solution has three components:
i. Formulate the answer as a recurrence relation or recursive algorithm.
ii. Show that the number of different instances of the recurrence is bounded by a
polynomial.
iii.Specify an order of evaluation for the recurrence so that the solution is
obtained the way we desire.
UNIT 4
A:
11) How can you represent the solution for 8 queen problem?
A:
All solutions to the 8 queen problem can be represented as an 8-tuple
(x1,x2,,x8) where xi is the column on which queen i is placed.
For example: The Solution is expressed as an 8-tuple, (4,6,8,2,7,1,3,5)
Q
Q
Q
Q
Q
Q
Q
Q
12) Give the explicit and implicit constraint for 8 queen problem.
A:
Explicit constraint using 8-tuple formulation are Si = { 1,2,3,4,5,6,7,8 },
1 i 8. Therefore the solution space consists of 88 8-tuples
Implicit constraints are
No two xi can be the same, or all the queens must be in different columns
All solutions are permutations of the 8-tuple (1, 2, 3, 4, 5, 6, 7, 8)
Reduces the size of solution space from 88 to 8! tuples
No two queens can be on the same diagonal
13) Give the condition that says two queens are on the same diagonal?
A: The diagonal conflicts can be checked by the following formula:
P1 and P2 are two positions that are on same diagonal if
i + j = k + l or
ij=k-l
14) Define Subset sum Problem
A: Given n positive numbers wi, 1 i n, and m, find all subsets of wi whose sums
are m
For example, n = 4, w = (11, 13, 24, 7), and m = 31, the desired subsets
are (11, 13, 7) and (24, 7)
The solution vectors can also be represented by the indices of the numbers
as (1, 2, 4) and (3, 4)
All solutions are k-tuples, 1 k n
15) Give the explicit and implicit constraint for Subset sum Problem
A: Explicit constraints
xi = {j | j is an integer and 1 j n}
Implicit constraints
No two xi can be the same
wxi = m
xi < x i+1, 1 i k (total order in indices): Helps in avoiding the generation
of multiple instances of same subset; (1, 2, 4) and (1, 4, 2) are the same
subset
16) Define Problem state
A: Problem state is each node in the depth-first search tree
17) Define Solution states
A: Solution states are the problem states s for which the path from the root node to s
defines a tuple in the solution space
In variable tuple size formulation tree, all nodes are solution states
In fixed tuple size formulation tree, only the leaf nodes are solution states
Partitioned into disjoint sub-solution spaces at each internal node
18) Define Answer states
A: Answer states are those 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.
19) Define Static trees and Dynamic trees
A: Static trees are ones for which tree organizations are independent of the problem
instance being solved
Fixed tuple size formulation
Dynamic trees are ones for which tree organizations are dependent of the
problem instance being solved
20) Define Live node
A: Live node is a generated node for which all of the children have not been
generated.
21) Define Dead node
A: Dead node is a generated node that is not to be expanded any further
All the children of a dead node are already generated
Live nodes are killed using a bounding function to make them dead nodes
22) Define m colorability Problem
A: Let G be a graph and m be a given positive integer. The problem is to find
whether the nodes of G can be colored in such a way that no two adjacent nodes have
the same color yet only m colors are used. This is called as m colorability decision
problem.
E.g.: If d is the degree of the given graph, then it can be colored with d+1 colors. The
m colorability optimization problem asks for the smallest integer m for which the
graph G can be colored. This integer is referred to as the chromatic number of the
graph.
23) Define graph coloring problem
A: same answer as in question 22 with e.g.
24) Define Knapsack Problem
A: Given n items, each with profit value v and weight w i and a knapsack that can
hold at most weight W, the objective is to find
The xis constitute a zero-one-valued vector. The solution space for this problem
consists of the 2n distinct ways to assign zero or one values to the xis.
25.Define Hamiltonian circuit problem
A: The Hamiltonian is defined as a cycle that passes through all the vertices of the
grapg exactly once.It s a sequence of n+1 adjacent vertice Vi0,Vi1,..,Vi(n-1).V i0
where the first vertex of the sequence is asme as the last one while all the other n-1
vertices are distinct.
26. Define graph.
A graph G consists of a nonempty set V which is a nodes of the graph, a set E
which is the set of edges of the graph, and a mapping from the set for edge E to ta set of
pairs of elements of V. It can also be represented as G=(V,E)
27. List out the types of graph
Undirected graph
Directed graph
Weighted graph
Connected graph
Dense graph
Sparse graph
Multigraph
28. Define weighted graph.
A graph in which weights are assigned to every edge is called a weighted graph
29. Define undirected graph
A graph in which every edge is undirected is called a undirected graph.
30. Define directed graph.
A graph in which every edge is directed is called a undirected graph
31. Define connected graph.
A graph is said to be connected if for every pair of its vertices u and v there is a
path from u to v.
32. Define multigraph
A multigraph allows multiple edges between the same vertices.
33. Define complete graph
A graph with every pair of its vertices connected by an edge is called complete
graph.
34. Define connected components of a graph.
A connected component is the maximal subgraph of a given graph.
35. How can you represent the graph?
Graphs can be represented in two ways:
Adjacency matrix
Adjacency linked lists
36. List out the types of graph traversal.
Breadth first search
Depth first search
37. Differentiate between DFS and BFS
Data structures
No of vertex ordering
Edge types(Undirected graphs)
Applications
Efficiency for adjacent matrix
Efficiency for adjacent linked
lists
DFS
stack
2 orderings
Tree and back edge
Connectivity, acyclicity,
articulation points
(|n|)
(|V|+|E|)
BFS
queue
1 orderings
Tree and back edges
Connectivity, acyclicity,
minimum edge paths
(|n|)
(|V|+|E|)
Spanning tree obtained using a breadth first search is called breadth first
spanning tree.
42. Define biconnected graph.
A graph G is biconnected if and only if it contains no articulation points.
43. Define biconnected components.
A maximal biconnected subgraph is called biconnected components.
UNIT 5
1. What are the additional features required in branch-and-bound when compared to
backtracking?
Compared to backtracking, branch-and-bound requires:
A way to provide, for every node of a state space tree, a bound on the
best value of the objective function on any solution that can be obtained
by adding further components to the partial solution represented by the
node.
The value of the best solution seen so far.
2. List out the types of branch-and-bound technique.
First in first out
Least cost or max profit
3. Give the applications of branch and bound technique.
Assignment problem
Knapsack problem
Traveling salesperson problem
4. What is the assignment problem?
Assigning n people to n jobs so that the total cost of the assignment is as small
as possible. The instance of the problem is specified as a n by n cost matrix C so
that the problem can be stated as: select one element in each row of the matrix so
that no two selected items are in the same column and the sum is the smallest
possible.
5. Give the formula used to find the upper bound for knapsack problem.
A simple way to find the upper bound ub is to add v, the total value of the
items already selected, the product of the remaining capacity of the Knapsack Ww and the best per unit payoff among the remaining items, which v i+1/wi+1
ub = v + (W-w)( vi+1/wi+1)
6. What are the strengths of backtracking and branch-and-bound?
The strengths are as follows:
NP Hard
problems
NP Complete Problems