Module-1
Module-1
(Integrated)
22CSE43
MODULE 1
• Read the problem’s description carefully and ask questions if you have any
doubts about the problem.
• Do a few small examples by hand, think about special cases, and ask
questions again if needed.
• If the problem in question is one of them, then able to use a known
algorithm for solving it.
If we fail to do this, algorithm may work correctly for a majority of inputs but
crash on some “boundary” value. Remember that a correct algorithm is not
one that works most of the time, but one that works correctly for all
legitimate inputs
Ascertain the capability of a computational Device
• How the objects would be represented?
:
• Design an algorithm Build a computational model of the solving process
and specify it in some fashion
• For the approximation algorithm the error produced by the algorithm does
not exceed a predefined limit
Analyze the algorithm
• Time Efficiency: How fast the algorithm is?
1 constant Short of best-case efficiencies, very few reasonable examples can be given since an
algorithm’s running time typically goes to infinity when its input size grows infinitely
large.
log n logarithmic Typically, a result of cutting a problem’s size by a constant factor on each iteration of
the algorithm (see Section 4.4). Note that a logarithmic algorithm cannot take into
account all its input or even a fixed fraction of it: any algorithm that does so will have
at least linear running time.
n linear Algorithms that scan a list of size n (e.g., sequential search) belong to this class.
n log n linearithmic Many divide-and-conquer algorithms (see Chapter 5), including mergesort and
quicksort in the average case, fall into this category.
Mathematical analysis of Non-Recursive algorithms
currentMax ← A[i]
return currentMax
• Analysis:
1. Input size: number of elements = n (size of the array)
2. Basic operation:
a) Comparison
b) Assignment
3. No best, worst, average cases
4. Let C (n) denotes number of comparisons: Algorithm makes one comparison on
each execution of the loop, which is repeated for each value of the loop‘s variable
i within the bound between 1 and n – 1.
Example: 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 the elements in A 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
1. Input size: number of elements = n (size of the array)
2. Basic operation: Comparison
3. Best, worst, average cases EXISTS.
Worst case input is an array giving largest comparisons.
• Array with no equal elements
• Array with last two elements are the only pair of equal elements
4. Let C (n) denotes number of comparisons in worst case: Algorithm makes one
comparison for each repetition of the innermost loop i.e., for each value of the
loop‘s variable j ranging from i + 1 to n – 1; and this is repeated for each value
of the outer loop i.e, for each value of the loop‘s variable i between its
limits 0 and n – 2
Mathematical analysis of Recursive algorithms
• General plan for analysing efficiency of recursive algorithms:
1. Decide on parameter n indicating input size
2. Identify the algorithm‘s basic operation
3. Check whether the number of times the basic operation is executed depends only on
the input size n. If it also depends on the type of input, investigate worst, average,
and best case efficiency separately.
4. Set up recurrence relation, with an appropriate initial condition, for the number of
times the algorithm‘s basic operation is executed.
• 5. Solve the recurrence.
Recurrence Relation
• The recurrence equation is an equation that defines a
sequence recursively.
• It is in the form of
T(n)=T(n-1)+n for n>0 -------(1)
T(0)=0 -------(2)
return 1
else
return Factorial (n – 1) * n
1. Input size: given number = n
2. Basic operation: multiplication
3. NO best, worst, average cases.
4. Let M (n) denote the number of multiplications.
M (n) = M (n – 1) + 1 for n > 0
M (0) = 0 initial condition
Where: M (n – 1) : to compute Factorial (n – 1)
1 : to multiply Factorial (n – 1) by n
5. Solve the recurrence: Solving using the “Backward substitution method”:
M (n) = M (n – 1) + 1
= [ M (n – 2) + 1 ] + 1
= M (n – 2) + 2
= [ M (n – 3) + 1 ] + 3
= M (n – 3) + 3
…
In the ith recursion, we have
= M (n – i ) + i
When i = n, we have
= M (n – n ) + n = M (0 ) + n
Since M (0) = 0
=n M (n) = Θ (n)
Example: Find the number of binary digits in the binary representation of a
positive decimal integer
ALGORITHM BinRec (n)
//Input: A positive decimal integer n
//Output: The number of binary digits in n‟binary representation
if n = = 1
return 1
else
return BinRec (└ n/2 ┘) + 1