Page | 1
MODULE1 QUESTION BANK
1 Discuss the various steps in algorithm design and analysis process with the flow diagram
Ans Sequence of Steps in designing and analyzing an algorithm:
• Understanding the Problem
• Ascertaining the capabilities of the computational device
• Choosing between exact and Approximate Problem solving
• Algorithm design techniques
• Designing an algorithm and Data Structures
• Methods of Specifying an algorithm
• Proving an Algorithm’s Correctness
• Analysing an Algorithm
• Coding an Algorithm
Flow Chart:
Understanding the Problem:
• Read the Problem’s description carefully.
• Ask questions if you have any doubts about the problem.
• Do a few small examples by hand.
• Think about special cases.
• Ask questions again if needed.
An input to an algorithm specifies an instance of the problem.
Specifying the legitimate input to the algorithm is very important. If the inputs are not legitimate, then
the algorithm may crash on some inputs.
Ascertaining the capabilities of the Computational device.:
• In older systems, Von-Neumann machine architecture is captured by the device called RAM
(Random Access Machine). Its central assumption is that the instructions are executed one after the
other (one operation at a time). The algorithms designed to be executed on such machines are
called Sequential Algorithms.
• The newer computers can execute the operations concurrently, i.e., in parallel. Algorithms that take
the advantage of this capability are called as Parallel Algorithms.
2.
Page | 2
Choosingbetween Exact and Approximate Problem Solving:
• Two types of algorithms: Exact Algorithm and Approximation Algorithm.
• Approximation Algorithm can be used in the following situations:
• Some problems cannot be solved exactly for most of their instances like (i)
Extracting square-root, (ii) Solving non-linear equations, (iii) Evaluating definite
integrals etc…
• Available algorithms for solving a problem exactly can be unacceptably slow
because of the problem’s intrinsic complexity
• An approximation Algorithm can be a part of a more sophisticated algorithm that
solves the problem exactly.
Algorithm Design Techniques:
• Algorithm Design Technique is a general approach to solving problems algorithmically that is
applicable to a variety of problems from different areas of computing.
Designing an Algorithm and Data Structures: Algorithm + Data Structures = Program
Three ways of specifying Algorithm:
• Flowchart: It is a Pictorial representation of problem-solving steps.
• Natural Language Description based algorithm
• Pseudocode based algorithm: It is a mixture of Natural Language description and Programming
Language constructs. Pseudocode is more precise than Natural language description method.
Proving an Algorithm’s Correctness:
• We have to prove that algorithm yields a required result for every legitimate input in a finite
amount of time.
• A Common technique for proving the correctness is to use mathematical induction method because
an algorithm’s iterations provide a natural sequence of steps.
• For an approximation algorithm, we have to show that the error produced by the algorithm does not
exceed a predefined limit.
Analyzing an Algorithm:
• Characteristics of the Algorithms:
• Efficiency: Two types: Time Efficiency and Space Efficiency.
Time Efficiency indicates how fast the algorithm runs.
Space Efficiency indicates How much extra memory it uses?
• Simplicity: Simpler programs are easier to understand and easier to program. Sometimes, simpler
algorithms are more efficient than more complicated alternatives.
• Generality: Sometimes, it is easier to design an algorithm for a problem in more general terms.
Sometimes, it is impossible or more difficult to design more general algorithm. Sometimes, it is
unnecessary to design more general algorithms.
Coding an Algorithm:
• Every algorithm must be implemented as a computer program using one computer language.
• Program cannot be considered as correct unless the correctness of the program is proven.
• Validity of the program is established by testing.
2 Analyze the formal definitions of various asymptotic notations
Ans Big Oh Notation (O):
Let f(n) be the time efficiency of the algorithm. The Function f(n) is said be O(g(n)) denoted by f(n)
∈ O(g(n)) or f(n) = O(g(n)) if and only if there exists a positive constant c and a positive integer n0
satisfying the condition f(n) ≤ c * g(n) for all n ≥ n0.
3.
Page | 3
Big-Omega(Ω):
Let f(n) be the time efficiency of the algorithm. The Function f(n) is said be Ω(g(n))
denoted by f(n) ∈ Ω(g(n)) or f(n) = Ω(g(n)) if and only if there exists a positive constant
c and a positive integer n0 satisfying the condition f(n) ≥ c * g(n) for all n ≥ n0.
(iii) Big-Theta (Θ)
Let f(n) be the time efficiency of the algorithm. The Function f(n) is said be Θ(g(n)) denoted by f(n)
∈ Θ(g(n)) or f(n) = Θ(g(n)) if and only if there exists two positive constants c1 and c2 and a nonnegative
integer n0 satisfying the condition c1*g(n) ≤ f(n) ≤ c2*g(n) for all n ≥ n0.
3 Build the algorithm that multiply the 2 n x n matrix and Analyze its efficiency
Ans ALGORITHM MatrixMultiplication(A[0..n-1, 0..n-1)
// Multiplies two n x n Matrices.
// Input: Two n x n Matrices A and B
// Output: Matrix C A * B
for i 0 to n-1 do
for j 0 to n-1 do
C[i,j] 0
for(k 0 to n-1)
C[i,j] C[i,j] + A[i,k] * B[k,j]
return C
4.
Page | 4
Analysisof the Efficiency:
Let cm is the cost of multiplication operation and ca is the cost of the addition operation, then
the total cost of the matrix multiplication is:
4 Consider the following Algorithm
ALGORITHM Foo(n)
// Input: A nonnegative integer n
Sum 0
for i 1 to n do
Sum Sum +
𝒊
𝒊!
return Sum
(a) What does the algorithm compute?
(b) What is the basic Operation?
(c) How many times the basic operation executed?
(d) What is the efficiency class of this algorithm?
Ans
(a) Program Computes Summation of
𝒊
𝒊!
(b) What is the basic Operation: Summation operation is the Basic Operation (Sum Sum +
𝒊
𝒊!
)
(c) How many times the basic operation executed: ∑ 1
𝑛
𝑖=1 = n
(d) What is the efficiency class of this algorithm: T(n) = n ∈ Θ(n)
5 Assume that 𝐶(𝑛) =
1
2
n(n - 1) . How much longer will the algorithm run if we double its input size?
Ans Assume that 𝐶(𝑛) =
1
2
n(n - 1) . How much longer will the algorithm run if we double its input size?
Given that, Number of times basic operation executed is: 𝐶(𝑛) =
1
2
n(n - 1) =
1
2
n2
-
1
2
n ≈ n2
for large value of n.
That is, for a given value of n, C(n) = n2
If we double the input size, C(2n) = (2n)2
= 4n2
= 4*C(n)
So, Algorithm runs four times longer than the original algorithm.
7 For each of the following functions, indicate how much the function’s value will change if
its argument is increased fourfold.
(i) log2n (ii) √𝑛 (iii) n (iv) n2
(v) n3
(vi) 2n
Ans (i) given: log2n:
Let f(n) = log2n
If the argument is increased by fourfold, g(n) = f(4n) = log24n
Express g(n) in terms of f(n), we get
g(n) = log24n = log24 + log2n = 2 + f(n) (log2ab = log2a + log2b)
So, function’s value is increased by 2.
(ii) Given: √𝒏
Let f(n) = √𝑛
If the argument is increased by fourfold, g(n) = f(4n) = √4𝑛
Express g(n) in terms of f(n), we get
g(n) = √4𝑛 =√4* √𝑛 = 2 ∗ √𝑛 = 2 * f(n)
So, function’s value is increased by 2 times.
(iii) Given: n
Let f(n) = n
5.
Page | 5
Ifthe argument is increased by fourfold, g(n) = f(4n) = 4n
Express g(n) in terms of f(n), we get
g(n) = 4n = 4 * f(n)
So, function’s value is increased by 4 times.
(iv) Given: n2
Let f(n) = n2
If the argument is increased by fourfold, g(n) = f(4n) = (4n)2
Express g(n) in terms of f(n), we get
g(n) = (4n)2
= 42
* n2
= 42
* f(n)
So, function’s value is increased by 42
times.
(v) Given: n3
Let f(n) = n3
If the argument is increased by fourfold, g(n) = f(4n) = (4n)3
Express g(n) in terms of f(n), we get
g(n) = (4n)3
= 43
* n3
= 43
* f(n)
So, function’s value is increased by 43
times.
(vi) Given: 2n
Let f(n) = 2n
If the argument is increased by fourfold, g(n) = f(4n) = 24𝑛
Express g(n) in terms of f(n), we get,
g(n) = 24𝑛
= 23𝑛
* 2𝑛
= 2𝑛3
* 2𝑛
=2𝑛3
* f(n)
So, function’s value is increased by 2𝑛3
times.
8 Discuss various Basic Efficiency of Asymptotic Classes
Ans Various Basic Efficiency of Asymptotic Classes:
• 1 or any constant: Indicates that running time of a program is Constant.
• log N: Indicates that running time of a program is logarithmic. This running time
occurs in programs that solve larger problems by reducing the problem size by a
constant factor at each iteration of the loop (Binary Search).
• N: It indicates that running time of a program is linear. So, when N is 1000, the
running time is 1000 units. If N is doubled, so does the running time. (Linear Search)
• N log N: Indicates that running time of a program is N log N. Algorithms to sort
elements in ascending order such as Quicksort, Merge sort, Heapsort etc… will have
this running time.
• N2
: Indicates that the running time of a program is a quadratic. The algorithms
normally will have two embedded loops. For Example, sorting algorithms like
bubble sort, selection sort, addition, and subtraction of two matrices, etc…
• N3
: Indicates that the running time of a program is a cubic. The algorithms normally
will have three embedded loops. For Example, Matrix Multiplication, Algorithms to
solve simultaneous equations using Gauss Elimination method, …
• 2N
: Indicates that the running time of a program is exponential. Examples: Towers-
of-Hanoi, the algorithm that generates subsets of a given set etc....
• N!: Indicates that the running time of a program is a Factorial. The algorithms that
generate all permutations of set.
6.
Page | 6
9Indicate whether the first function of each of the following pairs has a smaller, same, or
larger growth then second function within a constant multiple.
(i) n(n+1) and 2000n2
(ii) 100 n2
and 0.01 n3
(iii) log2n and ln n
(iv) log2
2
n and log2n2
(v) 2n–1
and 2n
(vi) (n–1)! And n!
Ans
(i) n(n+1) and 2000n2
n(n + 1) = n2
+ n
Here, order of growth is 2
2000 n2
Here, order of growth is 2
n2
is same order of growth as that of 2000 n2
(ii) 100 n2
and 0.01 n3
100 n2
0.01 n3
Here, order of growth is 2 Here, order of growth is 3
So, 100 n2
is lower order of growth than that of 0.01 n3
(iii) log2n and ln n Log2n ln n
We know that, log2n =
𝑙𝑜𝑔𝑒𝑛
𝑙𝑜𝑔𝑒2
log2n =
𝑙𝑛 𝑛
𝑙𝑜𝑔𝑒2
Hence, ln n = log2n * loge2 (Here, loge2 is a constant)
Hence, both are having the same growth.
(iv) log2
2
n and log2n2
log2
2
n log2n2
we know that,
log2
2
n = log2n * log2n log2n2
= 2 * log2n
We know that log2n * log2n >>2 * log2n
Hence, log2n * log2n is having the larger order of growth
than 2 * log2n.
(v) 2n–1
and 2n
2n–1
2n
We know that, 2n–1
=
2𝑛
2
Hence, both are same order of growth.
(vi) (n–1)! And n! (n–1)! n!
We know that, n! = n * (n–1)!
(n–1)! is >> n * (n–1)!
Hence, (n–1)! is lower order of growth than n!
10 Define Space Efficiency (Space Complexity). Determine the Space complexity of the following
algorithm. (any algorithm will be given)
Example:
float sum(float a[], int n)
{
float sum;
int i;
sum = 0
for(i = 0; i < n; i++)
sum = sum + a[i];
return sum;
}
OR
7.
Page | 7
Writea algorithm to calculate the sum of the elements of the array using recursive function.
Ans Space Efficiency of an algorithm is the amount of memory required to run the program completely and
efficiently. The Space Requirement S of a program P denoted by S(P) is given by: S(P) = c + SP(I)
Where, c is fixed space requirement
SP(I) is variable space requirement.
c = program space + Data Space + Stack Space.
SP(I) = program space + Data Space + Stack Space + Space used during Recursion
Space Complexity Calculation for the above given algorithm
Array declaration as a parameter is interpreted by C Compiler as a pointer (Size is 4 bytes).
In the parameter, the array name passes only the address of the first element
OR
Algorithm to calculate the sum of the elements of the array using recursive function.
float SumArrayRecursive(float a[], int n)
{
if(n == -1) return 0;
return a[n] + sum(a, n-1);
}
11 What is time efficiency? Explain the various methods of Time Efficiency calculations.
Ans The Time Efficiency of an algorithm is the amount of computer time required to run the program till the
completion. Time Efficiency is measured using step count (or Program Step), that is, how many times a
program step is executed. Step count can be obtained in two methods:
• Counting Method and
• Tabular Method
Counting Method:
• Use a Global variable Count with initial value of 0 and insert the statement count++ for each
executable statement.
• This can be done for following different types of statements:
• Simple Statement
• Conditional Statement (if and if-else)
• Looping statement (for loop, while loop, do-while loop)
Counting method for simple statement:
• Insert count++ for every assignment statements, Expressions etc…
• Example:
sum = 0; count++;
printf(“Hello”); count++;
return a + b; count++;
8.
Page | 8
Countingmethod for Conditional statement:
Counting method for Looping statement:
Insert count++ inside the loop. Once the condition fails, the control will come out of the loop. Insert
count++ immediately after the loop.
Example:
Tabular Method:
• Determine the step count for each statement. It is known as Step per execution (s/e)
• Find out the number of times for each statement. It is known as Frequency
• Multiply s/e and frequency to get total steps.
• Add the total steps to get step count for entire function.
12 Calculate the step count for the given problem (Any type of problem can be given)
(Example: Calculate the step count for adding array of n elements in the array using counting method)
Step count for adding array of n elements in the array using counting method
13 Write an Algorithm to compute the sum of first n natural Numbers and calculate step count by
using Tabular method
Ans
9.
Page | 9
14Any of the following problems may be asked
• Let f(n) = 10n3
+ 8. Express f(n) using Big-Oh Notation.
• Let f(n) = 6*2n
+ n2
. Express f(n) using Big-Oh Notation.
• Let f(n) = 10n3
+ 5. Express f(n) using Big-Omega Notation.
• Let f(n) = 6*2n
+ n2
. Express f(n) using Big-Omega Notation.
• Let f(n) = 10n3
+ 5. Express f(n) using Big-Theta Notation.
• Let f(n) = 6*2n
+ n2
. Express f(n) using Big-Theta Notation.
• Prove that every polynomial p(n) = aknk
+ ak–1nk–1
+ … a1n1
+ a0 with ak > 0 belongs to O(nk
)
For Solution, refer my module 1 ppt from slide number 115 to 123
15 Prove the property of Asymptotic Notations
OR
Prove that If f1(n) ∈ O(g1(n)) and f2(n) ∈ O(g2(n)), then f1(n) + f2(n) ∈ O(max(g1(n), g2(n)))
Ans Proof: Let us consider four arbitrary values a1, a2, b1, and b2 such that a1 ≤ b1 and a2 ≤ b2.
From simple algebraic rule, We know that a1 + a2 ≤ 2 * max(b1, b2).
Given that f1(n) ∈ O(g1(n)) and f2(n) ∈ O(g2(n)).
By definition, there exists relations:
f1(n) ≤ c1*g1(n) for all values of n ≥ n1 ----- (1)
and
f2(n) ≤ c2*g2(n) for all values of n ≥ n2 ----- (2)
Let is assume that c3 = max(c1, c2) and n ≥ max(n1, n2).
By adding two equations, we get, f1(n) + f2(n) ≤ c1*g1(n) + c2*g2(n).
It is also true for f1(n) + f2(n) ≤ c3*g1(n) + c3*g2(n).
That is, f1(n) + f2(n) ≤ c3*[g1(n) + g2(n)]. It is also true for
f1(n) + f2(n) ≤ c3*[max(g1(n), g2(n)) + max(g1(n), g2(n))].
That is, f1(n) + f2(n) ≤ c3*2*max(g1(n), g2(n)).
So, f1(n) + f2(n) ∈ O(max(g1(n), g2(n))) where, c = 2c3 = 2*max(c1, c2)
n0 = max(n1, n2)
Hence the Proof.
16 Consider the algorithm that contains two parts:
• Sorting part requiring ½*n(n–1) comparisons. and
• Consecutive checking requires not more than n–1 comparison.
Find the time complexity for the above algorithm.
Ans For first part, the time complexity is O(n2
). For second part, the time complexity is O(n).
From the property of asymptotic notations, we know that,
If f1(n) ∈ O(g1(n)) and f2(n) ∈ O(g2(n)), then
f1(n) + f2(n) ∈ O(max(g1(n), g2(n)))
According to this property, the time complexity of entire algorithm is: O(max(n2
, n)) = O(n2)
17 Compare the following orders of growth
(i)
𝟏
𝟐
𝒏(𝒏 − 𝟏) and n2 (ii) log2n and √𝒏 (iii) n! and 2n
Ans
(i)
𝟏
𝟐
𝒏(𝒏 − 𝟏) and n2
• Given: f(n) =
1
2
𝑛(𝑛 − 1) and g(n) = n2
• We can find the limits of the ratio of the functions:
lim
𝑛→∞
𝑓(𝑛)
𝑔(𝑛)
= lim
𝑛→∞
1
2
𝑛(𝑛−1)
𝑛2
=
1
2
lim
𝑛→∞
𝑛2−𝑛
𝑛2
=
1
2
lim
𝑛→∞
(1 −
1
𝑛
)
=
1
2
(1 −
1
∞
) =
1
2
Since the limit is a positive constant, both f(n) and g(n) have the
same order of growth, i.e., O(n2
).
log2n and √𝒏 Given that f(n) = log2n and g(n) = √𝑛 we can find the limits as
shown below:
10.
Page | 10
lim
𝑛→∞
𝑓(𝑛)
𝑔(𝑛)
=lim
𝑛→∞
𝑙𝑜𝑔2𝑛
√𝑛
= lim
𝑛→∞
(
𝑙𝑜𝑔𝑒𝑛
𝑙𝑜𝑔𝑒2
)
√𝑛
= lim
𝑛→∞
1
𝑙𝑜𝑔𝑒2
(𝑙𝑜𝑔𝑒𝑛)
√𝑛
= lim
𝑛→∞
𝑙𝑜𝑔2𝑒(𝑙𝑜𝑔𝑒𝑛)
√𝑛
= 𝑙𝑜𝑔2𝑒 ( lim
𝑛→∞
𝑙𝑜𝑔𝑒𝑛
√𝑛
)
According to L Hospital’s Rule,
lim
𝑛→∞
𝑓(𝑛)
𝑔(𝑛)
= lim
𝑛→∞
𝑓′(𝑛)
𝑔′(𝑛)
= log2e( lim
𝑛→∞
(𝑙𝑜𝑔𝑒𝑛)′
(√𝑛)
′ ) -------------- (1)
(𝑙𝑜𝑔𝑒𝑛)′
= derivative of (𝑙𝑜𝑔𝑒𝑛) =
1
𝑛
------------ (2)
(√𝑛)
′
= derivative of √𝑛 = 𝑛
1
2 can be defined as shown below:
We know that, derivative of 𝑥𝑛
= n* 𝑥𝑛−1
So, derivative of √𝑛 = derivative of 𝑛
1
2 =
1
2
𝑛
(
1
2
−1)
=
1
2
𝑛
(−
1
2
)
=
1
2 𝑛
1
2
=
1
2√𝑛
------------------- (3)
Substituting the equations (2) and (3) in equation (1), we get
lim
𝑛→∞
𝑓(𝑛)
𝑔(𝑛)
= lim
𝑛→∞
𝑓′(𝑛)
𝑔′(𝑛)
= log2e( lim
𝑛→∞
(𝑙𝑜𝑔𝑒𝑛)′
(√𝑛)
′ )
= log2e( lim
𝑛→∞
1
𝑛
1
2√𝑛
)
= log2e( lim
𝑛→∞
2√𝑛
𝑛
)
= 2*log2e( lim
𝑛→∞
√𝑛
𝑛
) = 0
Since limit is equal to 0, first condition holds good.
So, f(n) = log2n has a smaller order of growth than g(n) = √𝑛
That is, log2n ∈ O(√𝒏)
n! and 2n
Given that f(n) = n! and g(n) = 2n
.
We can find the limits as shown below:
lim
𝑛→∞
𝑓(𝑛)
𝑔(𝑛)
= lim
𝑛→∞
𝑛!
2𝑛
= lim
𝑛→∞
√2∗𝜋∗𝑛(
𝑛
𝑒
)
𝑛
2𝑛
= lim
𝑛→∞
√2 ∗ 𝜋 ∗ 𝑛
(
𝑛
𝑒
)
𝑛
2𝑛
= lim
𝑛→∞
√2 ∗ 𝜋 ∗ 𝑛 (
𝑛𝑛
2𝑛𝑒𝑛
) = lim
𝑛→∞
√2 ∗ 𝜋 ∗ 𝑛 (
𝑛
2𝑒
)
𝑛
= ∞
Since third Condition is satisfied, i.e., lim
𝑛→∞
𝑓(𝑛)
𝑔(𝑛)
= ∞,
Symbolically, it is represented as f(n) = n! ∈ Ω(2n
)
17 Prove that 3n
∉ O(2n
)
Ans Let f(n) = 3n
and g(n) = 2n
.
lim
𝑛→∞
𝑓(𝑛)
𝑔(𝑛)
= lim
𝑛→∞
3n
2𝑛
= lim
𝑛→∞
(
3
2
)
𝑛
= ∞
By definition, ∞ indicates f(n) has larger order of growth than g(n). Hence relation 3n
∉
O(2n
) is true.
11.
Page | 11
18Give the general plan for analyzing Time Efficiency of Non-Recursive Algorithms
Ans The general plan for analyzing Time Efficiency of Non-Recursive Algorithms
• Decide on a parameter (or Parameters) indicating an input’s size.
• Identify the Algorithm’s Basic Operations. (As a rule, it is locating in the innermost
loop).
• Check whether the number of times the basic operation is executed depends only on
the size of the input. (Worst-case, Average-case 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 the standard formulas and rules of sum manipulation, establish order of growth.
19 Write an algorithm to find largest element in a list of n numbers and Analyze the time
efficiency of the same
Ans ALGORITHM MaxElement(A[0..n–1 ])
// Determines the largest element in the array.
// Input: An Array A[0..n-1]
// Output: The value of Largest element in A
maxVal A[0]
for i 1 to n-1 do
if A[i] > maxVal
maxVal A[i]
return
Efficiency Analysis of the Algorithm:
Let us denote C(n) the number of times the comparison statement is executed. Algorithm
makes one comparison on each execution of the loop’s variable i, within the bounds 1 to
n–1.
So, C(n) =∑ 1
𝑛−1
𝑖=1 = n–1
So, C(n) ∈ Θ(n)
20 Write the algorithm for Element Uniqueness Problem and analyze the time efficiency
OR
Write a algorithm to check whether all the elements in the given array of n elements are
distinct and analyze the time efficiency.
Ans Algorithm for Element Uniqueness Problem:
ALGORITHM UniqueElements(S[0..n-1])
// Determines whether all the elements in a given array are distinct
// Input: An Array A[0..n-1]
// Output: Returns “True” if all the elements are distinct and “false” otherwise.
for i 0 to n-2 do
for j i+1 to n-1 do
if A[i] == A[j] return false
return true
Analysis of the Algorithm:
12.
Page | 12
•By the Definition, the worst-case input is an array for which the number of element
comparisons Cworst(n) is the largest among all arrays of size n.
• In the innermost loop, there are two kinds of worst-case inputs–Inputs for which the
algorithm does not exit the loop prematurely.
• Arrays with no equal elements
• Arrays in which the last two elements are the only pair of equal
elements.
21 Calculate the efficiency of the algorithm that multiply the two n x n matrix.
Ans ALGORITHM MatrixMultiplication(A[0..n-1, 0..n-1)
// Multiplies two n x n Matrices.
// Input: Two n x n Matrices A and B
// Output: Matrix C A * B
for i 0 to n-1 do
for j 0 to n-1 do
C[i,j] 0
for(k 0 to n-1)
C[i,j] C[i,j] + A[i,k] * B[k,j]
return C
Analysis of Time Efficiency of the algorithm:
Let M(n) is the number of times the Matrix Multiplication line is executed.
There are two operations in the matrix multiplication line: (addition and multiplication) Let
Ca is the cost of each addition and cm is the cost of each multiplication. Let T(N) is the total
time efficiency of the matrix multiplication,
22 Analyze the time complexity of the Algorithm for counting number of Binary Digits
13.
Page | 13
AnsALGORITHM Binary(n)
// Input: The Positive Decimal Integer n
// Output: The number of Binary bits in decimal Representation.
count 1
while n > 1 do
count count + 1
n ⌊n/2⌋
return count
• The basic operation will execute count=count + 1 repeats ⌊log2n + 1⌋ times
23 Consider the following Algorithm
ALGORITHM Foo(n)
// Input: A nonnegative integer n
Sum 0
for i 1 to n do
Sum Sum + i/i!
return Sum
(a) What does the algorithm compute?
(b) What is the basic Operation?
(c) How many times the basic operation executed?
(d) What is the efficiency class of this algorithm?
Ans Exercise to the Students
24 Consider the following Algorithm
ALGORITHM Foo(A[0..n-1])
// Input: An array A[0..n-1] of n real numbers
val 100; sumgreater 0; sumless 0
for i 0 to n-1 do
if A[i] > val sumgreater A[i]
if A[i] < val sumless A[i]
return sumgreater - sumless
(a) What does the algorithm compute?
(b) What is the basic Operation?
(c) How many times the basic operation executed?
(d) What is the efficiency class of this algorithm?
Ans Exercise to the Students
25 Consider the following Algorithm
ALGORITHM Enigma(A[0..n-1, 0..n-1])
// Input: A Matrix A[0..n-1, 0..n-1] of n real numbers
for i 0 to n-2 do
for j i+1 to n-2 do
if A[i,j] ≠ A[j,i]
return false
return true
(a) What does the algorithm compute?
(b) What is the basic Operation?
(c) How many times the basic operation executed?
(d) What is the efficiency class of this algorithm?
Ans Exercise to the Students
26 Give the general plan for analyzing Time Efficiency of Non-Recursive Algorithms
14.
Page | 14
AnsThe General Plan for analyzing time efficiency of Non-Recursive Algorithms:
1. Decide on a parameter indicating an input’s size.
2. Identify the algorithm’s basic operation.
3. Check whether the number of times the basic operation is executed can vary on
different inputs of the same size; if it can, the worst-case, average-case, and best-
case efficiencies must be investigated separately.
4. Set up a recurrence relation, with an appropriate initial condition, for the number of
times the basic operation is executed.
5. Solve the recurrence or, at least, ascertain the order of growth of its solution.
27 Write the recursive algorithm to compute the factorial function F(n) = n! for an arbitrary
non-negative integer n. Analyze the efficiency of the algorithm
Ans Algorithm:
ALGORITHM F(n)
// Computes n! recursively
// Input: A non-negative integer n
// Output: The value n!
if n == 0 return 1
else return F(n-1)* n
Analysis of the efficiency of the algorithm:
The basic operation of the algorithm is multiplication.
Let M(n) be the number of executions of this multiplication.
The function F(n) can be computed using the formula:
F(n) = F(n–1) * n, for n > 0 --→(1)
Total number of multiplication when n = 0 is M(0) = 0
Total number of multiplication when n = 1 is M(1) = 1, i.e., M(0) + 1
Total number of multiplication when n = 2 is M(2) = 2, i.e., M(1) + 1
Total number of multiplication when n = 3, M(3) = 3, i.e., M(2) + 1
So, the number of multiplications M(n) needed to compute is:
M(n) = M(n–1) + 1, for n > 0
Number of executions of the multiplications in F(n) is M(n) can be calculated as below
M(n) = M(n–1) + 1 = M(n–2) + 2 = … + M(n–i) + i = ... = M(n–n) + n = n
28 Write a Recursive Algorithm for Towers of Hanoi and analyze the efficiency of the algorithm
Ans Algorithm:
TowerOfHanoi(n, source, dest, aux)
if n == 1, then
move disk from source to dest
else
TowerOfHanoi (n - 1, source, aux, dest)
move disk from source to dest
TowerOfHanoi (n - 1, aux, dest, source)
End if
Analysis of the Efficiency:
The number of moves M(n) depends only on n. The recurrence equation is:
M(n) = M(n–1) + 1 + M(n–1) for n > 1
We have the following recurrence relation for the number of moves M(n):
M(n) = 2M(n–1) + 1 for n > 1
M(1) = 1
15.
Page | 15
Wesolve this recurrence by the same method of backward substitutions:
M(n) = 2M(n–1) + 1 where M(n–1) = 2M(n–2) + 1
= 2[2M(n–2) + 1] + 1 = 22
M(n–2) + 2 + 1 where M(n–2) = 2M(n–3) + 1
= 22
[2M(n–3) + 1] + 2 + 1 = 23
M(n–3) + 22
+ 2 + 1 where M(n–3) = 2M(n–4) + 1
= 24
M(n − 4) + 23
+ 22
+ 2 + 1
…
Generally, after i substitutions, we get
M(n) = 2i
M(n–i) + 2i–1
+ 2i–2
+ … + 2 + 1 = 2i
M(n–i) + 2i
– 1
Since the initial condition is specified for n = 1, which is achieved for i = n - 1,
we get the following formula for the solution to recurrence:
M(n) = 2n–1
M(n – (n–1)) + 2n–1
– 1
= 2n–1
M(1) + 2n–1
– 1
We know that M(1) = 1,
M(n) = 2n–1
+ 2n–1
– 1
= 2 * 2n–1
– 1
= 2n
– 1
29 Write recursive algorithm to count binary bits in a decimal number and analyze its efficiency.
Ans Algorithm
ALGORITHM BinRecursion(n)
// Input: a Positive decimal digit n
// Output: Number of binary bits in n
if n == 1 return 1
else return
Basic operation is Addition. The recurrence relation can be written as
A(n) = A(n/2) + 1 for n > 1
A(1) = A(0) = 1
Assuming n = 2k
30 Develop the Algorithm for brute-force Selection sort and analyze the efficiency
16.
Page | 16
Ans
ALGORITHMSelectionSort(A[0..n-1)
// Sorts a given array by selection sort.
// Input: An array A[0..n-1]
// Output: Array A[0..n-1] sorted in non-decreasing order.
for i 0 to n-2 do
min i
for j i+1 to n-1 do
if A[j] < A[min] min j
swap A[i] and A[min]
Analysis of the Algorithm:
Input Size is n. Basic Operation is the key comparison A[j] < A[min]. The number of times
this basic operation is executed is:
C(n) = = ∑ ∑ 1
𝑛−1
𝑗=𝑖+1
𝑛−2
𝑖=0 = ∑ [(𝑛 − 1) − (𝑖 + 1) + 1
𝑛−2
𝑖=0 ]
= ∑ (𝑛 − 1 − 𝑖)
𝑛−2
𝑖=0
By substituting various values of i in each summation terms, we get
= (n–1) + (n–2) + (n–3) +…+ 2+1
= ∑ 𝑖
𝑛−1
𝑖=1
=
(𝑛−1)𝑛
2
≈
𝟏
𝟐
n2
∈ Θ(n2
)
31 Develop the Algorithm for brute-force Bubble sort and analyze the efficiency
Ans
Algorithm BubbleSort(A[0..n-1])
// Sorts a given array by bubble Sort.
// Input: An Array A[0..n]
// Output: Ascending order sorted Array
for i 0 to n-2 do
for j 0 to n-2-i do
if A[j+1] < A[j] swap A[j] and A[j+1]
Analyzing the Efficiency of the Algorithm:
𝐶(𝑛) = ∑ ∑ 1
𝑛−2−𝑖
𝑗=0
𝑛−2
𝑖=0
= ∑[(𝑛 − 2 − 𝑖) − 0 + 1]
𝑛−2
𝑖=0
= ∑(𝑛 − 1 − 𝑖)
𝑛−2
𝑖=0
=
(𝑛−1)𝑛
2
≈
𝟏
𝟐
n2
∈ Θ(n2
)
32 Build the Sequential Search Algorithm and Analyze the Efficiency of the same.
Ans ALGORITHM SequentialSearch(A[0..n], K)
// Implements Sequential Search with a key.
// Input: An Array A of n elements and a search key K
// Output: The index of the element in the array A[0..n-1] for successful search and
// -1 for unsuccessful search
A[n] K
i 0
while A[i] ≠ K do
i i + 1
if i < n return i
else return – 1
Worst-Case Efficiency Analysis:
17.
Page | 17
Definition:The Worst-Case Efficiency of an algorithm is its efficiency for the worst-case input of size
n, for which the algorithm runs the longest among all possible inputs of that size.
CWorst(n) = n.
Best-Case Efficiency Analysis:
Definition: The Best-Case Efficiency of an algorithm is its efficiency for the best-case input of size n,
for which the algorithm runs the fastest among all possible inputs of that size.
CBest(n) = 1
Average-Case Efficiency Analysis:
Definition: The Average-Case Efficiency of an algorithm is the amount of time used by
the algorithm, averaged over all possible inputs.
Cavg(n) = [1*
𝒑
𝒏
+ 2*
𝒑
𝒏
+ 3*
𝒑
𝒏
+ ⋯ + i*
𝒑
𝒏
+ ⋯ + n*
𝒑
𝒏
] + n * (1–p)
=
𝒑
𝒏
[1 + 2 + 3 + … + i + … + n] + n * (1–p)
=
𝒑
𝒏
*
𝒏(𝒏+𝟏)
𝟐
+ n(1–p)
=
𝒑(𝒏+𝟏)
𝟐
+ n(1–p) =
𝒑(𝒏+𝟏)+𝟐𝒏(𝟏−𝒑)
𝟐
=
𝒑𝒏+𝒑+𝟐𝒏−𝟐𝒑𝒏
𝟐
=
𝒑−𝒑𝒏+𝟐𝒏
𝟐
=
𝒑(𝟏−𝒏)+𝟐𝒏
𝟐
=
𝒑(𝟏−𝒏)
𝟐
+ n
33 Develop Brute Force String Matching algorithm and analyze its efficiency
Ans Problem Statement: Given a string of n characters called the text and a string of m
characters called the pattern, find a substring of the text that matches the pattern.
Alternate Problem Statement: Find index of the leftmost character of the first matching
substring (pattern) in the main string (text).
ALOGORITHM FOR BRUTE FORCE STRING MATCHING:
ALGORITHM BruteForceStringMatch(T[0..n-1], P[0..m-1])
//Implements Brute Force String Matching
// Input: An Array T[0..n-1] as main string and
//an array P[0..m-1] as Pattern (or substring)
// Output: Returns Index of the First Character in T that
// starts a matching substring P. If no match is found, return -1
for i 0 to n-1 do
j 0
while j < m and P[j] = T[i+j] do
j j + 1
if j = m return i
return -1
Worst Case Efficiency of the Algorithm:
The size of the pattern string P is m. So, Algorithm may have to make all m comparisons
before shifting the pattern. This can happen for each of the n–m+1 tries. So, the algorithm
makes m(n–m+1) character comparisons which puts in O(nm) class
Average case Efficiency of the Algorithm:
• Most of the shifts would happen after very few comparisons. For searching in random
texts, it has been shown to the linear, that is, Θ(n)
Best case Efficiency:
The substring is found in the first m positions. So only m number of comparisons.
So, Efficiency is Θ(m)
18.
Page | 18
MODULE2 Question Bank
1 Identify the Optimal Solution for the Travelling Salesman Problem represented in the
below graph
Ans
OR
2 Build the Insertion sort Algorithm and Analyze the efficiency of the algorithm
Ans ALGORITHM InsertionSort(A[0..n-1])
//sorts the array using insertion sort
// Input: An array A[0..n-1]
// Output: Array A[0..n-1] sorted ordered.
for i 1 to n-1 do
v A[i]
j i-1
while j ≥ 0 and A[j] > v do
A[j+1] A[j]
j j-1
a[j+1] v
Analysis of the Efficiency:
The Basic Operation is Comparison. Number of executions of this operation depends
on the nature of the input.
Worst Case Efficiency Analysis:
When the array A contains the elements in the order A[0] > A[1] > A[2] > …. > A[n–1],
then the number of executions of comparison is largest. The number of executions of
comparison is as shown below:
CWorst(n)
=
(𝑛−1)𝑛
2
≈
1
2
n2 ∈ Θ(n2)
Best Case Efficiency Analysis: The array A is in sorted in ascending order. Only n-1
number of comparison will be executed.
𝐶(𝑛) = ∑ 1
𝑛−2
𝑖=0
= 𝑛 − 1 ∈ Θ(n)
Average Case Efficiency Analysis: On randomly ordered arrays, insertion sort makes
on average half as many comparisons as on decreasing arrays, ie.,
Cavg(n) ≈
𝑛2
4
∈ Θ(n2)
19.
Page | 19
3Assume that weights of the four items are {7, 3, 4, 5} in terms of Kgs. The Costs of these items
are {$42, $12, $40, $25} and the capacity of the knapsack is 10 Kg. Find the Optimal Solution
to hold the items in the knapsack with highest costs.
Ans Weights w = {7, 3, 4, 5} and Costs c = {$42, $12, $40, $25} M = 10
The various feasible solutions are:
The Optimal Solution is the item sequence [3, 4] with total weight 9 Kg having the cost
$65 is the highest value.
4 Assume that there are four persons in the organization can do four jobs each. The
matrix below shows the cost of these jobs by the respective persons. Find the optimal
job assignment sequence that achieves minimum cost.
Ans
5 What are the three factors in the decrease and conquer technique? Explain with
suitable flow diagram.
Ans There are Three Factors in Decrease and Conquer approach:
• Decrease by a constant
• Decrease by a Constant factor
• Variable Size Decrease
Decrease by a Constant:
The Size of the instance is reduced by the same constant on each iteration of the algorithm.
20.
Page | 20
Examplefor Decrease by Constant:
• Computing an using Recursive formula: an = an-1 * a
Where a is nonzero value. And n is nonnegative integer.
The exponentiation an can be computed either by using top down or bottom up.
• In Top-Down approach, to calculate f(n) = an, we use the recursive formula:
f(n) = an = {
𝒇(𝒏 − 𝟏) ∗ 𝒂 𝒊𝒇 𝒏 > 𝟎
𝟏 𝒊𝒇 𝒏 = 𝟎
}
• In Bottom-up Approach, to calculate f(n) = an, we multiply 1 a n times, that is:
an = 1 * a * a * a * … * a (n times)
Decrease by a constant Factor:
• Reducing a problem instance by the same constant factor on each iteration.
Example for Decrease by Constant Factor:
an = {
(𝑎𝑛/2
)
2
𝑖𝑓 𝑛 𝑖𝑠 𝑒𝑣𝑒𝑛 𝑎𝑛𝑑 𝑃𝑜𝑠𝑖𝑡𝑖𝑣𝑒
(𝑎(𝑛−1)/2
)
2
∗ 𝑛 𝑖𝑓 𝑛 𝑖𝑠 𝑜𝑑𝑑 𝑎𝑛𝑑 𝑝𝑜𝑠𝑖𝑡𝑖𝑣𝑒
1 𝑖𝑓 𝑛 = 0
}
The Efficiency of this technique is Θ(log n)
Variable Size Decrease:
• In this variation, the size-reduction pattern varies from one iteration of an
algorithm to another.
• Example: In problem of finding GCD of two number though the value of the
second argument is always smaller on the RHS than on the LHS, it decreases
neither by a constant nor by a constant factor.
Example for Variable Size Decrease:
GCD using Euclid’s algorithm:
gcd(m, n) = gcd(n, m mod n)
Here, Size reduction pattern varies from one iteration to another.
6 Develop the topological sorting algorithm and Apply the Topological sorting algorithm for the
following graph using (i) source removal method and (ii) DFS Method.
Ans Algorithm:
Demonstration of the algorithm for the given graph:
Using Source Removal Method: