530 likes | 756 Views
Advanced Algorithms Analysis and Design. Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine. Algorithm Comparison (5 elements). Quicksort. first call. 2 nd call. 3 rd call.
E N D
Advanced Algorithms Analysis and Design Lecture 6 (Continuation of 5th Lecture) By EngrHumaAyub Vine
Quicksort first call 2nd call 3rd call Each time the (sub) list is partitioned exactly one value (the pivot) is placed in its correct position. If the list is equally divided during each partitioning, it will require about lg (n) calls to quickSort to produce a sorted list.
Assignment 2 Prove that quick sort recursive case average analysis is Submit after midterm
Computation Time for 100,000 elements 250 200150 Series1 100 50 0
COMPARISON Sort Worst Average Best Method O(n ) O(n ) O(n) Bubble 2 2 Quick O(n ) O(n log n) O(log n) 2 Heap O(n log n) O(n log n) O(log n) O(n ) O(n ) O(n) Insertion 2 2 O(n ) O(n ) O(n) Selection 2 2 Merge O(n log n) O(n log n) O(log n) Radix O(n ) O(s*n) O(n log n) 2 O(n ) O(n ) O(n) Shell 2 5/3 Counting O(n + k) O(n) O(n) Bucket O(n ) O(n) O(n) 2 Pigeonhole O(n + s) O(n) O(n)
ALGORITHMS AND THEIR COMPLEXITY (Limits on Problem Size as Determined by Growth Rate) Maximum Problem Size (n)1 sec 1 min 1 hour TimeComplexity Algo A1 n 1000 6 x 104 3.6 x 106 A2 n log n 140 4893 2.0 x 105 A3 n2 31 244 1897 3 A4 10 39 153 n A5 2n 9 15 21
Calculating the Greatest Common DivisorFunction Euclid (m,n) while m > 0 do t ← m m ← n mod mn ← t return n Euclid (14, 21) : t m n 14 7 14 0 7 7 return = 7 Euclid (6, 15) : t m n 6 3 6 3 0 3 return = 3 Takes a time in the order of the algorithm of its arguments
Calculating the Fibonacci Sequenceƒ0 = 0, ƒ1 = 1 ƒn = ƒn-1 + ƒn-2 for n > = 2Function Fibrec (n) if n < 2 then return n else return Fibrec (n - 1) + Fibrec (n - 2) f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11 0 1 1 2 3 5 8 13 21 34 55 89 Time required to calculate fn is in the order of thevalue of fn. It is very inefficient compared to deMoivre’s formula • It is better to use Fibiter algorithm to calculate theFibonnaci Sequence
Function Fibiter (n) { Calculates the n-th term of the Fibonacci sequence;}i← 1; j ← 0 for k ← 1 to n do j ←i + j i← j - ireturn j Fibonacci (5)i = 1 j = 0 k = 1 2 3 4 5 j = 1 1 2 3 5 i= 0 1 1 2 3 • Time required to calculate fn is in the order of n
Calculating the Fibonacci Sequence de Moivre’s formula 1 n -n fn= [φ−(−φ) ] 5 1+ 5 (φ=1.61803) where φ= 2 Time required to calculate fn is value of fn is in theorder of φn
Example f10 = f10-1 + f10-2 = f9 + f8 = 34+21 = 55 ---------------------- (i) 1 n − n f 10= [φ−(−φ) ] 5 1 10 −10 = [1.61803) −(−1.61803) ] 2.236 1 122.98883−0.008131] = [ 2.236 1 122.9807] = [ 2.236 55.0003 = (ii) = 55
Function Fibiter (n) i ← 1; j ← 0 for k ← 1 to n d j ←i+j i← j - ireturn j Comparison of modulo fibonacci algorithms n 10 20 30 50 100 Fibrec 8 msec 1 sec 2 min 21 days 109 years Fibiter 1/6 1/3 ½ msec ¾ msec 1 ½ msec msec msec
Important Points for an Algorithm • Correct in execution • Execution time • Storage needs • Limitation of computing equipment to support operations for desired numbers to have precision within in limits • Efficient methodology for the specific task • Programming/ hardware implementation tools • Average/worst-case performance