Daa
Daa
5. What is the formula used in Euclid’s algorithm for finding the greatest common divisor of two
numbers?
Euclid’s algorithm is based on repeatedly applying the equality
Gcd(m,n)=gcd(n,m mod n) until m mod n is equal to 0, since gcd(m,0)=m.
6. What are the three different algorithms used to find the gcd of two numbers?
The three algorithms used to find the gcd of two numbers are ._
Euclid’s algorithm ._
Consecutive integer checking algorithm ._
Middle school procedure
7. What are the fundamental steps involved algorithmic problem solving?in
The fundamental steps are ._Understanding the problem
Ascertain the capabilities of computational device
Choose between exact and approximate problem solving .
Decide on appropriate data structures .
Algorithm design techniques .
Methods for specifying the algorithm .
Proving an algorithms correctness .
Analyzing an algorithm .
Coding an algorithm
8. What is an algorithm design technique?
An algorithm design technique is a general approach to solving problems algorithmically that is
applicable to a variety of problems from different areas of computing.
9. What is pseudocode?
A pseudocode is a mixture of a natural language and programming language constructs to
specify an algorithm. A pseudocode is more precise than a natural language and its usage often yields
more concise algorithm descriptions.
10. What are the types of algorithm efficiencies?
The two types of algorithm efficiencies are
Time efficiency: indicates how fast the algorithm runs
Space efficiency: indicates how much extra memory the algorithm
needs
11. Mention some of the important problem types?
Some of the important problem types are as follows
Sorting
Searching
String processing
Graph problems
Combinatorial problems
Geometric problems
Numerical problems
12. What are the classical geometric problems?
The two classic geometric problems are
The closest pair problem: given n points in a plane find the closest pair
among them
The convex hull problem: find the smallest convex polygon that would
include all the points of a given set.
13. What are the steps involved in the analysis framework?
The various steps are as follows
Measuring the input’s size
Units for measuring running time
Orders of growth
Worst case, best case and average case efficiencies
14. What is the basic operation of an algorithm and how is it identified?
The most important operation of the algorithm is called the basic operation of the
algorithm, the operation that contributes the most to the total running time.
It can be identified easily because it is usually the most time consuming
operation in the algorithms innermost loop.
15. What is the running time of a program implementing the algorithm?
The running time T(n) is given by the following formula
T(n) § copC(n)
cop is the time of execution of an algorithm’s basic operation on a particular
computer and C(n) is the number of times this operation needs to be
executed for the particular algorithm.
16. What are exponential growth functions?
The functions 2n and n! are exponential growth functions, because these two
functions grow so fast that their values become astronomically large even for
rather smaller values of n.
17. What is worst-case efficiency?
The worst-case efficiency of an algorithm is its efficiency for the worst-case input
of size n, which is an input or inputs of size n for which the algorithm runs the
longest among all possible inputs of that size.
18. What is best-case efficiency?
The best-case efficiency of an algorithm is its efficiency for the best-case input
of size n, which is an input or inputs for which the algorithm runs the fastest
among all possible inputs of that size.
19. What is average case efficiency?
The average case efficiency of an algorithm is its efficiency for an average case
input of size n. It provides information about an algorithm behavior on a “typical”
or “random” input.
20. What is amortized efficiency?
In some situations a single operation can be expensive, but the total time for the
entire sequence of n such operations is always significantly better that the worst
case efficiency of that single operation multiplied by n. this is called amortized
efficiency.
21. Define O-notation?
A function t(n) is said to be in O(g(n)), denoted by t(n) O(g(n)), if t(n) is
bounded above by some constant multiple of g(n) for all large n, i.e., if there exists
some positive constant c and some nonnegative integer n0 such that
T(n) ” cg(n) for all n • n0
22. Define -notation?
A function t(n) is said to be in (g(n)), denoted by t(n) (g(n)), if t(n) is
bounded below by some constant multiple of g(n) for all large n, i.e., if there exists
some positive constant c and some nonnegative integer n0 such that
T(n) • cg(n) for all n • n0
23. Define -notation?
A function t(n) is said to be in (g(n)), denoted by t(n) (g(n)), if t(n) is
bounded both above & below by some constant multiple of g(n) for all large n, i.e.,
if there exists some positive constants c1 & c2 and some nonnegative integer
n0 such that
c2g(n) ” t(n) ” c1g(n) for all n • n0
24. Mention the useful property, which can be applied to the asymptotic notations and its
use?
If t1(n) O(g1(n)) and t2(n) O(g2(n)) then t1(n)+t2(n) max {g1(n),g2(n)} this
property is also true for and notations. This property will be useful in
analyzing algorithms that comprise of two consecutive executable parts.
25. What are the basic asymptotic efficiency classes?
The various basic efficiency classes are
Constant : 1
Logarithmic : log n
Linear : n
N-log-n : nlog n
Quadratic : n2
Cubic :n 3
Exponential : 2n
Factorial : n!
26. Write the general plan for analyzing the efficiency for non-recursive algorithms.
The various steps include
Decide on a parameter indicating input’s size.
Identify the algorithms basic operation.
Check whether the number of times the basic operation is executed
depends on size of input. If it depends on some additional property the
worst, average and best-case efficiencies have to be investigated
separately.
Set up a sum expressing the number of times the algorithm’s basic
operation is executed.
Using standard formulas and rules of manipulation, find a closed-form
formula for the count or at least establish its order of growth.
27. Mention the non-recursive algorithm for matrix multiplication?
ALGORITHM MatrixMultiplication(A[0..n-1,0..n-1], B[0..n-1,0..n-1])
//Multiplies two square matrices of order n by the definition based
//algorithm
//Input : Two n-by-n matrices A and B
//Output : Matrix C = AB
for I Å 0 to n-1 do
for j Å 0 to n-1 do
C[I,j] Å 0.0
for k Å 0 to n-1 do
C[I,j] Å C[I,j] + A[I,k]*B[k,j]
return C
28. Write a recursive algorithm to find the n-th factorial number.
ALGORITHM F(n)
// Computes n! recursively
// Input A non-negative integer n
// Output The value of n!
if n=0 return 1
else return F(n-1) * n
29. What is the recurrence relation to find out the number of multiplications and the initial
condition for finding the n-th factorial number?
The recurrence relation and initial condition for the number of multiplications
is
M(n)=M(n-1)+1 for n>0
M(0)=0
30. Write the general plan for analyzing the efficiency for recursive algorithms.
The various steps include
Decide on a parameter indicating input’s size.
Identify the algorithms basic operation.
Check whether the number of times the basic operation is executed
depends on size of input. If it depends on some additional property the
worst, average and best-case efficiencies have to be investigated
separately.
Set up a recurrence relation with the appropriate initial condition , for
the number of times the basic operation is executed.
Solve the recurrence or at least ascertain the orders of growth of its
solution.
31. Write a recursive algorithm for solving Tower of Hanoi problem.
ALGORITHM
To move n>1 disks from peg1 to peg3, with peg2 as auxiliary, first
move recursively n-1 disks from peg1 to peg2 with peg3 as auxiliary.
(ndd) if
(n log if
T(n)
n)(nlog
a) a<bdd d
b a=b
if a>b
Mergesort(C[0..(n/2)-1])
Merge(B,C,A)
49. What is the difference between quicksort and mergesort?
Both quicksort and mergesort use the divide-and-conquer technique in which the
given array is partitioned into subarrays and solved. The difference lies in the
technique that the arrays are partitioned. For mergesort the arrays are partitioned
according to their position and in quicksort they are partitioned according to the
element values.
50. Give the algorithm for Quicksort.
ALGORITHM Quicksort(A[l..r])
//Sorts a array by quicksort
//Input: A subarray A[l..r] of A[0..n-1], defined by the left and right indices l & r
//Output: the subarray A[l..r] sorted in nondecreasing order
if l < r
s Å Partition(A[l..r])
Quicksort(A[l..s-1])
Quicksort(A[s+1..r])
51. What is binary search?
Binary search is a remarkably efficient algorithm for searching in a sorted array. It
works by comparing a search key K with the arrays middle element A[m]. if they
match the algorithm stops; otherwise the same operation is repeated recursively for
the first half of the array if K < A[m] and the second half if K > A[m].
K
UNIT – II
1. What is greedy technique?
Greedy technique suggests a greedy grab of the best alternative available in the
hope that a sequence of locally optimal choices will yield a globally optimal solution
to the entire problem. The choice must be made as follows
™ Feasible : It has to satisfy the problem’s constraints
™ Locally optimal : It has to be the best local choice among all feasible choices
available on that step.
™ Irrevocable : Once made, it cannot be changed on a subsequent step
of the algorithm
2.Mention the algorithm for Prim’s algorithm.
ALGORITHM Prim(G)
//Prim’s algorithm for constructing a minimum spanning tree
//Input A weighted connected graph G= V , E
//Output ET, the set of edges composing a minimum spanning tree of G VT Å
{v0}
ET Å
for i Å 1 to |V|-1 do
Find the minimum-weight edge e*=(v*,u*) among all the edges (v,u)
such that v is in VT and u is in V-VT
V T Å VT {u*}
ET Å E T {e*}
return ET
3.What are the labels in Prim’s algorithm used for?
Prim’s algorithm makes it necessary to provide each vertex not in the current tree with the
information about the shortest edge connecting the vertex to a tree vertex. The information
is provided by attaching two labels toa vertex
™ The name of the nearest tree vertex
™ The length of the corresponding edge
4.How are the vertices not in the tree split into?
The vertices that are not in the tree are split into two sets
™ Fringe : It contains the vertices that are not in the tree but are adjacent
to atleast one tree vertex.
™ Unseen : All other vertices of the graph are called unseen because they are
yet to be affected by the algorithm.
5.What are the operations to be done after identifying a vertex u* to be added to the
tree?
After identifying a vertex u* to be added to the tree, the following two operations need to be
performed
™ Move u* from the set V-VT to the set of tree vertices VT.
™ For each remaining vertex u in V-VT that is connected to u* by a shorter
edge than the u’s current distance label, update its labels by u* and the weight of the edge
between u* and u, respectively.
6.What is a min-heap?
A min-heap is a mirror image of the heap structure. It is a complete binary
tree in which every element is less than or equal to its children. So the root of the
min-heap contains the smallest element.
7.What is the use of Kruskal’s algorithm and who discovered it?
Kruskal’s algorithm is one of the greedy techniques to solve the
minimum spanning tree problem. It was discovered by Joseph Kruskal when he
was a second-year graduate student.
8.Give the Kruskal’s algorithm.
ALGORITHM Kruskal(G)
//Kruskal’s algorithm for constructing a minimum spanning tree
//Input A weighted connected graph G= V , E
//Output ET, the set of edges composing a minimum spanning tree of G
sort E in non decreasing order of the edge weights w(ei1) ……… w(ei|E|)
ET Å
Ecounter Å 0
kÅ0
while ecounter < |V|-1
k Å k+1
if ET {eik} is acyclic
ET Å ET {eik}; ecounter Å ecounter + 1
return ET
9.What is a subset’s representative?
One element from each of the disjoint subsets in a collection is used as the
subset’s representative. Some implementations do not impose any specific
constraints on such a representative, others do so by requiring the smallest
element of each subset to be used as the subset’s representative.
10.What is the use of Dijksra’s algorithm?
Dijkstra’s algorithm is used to solve the single-source shortest-paths
problem: for a given vertex called the source in a weighted connected graph, find
the shortest path to all its other vertices. The single-source shortest-paths problem
asks for a family of paths, each leading from the source to a different vertex in the
graph, though some paths may have edges in common.
11.What is encoding and mention its types?
Encoding is the process in which a text of ‘n’ characters from some
alphabet is assigned with some sequence of bits called codewords. There are two
types of encoding they are
™ Fixed-length encoding
™ Variable-length encoding
12.What is the problem faced by variable-length encoding and how can it be avoided?
Variable-length encoding which assigns codewords of different lengths to different characters
introduces a problem of identifying how many bits of an encoded text represent the first
character or generally the ith character. To avoid this prefix-free codes or prefix codes
are used. In prefix codes, no codeword is a prefix of a codeword of another character.
13. Mention the Huffman’s algorithm.
ALGOITHM Huffman
Initialize n one-node trees and label them with the characters of the
alphabet. Record the frequency of each character in its tree’s root to
indicate the tree’s weight.
Repeat the following operation until a single tree is obtained. Find two trees
with the smallest weight. Make them the left and right subtree of
a new tree and record the sum of their weights in the root of the new tree
as its weight.
A tree constructed by the above algorithm is called Huffman tree and it defines
the Huffman code
14.What is minimum cost spanning tree?
A spanning tree of a graph is an undirected tree consisting of only those edges that are
necessary to connect all the vertices in the original graph.
A Spanning tree has a property that for any pair of vertices there exist only one path
between them and the insertion of an edge to a spanning tree form a unique cycle.
15. What is feasible solution?
A problem with N inputs will have some constraints. Any subsets that satisfy these
constraints are called a feasible solution.
16. What is optimal solution?
A feasible solution that either maximize or minimize a given objectives function is
called an optimal solution.
17. What are the application of the spanning tree?
1. Analysis of electrical circuits.
2. Shortest route problems
18. What is minimization problem?
A feasible solution that minimize a given objectives function is called minimization
problem.
19.What is maximization Problem?
A feasible solution that maximize a given objectives function is called an
maximization Problem.
UNIT- III
1.What is dynamic programming and who discovered it?
Dynamic programming is a technique for solving problems with
overlapping subproblems. These subproblems arise from a recurrence
relating a solution to a given problem with solutions to its smaller
subproblems only once and recording the results in a table from which the
solution to the original problem is obtained. It was invented by a prominent U.S
Mathematician, Richard Bellman in the 1950s.
2.Define transitive closure.
The transitive closure of a directed graph with ‘n’ vertices is defined as the n-
by-n Boolean matrix T={tij}, in which the elements in the ith row (1 i n) and the
jth column (1 j n) is 1 if there exists a non trivial directed path from the ith
vertex to the jth vertex otherwise, tij is 0
3.What is the formula used by Warshall’s algorithm?
The formula for generating the elements of matrix R(k) from the matrix R(k-1) is
rij(k) = rij(k-1) or rik(k-1) and
j rk (k-1)
This formula implies the following rule for generating elements of matrix R(k)
from the elements of matrix R(k-1)
If an element rij is 1 in R(k-1), it remains 1 in R(k)
If an element rij is 0 in R(k-1), it has to be changed to 1 in R(k) if and only
if the element in its row ‘i’ and column ‘k’ and the element in its row ‘k’
and column ‘j’ are both 1’s in R(k-1)
4.Give the Warshall’s algorithm.
ALGORITHM Warshall(A[1..n,1..n])
//Implements Warshall’s algorithm for computing the transitive closure
//Input The adjacency matrix A of a digraph with ‘n’ vertices
//Output The transitive closure of the digraph
R(0) Å A
for k Å 1 to n do
for i Å 1 to n do
for j Å 1 to n do
R(k)[I,j] Å R(k-1)[I,j] or R(k-1)[I,k] and R(k-1)[k,j]
return R (n)
5.Give the Floyd’s algorithm
ALGORITHM Floyd(W[1..n,1..n])
//Implements Floyd’s algorithm for the all-pair shortest–path problem
//Input The weight matrix W of a graph
//Output The distance matrix of the shortest paths’ lengths
DÅW
for k Å 1 to n do
for i Å 1 to n do
for j Å 1 to n do
D[I,j] Å min{D[I,j], D[I,k] + D[k,j]}
return D
6.How many binary search trees can be formed with ‘n’ keys?
The total number of binary search trees with ‘n’ keys is equal to the nth
Catalan number
2n 1
c(n) = for n >0, c(0) = 1,
n n
1
which grows to infinity as
fast as 4n/n1.5.
7.Give the algorithm used to find the optimal binary search tree.
ALGORITHM OptimalBST(P[1..n])
//Finds an optimal binary search tree by dynamic programming
//Input An array P[1..n] of search probabilities for a sorted list of ‘n’ keys
//Output Average number of comparisons in successful searches in the
optimal //BST and table R of subtrees’ roots in the optimal BST
for I Å 1 to n do
C[I,I-1] Å 0
C[I,I] Å P[I]
R[I,I] Å I
C[n+1,n] Å 0
for d Å 1 to n-1 do
for i Å 1 to n-d do j
Å i +d
minval Å
for k Å I to j do
if C[I,k-1]+C[k+1,j] < minval
minval Å C[I,k-1]+C[k+1,j]; kmin Å k
R[I,j] Å k
Sum ÅP[I]; for s Å I+1 to j do sum Å sum + P[s]
C[I,j] Å minval+sum
Return C[1,n], R
UNIT - IV
1.What is backtracking?
Backtracking constructs solutions one component at a time and such
partially constructed solutions are evaluated as follows
If a partially constructed solution can be developed further without
violating the problem’s constraints, it is done by taking the
first remaining legitimate option for the next component.
If there is no legitimate option for the next component, no alternatives
for the remaining component need to be considered. In this case, the
algorithm backtracks to replace the last component of the partially
constructed solution with its next option.
2.What is a state space tree?
The processing of backtracking is implemented by constructing a tree of choices
being made. This is called the state-space tree. Its root represents a initial state
before the search for a solution begins. The nodes of the first level in the tree
represent the choices made for the first component of the solution, the nodes in
the second level represent the choices for the second component and so on.
3.What is a promising node in the state-space tree?
A node in a state-space tree is said to be promising if it corresponds to a partially
constructed solution that may still lead to a complete solution.
4.What is a non-promising node in the state-space tree?
A node in a state-space tree is said to be promising if it corresponds to a partially
constructed solution that may still lead to a complete solution;otherwise it is called non-
promising.
5.What do leaves in the state space tree represent?
Leaves in the state-space tree represent either non-promising dead ends or complete
solutions found by the algorithm.
6,What is the manner in which the state-space tree for a backtracking algorithm is
constructed?
In the majority of cases, a state-space tree for backtracking algorithm is constructed in
the manner of depth-first search. If the current node is promising, its child is
generated by adding the first remaining legitimate option for the next component of a
solution, and the processing moves to this child.
If the current node turns out to be non-promising, the algorithm backtracks to the node’s
parent to consider the next possible solution to the problem, it either stops or
backtracks to continue searching for other possible solutions.
7.What is n-queens problem?
The problem is to place ‘n’ queens on an n-by-n chessboard so that no two queens attack
each other by being in the same row or in the column or in the same diagonal.
8.Draw the solution for the 4-queen problem.
Q Q
Q Q
Q Q
Q Q
15.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)
UNIT – V
1.When can a search path be terminated in a branch-and-bound algorithm?
A search path at the current node in a state-space tree of a branch-and-bound
algorithm can be terminated if
™ 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 solution because the constraints of the problem
are already violated.
™ The subset of feasible solutions represented by the node consists of a single point
in this case compare the value of the objective function for this feasible solution
with that of the best solution seen so far and update the latter with the former
if the new solution is better.
2.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.
3.What is best-first branch-and-bound?
It is sensible to consider a node with the best bound as the most promising,
although this does not preclude the possibility that an optimal solution will
ultimately belong to a different branch of the state-space tree. This strategy is
called best-first branch-and-bound.
5.What is knapsack problem?
Given n items of known weights wi and values vi, i=1,2,…,n, and a knapsack of
capacity W, find the most valuable subset of the items that fit the knapsack. It is
convenient to order the items of a given instance in descending order by their
value-to-weight ratios. Then the first item gives the best payoff per weight unit and
the last one gives the worst payoff per weight unit.
6.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 W-w and
the best per unit payoff among the remaining items, which
is vi+1/wi+1
ub = v + (W-w)( vi+1/wi+1)
7.What is the traveling salesman problem?
The problem can be modeled as a weighted graph, with the graph’s vertices
representing the cities and the edge weights specifying the distances. Then the
problem can be stated as finding the shortest Hamiltonian circuit of the graph,
where the Hamiltonian is defined as a cycle that passes through all the vertices of
the graph exactly once.
8.What are the strengths of backtracking and branch-and-bound?
The strengths are as follows
™ It is typically applied to difficult combinatorial problems for which no
efficient algorithm for finding exact solution possibly exist
™ It holds hope for solving some instances of nontrivial sizes in an
acceptable amount of time
™ Even if it does not eliminate any elements of a problem’s state space and
ends up generating all its elements, it provides a specific technique for
doing so, which can be of some value.