Here, f( n ) <= g( n ) ……………..(eq1) where n>= k , C>0 and k>=1 if any algorithm satisfies the eq1 condition then it becomes f ( n )= O ( g ( n ) ) |
Here, f ( n ) >= g ( n ) where n>=k and c>0, k>=1 if any algorithm satisfies the eq1 condition then it becomes f ( n )= ῼ ( g ( n ) ) |
Let C1 g(n) be upper bound and C2 g(n) be lower bound. C1 g(n)<= f( n )<=C2 g(n) Where C1, C2>0 and n>=k |
ALGORITHM MaxElement(A[0..n-1]) //Determines the value of 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 currentMax ← A[0] for i ← 1 to n - 1 do if A[i] > currentMax currentMax ← A[i] return currentMax Analysis :
● Comparison ● 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. |
Algorithm UniqueElements (A[0..n-1]) //Checks if all elements are distinct in a given array //Input: An array A[0..n-1] //Output: If all the elements in A are distinct and false otherwise, returns true 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
An array giving the largest comparisons is the worst case input. ● Array without identical elements ● The only pair of equivalent elements is the series of the last two elements. 4. In the worst case, let C(n) denote the number of comparisons: for each repetition of the innermost loop, the algorithm makes one comparison, i.e., for each value of the loop 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 variable I between its limits 0 and n-2. |
ALGORITHM Factorial (n) //Computes n! recursively //Input: A nonnegative integer n //Output: The value of n! if n = = 0 return 1 else return Factorial (n – 1) * n Analysis 1. Input size: given number = n 2. Basic operation: multiplication 3. NO best, worst, average cases. 4. Let M (n) is 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. Resolve the recurrence: Resolving using "Method of backward substitution": 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 2: In the binary representation of a positive, find the number of binary digits Decimal Integer Integers ALGORITHM BinRec (n) //Input: A positive decimal integer n //Output: In n”s binary representation, the number of binary digits if n = = 1 return 1 else return BinRec (└ n/2 ┘) + 1 Analysis
A (n) = A (└ n/2 ┘) + 1 for n > 1 A (1) = 0 initial condition Where: A (└ n/2 ┘) : to compute BinRec (└ n/2 ┘) 1: to increase the returned value by 1 5. Solve the recurrence: A (n) = A (└ n/2 ┘) + 1 for n > 1 Assume n = 2k (smoothness rule) A (2k ) = A (2k-1) + 1 for k > 0; A (20) = 0 Solving using "Method of backward substitution": A (2k ) = A (2k-1) + 1 = [A (2k-2) + 1] + 1 = A (2k-2) + 2 = [A (2k-3) + 1] + 2 = A (2k-3) + 3 … In the recursion of ith, may have = A (2k-i) + i When i = k, we have = A (2k-k) + k = A (20 ) + k Since A (20 ) = 0 A (2k ) = k Since n = 2k , HENCE k = log2 n A (n) = log2 n A (n) = Θ ( log n) |
ALGORITHM SelectionSort(A[0..n - 1]) //The algorithm sorts a given array by selection sort //Input: An array A[0..n - 1] of orderable elements //Output: Array A[0..n - 1] sorted in ascending 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] Example: | 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
|
Algorithm BubbleSort(A[0..n - 1]) //The algorithm sorts array A[0..n - 1] by bubble sort //Input: An array A[0..n - 1] of orderable elements //Output: Array A[0..n - 1] sorted in ascending order 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 Example : 89 ↔️ 45 68 90 29 34 17 45 89 ↔️ 68 90 29 34 17 45 68 89 ↔️ 90 ↔️ 29 34 17 45 68 89 29 90 ↔️ 34 17 45 68 89 29 34 90 ↔️ 17 45 68 89 29 34 17 |90
45 ↔️ 68 ↔️ 89 ↔️ 29 34 17 |90 45 68 29 89 ↔️ 34 17 |90 45 68 29 34 89 ↔️ 17 |90 45 68 29 34 17 |89 90
|
ti = p0 , …….., ti+j = pj ,........, ti+m-1 = pm-1 : t0 …….ti ……..ti+j …...ti+m-1 ….tn-1 text T p0 … pj ….. pm-1 pattern P
Text : 10010101101001100101111010 2. Pattern : happy Text : It's never too late to be happy childhood. N O B O D Y _ N O T I C E D _ H I M N O T N O T N O T N O T N O T N O T N O T N O T
|
Algorithm BruteForceClosestPoints(P) // Input : A list P of n (n ≥ 2) points P1 = (x1 , y1) ,........, Pn = (xn , yn) // Output : Indicates index1 and index2 of the closet pair of points dmin ← ∞ for i ← 1 to n - 1 do for j ← i + 1 to n do d ← sqrt((xi - xj)2 + (yi - yj)2 ) // sqrt is the square root function if d < dmin dmin ← d ; index1 ← i ; index2 ← j Return index1, index2 |
ALGORITHM SequentialSearch2(A [0..n], K) //With a search key as a // sentinel, the algorithm implements sequential search //Input: An array A of n elements and a search key K //Output: The position of the first element in A[0..n - 1] whose value is // equal to K or -1 if no such element is found A[n]=K i=0 while A[i] = K do i=i + 1 if i < n return i else return |