Cs1201 Design and Analysis of Algorithm
Cs1201 Design and Analysis of Algorithm
Problem
Algorithm
Input
Computer
Output
5. What is the formula used in Euclids algorithm for finding the greatest
common divisor of two numbers?
Euclids 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.
-notation?
(g(n)), if t(n) is
A function t(n) is said to be in (g(n)), denoted by t(n)
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?
(g(n)), if t(n) is
A function t(n) is said to be in (g(n)), denoted by t(n)
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
ca = c
i
i=l
ai
i=l
(a b ) = a
i
i=l
i=l
i=l
i =
i=0
i = 1+2+3++n = n(n+1)/2 n /2
2
(n2)
i =1
29. Write the general plan for analyzing the efficiency for non-recursive
algorithms.
The various steps include
Decide on a parameter indicating inputs 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 algorithms 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.
30. Give a non-recursive algorithm for element uniqueness problem.
ALGORITHM UniqueElements(A[0..n-1])
//Checks whether all the elements in a given array are distinct
//Input :An array A[0..n-1]
//Output Returns true if all elements in A are distinct and false
//otherwise
for I to n-2 do
for j I+1 to n-1 do
if A[I] = A[j] return false
return true
31. 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
=(1+ 5 )/2
=(1- 5 )/2
41. Write a recursive algorithm for computing the nth fibonacci number?
ALGORITHM F(n)
// Computes the nth Fibonacci number recursively by using the definition
// Input A non-negative integer n
// Output The nth Fibonacci number
if n 1 return n
else return F(n-1)+F(n-2)
42. Write a non-recursive algorithm for computing the nth fibonacci number.
ALGORITHM Fib(n)
// Computes the nth Fibonacci number iteratively by using its definition
// Input A non-negative integer n
// Output The nth Fibonacci number
F[0] 0;
F (n 1) F (n) 01
F (n) F (n + 1) = 11
matrix powers
44. What is the general plan for the Empirical Analysis of Algorithm Efficiency?
The general plan is as follows
Understand the experiments purpose.
Decide on the efficiency metric M to be measured & the measurement
unit.
Decide on characteristics of the input sample
Prepare a program implementing the algorithm for the experimentation
Generate a sample of inputs
Run the algorithm on the sample input and record the data observed
Analyze the data obtained
45. Give the various system commands used for timing the program
implementing the algorithm.
The system commands are as follows
UNIX
time command
C & C++
function clock
Java
method currentTimeMillis() in the System class
46. Mention the linear congruential method for generating pseudorandom
numbers.
ALGORITHM Random(n,m,seed,a,b)
//Generates a sequence of n pseudorandom numbers using linear
congruential //method
//Input A positive integer n and positive integer parameters m, seed, a, b
//Output A sequence r1,..,rn of n pseudorandom integers uniformly
distributed //among integer values between 0 and m-1
r0 seed
for I 1 to n do
ri (a*ri-1+b) mod m
47. What are the guidelines in choosing the values of m, seed, a, b for linear
congruential method
The recommendations are as follows
seed May be chosen arbitrarily and is often set to current date and
time
A0, , Aj
10
11
(nd)
(ndlog n)
(nlogba)
if a<bd
if a=bd
if a>bd
12
13
78. What is decrease and conquer approach and mention its variations?
The decrease and conquer technique based on exploiting the
relationship between a solution to a given instance of a problem and a
14
15
16
17
18
101.
What are the two alternatives that are used to construct a heap?
The two alternatives to construct a heap are
Bottom-up heap construction
Top-down heap construction
102.
Give the pseudocode for Bottom-up heap construction.
ALGORITHM HeapBottomUp(H[1..n])
//Constructs a heap from the elements of the given array
//Input An array H[1..n] of orderable elements
//Output A heap H[1..n]
for I n/2 downto 1 do
k I ; v H[k]
heap false
while not heap and 2*k n do
j 2*k
if j < n
if H[j] < H[j+1] j j+1
if v H[j]
heap true
else H[k] H[j]; k j
H[k] v
103.
What is the algorithm to delete the roots key from the heap?
ALGORITHM
Exchange the roots key with the last key K of the heap
Decrease the heaps size by one
Heapify the smaller tree by sifting K down the tree exactly in the
same way as bottom-up heap construction. Verify the parental
dominance for K: if it holds stop the process, if not swap K with the
larger of its children and repeat this operation until the parental
dominance holds for K in its new position.
104.
19
106.
107.
What is the formula used by Warshalls 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 rkj(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 1s in R(k-1)
108.
Give the Warshalls algorithm.
ALGORITHM Warshall(A[1..n,1..n])
//Implements Warshalls 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]
(n)
return R
109.
Give the Floyds algorithm
ALGORITHM Floyd(W[1..n,1..n])
//Implements Floyds algorithm for the all-pair shortestpath problem
//Input The weight matrix W of a graph
//Output The distance matrix of the shortest paths lengths
DW
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]}
20
2n 1
c(n) =
111.
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
112.
113.
Mention the algorithm for Prims algorithm.
ALGORITHM Prim(G)
//Prims 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}
21
115.
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.
116.
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 us current distance label, update its labels by u*
and the weight of the edge between u* and u, respectively.
117.
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.
118.
119.
Give the Kruskals algorithm.
ALGORITHM Kruskal(G)
//Kruskals 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
22
121.
122.
123.
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.
124.
Mention the Huffmans 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 trees root to
indicate the trees 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
UNIT V ALGORITHM DESIGN METHODS
125.
What is backtracking?
23
127.
128.
129.
130.
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 nodes parent to consider the next possible solution to the problem, it
either stops or backtracks to continue searching for other possible solutions.
131.
132.
24
133.
134.
135.
j=i+1
136.
137.
Give a template for a generic backtracking algorithm.
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 problems solution
if X[1..i] is a solution write X[1..i]
else
for each element xSi+1 consistent with X[1..i] and the constraints do
X[i+1] x
Backtrack(X[1..i+1])
25
142.
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 branchand-bound algorithm can be terminated if
The value of the nodes 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.
143.
Backtracking
State-space tree is constructed using
depth-first search
Finds solutions for combinatorial nonoptimization problems
No bounds are associated with the
Branch-and-bound
State-space tree is constructed using
best-first search
Finds solutions for combinatorial
optimization problems
Bounds are associated with the each
26
144.
145.
146.
147.
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)
148.
149.
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 problems state space
and ends up generating all its elements, it provides a specific
technique for doing so, which can be of some value.
27