Module1 PPT
Module1 PPT
-- It depicts the Sequence of steps to be followed in designing and analyzing any algorithm
-- Algorithms to be procedural solutions to problems.
-- Selection of suitable Programming Language ; features mentioned in the design should support
Program Testing : is the process of identifying errors in the program and finding how well the program works.
• The change in behavior of the algorithm as the value of n increases is called Order of Growth
Common Computing Time functions
N!: Running time of an algorithm is factorial. That generate all permutations of set , bruteforce technique
Sequential Search i
i=0 Index 0 1 2 3 4
N=5
While i<n & a[i]!=k do Values 10 20 30 40 50 Case 1: Key = 10
Case 2: 50
i=i+1 Case 3: 30
If i<n return i
else
Return -1
The efficiency of an algorithm for the input size of n for which the algorithm takes least time to execute among all possible
inputs
The efficiency of an algorithm for the input size of n for which the algorithm takes longest time to execute among all possible
inputs
• Average case efficiency required for randomized input
• Amortized Efficiency : applies to sequence of operations performed on same data structure and is used to handle
worst case
Example : Sequential Search
1. Let f(n)=10n3+8
Find g(n) which is slightly greater than f(n) so replace
10n3+8 with 10n3+n3 ie 11n3
Therefore c= 11 g(n)=n3 for n>=2
Other Problems (Refer classnotes)
1. logn+ √n Prove that 3n3+2n2=O(n3) and 3n!=O(2n )
2. N+nlogn
2. 2n+2
3. 6*2n+n2
4. 100n+5
5. 2n2+3
Prove that n(n-1)/2=O(n2)
Prove that 2n(n-1)/2=O(n3)
Prove that 1/2n(n-1)=Θ (n2)
1. Prove that 3n3+2n2=O(n3) 2. Prove that 3n!=O(2n)
We can write f(n)<=c*g(n) then f(n) ɛ O(g(n)) Let f(n)= 3n , g(n)= 2n then let us find
Let us find the value of N0 and C such that 3n <=c* 2n
So by definition
F(n) ɛ Theta(n)
Useful Property involving Asymptotic Notations :
• Useful in analyzing algorithms that comprise two consecutively executed parts
THEOREM
T(n) =Algorithm running time
If t1(n) ∈ O(g1(n)) and t2(n) ∈ O(g2(n)), then g(n)= function to compare with
count
t1(n) + t2(n) ∈ O(max{g1(n), g2(n)})
PROOF
The proof extends to orders of growth the following simple fact about four
arbitrary real numbers a1, b1, a2, b2:
X Y X AND Y
if a1 ≤ b1 and a2 ≤ b2, then a1 + a2 ≤ 2 max{b1, b2}. F F F
F T F
T F F
Ex: a1=5 a2 =2, b1 = 3 b2=4 T T T
Use a sorting algorithm (like quicksort, mergesort, or heapsort) to arrange the array
Iterate through the sorted array, comparing each element to its immediate next element.
• It uses no more than (n-1) comparisons and hence is in O(n)
3. Return result: If at any point, two adjacent elements are equal, then the array
∞/ ∞ form
General Plan for Analyzing the Time Efficiency of Non-recursive Algorithms
1. Decide on a parameter (or parameters) indicating an input’s size.
2. Identify the algorithm’s basic operation. (As a rule, it is located in the
innermost loop.)
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 very least, establish its order of growth
Sum Manipulation Rules Two Summation formulas
S1
S2
Mathematical Analysis of Non-Recursive Algorithms
Basic operation
Analysis
break
or false
Analysis
1. Input size=n
2. Basic Operation : Comparison
3. Number of times basic operation executed, based on the
following 2 scenarios in worst case
• No Equal elements at all
• pair of equal elements
4. Express in summation form = O(n2 )
5. Establish order of Growth
3) Given two n × n matrices A and B, compute their product C = AB
Input size = n*n
Basic operation = Multiplication
4) finds the number of binary digits in the binary representation of a positive
decimal integer
General Plan for Analyzing the Time Efficiency of Recursive Algorithms
1. Decide on a parameter (or parameters) 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
Mathematical Analysis of Recursive
Algorithms
1) Compute the factorial function F (n) = n!
we need an initial condition that tells us the value with which the
sequence starts
=[M[n-i]+I
=M[n-n]+n
=O(n)
Analysis
Input size = n
Basic operation = f(n) = f(n-1)*n
2) Tower of Hanoi puzzle.
Tower of Hanoi
Algorithm: TOH(n,a,b,c)
//i/p: No of disks n’ Analysis
//o/p: Arranging the disks orderly to destination 1. Input size=3
2. Movement of disk =M(n)
{ if n=1 , Move(a,b)
n=1, M(1)=1
else n>1, M(n)=1+M(n-1)
TOH(n-1,a,b,c); =1+M(n-1)
M(n) =2M(n-1)+1
Move(a,c)
TOH(n-1,b,c,a);
}
Analysis
1. Input size = 3
2. Basic operation
(movement of disc) = m(n)
n=1 , m(n) = 1
n > 1 , m(n) = 1+m(n-1)
= 1 + m(n-1)
i.e m(n) = 2 m(n-1) + 1
3) finds the number of binary digits in the binary representation of a positive
decimal integer
Example=8
Floor(8/2)+1 =4
Floor(4/2)+1 = 3
Floor(2/2)+1=2
1
(1)=1
Chapter 3
Brute Force
Introduction
• Brute force is a straightforward approach to solving a problem
• Directly based on the problem statement and definitions of the concepts
involved
• “Just do it!” would be another way to describe the describing the brute-force
approach
• Ex: compute a^n
a^n = a ∗ ... ∗ a
n times
• Two Algorithms :
1) Selection Sort
2) Sequential Search
Selection Sort :