66
Algorithm Analysis October 12, 2016 CMPE 250 Algorithm Analysis October 12, 2016 1 / 66

Algorithm Analysis - CmpE WEB · Bachmann, Edmund Landau, ... Bachmann-Landau notationorasymptotic notation. CMPE 250 Algorithm Analysis October 12, 2016 30 / 66

  • Upload
    vudung

  • View
    220

  • Download
    0

Embed Size (px)

Citation preview

Algorithm Analysis

October 12, 2016

CMPE 250 Algorithm Analysis October 12, 2016 1 / 66

Problem Solving: Main Steps

1 Problem definition2 Algorithm design / Algorithm specification3 Algorithm analysis4 Implementation5 Testing6 Maintenance

CMPE 250 Algorithm Analysis October 12, 2016 2 / 66

1. Problem Definition

What is the task to be accomplished?Calculate the average of the grades for a given studentUnderstand the talks given out by politicians and translate them inChinese

What are the time / space / speed / performance requirements ?

CMPE 250 Algorithm Analysis October 12, 2016 3 / 66

2. Algorithm Design / Specifications

Algorithm: Finite set of instructions that, if followed, accomplishes aparticular task.

Describe: in natural language / pseudo-code / diagrams / etc.Criteria to follow:

Input: Zero or more quantities (externally produced)Output: One or more quantitiesDefiniteness: Clarity, precision of each instructionFiniteness: The algorithm has to stop after a finite (may be very large)number of stepsEffectiveness: Each instruction has to be basic enough and feasible

Understand speechTranslate to Chinese

CMPE 250 Algorithm Analysis October 12, 2016 4 / 66

Computer Algorithms+

An algorithm is a procedure (a finite set of well-defined instructions)for accomplishing some tasks which,

given an initial stateterminate in a defined end-state

The computational complexity and efficient implementation of thealgorithm are important in computing, and this depends on usingsuitable data structures.

CMPE 250 Algorithm Analysis October 12, 2016 5 / 66

4,5,6: Implementation, Testing, Maintenance

ImplementationDecide on the programming language to use

C, C++, Lisp, Java, Perl, Prolog, assembly, etc.

Write clean, well documented code

Test, test, test

Integrate feedback from users, fix bugs, ensure compatibility acrossdifferent versions→ Maintenance

CMPE 250 Algorithm Analysis October 12, 2016 6 / 66

3. Algorithm Analysis

Space complexityHow much space is required

Time complexityHow much time does it take to run the algorithm

Often, we deal with estimates!

CMPE 250 Algorithm Analysis October 12, 2016 7 / 66

Space Complexity

Space complexity = The amount of memory required by an algorithmto run to completion

Core dumps = the most often encountered cause is "dangling pointers"

Some algorithms may be more efficient if data completely loaded intomemory

Need to look also at system limitations e.g. Classify 20GB of text invarious categories [politics, tourism, sport, natural disasters, etc.] – canI afford to load the entire collection?

CMPE 250 Algorithm Analysis October 12, 2016 8 / 66

Space Complexity (cont’d)

1 Fixed part: The size required to store certain data/variables, that isindependent of the size of the problem:

e.g. name of the data collectionsame size for classifying 2GB or 1MB of texts

2 Variable part: Space needed by variables, whose size is dependenton the size of the problem:

e.g. actual textload 2GB of text vs. load 1MB of text

CMPE 250 Algorithm Analysis October 12, 2016 9 / 66

Space Complexity (cont’d)

S(P) = c + S(instance characteristics)

c = constant

Example:

float summation(const float (&a) [10], int n)

float s=0;

for (int i = 0; i < n; i++ )

s+=a[i];return s;

Space? one for n , one for a (passed by reference!), one for s ,one for i → constant space!

CMPE 250 Algorithm Analysis October 12, 2016 10 / 66

Time Complexity

Often more important than space complexityspace available (for computer programs!) tends to be larger and largertime is still a problem for all of us

3-4GHz processors on the marketstill ...researchers estimate that the computation of various transformationsfor 1 single DNA chain for one single protein on 1 TerraHZ computerwould take about 1 year to run to completion

Algorithm’s running time is an important issue.

CMPE 250 Algorithm Analysis October 12, 2016 11 / 66

Running time

Suppose the program includes an if-then statement that mayexecute or not: → variable running time.Typically algorithms are measured by their worst case

CMPE 250 Algorithm Analysis October 12, 2016 12 / 66

Running Time

The running time of an algorithmvaries with the inputs, andtypically grows with the size of theinputs.

To evaluate an algorithm or tocompare two algorithms, we focuson their relative rates of growthwrt the increase of the input size.

The average running time isdifficult to determine.

We focus on the worst caserunning time

Easier to analyzeCrucial to applications suchas finance, robotics, andgames

CMPE 250 Algorithm Analysis October 12, 2016 13 / 66

Running Time

Problem: prefix averagesGiven an array XCompute the array A such that A[i] is the average of elementsX[0] ... X[i] , for i=0..n-1

Sol 1At each step i , compute the element X[i] by traversing thearray A and determining the sum of its elements, respectively theaverage

Sol 2At each step i update a sum of the elements in the array ACompute the element X[i] as sum/i

Big question: Which solution to choose?

CMPE 250 Algorithm Analysis October 12, 2016 14 / 66

Experimental Approach

Write a program to implementthe algorithm.

Run this program with inputs ofvarying size and composition.

Get an accurate measure ofthe actual running time (e.g.system call date).

Plot the results.

Problems?

CMPE 250 Algorithm Analysis October 12, 2016 15 / 66

Limitations of Experimental Studies

The algorithm has to be implemented, which may take a long timeand could be very difficult.

Results may not be indicative for the running time on other inputs thatare not included in the experiments.

In order to compare two algorithms, the same hardware and softwaremust be used.

CMPE 250 Algorithm Analysis October 12, 2016 16 / 66

Use a Theoretical Approach

Based on the high-level description of the algorithms, rather thanlanguage dependent implementations

Makes possible an evaluation of the algorithms that is independent ofthe hardware and software environments→ Generality

CMPE 250 Algorithm Analysis October 12, 2016 17 / 66

Pseudocode

High-level description of analgorithm.

More structured than plainEnglish.

Less detailed than a program.

Preferred notation fordescribing algorithms.

Hides program design issues.

Example: find the maximumelement of an array

Algorithm 1 arrayMax(A, n)1: Input array A of n integers2: Output maximum element of A3: currentMax ← A[0].4: for i ← 1 to n − 1 do5: if A[i] > currentMax then

currentMax← A[i]return currentMax

CMPE 250 Algorithm Analysis October 12, 2016 18 / 66

Pseudocode

Control flowif ... then ... [else ...]while ... do ...repeat ... until ...for ... do ...Indentation replaces braces

Method declarationAlgorithm method (arg [,arg...])

Input ...Output

Method callvar.method (arg [, arg. . . ])

MethodReturn valuereturn expression

MethodExpressions

←Assignment ( equivalentto =)= Equality testing(equivalent to ==)n2 Superscripts and othermathematical formattingallowed

CMPE 250 Algorithm Analysis October 12, 2016 19 / 66

Primitive Operations

The basic computationsperformed by an algorithm

Identifiable in pseudocode

Largely independent from theprogramming language

Exact definition not important

Use comments

Instructions have to be basicenough and feasible

Examples:Evaluating an expressionAssigning a value to avariableCalling a methodReturning from a method

CMPE 250 Algorithm Analysis October 12, 2016 20 / 66

Low Level Algorithm Analysis

Based on primitive operations (low-level computations independentfrom the programming language)For example:

Make an addition = 1 operationCalling a method or returning from a method = 1 operationIndex in an array = 1 operationComparison = 1 operation, etc.

Method: Inspect the pseudo-code and count the number of primitiveoperations executed by the algorithm

CMPE 250 Algorithm Analysis October 12, 2016 21 / 66

Counting Primitive Operations

By inspecting the code, we can determine the number of primitiveoperations executed by an algorithm, as a function of the input size.

Algorithm 2 arrayMax(A, n)1: currentMax ← A[0].2: for i ← 1 to n − 1 do3: if A[i] > currentMax then

currentMax← A[i]4: increment counter i

return currentMax

#operations2

2+n2(n-1)2(n-1)2(n-1)

1Total 7n-1

CMPE 250 Algorithm Analysis October 12, 2016 22 / 66

Estimating Running Time

Algorithm arrayMax executes 7n − 1 primitive operations.

Let’s definea := Time taken by the fastest primitive operationb := Time taken by the slowest primitive operation

Let T (n) be the actual running time of arrayMax. We have

a(7n − 1) ≤ T (n) ≤ b(7n − 1)

Therefore, the running time T (n) is bounded by two linear functions

CMPE 250 Algorithm Analysis October 12, 2016 23 / 66

Growth Rate of Running Time

Changing computer hardware / softwareAffects T (n) by a constant factorDoes not alter the growth rate of T (n)

The linear growth rate of the running time T (n) is an intrinsic propertyof algorithm arrayMax

CMPE 250 Algorithm Analysis October 12, 2016 24 / 66

Constant Factors

The growth rate is not affectedby

constant factors orlower-order terms

Examples102n + 105 is a linearfunction105n2 + 108n is a quadraticfunction

CMPE 250 Algorithm Analysis October 12, 2016 25 / 66

Growth Rates

Growth rates of functions:Linear ≈ nQuadratic ≈ n2

Cubic ≈ n3

In a log-log chart, the slope ofthe line corresponds to thegrowth rate of the function

CMPE 250 Algorithm Analysis October 12, 2016 26 / 66

Asymptotic Notation

Need to abstract further

Give an "idea" of how the algorithm performs

n steps vs. n + 5 steps

n steps vs. n2 steps

CMPE 250 Algorithm Analysis October 12, 2016 27 / 66

Problem

Fibonacci numbersF[0] = 0F[1] = 1F[i] = F[i-1] + F[i-2] for i ≥ 2

Pseudo-code

Number of operations

CMPE 250 Algorithm Analysis October 12, 2016 28 / 66

Algorithm Analysis

We know:Experimental approach – problemsLow level analysis – count operations

Abstract even further

Characterize an algorithm as a function of the "problem size"e.g.

Input data = array→ problem size is N (length of array)Input data = matrix→ problem size is N × M

CMPE 250 Algorithm Analysis October 12, 2016 29 / 66

Asymptotic Notation

Goal: to simplify analysis by getting rid of unneeded information (like“rounding” 1,000,001≈1,000,000)

We want to say in a formal way 3n2 ≈ n2

The “Big-Oh” Notation:Given functions f (n) and g(n), we say that f (n) is O(g(n)) if and only ifthere are positive constants c and n0 such that f (n) ≤ cg(n) for n ≥ n0

Big O notation is a mathematical notation that describes the limitingbehavior of a function when the argument tends towards a particularvalue or infinity. It is a member of a family of notations invented by PaulBachmann, Edmund Landau, and others, collectively calledBachmann-Landau notation or asymptotic notation.

CMPE 250 Algorithm Analysis October 12, 2016 30 / 66

Graphic Illustration

f (n) = 2n + 6Conf. def:

Need to find a function g(n)and a const. c and aconstant n0 such asf (n) < cg(n) when n > n0

g(n) = n and c = 4 andn0 = 3→ f (n) is O(n)

The order of f (n) is n

CMPE 250 Algorithm Analysis October 12, 2016 31 / 66

More examples

What about f (n) = 4n2 ? Is it O(n)?Find a c and n0 such that 4n2 < cn for any n > n0

50n3 + 20n + 4 is O(n3)

Would be correct to say is O(n3 + n)?Not useful, as n3 exceeds by far n, for large values

Would be correct to say is O(n5)?OK, but g(n) should be as close as possible to f (n)

3log(n) + log(log(n)) = O(?)

Simple Rule: Drop lower order terms and constant factors

CMPE 250 Algorithm Analysis October 12, 2016 32 / 66

Big-Oh Rules

If f1(n) is O(g1(n)) and f2(n) is O(g2(n))

f1(n) + f2(n) is O(g1(n) + g2(n)) = max(O(g1(n) + g2(n))f1(n)× f2(n) is O(g1(n)× g2(n))

logk N is O(N) for any constant k

The relative growth rate of two functions can always be determined bycomputing their limit

limn→∞ f1(n)/f2(n)

But using this method is almost always an overkill

CMPE 250 Algorithm Analysis October 12, 2016 33 / 66

Big-Oh and Growth Rate

The big-Oh notation gives an upper bound on the growth rate of afunction.

The statement “f (n) is O(g(n))” means that the growth rate of f (n) isno more than the growth rate of g(n).

We can use the big-Oh notation to rank functions according to theirgrowth rate

f (n) is O(g(n)) g(n) is O(f (n))

g(n) grows faster Yes No

f (n) grows faster No Yes

Same growth Yes Yes

CMPE 250 Algorithm Analysis October 12, 2016 34 / 66

Classes of Functions

Let g(n) denote the class (set) of functions that are O(g(n))

We have n ⊂ n2 ⊂ n3 ⊂ n4 ⊂ n5 ⊂ . . .where the containment is strict

CMPE 250 Algorithm Analysis October 12, 2016 35 / 66

Big-Oh Rules

If is f (n) a polynomial of degree d , then f (n) is O(nd ), i.e.,1 Drop lower-order terms2 Drop constant factors

Use the smallest possible class of functionsSay “2n is O(n)” instead of “2n is O(n2)”

Use the simplest expression of the classSay “3n + 5 is O(n)” instead of “3n + 5 is O(3n)”

CMPE 250 Algorithm Analysis October 12, 2016 36 / 66

Inappropriate Expressions

f (n)@@≤O(g(n))f (n)@@≥O(g(n))

CMPE 250 Algorithm Analysis October 12, 2016 37 / 66

Properties of Big-Oh

If f (n) is O(g(n)) then af (n) is O(g(n)) for any a.

If f (n) is O(g(n)) and h(n) is O(g′(n)) then f (n) + h(n) isO(g(n) + g′(n))

If f (n) is O(g(n)) and h(n) is O(g′(n)) then f (n)h(n) is O(g(n)g′(n))

If f (n) is O(g(n)) and g(n) is O(h(n)) then f (n) is O(h(n))

CMPE 250 Algorithm Analysis October 12, 2016 38 / 66

Properties of Big-Oh

nx is O(an), for any fixed x > 0 and a > 1An algorithm of order n to a certain power is better than an algorithm oforder a(> 1) to the power of n

log nx is O(log n), for x > 0 – how?logx n is O(ny ) for x > 0 and y > 0

An algorithm of order log n (to a certain power) is better than analgorithm of n raised to a power y .

CMPE 250 Algorithm Analysis October 12, 2016 39 / 66

Asymptotic analysis - terminology

Special classes of algorithms:logarithmic: O(log n)linear: O(n)quadratic: O(2)polynomial: O(nk ), k ≥ 1exponential: O(an), n > 1

Polynomial vs. exponential ?

Logarithmic vs. polynomial ?

CMPE 250 Algorithm Analysis October 12, 2016 40 / 66

Some Numbers

log n n n log n n2 n3 2n

0 1 0 1 1 21 2 2 4 8 42 4 8 16 64 163 8 24 64 512 2564 16 64 256 4096 655365 32 160 1024 32768 4294967296

CMPE 250 Algorithm Analysis October 12, 2016 41 / 66

Example:Computing Prefix Averages

The i-th prefix average of anarray X is average of thefirst (i + 1) elements of XA[i] = (X[0] + X[1] +... + X[i])/(i+1)

Computing the array A ofprefix averages of anotherarray X has applications tofinancial analysis

CMPE 250 Algorithm Analysis October 12, 2016 42 / 66

Prefix Averages (Quadratic)

The following algorithm computes prefix averages in quadratic time byapplying the definition.

Algorithm 3 prefixAverages1(X, n)1: Input array X of n integers.2: Output

array A of prefix averages.3: A← new array of n integers.4: for i ← 0 to n − 1 do5: s ← X [0]6: for j ← 1 to i do7: s ← s + X [j]

8: A[i]← s/(i + 1)

return A

#operationsnnn

1+2+...+(n-1)1+2+...+(n-1)

n1

CMPE 250 Algorithm Analysis October 12, 2016 43 / 66

Arithmetic Progression

The running time ofprefixAverages1 isO(1 + 2 + · · ·+ n)

The sum of the first n integersis n(n + 1)/2

There is a simple visualproof of this fact

Thus, algorithmprefixAverages1 runs in O(n2)time

CMPE 250 Algorithm Analysis October 12, 2016 44 / 66

Prefix Averages (Linear)

The following algorithm computes prefix averages in linear time bykeeping a running sum.

Algorithm 4 prefixAverages2(X, n)1: Input array X of n integers.2: Output

array A of prefix averages.3: A← new array of n integers.4: s ← 05: for i ← 0 to n − 1 do6: s ← s + X [i]7: A[i]← s/(i + 1)

return A

#operationsn1nnnn1

Algorithm prefixAverages2 runs in O(n) time

CMPE 250 Algorithm Analysis October 12, 2016 45 / 66

How many operations?

Table: of functions with respect to input n, assume that each primitive operationtakes one microsecond (1 second = 106 microsecond).

O(g(n)) 1 Second 1 Hour 1 Month 1 Century

log2n ≈ 10300000 ≈ 10109 ≈ 100.8×1012 ≈ 101015

√n ≈ 1012 ≈ 1.3× 1019 ≈ 6.8× 1024 ≈ 9.7× 1030

n 106 3.6× 109 ≈ 2.6× 1012 ≈ 3.12× 1015

nlog2n ≈ 105 ≈ 109 ≈ 1011 ≈ 1014

n2 1000 6× 104 ≈ 1.6× 106 ≈ 5.6× 107

n3 100 ≈ 1500 ≈ 14000 ≈ 15000002n 19 31 41 51n! 9 12 15 17

CMPE 250 Algorithm Analysis October 12, 2016 46 / 66

Running Time Calculations

General RulesFOR loop

The number of iterations times the time of the inside statements.

Nested loopsThe product of the number of iterations times the time of the insidestatements.

Consecutive StatementsThe sum of running time of each segment.

If/ElseThe testing time plus the larger running time of the cases.

CMPE 250 Algorithm Analysis October 12, 2016 47 / 66

Some Examples

Case1: for (i=0; i<n; i++)for (j=0; j<n; j++)

k++;

Case 2: for (i=0; i<n; i++)k++;

for (i=0; i<n; i++)for (j=0; j<n; j++)

k++;Case 3: for (int i=0; i<n; i++)

for (int j=0; j<n; j++)k+=1;

O(n2)

O(n2)

O(n2)

CMPE 250 Algorithm Analysis October 12, 2016 48 / 66

Maximum Subsequence Sum Problem

Given a set of integers A1,A2, . . . ,AN , find the maximum value of∑jk=i Ak

For convenience, the maximum subsequence sum is zero if all the integersare negative.

CMPE 250 Algorithm Analysis October 12, 2016 49 / 66

The First Algorithm

int MaxSubSum1(const vector<int> & a) int maxSum = 0;

for (int i = 0; i < a.size(); i++)for (int j = i; j < a.size(); j++)

int thisSum = 0;

for (int k = i; k <= j; k++)thisSum += a[k];

if (thisSum > maxSum)maxSum = thisSum;

return maxSum;

O(n3)

CMPE 250 Algorithm Analysis October 12, 2016 50 / 66

The Second Algorithm

int MaxSubSum2(const vector<int> & a) int maxSum = 0;

for (int i = 0; i < a.size(); i++) thisSum = 0;for (int j = i; j < a.size(); j++)

thisSum += a[j];

if (thisSum > maxSum)maxSum = thisSum;

return maxSum;

O(n2)

CMPE 250 Algorithm Analysis October 12, 2016 51 / 66

The Third Algorithm

// Recursive maximum contiguous sum algorithmint maxSubSum3(const vector<int> & a)

return maxSumRec(a, 0, a.size() - 1);

// Used by driver function above, this one is calledrecursively

int maxSumRec(const vector<int>& a, int left, int right) if (left == right) // base case

if (a[left] > 0)return a[left];

elsereturn 0;

int center = (left + right) / 2;int maxLeftSum = maxSumRec(a, left, center);

// recursive callint maxRightSum = maxSumRec(a, center + 1, right);

// recursive call

int maxLeftBorderSum = 0, leftBorderSum = 0;

T (n′) = T (n/2)

O(n)

CMPE 250 Algorithm Analysis October 12, 2016 52 / 66

The Third Algorithm (Cont.)

for (int i = center; i >= left; --i)

leftBorderSum += a[i];if (leftBorderSum > maxLeftBorderSum)

maxLeftBorderSum = leftBorderSum;

int maxRightBorderSum = 0, rightBorderSum = 0;for (int j = center+1; j <= right; ++j)

rightBorderSum += a[j];if (rightBorderSum > maxRightBorderSum)

maxRightBorderSum = rightBorderSum;

return max3(maxLeftSum, maxRightSum,maxLeftBorderSum + maxRightBorderSum);

int max3(int x, int y, int z) int max = x;if (y > max)

max = y;if (z > max)

max = z;return max;

CMPE 250 Algorithm Analysis October 12, 2016 53 / 66

T (1) = 1T (n) = 2T (n/2) + O(n)

T (2) = ?T (4) = ?T (8) = ?. . .

If n = 2k , T (n) = n × (k + 1)

k = log nT (n) = n(log n + 1)

CMPE 250 Algorithm Analysis October 12, 2016 54 / 66

Logarithms in the Running Time

An algorithm is O(log N) if it takes constant time to cut the problemsize by a fraction (usually 1

2 ).

On the other hand, if constant time is required to merely reduce theproblem by a constant amount (such as to make the problem smallerby 1), then the algorithm is O(N)

Examples of the O(log N)

Binary SearchEuclid’s algorithm for computing the greatest common divisor

CMPE 250 Algorithm Analysis October 12, 2016 55 / 66

Analyzing recursive algorithms

function foo(param A, param B) statement_1;statement_2;if (termination condition)

return;foo(A_1, B_1);

CMPE 250 Algorithm Analysis October 12, 2016 56 / 66

Solving recursive equations by repeated substitution

T(n) = T(n/2) + c substitute for T(n/2)= T(n/4) + c + c substitute for T(n/4)= T(n/8) + c + c + c= T(n/23) + 3c in more compact form= . . .= T(n/2k ) + kc "inductive leap"

T(n) = T(n/2log n) + clogn "choose k = logn"= T(n/n) + clogn= T(1) + clogn = b + clogn = Θ(log n)

CMPE 250 Algorithm Analysis October 12, 2016 57 / 66

Solving recursive equations by telescoping

T(n) = T(n/2) + c initial equationT(n/2) = T(n/4) + c so this holdsT(n/4) = T(n/8) + c and this . . .T(n/8) = T(n/16) + c and this . . .. . .T(4) = T(2) + c eventually . . .T(2) = T(1) + c and this . . .T(n) = T(1) + clogn sum equations, canceling the terms appearing

on both sidesT(n) = Θ(log n)

CMPE 250 Algorithm Analysis October 12, 2016 58 / 66

The Fourth Algorithm

int MaxSubSum4(const vector <int> & a) int maxSum=0, thisSum=0;

for (int j=0; j<a.size(); j++) thisSum+=a[j];

if (thisSum>maxSum)maxSum=thisSum;

else if (thisSum<0)thisSum=0;

return maxSum;

O(n)

CMPE 250 Algorithm Analysis October 12, 2016 59 / 66

Problem:

Order the following functions by their asymptotic growth ratesnlognlogn3

n2

n2/5

2logn

log(logn)Sqr(logn)

CMPE 250 Algorithm Analysis October 12, 2016 60 / 66

Back to the original question

Which solution would you choose?O(n2) vs. O(n)

Some math . . .properties of logarithms:

logb(xy) = logbx + logbylogb(x/y) = logbx − logbylogbxa = alogbxlogba = logx a/logx b

properties of exponentials:a(b+c) = abac

abc = (ab)c

ab/ac = a(b−c)

b = alogab

bc = ac×logab

CMPE 250 Algorithm Analysis October 12, 2016 61 / 66

“Relatives” of Big-Oh

“Relatives” of the Big-OhΩ(f (n)): Big Omega – asymptotic lower boundΘ(f (n)): Big Theta – asymptotic tight bound

Big-Omega – think of it as the inverse of O(n)

g(n) is Ω(f (n)) if f (n) is O(g(n))

Big-Theta – combine both Big-Oh and Big-Omegaf (n) is Θ(g(n)) if f (n) is O(g(n)) and g(n) is Ω(f (n))

Consider the difference:3n + 3 is O(n) and is Θ(n)3n + 3 is O(n2) but is not Θ(n2)

CMPE 250 Algorithm Analysis October 12, 2016 62 / 66

More “relatives”

Little-oh – f (n) is o(g(n)) if f (n) is O(g(n)) and f (n) is not Θ(g(n))

2n + 3 is o(n2)

2n + 3 is o(n) ?

CMPE 250 Algorithm Analysis October 12, 2016 63 / 66

In other words...

"f (n) is O(g(n))"→ growth rate of f (n) ≤ growth rate of g(n)

"f (n) is Ω(g(n))"→ growth rate of f (n) ≥ growth rate of g(n)

"f (n) is Θ(g(n))"→ growth rate of f (n) = growth rate of g(n)

"f (n) is o(g(n))"→ growth rate of f (n) < growth rate of g(n)

CMPE 250 Algorithm Analysis October 12, 2016 64 / 66

Important Series

Sum of squares:

S(N) = 1 + 2 + · · ·+ N =∑N

i=1 i = N (1+N)2

Sum of exponents:∑Ni=1 i2 = N(N+1)(2N+1)

6 ≈ N3

3 for large NGeometric series:

Special case when A = 220 + 21 + 22 + · · ·+ 2N = 2N+1 − 1∑N

i=1 ik ≈ Nk+1

|k+1| for large N and k 6= −1

CMPE 250 Algorithm Analysis October 12, 2016 65 / 66

Problem

Running time for finding a number in a sorted array [binary search]Pseudo-codeRunning time analysis

CMPE 250 Algorithm Analysis October 12, 2016 66 / 66