• No results found

DAA Sadaf Riaz

N/A
N/A
Protected

Academic year: 2020

Share "DAA Sadaf Riaz"

Copied!
25
0
0

Loading.... (view fulltext now)

Full text

(1)
(2)

Design and Analysis of Algorithms

Lecture 2

Instructor : Sadaf Riaz

Class: CS 6th A & B

(3)

Algorithm Analysis

❖ Analysis determines the runtime of an algorithm

based on the input size

❖ Gives you the relative rate of growth but not

specific time/space of execution

❖ Allows for comparison of algorithms without

implementation

❖ Not hardware specific

❖ Describes the number of operations for a given

(4)

What is “Input Size”?

❖ Depends on the problem being analyzed

❖ The number of items in the input is often used

Number of values in a sorting algorithm

❖ May use more than one parameter

(5)

Asymptotic Analysis of Algorithms

(Growth of function)

❖ The word Asymptotic means approaching a value or curve arbitrarily closely (i.e., as some sort of limit is taken).

❖ Execution time of an algorithm depends on the instruction set, processor speed, disk I/O speed, etc. Hence, we

estimate the efficiency of an algorithm asymptotically.

❖ It is a technique of representing limiting behavior.

❖ It can be used to analyze the performance of an algorithm for some large input data set.

❖ Asymptotic notations are used to write fastest and slowest possible running time for an algorithm('best case' and

'worst case' scenarios respectively).

(6)

Asymptotic Analysis of Algorithms

(Growth of function)

❖ Examples:

❖ Let f (n) = an2+bn+c

(n2 dominates the function that is when n gets sufficiently large)

❖ ƒ (n) = n2+3n

(The term 3n becomes insignificant compared to n2 when n is very large.)

❖ The function "ƒ (n) is said to be asymptotically equivalent to n2 as n → ∞“

(7)

Types of Asymptotic Notations

❖ Following asymptotic notations are used to

calculate the running time complexity of an algorithm.

1) O − Big Oh (Asymptotic Upper Bound)

2) Ω − Big omega (Asymptotic Lower Bound)

3) θ − Big theta (Asymptotic Tight Bound)

4) o − Little Oh

(8)

1) O − Big Oh (Asymptotic Upper Bound)

❖ Big-oh is the formal method of expressing the

upper bound of an algorithm's running time.

❖ It is the measure of the longest amount of time.

❖ The function f (x) = O (g (x)) [read as "f of x is

big-oh of g of x"] if and only if exist positive constant C and k such that

|f(x)| ≤ C|g(x)| whenever x > k in all cases

❖ function g(x) is an upper bound for function f(x),

(9)
(10)

1) O − Big Oh (Asymptotic Upper Bound)

❖ Big-O provides an upper bound on a function (to

within a constant factor)

❖ O(g(x)) is a set of functions

❖ Commonly used notation

f(x) = O(g(x))

❖ Correct notation

f(x) ∈ O(g(x))

❖ Meaningless statement

O(g(x)) = f(x)

We will use both

(11)

1) O − Big Oh (Asymptotic Upper Bound)

Properties of O − Big Oh

❖ If “f(x) is O(g(x))” then g(x) grows at least as fast

as f(x) (reflexive)

❖ “f(x) is O(g(x))” iff O(f(x)) ⊆ O(g(x))

❖ If “f(x) is O(g(x))” and “g(x) is O(f(x))” then

O(f(x))=O(g(x)) (symetric)

❖ If “f(x) is O(g(x))” and “h(x) is O(g(x))” then

(12)

1) O − Big Oh (Asymptotic Upper Bound)

Properties of O − Big Oh

❖ If a is a scalar and “f(x) is O(g(x))”, then

a*f(x) is O(g(x)) If “f(x) is O(g(x))” and “g(x) is O(h(x))”, then

f(x) is O(h(x)) (transitive property)

❖ f(x) = O(g(x))

(13)

Common Complexity Functions

Complexity

Term

O(1) constant

O(log n) logarithmic

O(n) linear

O(n log n) n log n

O(nb) polynomial

O(bn) b > 1 exponential

(14)
(15)

Important Big-O Result

❖ Let f(x)=anxn+an-1xn-1+…+a1x+a0, where a0, a1,

…,an-1, an are real numbers.

Then f(x) is O(xn).

❖ 3n+2=O(n) as 3n+2≤4n for all n≥2

❖ 3n+3=O(n) as 3n+3≤4n for all n≥3

❖ f(n)=4.n3+10.n2+5.n+1

Considering g(n)=n3, f(n)⩽5.g(n) for all the

values of n>2 so the complexity of f(n) can be

(16)

Big-O Estimates

❖ What is the big-O estimate for n!?

n! = 1 • 2 • 3 • … • n

n! ≤ nnn • … • n

n! ≤ nn n! is O(nn)

log n! ≤ log nn

log n! ≤ n log n log n! is O(n log n)

❖ What is the big-O estimate for log(n!)?

(17)

2) Ω − Big omega (Asymptotic Lower

Bound)

❖ Big-Omega is the formal method of expressing

lower bound for a function to within a constant factor.

❖ Let f and g be functions from N or R to R. We

say that “f(x) is Ω(g(x))” if there are positive constants C and k such that

|f(x)| ≥ C |g(x)| whenever x > k.

(18)
(19)

3) θ − Big theta (Asymptotic Tight

Bound)

❖ f(x)=O(g(x)) only provides an upper bound in

terms of g(x) for f(x).

❖ f(x)= Ω(g(x)) only provides a lower bound in

terms of g(x) for f(x).

❖ Big-Theta provides both an upper bound and a

(20)

3) θ − Big theta (Asymptotic Tight

Bound)

❖ Let f and g be functions from N or R to R. We

say that “f(x) is θ(g(x))” if “f(x) is O(g(x))” and “f(x) is Ω(g(x))”, i.e., there are positive constants

C1, C2, and k such that

C1|g(x)| ≤ |f(x)| ≤ C2|g(x)| whenever x>k.

❖ Read as: “f(x) is big-Theta of g(x)”

(21)
(22)

The “sets” and their use – big Oh

❖ Big “oh” - asymptotic upper bound on the growth

of an algorithm

❖ When do we use Big Oh?

1. Theory of NP-completeness

2. To provide information on the maximum number of operations that an algorithm performs

– Insertion sort is O(n2) in the worst case

• This means that in the worst case it performs at most cn2 operations – Insertion sort is also O(n6) in the worst case since it also performs at most

(23)

The “sets” and their use – Omega

Omega - asymptotic lower bound on the growth of an algorithm or a

problem*

When do we use Omega?

1. To provide information on the minimum number of operations that an algorithm performs

– Insertion sort is Ω(n) in the best case

• This means that in the best case its instruction count is at least cn,

– It is Ω(n2) in the worst case

(24)

The “sets” and their use – Omega

2. To provide information on a class of algorithms that solve a problem

– Sort algorithms based on comparison of keys are Ω(nlgn) in the worst case

• This means that all sort algorithms based only on

comparison of keys have to do at least cnlgn operations – Any algorithm based only on comparison of keys to find the

maximum of n elements is Ω(n) in every case

(25)

References

Related documents

The results of this experiment indicate that reverse flushing of porous pavements with water at relatively low pressure levels should be an effective process for maintaining

The 2011 ePortfolio review established that implementation of a Google platform at Griffith University provided an immediate ePortfolio capability within the enterprise

The rationale for the project was to design a series of lessons that provided year nine (aged 13/14) pupils with an opportunity to apply information literacy skills to a research

o 30 Days: Once an Investigation Request is initiated in JPAS, an applicant has 30 days to login to e-QIP and start their Personnel Security Questionnaire (PSQ).. If they do

In this study, the in− fluence of the extract from the radix of Scutellaria baicalensis on the activity of ALT manifested itself as an increase in the activity of this enzyme in

Students can earn credits toward their GSPIA degrees by taking courses in the Graduate School of Public Administration and/or the Graduate School of International Studies at

the land. In the 1920s and 1930s, land grabbing was prevalent in agricultural regions in the Philippines. The refusal of tenants to vacate the land could be traced, in some

Building on the marketing analytics in the core part of the program, this course looks at Search Engine Optimization (improving the ranking of web- sites on Google and other