CS2251 Design and Analysis of Algorithms - Question Bank: Unit - I
CS2251 Design and Analysis of Algorithms - Question Bank: Unit - I
UNIT –I
Part - A
1. What is an Algorithm?
An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a
required output for any legitimate input in a finite amount of time
2. State the Euclid’s algorithm for finding GCD of two given numbers.
ALGORITHM Euclid (m, n)
om
//Input : Two nonnegative, not-both-zero integers m and n
while n ≠ 0 do
c
a.
r m mod n
di
m n
n r
in
return m.
in
.k
operation at a time. Accordingly, algorithms designed to be executed on such machines are called
Sequential algorithms.
w
The central assumption of the RAM model does not hold for some newer computers that can execute
operations concurrently, i.e., in parallel algorithms that take advantage of this capability are called
Parallel algorithms.
Page 1 of 14
A Pseudo code is a mixture of a natural language and programming language like constructs. A
pseudo code is usually more precise than a natural language, and its usage often yields more succinct
algorithm descriptions.
8. Define Flowchart.
A method of expressing an algorithm by a collection of connected geometric shapes containing
descriptions of the algorithm’s steps.
om
10.
Efficiency of an algorithm can be precisely defined and investigated with mathematical rigor. There
are two kinds of algorithm efficiency
i. Time Efficiency – Indicates how fast the algorithm runs
ii. Space Efficiency – Indicates how much extra memory the algorithm needs.
c
a.
11. What is generality of an algorithm?
It is a desirable characteristic of an algorithm. Generality of the problem the algorithm solves is
sometimes easier to design an algorithm for a problem posed in more general terms.
di
Optimality is about the complexity of the problem that algorithm solves. What is the minimum
amount of effort any algorithm will need to exert to solve the problem in question is called
in
algorithm’s Optimality.
13.
The sorting problem asks us to rearrange the items of a given list in ascending
order (or descending order)
w
w
The searching problem deals with finding a given value, called a search key, in a
given set.
Ex: if you want to sort a list of numbers in ascending order when the numbers are
given i n descending order. In this running time will be the longest.
Page 2 of 14
The ″Best case-Efficiency” of an algorithm is its efficiency for the Best-case input of size n, which is an
input(or inputs) of size n for which the algorithm runs the fastest among all possible inputs of that
size.
Ex: if you want to sort a list of numbers in ascending order when the numbers are
given i n ascending order. In this running time will be the smallest.
om
The “Amortized efficiency” applies not only a single run of an algorithm but rather to
a sequence of operations performed on the same data structure. It turns out that in
some situations a single operation can be expensive ,but the total time for an entire
sequence of n such operations is always significantly better than the worst case
c
efficiency of that single operation multiplied by n. This is known as “Amortized
efficiency”.
a.
di
19. How to measure the algorithm’s efficiency?
It is logical to investigate the algorithm’s efficiency as a function of some parameter n indicating the
in
Let Cop be the time of execution of an algorithm’s basic iteration on a particular computer and let C
(n) be the number of times this operation needs to be executed for this algorithm. Then we can
w
estimate the running time T(n) of a program implementing this algorithm on that computer by the
formula
T(n) ≈ Cop C(n)
Page 3 of 14
A function t(n) is said to be in O(g(n)) denoted 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 exist some positive constant c and some non
negative integer n0 such that
T (n) < c g (n) for n > n0
om
T (n) < c g (n) for n > n0
c
a.
27. Define Θ - notation
di
A function t(n) is said to be in Θ(g(n)), denoted t(n) ∈ Θ (g(n)), if t(n) is bounded both above and
below by some positive constant multiples of g(n) for all large n, i.e., if there exist some positive
in
PART - B
algorithm. (6)
Page 4 of 14
(b) Explain the various asymptotic notations used in algorithm design. (6)
3. (a) Explain the general framework for analyzing the efficiency of algorithm. (8)
(b) Find the complexity C(n) of the algorithm for the worst
om
case,best case and average case.(Evaluate average case complexity for n=3,Where n is the
c
7. Set up & solve a recurrence relation for the number of key comparisons made by above
a.
pseudo code. (4)
di
in
in
UNIT II
Part - A
.k
w
Divide and conquer is probably the best known general algorithm design technique. It work
according to the following general plan
w
i)A problem's instance is divided into several smaller instances of the same problem, ideally of about
the same size.
iii) If necessary, the solutions obtained for the smaller instances are combined to get a solution to the
original problem
Merge sort is a perfect example of a successful application of the divide and conquer technique. It
sorts a given array A[0...n-l] by dividing it into two halves A[0...[n/2] - 1] and A[[n/2]....n-l], sorting each of
them recursively, and then merging the two smaller sorted arrays into a single sorted one.
CS2251 - Design and Analysis of Algorithms Question Bank
Page 5 of 14
3) Define Binary Search
Binary Search is remarkably efficient algorithm for searching in a sorted array. It works by
comparing a search key K with the array's 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 for the
second half if K > A[m]
4) What can we say about the average case efficiency of binary search?
A sophisticated analysis shows that the average number of key comparisons made by binary search
om
is only slightly smaller than that in the worst case
Cavg(n) ≈ log2n
c
5) Define Binary Tree
a.
A binary tree T is defined as a finite set of nodes that is either empty or consists of s root and two
di
disjoint binary trees TL, and TR called, respectively the left and right subtree of the root.
in
Since the binary tree definition itself divides a binary tree into two smaller structures of the same
.k
type, the left subtree and the right subtree, many problems about binary trees can be solved by applying the
divide-conquer technique.
w
w
To draw the tree's extension by replacing the empty subtrees by special nodes.The extra nodes
shown by little squares are called external. The original nodes shown by littile circles are called internal.
PREORDER -The root is visited before the left and right subtrees are visited (in that order)
INORDER -The root is visited after visiting its left subtree but before visiting the right Subtree
POSTORDER - The root is visited after visiting the left and right subtrees(in that order)
Page 6 of 14
The Internal Path Length I of an extended binary tree is defined as the sum of the lengths of the paths
- taken over all internal nodes- from the root to each internal node.
The External Path Length E of an extended binary tree is defined as the sum of the lengths of the
paths - taken over all external nodes- from the root to each external node.
om
to the problem is reached. On each step, the choice made must be feasible, locally optimal and irrevocable.
c
A Spanning Tree of a connected graph is its connected acyclic subgraph(i.e., a tree) that contains all
a.
the vertices of the graph.
di
A minimum spanning tree of a weighted connected graph is its spanning tree of the smallest weight
in
,where the weight of a tree is defined as the sum of the weights on all its edges.
.k
A min-heap is a complete binary tree in which every element is less than or equal to its children. All
w
the principal properties of heaps remain valid for min-heaps, with some obvious modifications.
w
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.
Prim's algorithm is a greedy algorithm for constructing a minimum spanning tree of a weighted
connected graph.It works by attaching to a previously constructed subtree a vertex to the vertices already in
the tree.
Dijkstra's algorithm solves the single - source shortest - path problem of finding shortest paths from
a given vertex (the source) to all the other vertices of a weighted graph or digraph. It works as prim's
CS2251 - Design and Analysis of Algorithms Question Bank
Page 7 of 14
algorithm but compares path lengths rather than edge lengths. Dijkstra's algorithm always yields a correct
solution for a graph with nonnegative weights.
Part - B
1) Write a pseudo code for divide & conquer algorithm for merging two sorted arrays in to a single sorted
one.Explain with example. (12)
2) Construct a minimum spanning tree using Kruskal’s algorithm with your own example. (10)
om
4) Explain Dijikstra algorithm (8)
5) Define Spanning tree.Discuss design steps in Prim’s algorithm to construct minimum spanning
UNIT III
.k
Part - A
w
Dynamic programming is a technique for solving problems with overlapping problems. Typically,
these subproblems arise from a recurrence relating a solution to a given problem with solutions to its smaller
w
subproblems of the same type. Rather than solving overlapping subproblems again and again, dynamic
programming suggests solving each of the smaller sub problems only once and recording the results in a table
from which we can then obtain a solution to the original problem.
(a+b)n =(n,0)an+..........+C(n,i)an-ibi+.......+C(n,n)bn
Page 8 of 14
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 ith 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.
om
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.
It says that an optimal solution to any instance of an optimization problem is composed of optimal
w
One of the principal application 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.
Page 9 of 14
Given n items of known weights w1,w2...........wn and values v1,v2............vn and a knapsack of capacity W,
find the most valuable subset of the items that fit into the knapsack.(Assuming all the weights and the
knapsack's capacity are positive integers the item values do not have to be integers.)
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.
om
a manner that either the distance is minimum or cost is minimum. This is known as
traveling salesman problem.
c
Part - B
a.
di
1) Solve the all pair shortest path problem for the diagraph with the weighted matrix given below:-
in
abcd
a0∞3∞
in
b20∞∞
.k
c∞701
w
d 6 ∞ ∞ 0(16)
w
w
6) Discuss the solution for Travelling salesman problem using branch & bound technique. (16)
Page 10 of 14
UNIT IV
Part - A
l) Explain Backtracking
The principal idea is to construct solutions one component at a time and evaluate such partially
constructed candidates as follows.
> If a partially constructed solution can be developed further without violating the problem's constraints, it is
done by taking the first remaininig legitimate option for the next component.
> If there is no legitimate option for the next component, no alternatives for any remaining component need
om
to be considered.
c
In this case, the algorithm backtracks to replace the last component of the partially constructed solution with
its next option
a.
di
2) Explain State Space Tree
in
If it is convenient to implement backtracking by constructing a tree of choices being made, the tree is
called a state space tree. Its root represents an initial state before the search for a solution begins.
in
.k
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 same column or on the same diagonal.
set S={S1,S2,..........Sn} of n positive integers whose sum is equal to a given positive integer d.
Page 11 of 14
Compared to backtracking, branch and bound requires
The idea to be strengthened further if we deal with an optimization problem, one that seeks to minimize or
maximize an objective function, usually subject to some constraints.
A feasible solution is a point in the problem's search space that satisfies all the problem's constraints.
Ex: A Hamiltonian Circuit in the traveling salesman problem. A subset of items whose total weight does not
exceed the knapsack's Capacity
om
Is a feasible solution with the best value of the objective function
The value of the node's bound is not better than the value of the best solution seen so far. The node
in
represents no feasible solutions because the constraints of the problem are already violated.
.k
vertices of a graph so that no two adjacent vertices are the same color.
w
w
Find the most valuable subset of n items of given positive integer weights and values that fit into a
knapsack of a given positive integer capacity.
Part - B
1. Explain the 8-Queen’s problem & discuss the possible solutions. (16)
2. Solve the following instance of the knapsack problem by the branch & bound algorithm. (16)
3. Apply backtracking technique to solve the following instance of subset sum problem :
Page 12 of 14
S={1,3,4,5} and d=11 (16)
5. Explain subset sum problem & discuss the possible solution strategies using backtracking.(16)
UNIT V
Part - A
Problems that can be solved in polynomial time are called tractable problems, problems that cannot
om
be solved in polynomial time are called intractable problems.
Class P is a class of decision problems that can be solved in polynomial time by(deterministic)
algorithms. This class of problems is called polynomial.
.k
w
If the decision problem cannot be solved in polynomial time, and if the decision problems cannot be
solved at all by any algorithm. Such problems are called Undecidable.
w
Given a computer program and an input to it,determine whether the program will halt on that input
or continue working indefinitely on it.
Class NP is the class of decision problems that can be solved by nondeterministic polynomial
algorithms.Most decision problems are in NP. First of all, this class includes all the problems in P. This class of
problems is called Nondeterministic polynomial.
Page 13 of 14
7)Explain NP-complete problems
1) it belongs to class NP
i) t maps all yes instances of d1 to yes instances odf d2 and all no instances of dl to no instances ofd2
om
9) Define a Heuristic
A heuristic is a common-sense rule drawn from experience rather than from a mathematically
proved assertion.
c
Ex: Going to the nearest unvisited city in the traveling salesman problem is a good illustration for
a.
Heuristic
di
10) Explain NP-Hard problems
The notion of an NP-hard problem can be defined more formally by extending the notion of polynomial
in
reducability to problems that are not necessary in class NP including optimization problems.
in
11)Define Traversals.
When the search necessarilyinvolves the examination of every vertex in the object being searched it
.k
is called a traversal.
A vertex v in a connected graph G is an articulation point if and only if the deletion of vertex v
together with all edged incident to v disconnects the graph in to two or more nonempty components.
Part - B
1. Give a suitable example & explain the Breadth first search & Depth first search. (16)
2. Explain about biconnected components with example.
3. Briefly explain NP-Hard and NP-Completeness with examples.
4. Explain about 0/1 Knapsack Problem using branch and bound with example.
-
Page 14 of 14