Advanced Algorithms Analysis and Design

Preview:

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Advanced Algorithms Analysis and Design

Lecture 6(Continuation of 5th Lecture)

ByEngr Huma Ayub Vine

1

Algorithm Comparison (5 elements)

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

Algorithm Comparison (2K Elements)

9

Computation Time for 100,000 elements

250

200

150100

50

0

Series1

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)

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

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

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

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

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

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)

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

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

Recommended