Download - Advanced Algorithms Analysis and Design
![Page 1: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/1.jpg)
Advanced Algorithms Analysis and Design
Lecture 6(Continuation of 5th Lecture)
ByEngr Huma Ayub Vine
1
![Page 2: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/2.jpg)
![Page 3: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/3.jpg)
Algorithm Comparison (5 elements)
![Page 4: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/4.jpg)
![Page 5: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/5.jpg)
![Page 6: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/6.jpg)
![Page 7: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/7.jpg)
![Page 8: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/8.jpg)
![Page 9: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/9.jpg)
![Page 10: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/10.jpg)
![Page 11: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/11.jpg)
![Page 12: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/12.jpg)
![Page 13: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/13.jpg)
![Page 14: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/14.jpg)
![Page 15: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/15.jpg)
![Page 16: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/16.jpg)
![Page 17: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/17.jpg)
![Page 18: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/18.jpg)
![Page 19: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/19.jpg)
![Page 20: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/20.jpg)
![Page 21: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/21.jpg)
![Page 22: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/22.jpg)
![Page 23: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/23.jpg)
![Page 24: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/24.jpg)
![Page 25: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/25.jpg)
![Page 26: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/26.jpg)
![Page 27: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/27.jpg)
![Page 28: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/28.jpg)
![Page 29: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/29.jpg)
![Page 30: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/30.jpg)
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.
![Page 31: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/31.jpg)
![Page 32: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/32.jpg)
![Page 33: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/33.jpg)
![Page 34: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/34.jpg)
![Page 35: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/35.jpg)
![Page 36: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/36.jpg)
Assignment 2
Prove that quick sort recursive case average analysis is
Submit after midterm
![Page 37: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/37.jpg)
Algorithm Comparison (2K Elements)
9
![Page 38: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/38.jpg)
Computation Time for 100,000 elements
250
200
150100
50
0
Series1
![Page 39: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/39.jpg)
COMPARISONSort Worst Average Best
Method2 2Bubble O(n ) O(n ) O(n)
Quick O(n2 ) O(n log n) O(log n)Heap O(n log n) O(n log n) O(log n)
2 2Insertion O(n ) O(n ) O(n)2 2Selection O(n ) O(n ) O(n)
Merge O(n log n) O(n log n) O(log n)
Radix O(n2 ) O(s*n) O(n log n)2 5/3Shell O(n ) O(n ) O(n)
Counting O(n + k) O(n) O(n)
Bucket O(n2 ) O(n) O(n)
Pigeonhole O(n + s) O(n) O(n)
![Page 40: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/40.jpg)
ALGORITHMS AND THEIR COMPLEXITY(Limits on Problem Size as Determined by Growth Rate)
Algo TimeComplexity
Maximum Problem Size (n)
1 sec 1 min 1 hour
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
A4 3n 10 39 153
A5 2n 9 15 21
![Page 41: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/41.jpg)
Calculating the Greatest Common DivisorFunction Euclid (m,n)
while m > 0 dot ← mm ← n mod mn ← t
return n
Euclid (14, 21) : t m n14 7 147 0 7
return = 7
Euclid (6, 15) : t m n6 3 63 0 3
return = 3Takes a time in the order of the algorithm of its arguments
![Page 42: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/42.jpg)
Calculating the Fibonacci Sequenceƒ0 = 0, ƒ1 = 1ƒn = ƒn-1 + ƒn-2 for n > = 2Function Fibrec (n)
if n < 2 then return nelse return Fibrec (n - 1) + Fibrec (n - 2)
f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f110 1 1 2 3 5 8 13 21 34 55 89Time 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
![Page 43: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/43.jpg)
Function Fibiter (n){ Calculates the n-th term of the Fibonacci sequence;}i ← 1;
j ← 0for k ← 1 to n do
j ← i + ji ← j
- ireturn jFibonacci (5)
i = 1j = 0k = 1 2 3 4 5j = 1 1 2 3 5i= 0 1 1 2 3• Time required to calculate fn is in the order of n
![Page 44: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/44.jpg)
Calculating the Fibonacci Sequencede Moivre’s formula
fn=1 n -n[φ−(−φ) ]5
1+ 5where φ=2
(φ=1.61803)
Time required to calculate fn is value of fn is in theorder of φn
![Page 45: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/45.jpg)
Examplef10 = f10-1 + f10-2
= f9 + f8= 34+21= 55 ---------------------- (i)
f 10=
=
1 n − n[φ−(−φ) ]5
1 10 −10[1.61803) −(−1.61803) ]2.236
=
=
1 [2.236
1 [2.236
122.98883−0.008131]
122.9807]= 55.0003= 55 (ii)
![Page 46: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/46.jpg)
Function Fibiter (n)i ← 1; j ← 0for k ← 1 to n d
j ← i+ji
← j - ireturn jComparison of modulo fibonacci algorithms
n 10 20 30 50 100Fibrec 8 msec 1 sec 2 min 21 days 109 years
Fibiter 1/6 1/3 ½ msec ¾ msec 1 ½ msecmsec msec
![Page 47: Advanced Algorithms Analysis and Design](https://reader036.vdocuments.us/reader036/viewer/2022062302/56814d08550346895dba3cc9/html5/thumbnails/47.jpg)
Important Points for an Algorithm• Correct in execution
• Execution time
• Storage needs
• Limitation of computing equipment to supportoperations for desired numbers to haveprecision within in limits
• Efficient methodology for the specific task
• Programming/ hardware implementation tools
• Average/worst-case performance