14
Algorithms analysis and design BY Lecturer: Aisha Dawood

BY Lecturer: Aisha Dawood. stands alone on the right-hand side of an equation (or inequality), example : n = O(n 2 ). means set membership :n ∈ O(n

Embed Size (px)

Citation preview

Page 1: BY Lecturer: Aisha Dawood.  stands alone on the right-hand side of an equation (or inequality), example : n = O(n 2 ). means set membership :n ∈ O(n

Algorithms analysis and design

BYLecturer: Aisha Dawood

Page 2: BY Lecturer: Aisha Dawood.  stands alone on the right-hand side of an equation (or inequality), example : n = O(n 2 ). means set membership :n ∈ O(n

2

stands alone on the right-hand side of an equation (or inequality), example : n = O(n2). means set membership :n O(n∈ 2).

when it appears in a formula, we interpret it as standing for some function that we do not care to name. For example, 2n2 + 3n + 1 = 2n2 + (n).

Left hand side and right hand side example : 2n2 + (n) = O(n2).

Order of growth notations in equations and inequalities

Page 3: BY Lecturer: Aisha Dawood.  stands alone on the right-hand side of an equation (or inequality), example : n = O(n 2 ). means set membership :n ∈ O(n

3

the relational properties of real numbers apply to asymptotic comparisons as well.

Comparing functions

Page 4: BY Lecturer: Aisha Dawood.  stands alone on the right-hand side of an equation (or inequality), example : n = O(n 2 ). means set membership :n ∈ O(n

4

Page 5: BY Lecturer: Aisha Dawood.  stands alone on the right-hand side of an equation (or inequality), example : n = O(n 2 ). means set membership :n ∈ O(n

5

For any two functions f(n) and g(n), we have f(n) = (g(n)), if and only if f(n) = O(g(n)) and f(n) = Ω(g(n)).

Proof: left as a home work….

Theorem

Page 6: BY Lecturer: Aisha Dawood.  stands alone on the right-hand side of an equation (or inequality), example : n = O(n 2 ). means set membership :n ∈ O(n

6

Common expressions used to describe algorithm’s running time:

Name Expression

Constant 1 ,2,…,c

Logarithmic log(n)

Log squared log2(n)

Linear n

n log n n.log(n)

Quadratic n2

Cubic n3

Exponential 2n

Page 7: BY Lecturer: Aisha Dawood.  stands alone on the right-hand side of an equation (or inequality), example : n = O(n 2 ). means set membership :n ∈ O(n

7

Conclusions (f(n)) describes f(n) f(n) = O(g(n)) and f(n) = Ω(g(n)). o(f(n) describes f(n) f(n) = O(g(n)) but not f(n) = Ω(g(n)). ω(f(n)) describes f(n) f(n) = Ω(g(n)) but not f(n) = O(g(n)).

Page 8: BY Lecturer: Aisha Dawood.  stands alone on the right-hand side of an equation (or inequality), example : n = O(n 2 ). means set membership :n ∈ O(n

8

Rule #1: Sequential composition: Worst case running time of a sequence :

Rules For Big Oh Analysis of Running Time

Page 9: BY Lecturer: Aisha Dawood.  stands alone on the right-hand side of an equation (or inequality), example : n = O(n 2 ). means set membership :n ∈ O(n

9

Rule #2: Iteration: Worst case running time of an iteration:

Rules For Big Oh Analysis of Running Time

For (S1)S2S3...SmO(T(n)) = MAX (O(T1(n)),O(T2(n)),...,O(Tm(n)))

Page 10: BY Lecturer: Aisha Dawood.  stands alone on the right-hand side of an equation (or inequality), example : n = O(n 2 ). means set membership :n ∈ O(n

10

Rule #3: Conditional execution: Worst case running time of a conditional

statement :

Rules For Big Oh Analysis of Running Time

Page 11: BY Lecturer: Aisha Dawood.  stands alone on the right-hand side of an equation (or inequality), example : n = O(n 2 ). means set membership :n ∈ O(n

11

Determine a tight big-oh bound on the running time of a program to compute the series of sums S0 , S1, S2..., Sn-1 where

T(n) = O(n2)

Example-Prefix Sums

Page 12: BY Lecturer: Aisha Dawood.  stands alone on the right-hand side of an equation (or inequality), example : n = O(n 2 ). means set membership :n ∈ O(n

12

The Fibonacci numbers are the series of numbers F0,F1 ,F2 ... given by

Consider the sequence of Fibonacci numbers

T(n) = O(n)

Example-Fibonacci Numbers

Page 13: BY Lecturer: Aisha Dawood.  stands alone on the right-hand side of an equation (or inequality), example : n = O(n 2 ). means set membership :n ∈ O(n

13

we use the definition of Fibonacci numbers to implement directly a recursive algorithm

Example-Fibonacci Numbers

Page 14: BY Lecturer: Aisha Dawood.  stands alone on the right-hand side of an equation (or inequality), example : n = O(n 2 ). means set membership :n ∈ O(n

14

T(n) = Ω(Fn+1) By induction from the nature of the problem we get T(n) = Ω(2n)

Example-Fibonacci Numbers