Mod 1 Bcs 401
Mod 1 Bcs 401
Characteristics of an algorithm:
Input: Zero / more quantities are externally supplied.
Output: At least one quantity is produced.
Definiteness: Each instruction is clear and unambiguous.
Finiteness: If the instructions of an algorithm is traced then for all cases the algorithm must
terminates after a finite number of steps.
Efficiency: Every instruction must be very basic and runs in short time.
1
BCS401-Module1 | Anooplal KS
Step 3 Divide n by t. If the remainder of this division is 0, return the value of t as the answer
and stop; otherwise, proceed to Step 4.
Step 4 Decrease the value of t by 1. Go to Step 2
unlike Euclid’s algorithm, this algorithm, in the form presented, does not work correctly
when one of its input numbers is zero.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
2 3 5 7 9 11 13 15 17 19 21 23 25
2 3 5 7 11 13 17 19 23 25
2 3 5 7 11 13 17 19 23
ALGORITHM Sieve(n)
//Implements the sieve of Eratosthenes
//Input: A positive integer n > 1
//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 //see note before pseudocode
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
2
BCS401-Module1 | Anooplal KS
j→j+p
//copy the remaining elements of A to array L of the primes
i ←0
for p← 2 to n do
if A[p] ≠ 0
L[i] ← A[p]
i←i+1
return L
1. Understanding the Problem: The first thing you need to do before designing an
algorithm is to understand completely the problem given. Read the problem’s
description carefully and ask questions if you have any doubts about the problem, An
input to an algorithm specifies an instance of the problem the algorithm solves. It is
very important to specify exactly the set of instances the algorithm needs to handle.
2. Ascertaining the Capabilities of the Computational Device:
Ascertain the capability of a computational device
• How the objects would be represented
Is it RAM model or concurrent execution
• How the operations would be implemented?
Is it sequential algorithm or parallel algorithm
3. Choosing between Exact and Approximate Problem Solving
The next principal decision is to choose between solving the problem exactly or
solving it approximately. In the formeru case, an algorithm is called an exact
algorithm; in the latter case, an algorithm is called an approximation algorithm.
3
BCS401-Module1 | Anooplal KS
4
BCS401-Module1 | Anooplal KS
5
BCS401-Module1 | Anooplal KS
So, we need metric to measure an algorithm’s efficiency that does not depend on these
extraneous factors. One possible approach is to count the number of times each of the
algorithm’s operations is executed. This approach is excessively difficult. The most important
operation (+, -, *, /) of the algorithm, called the basic operation. Computing the number of
times the basic operation is executed is easy. The total running time is determined by basic
operations count.
6
BCS401-Module1 | Anooplal KS
Worst-case efficiency : The worst-case efficiency of an algorithm is its efficiency for the
worst case input of size n. The algorithm runs the longest among all possible inputs of that
size. For the input of size n, the running time is Cworst(n) = n.
Best case efficiency :The best-case efficiency of an algorithm is its efficiency for the best
case input of size n.
• The algorithm runs the fastest among all possible inputs of that size n.
• In sequential search, If we search a first element in list of size n. (i.e. first element
equal to a search key), then the running time is Cbest(n) = 1
Average case efficiency
• The Average case efficiency lies between best case and worst case.
• To analyze the algorithm’s average case efficiency, we must make some
assumptions about possible inputs of size n.
• The standard assumptions are that
o The probability of a successful search is equal to p (0 ≤ p ≤ 1) and
o The probability of the first match occurring in the ith position of the list is
the same for every i.
7
BCS401-Module1 | Anooplal KS
1. Big-Oh notation
Definition: 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 nonnegative integer n0 such that
t(n) ≤ c g(n) for all n ≥ n0
Example:1
Example2:
8
BCS401-Module1 | Anooplal KS
Ω = Asymptotic lower bound = Useful for best case analysis = Loose bound
Example:
Prove the assertions: n3 +10n2 +4n+2 ∈ Ω (n2 ).
Proof: n3 +10n2 +4n+2 ≥ n2 (for all n ≥ 0)
i.e., by definition t(n) ≥ cg(n), where c=1 and n0=0
Where t(n) and g(n) are nonnegative functions defined on the set of natural numbers.
Θ = Asymptotic tight bound = Useful for average case analysis
9
BCS401-Module1 | Anooplal KS
10
BCS401-Module1 | Anooplal KS
11
BCS401-Module1 | Anooplal KS
3. Check whether the number of times the basic operation is executed depends only on the
size of an input. If it also depends on some additional property, the worst-case, average-case,
and, if necessary, best-case efficiencies have to be investigated separately.
4. Set up a sum expressing the number of times the algorithm’s basic operation is executed.
5. Using standard formulas and rules of sum manipulation either find a closed form
formula for the count or at the least, establish its order of growth.
Consider the problem of finding the value of the largest element in a list of n numbers.
Assume that the list is implemented as an array for simplicity.
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
Algorithm analysis
• The measure of an input’s size. here is the number of elements in the array, i.e., n.
• There are two operations in the for loop’s body:
• The comparison A[i]> maxval and
• The assignment maxval←A[i].
• Therefore the basic operation is comparison
• The number of comparisons will be the same for all arrays of size n; therefore, there is
no need to distinguish among the worst, average, and best cases here.
• Let C(n) denotes the number of times this comparison is executed. The algorithm
makes one comparison on each execution of the loop, which is repeated for each
value of the loop’s variable i within the bounds 1 and n- 1, inclusive.
12
BCS401-Module1 | Anooplal KS
EXAMPLE 2: Consider the element uniqueness problem: check whether all the
Elements in a given array of n elements are distinct.
• The natural measure of the input’s size here is again n (the number of elements in the
array).
• the comparison of two elements is the algorithm’s basic operation.
• The number of element comparisons depends 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 will limit our investigation to the worst case only.
• One comparison is made for each repetition of the innermost loop, i.e., for each value
of the loop variable j between its limits i + 1 and n − 1; this is repeated for each value
of the outer loop, i.e., for each value of the loop variable i between its limits 0 and n −
2.
13
BCS401-Module1 | Anooplal KS
Example 3:The following algorithm finds the number of binary digits in the binary
representation of a positive decimal integer.
Since the value of n is about halved on each repetition of the loop, the answer should be
about log2 n. The exact formula for the number of times the comparison n> 1 will be
executed is actually log2 n +1
Example4: Given two n n matrices A and B, find the time efficiency of the definition-based
algorithm for computing their product C AB. By definition, C is an n *n matrix whose
elements are computed as the scalar (dot) products of the rows of matrix A and the columns
of matrix B:
Efficiency:
14
BCS401-Module1 | Anooplal KS
• The total number of multiplications M(n) is expressed by the following triple sum:
The algorithm computes n2 elements of the product matrix. Each of the product’s elements is
computed as the scalar (dot) product of an n-element row of the first matrix and an n-element
column of the second matrix, which takes n multiplications. So the total number of
multiplications is n . n2 n3. To estimate the running time of the algorithm on a particular
machine,
where cm is the time of one multiplication on the machine.Total time taken is given by
15
BCS401-Module1 | Anooplal KS
Algorithm analysis
• For simplicity, we consider n itself as an indicator of this algorithm’s input size. i.e. 1.
• The basic operation of the algorithm is multiplication, whose number of executions
we denote M(n). M(0)=0(no multiplications done)
• Since the function F(n) is computed according to formula F(n) = F(n −1)*n for n > 0.
F(0)=1.
16
BCS401-Module1 | Anooplal KS
Algorithm analysis
• The number of moves M(n) depends on n only, and we get the following recurrence
equation for it:
M(n) = M(n − 1) + 1+ M(n − 1) for n > 1.
• With the obvious initial condition M(1) = 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.
EXAMPLE 3:A recursive version of the algorithm for binary representation of positive
decimal number
ALGORITHM: BinRec(n)
//Input: A positive decimal integer n
//Output: The number of binary digits in n’s binary representation
if n = 1 return 1
else return BinRec( n/2]) + 1
A(n) = A( n/2]) + 1 for n> 1. (2.4) Since the recursive calls end when n is equal to 1 and there are
no additions made then, the initial condition is A(1) = 0.
17
BCS401-Module1 | Anooplal KS
= [A(2k−3) + 1] + 2 = A(2k−3) + 3
... . . .
= A(2k−i) + i
= A(2k−k) + k.
A(2k) = A(1) + k = k, or, after returning to the original variable n = 2k and hence k = log2 n,
Selection Sort
We start selection sort by scanning the entire given list to find its smallest element and exchange it
with the first element, putting the smallest element in its final − position in the sorted list. Then we
scan the list, starting with the second element, to find the smallest among the last n 1 elements and
exchange it with the second element, putting the second smallest element in its final
position.Generally the algorithm searches for the smallest item among the last n − i elements and
swaps it with Ai :
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]
18
BCS401-Module1 | Anooplal KS
| 89 45 68 90 29 34 17
17 | 45 68 90 29 34 89
17 29 | 68 90 45 34 89
17 29 34 | 90 45 68 89
17 29 34 45 | 90 68 89
17 29 34 45 68 | 90 89
17 29 34 45 68 89 | 90
19
BCS401-Module1 | Anooplal KS
• Linear search can be used irrespective of whether the array is sorted or not. It can be used
on arrays of any data type.
• Linear search has a time complexity of O(N)(Worst Case), which in turn makes it slow for
large datasets.
Step 2 Moving from left to right, compare each character of pattern to the corresponding character
in text until
• the algorithm shifts the pattern almost always after a single character comparison.
• The worst case is much worse:
20
BCS401-Module1 | Anooplal KS
• the algorithm may have to make all m comparisons before shifting the pattern,
and this can happen for each of the m(n-m+1) tries.Hence O(nm)
21