4.1 compsci 102© michael frank today’s topics algorithmsalgorithms – –algorithm review –...

46
CompSci 102 © Michael Frank 4.1 Today’s topics Algorithms Algorithms Algorithm review Orders of Growth Reading: Sections 2.1-3.3 Reading: Sections 2.1-3.3 Upcoming Upcoming Integers Integers

Upload: aubrey-chandler

Post on 18-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.1

Today’s topics

• AlgorithmsAlgorithms– Algorithm review

– Orders of Growth

• Reading: Sections 2.1-3.3Reading: Sections 2.1-3.3

• UpcomingUpcoming– IntegersIntegers

Page 2: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.2

§2.1: Algorithms

• The foundation of computer programming.The foundation of computer programming.

• Most generally, an Most generally, an algorithmalgorithm just means a definite just means a definite procedure for performing some sort of task.procedure for performing some sort of task.

• A computer A computer programprogram is simply a description of an is simply a description of an algorithm, in a language precise enough for a computer to algorithm, in a language precise enough for a computer to understand, requiring only operations that the computer understand, requiring only operations that the computer already knows how to do.already knows how to do.

• We say that a program We say that a program implementsimplements (or “is an (or “is an implementation of”) its algorithm.implementation of”) its algorithm.

Page 3: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.3

Algorithm Characteristics

Some important general features of algorithms:Some important general features of algorithms:• InputInput. . Information or data that comes in.Information or data that comes in.• Output. Output. Information or data that goes out.Information or data that goes out.• Definiteness. Definiteness. Algorithm is precisely defined.Algorithm is precisely defined.• Correctness.Correctness. Outputs correctly relate to inputs.Outputs correctly relate to inputs.• Finiteness. Finiteness. Won’t take forever to describe or run.Won’t take forever to describe or run.• Effectiveness. Effectiveness. Individual steps are all do-able.Individual steps are all do-able.• Generality. Generality. Works for many possible inputs.Works for many possible inputs.• Efficiency.Efficiency. Takes little time & memory to run.Takes little time & memory to run.

Page 4: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.4

Our Pseudocode Language: §A2

procedureprocedurenamename((argumentargument: : typetype))

variablevariable :=:= expressionexpression

informal statementinformal statement

beginbegin statementsstatements endend

{{commentcomment}}

ifif conditioncondition then then statementstatement [else [else statementstatement]]

for for variablevariable :=:= initial initial valuevalue to to final valuefinal value statementstatement

whilewhile conditioncondition statementstatement

procnameprocname((argumentsarguments))

Not defined in book:Not defined in book:

returnreturn expressionexpression

Page 5: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.5

procedure procname(arg: type)

• Declares that the following text defines a Declares that the following text defines a procedure named procedure named procnameprocname that takes that takes inputs (inputs (argumentsarguments) named ) named argarg which are which are data objects of the type data objects of the type typetype..– Example:Example:

procedureprocedure maximummaximum((LL: list of integers): list of integers)[statements defining [statements defining maximummaximum…]…]

Page 6: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.6

variable := expression

• An An assignment assignment statement evaluates the statement evaluates the expression expression expressionexpression, then reassigns the , then reassigns the variable variable variablevariable to the value that results. to the value that results.– Example assignment statement:Example assignment statement:

vv :=:= 3 3xx+7 (If +7 (If xx is 2, changes is 2, changes vv to 13.) to 13.)

• In pseudocode (but not real code), the In pseudocode (but not real code), the expressionexpression might be informally stated: might be informally stated:– xx :=:= the largest integer in the list the largest integer in the list LL

Page 7: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.7

Informal statement

• Sometimes we may write a statement as an Sometimes we may write a statement as an informal English imperative, if the meaning informal English imperative, if the meaning is still clear and precise: is still clear and precise: e.g.e.g.,“swap ,“swap xx and and yy””

• Keep in mind that real programming Keep in mind that real programming languages never allow this.languages never allow this.

• When we ask for an algorithm to do so-and-When we ask for an algorithm to do so-and-so, writing “Do so-and-so” isn’t enough!so, writing “Do so-and-so” isn’t enough!– Break down algorithm into detailed steps.Break down algorithm into detailed steps.

Page 8: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.8

begin statements end

• Groups a sequence of Groups a sequence of statements together:statements together:beginbegin statement 1statement 1 statement 2statement 2 … … statement nstatement n endend

• Allows the sequence to Allows the sequence to be used just like a be used just like a single statement.single statement.

• Might be used:Might be used:– After a After a procedureprocedure

declaration.declaration.– In an In an ifif statement after statement after

thenthen or or elseelse..– In the body of a In the body of a forfor or or

whilewhile loop. loop.Curly braces {} are used insteadin many languages.

Page 9: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.9

{comment}

• Not executed (does nothing).Not executed (does nothing).

• Natural-language text explaining some Natural-language text explaining some aspect of the procedure to human readers.aspect of the procedure to human readers.

• Also called a Also called a remarkremark in some real in some real programming languages, programming languages, e.g.e.g. BASIC. BASIC.

• Example, might appear in a Example, might appear in a maxmax program: program:– {Note that {Note that vv is the largest integer seen so far.} is the largest integer seen so far.}

Page 10: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.10

if condition then statement

• Evaluate the propositional expression Evaluate the propositional expression conditioncondition..– If the resulting truth value is If the resulting truth value is TrueTrue, then execute , then execute

the statement the statement statementstatement; ; – otherwise, just skip on ahead to the next otherwise, just skip on ahead to the next

statement after the statement after the ifif statement. statement.

• Variant: Variant: ifif condcond thenthen stmt1stmt1 elseelse stmt2stmt2– Like before, but iff truth value is Like before, but iff truth value is FalseFalse, ,

executes executes stmt2stmt2..

Page 11: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.11

while condition statement

• EvaluateEvaluate the propositional (Boolean) the propositional (Boolean) expression expression conditioncondition..

• If the resulting value is If the resulting value is TrueTrue, then execute , then execute statementstatement..

• Continue repeating the above two actions Continue repeating the above two actions over and over until finally the over and over until finally the conditioncondition evaluates to evaluates to FalseFalse; then proceed to the next ; then proceed to the next statement.statement.

Page 12: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.12

while condition statement

• Also equivalent to infinite nested Also equivalent to infinite nested ififs, like so: s, like so: if if conditioncondition beginbegin statement statement if if conditioncondition beginbegin statement statement …(continue infinite nested if’s) …(continue infinite nested if’s) endend endend

Page 13: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.13

for var := initial to final stmt

• InitialInitial is an integer expression. is an integer expression.

• FinalFinal is another integer expression. is another integer expression.

• Semantics:Semantics: Repeatedly execute Repeatedly execute stmtstmt, first with variable , first with variable var var :=:= initialinitial, then with , then with var var :=:= initialinitial+1+1, then with , then with var var :=:= initialinitial+2+2, , etcetc., then finally with ., then finally with var var :=:= finalfinal..

• Question:Question: What happens if What happens if stmtstmt changes the value of var, changes the value of var, or the value that or the value that initialinitial or or finalfinal evaluates to? evaluates to?

Page 14: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.14

for var := initial to final stmt

• ForFor can be exactly defined in terms of can be exactly defined in terms of while,while, like so: like so:

begin var := initial while var final begin stmt var := var + 1 endend

Page 15: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.15

procedure(argument)

• A A procedure callprocedure call statement invokes the statement invokes the named named procedureprocedure, giving it as its input the , giving it as its input the value of the value of the argumentargument expression. expression.

• Various real programming languages refer Various real programming languages refer to procedures as to procedures as functionsfunctions (since the (since the procedure call notation works similarly to procedure call notation works similarly to function application function application ff((xx)), or as )), or as subroutinessubroutines, , subprogramssubprograms, or , or methodsmethods..

Page 16: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.16

Max procedure in pseudocode

procedureprocedure maxmax((aa11, , aa22, …, , …, aann: integers): integers)

vv :=:= aa11 {largest element so far}{largest element so far}

forfor ii :=:= 2 2 toto n n {go thru rest of elems}{go thru rest of elems}

ifif aaii > > vv then then vv :=:= aai i {found bigger?}{found bigger?}

{at this point {at this point vv’s value is the same as’s value is the same as the largest integer in the list} the largest integer in the list}

returnreturn vv

Page 17: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.17

Inventing an Algorithm

• Requires a lot of creativity and intuitionRequires a lot of creativity and intuition– Like writing proofs.Like writing proofs.

• Unfortunately, we can’t give you an Unfortunately, we can’t give you an algorithm for inventing algorithms.algorithm for inventing algorithms.– Just look at lots of examples…Just look at lots of examples…– And practice (preferably, on a computer)And practice (preferably, on a computer)– And look at more examples…And look at more examples…– And practice some more… etc., etc.And practice some more… etc., etc.

Page 18: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.18

Algorithm-Inventing Example

• Suppose we ask you to write an algorithm Suppose we ask you to write an algorithm to compute the predicate:to compute the predicate:

IsPrimeIsPrime::NN→→{T,F}{T,F}– Computes whether a given natural number is a Computes whether a given natural number is a

prime number.prime number.

• First, start with a correct predicate-logic First, start with a correct predicate-logic definition of the desired function:definition of the desired function:nn: : IsPrimeIsPrime((nn) ) ¬¬1<1<dd<<nn: : dd||nn

Means d divides nevenly (without remainder)

Page 19: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.19

IsPrime example, cont.

• Notice that the negated exponential can be rewritten as a Notice that the negated exponential can be rewritten as a universal:universal:¬¬1<1<dd<<nn: : dd||n n 1<1<dd<<nn: : dd | | nn

22≤ ≤ d d ≤ ≤ nn−1−1: : dd | | nn

• This universal can then be translated directly into a This universal can then be translated directly into a corresponding corresponding forfor loop: loop:forfor dd := 2 to := 2 to nn−1 { Try all potential divisors >1 & <−1 { Try all potential divisors >1 & <nn } } ifif d|nd|n thenthen returnreturn F F { { nn has divisor has divisor dd; not prime }; not prime }return return TT { no divisors were found; { no divisors were found; nn must be prime} must be prime}

Means d does not divide n evenly (the remainder is ≠0)

Page 20: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.20

Optimizing IsPrime

• The The IsPrimeIsPrime algorithm can be further optimized: algorithm can be further optimized:forfor dd := 2 to := 2 to nn1/21/2 ifif dd||n n then return Fthen return Freturn Treturn T

• This works because of this This works because of this theorem: theorem: If If nn has any has any (integer) divisors, it must have one less than (integer) divisors, it must have one less than nn1/21/2..Proof:Proof: Suppose Suppose nn’s smallest divisor >1 is ’s smallest divisor >1 is aa, and let , and let b b ::≡≡ nn//aa. Then . Then nn = = abab, but if , but if aa > > nn1/21/2 then then bb > > nn1/21/2 (since (since aa is is nn’s smallest divisor) and so ’s smallest divisor) and so nn = = abab > ( > (nn1/21/2))22 = = nn, an , an absurdity.absurdity. Further optimizations are possible:

- E.g., only try divisors that are primes less than n1/2.

Note smaller range of search.

Page 21: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.21

Another example task

• Problem of Problem of searching an ordered listsearching an ordered list..– Given a list Given a list L L of of nn elements that are sorted into elements that are sorted into

a definite order (a definite order (e.g.e.g., numeric, alphabetical),, numeric, alphabetical),– And given a particular element And given a particular element xx,,– Determine whether Determine whether xx appears in the list, appears in the list,– and if so, return its index (position) in the list.and if so, return its index (position) in the list.

• Problem occurs often in many contexts.Problem occurs often in many contexts.

• Let’s find an Let’s find an efficientefficient algorithm! algorithm!

Page 22: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.22

Search alg. #1: Linear Search

procedureprocedure linear searchlinear search((xx: integer, : integer, aa11, , aa22, …, , …, aann: distinct integers): distinct integers)

ii :=:= 1 1 {start at beginning of list}{start at beginning of list}whilewhile ( (ii nn xx aaii) ) {not done, not found}{not done, not found}

ii :=:= ii + 1 + 1 {go to the next position}{go to the next position}ifif ii n n then then locationlocation :=:= ii {it was found}{it was found}elseelse locationlocation :=:= 0 0 {it wasn’t found}{it wasn’t found}return return location location {index or 0 if not found}{index or 0 if not found}

Page 23: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.23

Search alg. #2: Binary Search

• Basic idea: On each step, look at the Basic idea: On each step, look at the middlemiddle element of the remaining list to eliminate element of the remaining list to eliminate half of it, and quickly zero in on the desired half of it, and quickly zero in on the desired element.element.

<x >x<x <x

Page 24: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.24

Search alg. #2: Binary Search

procedureprocedure binary searchbinary search((xx:integer:integer, a, a11, , aa22, …, , …, aann: distinct integers): distinct integers)

ii :=:= 1 1 {left endpoint of search interval}{left endpoint of search interval}jj :=:= nn {right endpoint of search interval}{right endpoint of search interval}whilewhile i i<<j j beginbegin {while interval has >1 item}{while interval has >1 item}

mm :=:= ((ii++jj)/2)/2 {midpoint}{midpoint}ifif xx>>aamm thenthen i i :=:= m m+1 +1 else else j j :=:= m m

endendifif xx = = aaii thenthen locationlocation :=:= ii elseelse locationlocation :=:= 0 0

returnreturn locationlocation

Page 25: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.25

Practice exercises

• 2.1.3: Devise an algorithm that finds the 2.1.3: Devise an algorithm that finds the sum of all the integers in a list. [2 min]sum of all the integers in a list. [2 min]

Page 26: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.26

Orders of Growth - Motivation

• Suppose you are designing a web site to Suppose you are designing a web site to process user data (process user data (e.g.e.g., financial records)., financial records).

• Suppose database program A takes Suppose database program A takes ffAA((nn)=30)=30n+n+8 microseconds to process any 8 microseconds to process any nn records, while program B takes records, while program B takes ffBB((nn)=)=nn22+1 +1 microseconds to process the microseconds to process the nn records. records.

• Which program do you choose, knowing Which program do you choose, knowing you’ll want to support millions of users?you’ll want to support millions of users?

Page 27: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.27

Visualizing Orders of Growth

• On a graph, asOn a graph, asyou go to theyou go to theright, the faster-right, the faster-growing func-growing func-tion always tion always eventuallyeventuallybecomes thebecomes thelarger one... larger one...

fA(n)=30n+8

Increasing n

fB(n)=n2+1

Val

ue o

f fu

ncti

on

Page 28: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.28

Concept of order of growth

• We say We say ffAA((nn)=30)=30n+n+88 is is (at most)(at most) order norder n, or , or O(O(nn))..– It is, at most, roughly It is, at most, roughly proportionalproportional to to nn..

• ffBB((nn)=)=nn22+1+1 is is order norder n22, or , or O(O(nn22)). . – It is (at most) roughly proportional to It is (at most) roughly proportional to nn22..

• Any function whose Any function whose exactexact (tightest) (tightest) order is O(order is O(nn22) is ) is faster-growing than any O(faster-growing than any O(nn) function.) function.– Later we will introduce Later we will introduce ΘΘ f for expressing or expressing exact exact orderorder..

• For large numbers of user records, the exactly order For large numbers of user records, the exactly order nn22 function will always take more time.function will always take more time.

Page 29: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.29

Definition: O(g), at most order g

Let Let gg be any function be any function RRRR..

• Define “Define “at most order gat most order g”, written O(”, written O(gg), to ), to be: be: {{ff::RRR | R | cc,,kk: : xx>>kk:: f f((xx) ) cgcg((xx)})}..– ““Beyond some point Beyond some point kk, function , function ff is at most a is at most a

constant constant cc times times gg ( (i.e., i.e., proportional to proportional to gg).”).”

• ““ff is is at most order gat most order g”, or ”, or “f“f is O( is O(gg))”,”, or or ““ff=O(=O(gg)” all just mean that )” all just mean that ffO(O(gg).).

• Often the phrase “at most” is omitted.Often the phrase “at most” is omitted.

Page 30: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.30

Points about the definition

• Note that Note that ff is O( is O(gg) so long as ) so long as anyany values of values of cc and and kk exist that satisfy the definition. exist that satisfy the definition.

• But: The particular But: The particular cc, , kk, values that make , values that make the statement true are the statement true are notnot unique: unique: Any Any larger value of larger value of cc and/or and/or k k will also work.will also work.

• You are You are notnot required to find the smallest required to find the smallest cc and and kk values that work. (Indeed, in some values that work. (Indeed, in some cases, there may be no smallest values!)cases, there may be no smallest values!)

However, you should prove that the values you choose do work.

Page 31: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.31

“Big-O” Proof Examples

• Show that 30Show that 30nn+8 is O(+8 is O(nn).).– Show Show cc,,kk: : nn>>kk:: 3030nn+8 +8 cncn..

• Let Let c=c=31, 31, kk=8. Assume =8. Assume nn>>kk=8. Then=8. Thencncn = 31 = 31nn = 30 = 30nn + + nn > 30 > 30nn+8, so 30+8, so 30nn+8 < +8 < cncn..

• Show that Show that nn22+1 is O(+1 is O(nn22).).– Show Show cc,,kk: : nn>>kk: : nn22+1 +1 cncn22..

• Let Let cc=2, =2, kk=1. Assume =1. Assume nn>1. Then >1. Then cncn22 = 2 = 2nn22 = = nn22++nn22 > > nn22+1, or +1, or nn22+1< +1< cncn22..

Page 32: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.32

• Note 30Note 30nn+8 isn’t+8 isn’tless than less than nnanywhere anywhere ((nn>0).>0).

• It isn’t evenIt isn’t evenless than 31less than 31nneverywhereeverywhere..

• But it But it isis less than less than3131nn everywhere toeverywhere tothe right of the right of nn=8=8. . n>k=8

Big-O example, graphically

Increasing n

Val

ue o

f fu

ncti

on

n

30n+8cn =31n

30n+8O(n)

Page 33: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.33

Useful Facts about Big O

• Big O, as a relation, is transitive: Big O, as a relation, is transitive: ffO(O(gg) ) ggO(O(hh) ) ffO(O(hh))

• O with constant multiples, roots, and logs...O with constant multiples, roots, and logs... f f (in (in (1)) & constants (1)) & constants aa,,bbRR, with , with bb0,0, afaf, , f f 1-1-bb, and (log, and (logbb ff))aa are all O(are all O(ff).).

• Sums of functions:Sums of functions:If If ggO(O(ff) and ) and hhO(O(ff), then ), then gg++hhO(O(ff).).

Page 34: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.34

More Big-O facts

cc>0, O(>0, O(cfcf))==O(O(f+cf+c)=O()=O(ffc)c)==O(O(ff))

• ff11O(O(gg11) ) ff22O(O(gg22) ) – ff1 1 ff2 2 O(O(gg11gg22))

– ff11++ff2 2 O(O(gg11++gg22) )

= O(max( = O(max(gg11,,gg22))))

= O( = O(gg11) if ) if gg22O(O(gg11) (Very ) (Very

useful!)useful!)

Page 35: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.35

Orders of Growth (§1.8) - So Far

• For any For any gg::RRRR, “, “at most order gat most order g”,”,O(O(gg) ) { {ff::RRR | R | cc,,kk xx>>k |fk |f((xx)| )| | |cgcg((xx)|}.)|}.– Often, one deals only with positive functions and Often, one deals only with positive functions and

can ignore absolute value symbols.can ignore absolute value symbols.

• ““ffO(O(gg)” often written “)” often written “ff is O( is O(gg)”)”or “or “ff=O(=O(gg)”.)”.– The latter form is an instance of a more general The latter form is an instance of a more general

convention...convention...

Page 36: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.36

Order-of-Growth Expressions

• ““O(O(ff)” when used as a term in an arithmetic )” when used as a term in an arithmetic expression means: “some function expression means: “some function ff such such that that ffO(O(ff)”.)”.

• E.g.E.g.: “: “xx22+O(+O(xx)” means “)” means “xx22 plus some plus some function that is O(function that is O(xx)”.)”.

• Formally, you can think of any such Formally, you can think of any such expression as denoting a expression as denoting a set set of functions:of functions: xx22+O(+O(xx) :) : { {gg | | ffO(O(xx): ): gg((xx)= )= xx22++ff((xx)})}

Page 37: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.37

Order of Growth Equations

• Suppose Suppose EE11 and and EE22 are order-of-growth are order-of-growth

expressions corresponding to the sets of expressions corresponding to the sets of functions functions SS and and TT, respectively. , respectively.

• Then the “equation” Then the “equation” EE11==EE22 really means really means

ffSS, , ggT T : : ff==ggor simply or simply SST.T.

• Example: Example: xx22 + O( + O(xx) = O() = O(xx22) means) means ffO(O(xx): ): ggO(O(xx22): ): xx22++ff((xx)=)=gg((xx))

Page 38: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.38

Useful Facts about Big O

f,gf,g & constants & constants aa,,bbRR, with , with bb0,0,– af af = O(= O(ff); (); (ee..g. g. 33xx2 2 = O(= O(xx22))))– f+f+O(O(ff)) = = O(O(ff); (); (ee..g. xg. x22+x = +x = O(O(xx22))))

• Also, if Also, if ff==(1) ((1) (at leastat least order 1), then: order 1), then:– |f||f|1-1-b b = O(= O(ff); (); (e.g.e.g. xx1 1 = O(= O(xx))))

– (log(logbb | |f|f|))aa = O(= O(ff). (). (e.g.e.g. log log xx = O( = O(xx))))

– gg=O(=O(fgfg)) ( (ee..g.g. x = x = O(O(xx log log xx))))– fgfg O( O(gg)) ( (e.g.e.g. xx log log xx O( O(xx))))– a=a=O(O(ff) () (e.g.e.g. 3 = O( 3 = O(xx))))

Page 39: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.39

Definition: (g), exactly order g

• If If ffO(O(gg) and ) and ggO(O(ff), then we say “), then we say “g and f g and f are of the same orderare of the same order” or “” or “f is (exactly) order f is (exactly) order gg” and write ” and write ff((gg).).

• Another, equivalent definition:Another, equivalent definition:((gg) ) { {ff::RRR R | | cc11cc22kk>0 >0 xx>>kk: |: |cc11gg((xx)|)|||ff((xx)|)|||cc22gg((xx)| })| }

– ““Everywhere beyond some point Everywhere beyond some point kk, , ff((xx)) lies in lies in between two multiples of between two multiples of gg((xx).”).”

Page 40: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.40

Rules for

• Mostly like rules for O( ), except:Mostly like rules for O( ), except: f,gf,g>0 & constants >0 & constants aa,,bbRR, with , with bb>0,>0,

afaf ((ff)),, but but Same as with O. Same as with O. f f ((fgfg)) unless unless g=g=(1) (1) Unlike O. Unlike O.||f| f| 1-1-bb ((ff)), and , and Unlike with O. Unlike with O. (log(logbb | |f|f|))cc ((ff)). . Unlike with O. Unlike with O.

• The functions in the latter two cases we say The functions in the latter two cases we say are are strictly of lower orderstrictly of lower order than than ((ff).).

Page 41: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.41

example

• Determine whether:Determine whether:

• Quick solution:Quick solution:)( 2

?

1

nin

i

∈⎟⎠

⎞⎜⎝

⎛∑=

Page 42: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.42

Other Order-of-Growth Relations

((gg) = {) = {ff | | ggO(O(ff)})}“The functions that are “The functions that are at least orderat least order gg.”.”

• o(o(gg) = {) = {ff | | cc>0 >0 kk x>kx>k : | : |ff((xx)| < |)| < |cgcg((xx)|})|}“The functions that are “The functions that are strictly lower order strictly lower order than gthan g.”.” o(o(gg) ) O( O(gg) ) ((gg))..

((gg) = {) = {ff | | cc>0 >0 kk x>kx>k : | : |cgcg((xx)| < |)| < |ff((xx)|})|}“The functions that are “The functions that are strictly higher order strictly higher order than gthan g.”.” ((gg) ) ((gg) ) ((gg))..

Page 43: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.43

• Subset relations between order-of-growth Subset relations between order-of-growth sets.sets.

Relations Between the Relations

RR( f )O( f )

( f ) ( f )o( f )• f

Page 44: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.44

Why o(f)O(x)(x)

• A function that is O(A function that is O(xx), but neither o(), but neither o(xx) nor ) nor ((xx):):

Page 45: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.45

Strict Ordering of Functions

• Temporarily let’s write Temporarily let’s write ffgg to mean to mean ffo(o(gg)),, ff~~gg to mean to mean ff((gg))

• Note that: Note that:

• Let Let kk>1. Then the following are true:>1. Then the following are true:1 1 log log log log nn log log nn ~ log ~ logkk nn log logkk nn

nn1/1/kk n n n n log log nn nnkk kknn n n! ! nnnn … …

.0)(

)(lim =⇔

∞→ xgxf

gfx

p

Page 46: 4.1 CompSci 102© Michael Frank Today’s topics AlgorithmsAlgorithms – –Algorithm review – –Orders of Growth Reading: Sections 2.1-3.3Reading: Sections

CompSci 102 © Michael Frank 4.46

Review: Orders of Growth (§1.8)

Definitions of order-of-growth sets, Definitions of order-of-growth sets, gg::RRRR

• O(O(gg) :) : { {ff || cc>0 >0 kk xx>>k |fk |f((xx)| < |)| < |cgcg((xx)|})|}

• o(o(gg) :) : { {ff | | cc>0 >0 kk x>kx>k | |ff((xx)| < |)| < |cgcg((xx)|})|}((gg) :) : { {ff | | ggO(O(ff)})}((gg) :) : { {ff | | ggo(o(ff)})}((gg) :) : O( O(gg) ) ((gg))