lecture 2-the big-oh notation

30
•Complexity –The complexity of an algorithm is simply the amount of work the algorithm performs to complete its task.

Upload: orniel-naces-jamandra

Post on 28-Oct-2014

79 views

Category:

Documents


1 download

TRANSCRIPT

ComplexityThe complexity of an algorithm is simply the amount of work the algorithm performs to complete its task.

Analysis of Algorithms Overall goal is have an understanding of the complexity of algorithmsEstimate the running time Estimate the memory space required Depends on the input size

The Big-Oh Notation Describes the performance or complexity of an Algorithm Specifically describes the worst-case scenario, and can be used to describe the execution time required or the space used by an algorithm.

Frequency Count and Time Complexity (Big-Oh) Big-Oh Notation To simplify the running time estimation, for a functionf(n), we ignore the constants and lower order terms. Example: 10n3+4n2-4n+5 is O(n3). Common computing times. N is the input parameter O(1) < O(log n) < O(n) < O(n log n) < O (n2) < O (n3)< O (2n)

O(1):constant computing time

Constant; most instructions are executed once or at most only a few times. Describes an algorithm that will always execute in the same time (or space) regardless of the size of the input.

O(log n)

Program slightly slower as N grows; normally in programs that solve a big problem by transforming it into a small problem, cutting the size by some constant factor.

O(n):Linear

Describes an algorithm whose performance will grow linearly and in direct proportion to the size of the input.

O(n log n)

Occurs in algorithms that solve a problem by breaking it up into smaller sub-problems, solve them independently and then combining the solution.

O (n2):quadratic Represents an algorithm whose performance is directly proportional to the square of the size of the input. This is common with algorithms that involve nested iterations over the data set. Deeper nested iterations will result in O(n3), O(n4), etc

O (2n): exponential

Denotes an algorithm whose growth will double with each additional element in the input.

Function of Growth rate

Tuesday, September 11, 2012

Design and Analysis of Computer Algorithm

11

The Big-Oh Notation cont... Gives an appropriate measure of the computing time of an algorithm for a large no. of inputs If 2 algorithms for performing the same task have different complexities, the algorithm with the lower order computing time is usually preferred. EgAlgorithm 1T1(n) is o(n)

vs.

Algorithm 2T2(n) is o(n2)

The Big-Oh Notation cont... Then Algorithm 1 is usually considered better than algorithm 2 However that for small values of n, Algorithm 2 might well outperform Algorithm 1Eg. T1(n) is 10n T2(n) is 0.1n2 10n>0.1n2 for values n up to 100 T1(n) < T2(n) only for n >100

The core of the algorithm analysis: to find out how the number of the basic operations depends on the size of the input

Example #1 Below is an algorithm SIMPLE in form of pseudo code SIMPLE 1 for x 4 to 1 2 do x * 2 3 x x- 1 4 print xFor analysis we will execute the code and calculate how many times each line of our algorithm is executed.UOH-CCSE-ICS353-prepared by:Shamiel .H -Natalia .A

First hand AnalysisSIMPLE 1 for x 4 to 1 2 do x * 2 3 x x- 1 4 print xLine # 1 2 Number of Executions 1,1,1,1 1,1,1,1 Total Executions 4 4

34

1,1,11

31UOH-CCSE-ICS353-prepared by:Shamiel .H -Natalia .A

First hand Analysis So we have total of 12 executions If one operation takes 10 nanoseconds to execute then total execution time for our SIMPLE algorithm will be 10 * 12= 120 nanoseconds.

This is the running time of our algorithm.

UOH-CCSE-ICS353-prepared by:Shamiel .H -Natalia .A

Example #2Algorithm to Calculate Mean Accepts: An integer n and an array X[1],..,X[n] of real numbers Function: Finds the mean of n real nos. Returns: The of X[1],...X[n] 1. Initialize sum to 0 2. Initialize index variable i to 0 3. While i