ADA_Unit-1
ADA_Unit-1
ADA_Unit-1
Definition of 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.
Problem
Algorithm
Properties of an Algorithm
Example:
ALGORITHM Euclid(m, n)
PSEUDOCODE
ALGORITHM Euclid(m, n)
• Example: gcd(10,6) = 2
t m%t n%t
6 10 % 6 = 4
5 10 % 5 = 0 6%5=1
4 10 % 4 = 2
3 10 % 3 = 1
2 10 % 2 = 0 6%2=0
2 is the GCD, since m % t and n % t are zero.
Note: Unlike Euclid’s algorithm, this algorithm, in the form presented, does not
work correctly when one of its input numbers is zero.
Step 3 Identify all the common factors in the two prime expansions found in
step1 and step2 (If p is a common factor occurring pm and pn times in
m and n, respectively, it should be repeated min{pm,pn} times.)
Step 4 Compute the product of all the common factors and return it as
gcd(m,n)
• Example:
If m = 60 and n = 24 then
60 = 2 . 2 . 3 . 5
24 = 2 . 2 . 2 . 3
gcd(60,24) = 2 . 2 . 3 =12
This is not a legitimate algorithm since steps 2 and 3 are not defined
clearly.
Note: If p is a number whose multiples are being eliminated, then p.p should
not be greater than n, and therefore p cannot exceed √n .
ALGORITHM Sieve(n)
// Implements the Sieve of Eratosthenes
// Input: An integer n ≥ 2
// Output: Array L of all prime numbers less than or equal to n.
for p ← 2 to n do A[p] ← p
for p ← 2 to √n do
if A[p] ≠ 0 // p hasn’t been eliminated on previous passes
j←p*p
while j ≤ n do
A[j] ← 0 // mark element as eliminated
j←j+p
Example: n = 25
Designing an Algorithm:
Analyzing an Algorithm:
Coding an Algorithm:
Analysis Framework
Analysis of algorithms means investigation of an algorithm’s efficiency
with respect to two resources: running time and memory space.
Example:
b= log2 n + 1
Example:
C(n) be the number of times this basic operation needs to be executed for
this algorithm.
Then, the running time T(n) can be estimated by
T(n) ≈ Cop . C(n)
Note: Cop is an approximation & C(n) does not contain any information
about operations that are not basic. Therefore, above formula can give a
reasonable estimate of the algorithm’s running time.
Problem: Assuming C(n) = 1 n (n – 1), how much longer will the algorithm
2
run if we double its input size?
Solution: C(n) = 1 n (n – 1) = 1 n2 - 1 n ≈ 1 n2
2 2 2 2
Orders of Growth
For GCD(m, n) of two small numbers, it is not clear how much more
efficient Euclid’s algorithm is, compared to other two algorithms.
It is only when we have to find the GCD of two large numbers (eg.,
GCD(31415, 14142)) that the difference in algorithm efficiencies
becomes both clear and important.
There are many algorithms for which running time depend not only on
an input’s size, but also on the specifics of a particular input.
ALGORITHM SequentialSearch(A[0…n-1], k)
//Searches for a given value in a given array by sequential search
//Input: An array A[0…n-1] and a search key k
//Output: The index of the first element of A that matches k or -1 if
// there are no matching elements.
i ← 0
while i < n and A[i] ≠ k do
i ← i+1
if i < n return i
else return -1
Running Time of this algorithm can be quite different for the same list of
size n.
Cworst(n) = n
Analyze the algorithm to see what kind of inputs yields the largest value
of the basic operation’s count C(n) among all possible inputs of size n
and then compute Cworst(n).
Analyze the algorithm to see what kind of inputs yields the smallest
value of the basic operation’s count C(n) among all possible inputs of size
n and then compute Cbest(n).
For Sequential search, best-case inputs are lists of size n with their first
elements equal to a search key.
Neither the worst-case analysis nor its best-case counterpart yields the
necessary information about an algorithm’s behavior on a typical or
random input. This is provided by Average-case analysis.
(b) Probability of first match occurring at ith position is same for every i.
Asymptotic Notations
Let, t(n) and g(n) can be any two nonnegative functions defined on the set
of natural numbers.
Examples:
Informally, Ω(g(n)) stands for the set of all functions with a larger or
same order of growth as g(n).
Examples:
Informally, Θ(g(n)) is the set of all functions that have the same order of
growth as g(n).
Example:
Note:
O – Notation
Example Problems:
Prove the following inequalities
Ω – Notation
Θ-notation
Example Problem:
Proof: a1, b1, a2, & b2 are four arbitrary real numbers.
≤ c3 2 max{g1(n), g2(n)}
n0 = max{n1,n2}
ALGORITHM MaxElement(A[0…n-1])
//Determines the value of the largest element in a given array
//Input: An array A[0…n-1] of real numbers
//Output: The value of the largest element in A
maxval ← A[0]
for i ← 1 to n-1 do
if A[i] > maxval
maxval ← A[i]
return maxval
Analysis:
ALGORITHM UniqueElements(A[0…n-1])
for i ← 0 to n - 2 do
for j ← i+1 to n - 1 do
return true
The natural measure of the input’s size is the number of elements in the
array. i.e., n.
Since the innermost loop contains a single operation (comparison), we
should consider it as the algorithm’s basic operation.
Here, no. of element comparisons will depend not only on n but also on
whether there are equal elements in the array and, if there are, which
array positions they occupy.
We shall do only worst case analysis.
The worst case input is an array for which the number of elements
comparisons Cworst(n) is the largest among all arrays of size n.
Arrays with no equal elements and arrays in which the last two
elements are the only pair of equal elements.
For such inputs, one comparison is made for each repetition of the
innermost loop, i.e., for each value of the loop’s variable j between its
limits i+1 and 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.
Analysis:
Parameter indicating input size: n
Basic operation: Comparison
n-2 n-1 n-2 n-2
CWorst(n) = ∑ ∑ 1 = ∑ n-1- i-1+1 = ∑n-1-i
i=0 j=i+1 i=0 i=0
n-2 n-2
= (n-1)∑1 - ∑i = (n-1)2 - (n-2)(n-1) = (n-1)n ≈ 1n2 Є Θ(n2)
i=0 i=0 2 2 2
In the worst case, the algorithm needs to compare all n(n-1)/2 distinct
pairs of its n elements
Analysis:
= n(n-1-0+1) = n2 Є Θ(n2)
Soln:
a) This algorithm checks if the input square matrix is symmetric or not.
C(n) Є O(n2)
Recurrence Relations
An Equation such as
An equation such as (2) is called its initial condition which tells us the
value with which the sequence starts.
Check whether the number of times the basic operation is executed can
vary on different inputs of same size; If it can, the worst-case, average-
case, and best-case efficiencies must be investigated separately.
ALGORITHM F(n)
//Computes n! recursively
//Input: A nonnegative integer n
//Output: The value of n!
if n = 0 return 1
else return F(n-1)* n
Since the function F(n) is computed as F(n) = F(n-1).n for n>0, the
number of multiplications M(n) needed to computed it must satisfy the
equality
Our goal now is to solve the recurrence relation M(n) = M(n-1) + 1, i.e., to
find an explicit formula for M(n) in terms of n only.
We can obtain this value by inspecting the condition that makes the
algorithm stop its recursive calls:
if n=0 return 1
Initially, all the disks are on the first peg in the order of size, the largest
on the bottom and the smallest on top.
The goal is to move all the disks to the third peg, using the second one as
an auxiliary, if necessary.
We can move only one disk at a time, and it is forbidden to place a larger
disk on top of a smaller one.
To move n>1 disks from peg1 to peg3, we first move recursively n-1 disks
from peg1 to peg2 (with peg3 as auxiliary), then move the largest disk
directly from peg1 to peg3, and finally, move recursively n-1 disks from
peg2 to peg3 (using peg1 as auxiliary).
If n=1, we can simply move the single disk directly from the source peg to
the destination peg.
The number of disks n is the choice for the input’s size indicator.
Clearly, the number of moves M(n) depends on n only, and we get the
following recurrence equation for it: