advanced algorithms analysis and design lecture 6 (continuation of 5 th lecture) by engr huma ayub...

47
Advanced Algorithms Analysis and Design Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1

Upload: jeremy-cannon

Post on 04-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Advanced Algorithms Analysis and Design Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1

Advanced Algorithms Analysis and Design

Lecture 6

(Continuation of 5th Lecture)

By

Engr Huma Ayub Vine

1

Page 2: Advanced Algorithms Analysis and Design Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1
Page 3: Advanced Algorithms Analysis and Design Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1

Algorithm Comparison (5 elements)

Page 4: Advanced Algorithms Analysis and Design Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1
Page 5: Advanced Algorithms Analysis and Design Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1
Page 6: Advanced Algorithms Analysis and Design Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1
Page 7: Advanced Algorithms Analysis and Design Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1
Page 8: Advanced Algorithms Analysis and Design Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1
Page 9: Advanced Algorithms Analysis and Design Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1
Page 10: Advanced Algorithms Analysis and Design Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1
Page 11: Advanced Algorithms Analysis and Design Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1
Page 12: Advanced Algorithms Analysis and Design Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1
Page 13: Advanced Algorithms Analysis and Design Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1
Page 14: Advanced Algorithms Analysis and Design Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1
Page 15: Advanced Algorithms Analysis and Design Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1
Page 16: Advanced Algorithms Analysis and Design Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1
Page 17: Advanced Algorithms Analysis and Design Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1
Page 18: Advanced Algorithms Analysis and Design Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1
Page 19: Advanced Algorithms Analysis and Design Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1
Page 20: Advanced Algorithms Analysis and Design Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1
Page 21: Advanced Algorithms Analysis and Design Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1
Page 22: Advanced Algorithms Analysis and Design Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1
Page 23: Advanced Algorithms Analysis and Design Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1
Page 24: Advanced Algorithms Analysis and Design Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1
Page 25: Advanced Algorithms Analysis and Design Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1
Page 26: Advanced Algorithms Analysis and Design Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1
Page 27: Advanced Algorithms Analysis and Design Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1
Page 28: Advanced Algorithms Analysis and Design Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1
Page 29: Advanced Algorithms Analysis and Design Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1
Page 30: Advanced Algorithms Analysis and Design Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1

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 Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1
Page 32: Advanced Algorithms Analysis and Design Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1
Page 33: Advanced Algorithms Analysis and Design Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1
Page 34: Advanced Algorithms Analysis and Design Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1
Page 35: Advanced Algorithms Analysis and Design Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1
Page 36: Advanced Algorithms Analysis and Design Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1

Assignment 2

Prove that quick sort recursive case average analysis is

Submit after midterm

Page 37: Advanced Algorithms Analysis and Design Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1

Algorithm Comparison (2K Elements)

9

Page 38: Advanced Algorithms Analysis and Design Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1

Computation Time for 100,000 elements

250

200

150

100

50

0

Series1

Page 39: Advanced Algorithms Analysis and Design Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1

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 Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1

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 Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1

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 Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1

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 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 Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1

Function Fibiter (n)

{ Calculates the n-th term of the Fibonacci sequence;}

i ← 1;j ← 0

for k ← 1 to n doj ← i + j

i ← 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 Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1

Calculating the Fibonacci Sequence

de 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 Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1

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 Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1

Function Fibiter (n)i ← 1; j ← 0for k ← 1 to n d

j ← i+ji

← 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 ½ msecmsec msec

Page 47: Advanced Algorithms Analysis and Design Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1

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