Advanced Algorithms Analysis and Design Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1
Algorithm Comparison (5 elements)
Quicksort first call 2 nd call 3 rd 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
Algorithm Comparison (2K Elements) 9
Computation Time for 100,000 elements Series1
COMPARISON SortWorstAverageBest Method 22 Bubble O(n) )O(n) QuickO(n 2 )O(n log n)O(log n) HeapO(n log n) O(log n) 22 Insertion O(n) )O(n) 22 Selection O(n) )O(n) Merge O(n log n) O(log n) RadixO(n 2 )O(s*n)O(n log n) 25/3 Shell O(n) )O(n) CountingO(n + k)O(n) Bucket O(n 2 )O(n) PigeonholeO(n + s)O(n)
ALGORITHMS AND THEIR COMPLEXITY (Limits on Problem Size as Determined by Growth Rate) Algo Time Complexity Maximum Problem Size (n) 1 sec1 min1 hour A1n10006 x x 10 6 A2n log n x 10 5 A3n2n A4 3 n A52n2n 91521
Calculating the Greatest Common Divisor Function Euclid (m,n) while m > 0 do t ← m m ← n mod m n ← t return n Euclid (14, 21) :tm n return = 7 Euclid (6, 15) :tm n 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 > = 2 Function 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 f Time required to calculate f n is in the order of the value of f n. It is very inefficient compared to de Moivre’s formula It is better to use Fibiter algorithm to calculate the Fibonnaci 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 - i return j Fibonacci (5) i = 1 j = 0 k =12345 j =11235 i=01123 Time required to calculate f n is in the order of n
Calculating the Fibonacci Sequence de Moivre’s formula fn = 1 n - n [ φ− ( −φ ) ] where φ= 2 ( φ= ) Time required to calculate f n is value of f n is in the order of φ n
Example f10= f f10-2 = f9 + f8 =34+21 = (i) f 10= = 1n−n1n−n [ φ− ( −φ ) ] −10 [ ) − ( − ) ] = = 1 [ 1 [ − ] ] = = 55 (ii)
Function Fibiter (n) i ← 1; j ← 0 for k ← 1 to n d j ← i+j i ← j - i return j Comparison of modulo fibonacci algorithms n Fibrec8 msec1 sec2 min21 days10 9 years Fibiter1/61/3½ msec¾ msec1 ½ 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