Analysis of Algorithms

323
Analysis of Algorithms Algorithm Input Output © 2015 Goodrich and Tamassia 1 Analysis of Algorithms Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015

Transcript of Analysis of Algorithms

Page 1: Analysis of Algorithms

Analysis of Algorithms

AlgorithmInput Output

© 2015 Goodrich and Tamassia 1Analysis of Algorithms

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015

Page 2: Analysis of Algorithms

Scalabilityq Scientists often have to deal

with differences in scale, from the microscopically small to the astronomically large.

q Computer scientists must also deal with scale, but they deal with it primarily in terms of data volume rather than physical object size.

q Scalability refers to the ability of a system to gracefully accommodate growing sizes of inputs or amounts of workload.

© 2015 Goodrich and Tamassia Analysis of Algorithms 2

Page 3: Analysis of Algorithms

Application: Job Interviewsq High technology companies tend to ask questions about

algorithms and data structures during job interviews.q Algorithms questions can be short but often require critical

thinking, creative insights, and subject knowledge.n All the “Applications” exercises in Chapter 1 of the Goodrich-

Tamassia textbook are taken from reports of actual job interview questions.

© 2015 Goodrich and Tamassia Analysis of Algorithms 3

xkcd “Labyrinth Puzzle.” http://xkcd.com/246/Used with permission under Creative Commons 2.5 License.

Page 4: Analysis of Algorithms

Algorithms and Data Structuresq An algorithm is a step-by-step procedure for

performing some task in a finite amount of time.n Typically, an algorithm takes input data and

produces an output based upon it.

q A data structure is a systematic way of organizing and accessing data.

© 2015 Goodrich and Tamassia Analysis of Algorithms 4

AlgorithmInput Output

Page 5: Analysis of Algorithms

Analysis of Algorithms 5

Running Timeq Most algorithms transform

input objects into output objects.

q The running time of an algorithm typically grows with the input size.

q Average case time is often difficult to determine.

q We focus primarily on the worst case running time.n Easier to analyzen Crucial to applications such as

games, finance and robotics

0

20

40

60

80

100

120

Run

ning

Tim

e1000 2000 3000 4000

Input Size

best caseaverage caseworst case

© 2015 Goodrich and Tamassia

Page 6: Analysis of Algorithms

Analysis of Algorithms 6

Experimental Studies

q Write a program implementing the algorithm

q Run the program with inputs of varying size and composition, noting the time needed:

q Plot the results

© 2015 Goodrich and Tamassia

0

1000

2000

3000

4000

5000

6000

7000

8000

9000

0 50 100Input Size

Tim

e (m

s)

Page 7: Analysis of Algorithms

Analysis of Algorithms 7

Limitations of Experiments

q It is necessary to implement the algorithm, which may be difficult

q Results may not be indicative of the running time on other inputs not included in the experiment.

q In order to compare two algorithms, the same hardware and software environments must be used

© 2015 Goodrich and Tamassia

Page 8: Analysis of Algorithms

Analysis of Algorithms 8

Theoretical Analysisq Uses a high-level description of the

algorithm instead of an implementationq Characterizes running time as a

function of the input size, nq Takes into account all possible inputsq Allows us to evaluate the speed of an

algorithm independent of the hardware/software environment

© 2015 Goodrich and Tamassia

Page 9: Analysis of Algorithms

Analysis of Algorithms 9

Pseudocodeq High-level description of an algorithmq More structured than English proseq Less detailed than a programq Preferred notation for describing

algorithmsq Hides program design issues

© 2015 Goodrich and Tamassia

Page 10: Analysis of Algorithms

Analysis of Algorithms 10

Pseudocode Detailsq Control flow

n if … then … [else …]n while … do …n repeat … until …n for … do …n Indentation replaces braces

q Method declarationAlgorithm method (arg [, arg…])

Input …Output …

q Method callmethod (arg [, arg…])

q Return valuereturn expression

q Expressions:¬Assignment

= Equality testing

n2 Superscripts and other mathematical formatting allowed

© 2015 Goodrich and Tamassia

Page 11: Analysis of Algorithms

Analysis of Algorithms 11

The Random Access Machine (RAM) ModelA RAM consists ofq A CPUq An potentially unbounded bank

of memory cells, each of which can hold an arbitrary number or character

q Memory cells are numbered and accessing any cell in memory takes unit time

012

© 2015 Goodrich and Tamassia

Memory

CPU

Page 12: Analysis of Algorithms

Analysis of Algorithms 12

Seven Important Functionsq Seven functions that

often appear in algorithm analysis:n Constant » 1n Logarithmic » log nn Linear » nn N-Log-N » n log nn Quadratic » n2

n Cubic » n3

n Exponential » 2n

q In a log-log chart, the slope of the line corresponds to the growth rate

1E+01E+21E+41E+61E+81E+101E+121E+141E+161E+181E+201E+221E+241E+261E+281E+30

1E+0 1E+2 1E+4 1E+6 1E+8 1E+10n

T(n)

Cubic

Quadratic

Linear

© 2015 Goodrich and Tamassia

Page 13: Analysis of Algorithms

Functions Graphed Using “Normal” Scale

© 2015 Goodrich and Tamassia 13Analysis of Algorithms

g(n) = 2ng(n) = 1

g(n) = lg n

g(n) = n lg n

g(n) = n

g(n) = n2

g(n) = n3

Slide by Matt Stallmann included with permission.

Page 14: Analysis of Algorithms

Analysis of Algorithms 14

Primitive Operationsq Basic computations

performed by an algorithmq Identifiable in pseudocodeq Largely independent from the

programming languageq Exact definition not important

(we will see why later)q Assumed to take a constant

amount of time in the RAM model

q Examples:n Evaluating an

expressionn Assigning a value

to a variablen Indexing into an

arrayn Calling a methodn Returning from a

method

© 2015 Goodrich and Tamassia

Page 15: Analysis of Algorithms

Analysis of Algorithms 15

Counting Primitive Operationsq Example: By inspecting the pseudocode, we can

determine the maximum number of primitive operations executed by an algorithm, as a function of the input size

© 2015 Goodrich and Tamassia

Page 16: Analysis of Algorithms

Analysis of Algorithms 16

Estimating Running Timeq Algorithm arrayMax executes 7n - 2 primitive

operations in the worst case, 5n in the best case. Define:a = Time taken by the fastest primitive operationb = Time taken by the slowest primitive operation

q Let T(n) be worst-case time of arrayMax. Thena(5n) £ T(n) £ b(7n - 2)

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

© 2015 Goodrich and Tamassia

Page 17: Analysis of Algorithms

Analysis of Algorithms 17

Growth Rate of Running Time

q Changing the hardware/ software environment n Affects T(n) by a constant factor, butn Does not alter the growth rate of T(n)

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

© 2015 Goodrich and Tamassia

Page 18: Analysis of Algorithms

Why Growth Rate Matters

© 2015 Goodrich and Tamassia 18Analysis of Algorithms

Slide by Matt Stallmann included with permission.

if runtime is... time for n + 1 time for 2 n time for 4 n

c lg n c lg (n + 1) c (lg n + 1) c(lg n + 2)

c n c (n + 1) 2c n 4c n

c n lg n~ c n lg n

+ c n2c n lg n +

2cn4c n lg n +

4cn

c n2 ~ c n2 + 2c n 4c n2 16c n2

c n3 ~ c n3 + 3c n2 8c n3 64c n3

c 2n c 2 n+1 c 2 2n c 2 4n

runtimequadrupleswhen problemsize doubles

Page 19: Analysis of Algorithms

Analyzing Recursive Algorithmsq Use a function, T(n), to derive a recurrence

relation that characterizes the running time of the algorithm in terms of smaller values of n.

© 2015 Goodrich and Tamassia Analysis of Algorithms 19

Page 20: Analysis of Algorithms

Analysis of Algorithms 20

Constant Factors

q The growth rate is minimally affected byn constant factors or n lower-order terms

q Examplesn 102n + 105 is a linear

functionn 105n2 + 108n is a

quadratic function 1E+01E+21E+41E+61E+81E+101E+121E+141E+161E+181E+201E+221E+241E+26

1E+0 1E+2 1E+4 1E+6 1E+8 1E+10n

T(n)

QuadraticQuadraticLinearLinear

© 2015 Goodrich and Tamassia

Page 21: Analysis of Algorithms

Analysis of Algorithms 21

Big-Oh Notationq Given functions f(n) and

g(n), we say that f(n) is O(g(n)) if there are positive constantsc and n0 such thatf(n) £ cg(n) for n ³ n0

q Example: 2n + 10 is O(n)n 2n + 10 £ cnn (c - 2) n ³ 10n n ³ 10/(c - 2)n Pick c = 3 and n0 = 10

1

10

100

1,000

10,000

1 10 100 1,000n

3n

2n+10

n

© 2015 Goodrich and Tamassia

Page 22: Analysis of Algorithms

Analysis of Algorithms 22

Big-Oh Exampleq Example: the function

n2 is not O(n)n n2 £ cnn n £ cn The above inequality

cannot be satisfied since c must be a constant

1

10

100

1,000

10,000

100,000

1,000,000

1 10 100 1,000n

n^2100n10nn

© 2015 Goodrich and Tamassia

Page 23: Analysis of Algorithms

Analysis of Algorithms 23

More Big-Oh Examplesq 7n - 2

7n-2 is O(n)need c > 0 and n0 ³ 1 such that 7 n - 2 £ c n for n ³ n0this is true for c = 7 and n0 = 1

q 3 n3 + 20 n2 + 53 n3 + 20 n2 + 5 is O(n3)need c > 0 and n0 ³ 1 such that 3 n3 + 20 n2 + 5 £ c n3 for n ³ n0this is true for c = 4 and n0 = 21

q 3 log n + 53 log n + 5 is O(log n)need c > 0 and n0 ³ 1 such that 3 log n + 5 £ c log n for n ³ n0this is true for c = 8 and n0 = 2

© 2015 Goodrich and Tamassia

Page 24: Analysis of Algorithms

Analysis of Algorithms 24

Big-Oh and Growth Rateq The big-Oh notation gives an upper bound on the

growth rate of a functionq The statement “f(n) is O(g(n))” means that the growth

rate of f(n) is no more than the growth rate of g(n)q We can use the big-Oh notation to rank functions

according to their growth rate

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

g(n) grows more Yes Nof(n) grows more No YesSame growth Yes Yes

© 2015 Goodrich and Tamassia

Page 25: Analysis of Algorithms

Analysis of Algorithms 25

Big-Oh Rules

q 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

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

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

© 2015 Goodrich and Tamassia

Page 26: Analysis of Algorithms

Analysis of Algorithms 26

Asymptotic Algorithm Analysisq The asymptotic analysis of an algorithm determines

the running time in big-Oh notationq To perform the asymptotic analysis

n We find the worst-case number of primitive operations executed as a function of the input size

n We express this function with big-Oh notationq Example:

n We say that algorithm arrayMax “runs in O(n) time”q Since constant factors and lower-order terms are

eventually dropped anyhow, we can disregard them when counting primitive operations

© 2015 Goodrich and Tamassia

Page 27: Analysis of Algorithms

Analysis of Algorithms 27

A Case Study in Algorithm Analysisq Given an array of n integers,

find the subarray, A[j:k] that maximizes the sum

q In addition to being an interview question for testing the thinking skills of job candidates, this maximum subarray problem also has applications in pattern analysis in digitized images.

© 2015 Goodrich and Tamassia

Page 28: Analysis of Algorithms

Analysis of Algorithms 28

A First (Slow) Solution

Compute the maximum of every possible subarraysummation of the array Aseparately.

© 2015 Goodrich and Tamassia

• The outer loop, for index j, will iterate n times, its inner loop, for index k, will iterate at most n times, and the inner-most loop, for index i, will iterate at most n times.

• Thus, the running time of the MaxsubSlow algorithm is O(n3).

Page 29: Analysis of Algorithms

An Improved Algorithmq A more efficient way to calculate these

summations is to consider prefix sums

q If we are given all such prefix sums (and assuming S0=0), we can compute any summation sj,k in constant time as

© 2015 Goodrich and Tamassia Analysis of Algorithms 29

Page 30: Analysis of Algorithms

An Improved Algorithm, cont.q Compute all the prefix sumsq Then compute all the subarray sums

© 2015 Goodrich and Tamassia Analysis of Algorithms 30

Page 31: Analysis of Algorithms

Analysis of Algorithms 31

Arithmetic Progressionq The running time of

MaxsubFaster isO(1 + 2 + …+ n)

q The sum of the first nintegers is n(n + 1) / 2n There is a simple visual

proof of this factq Thus, algorithm

MaxsubFaster runs in O(n2) time 0

1

2345

67

1 2 3 4 5 6

© 2015 Goodrich and Tamassia

Page 32: Analysis of Algorithms

A Linear-Time Algorithmq Instead of computing prefix sum St = s1,t, let us

compute a maximum suffix sum, Mt, which is the maximum of 0 and the maximum sj,t for j = 1,…, t.

q if Mt > 0, then it is the summation value for a maximum subarray that ends at t, and if Mt = 0, then we can safely ignore any subarray that ends at t.

q if we know all the Mt values, for t = 1, 2, …, n, then the solution to the maximum subarray problem would simply be the maximum of all these values.

© 2015 Goodrich and Tamassia Analysis of Algorithms 32

Page 33: Analysis of Algorithms

A Linear-Time Algorithm, cont.q for t ≥ 2, if we have a maximum subarray that ends at t, and it has

a positive sum, then it is either A[t : t] or it is made up of the maximum subarray that ends at t − 1 plus A[t]. So we can define M0 = 0 and

q If this were not the case, then we could make a subarray of even larger sum by swapping out the one we chose to end at t − 1 with the maximum one that ends at t − 1, which would contradict the fact that we have the maximum subarray that ends at t.

q Also, if taking the value of maximum subarray that ends at t − 1 and adding A[t] makes this sum no longer be positive, then Mt = 0, for there is no subarray that ends at t with a positive summation.

© 2015 Goodrich and Tamassia Analysis of Algorithms 33

Page 34: Analysis of Algorithms

A Linear-Time Algorithm, cont.

q The MaxsubFastest algorithm consists of two loops, which each iterate exactly n times and take O(1) time in each iteration. Thus, the total running time of the MaxsubFastest algorithm is O(n).

© 2015 Goodrich and Tamassia Analysis of Algorithms 34

Page 35: Analysis of Algorithms

Math you need to Reviewq Properties of powers:

a(b+c) = aba cabc = (ab)c

ab /ac = a(b-c)

b = a logab

bc = a c*logab

q Properties of logarithms:logb(xy) = logbx + logbylogb (x/y) = logbx - logbylogbxa = alogbxlogba = logxa/logxb

q Summationsq Powersq Logarithmsq Proof techniquesq Basic probability

© 2015 Goodrich and Tamassia Analysis of Algorithms 35

Page 36: Analysis of Algorithms

Analysis of Algorithms 36

Relatives of Big-Ohbig-Omega

n f(n) is W(g(n)) if there is a constant c > 0 and an integer constant n0 ³ 1 such that

f(n) ³ c g(n) for n ³ n0

big-Thetan f(n) is Q(g(n)) if there are constants c’ > 0 and

c’’ > 0 and an integer constant n0 ³ 1 such thatc’g(n) £ f(n) £ c’’g(n) for n ³ n0

© 2015 Goodrich and Tamassia

Page 37: Analysis of Algorithms

Analysis of Algorithms 37

Intuition for Asymptotic Notation

big-Ohn f(n) is O(g(n)) if f(n) is asymptotically

less than or equal to g(n)big-Omegan f(n) is W(g(n)) if f(n) is asymptotically

greater than or equal to g(n)big-Thetan f(n) is Q(g(n)) if f(n) is asymptotically

equal to g(n)

© 2015 Goodrich and Tamassia

Page 38: Analysis of Algorithms

Analysis of Algorithms 38

Example Uses of the Relatives of Big-Oh

f(n) is Q(g(n)) if it is W(n2) and O(n2). We have already seen the former, for the latter recall that f(n) is O(g(n)) if there is a constant c > 0 and an integer constant n0 ³ 1 such that f(n) < c g(n) for n ³ n0

Let c = 5 and n0 = 1

n 5n2 is Q(n2)

f(n) is W(g(n)) if there is a constant c > 0 and an integer constant n0 ³ 1 such that f(n) ³ c g(n) for n ³ n0

let c = 1 and n0 = 1

n 5n2 is W(n)

f(n) is W(g(n)) if there is a constant c > 0 and an integer constant n0 ³ 1 such that f(n) ³ c g(n) for n ³ n0

let c = 5 and n0 = 1

n 5n2 is W(n2)

© 2015 Goodrich and Tamassia

Page 39: Analysis of Algorithms

1-15

O (“big oh”)

Informally:

I g ∈ O(f ) if g is bounded above by a constant multiple of f(for sufficiently large values of n).

I g ∈ O(f ) if “g grows no faster than (a constant multiple of)f .”

I g ∈ O(f ) if the ratio g/f is bounded above by a constant (forsufficiently values of n).

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 40: Analysis of Algorithms

1-15

O (“big oh”)

Informally:

I g ∈ O(f ) if g is bounded above by a constant multiple of f(for sufficiently large values of n).

I g ∈ O(f ) if “g grows no faster than (a constant multiple of)f .”

I g ∈ O(f ) if the ratio g/f is bounded above by a constant (forsufficiently values of n).

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 41: Analysis of Algorithms

1-15

O (“big oh”)

Informally:

I g ∈ O(f ) if g is bounded above by a constant multiple of f(for sufficiently large values of n).

I g ∈ O(f ) if “g grows no faster than (a constant multiple of)f .”

I g ∈ O(f ) if the ratio g/f is bounded above by a constant (forsufficiently values of n).

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 42: Analysis of Algorithms

1-15

O (“big oh”)

Informally:

I g ∈ O(f ) if g is bounded above by a constant multiple of f(for sufficiently large values of n).

I g ∈ O(f ) if “g grows no faster than (a constant multiple of)f .”

I g ∈ O(f ) if the ratio g/f is bounded above by a constant (forsufficiently values of n).

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 43: Analysis of Algorithms

1-15

O (“big oh”)

Informally:

I g ∈ O(f ) if g is bounded above by a constant multiple of f(for sufficiently large values of n).

I g ∈ O(f ) if “g grows no faster than (a constant multiple of)f .”

I g ∈ O(f ) if the ratio g/f is bounded above by a constant (forsufficiently values of n).

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 44: Analysis of Algorithms

1-16

O (“big oh”)

Formally:

I g ∈ O(f ) if and only if:

∃C>0 ∃n0>0 ∀n>n0 g(n) ≤ C · f (n).

I Equivalently: g ∈ O(f ) if and only if:

∃C>0 ∃n0>0 ∀n>n0

g(n)

f (n)≤ C .

I Sometimes we write: g = O(f ) rather than g ∈ O(f )

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 45: Analysis of Algorithms

1-16

O (“big oh”)

Formally:

I g ∈ O(f ) if and only if:

∃C>0 ∃n0>0 ∀n>n0 g(n) ≤ C · f (n).

I Equivalently: g ∈ O(f ) if and only if:

∃C>0 ∃n0>0 ∀n>n0

g(n)

f (n)≤ C .

I Sometimes we write: g = O(f ) rather than g ∈ O(f )

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 46: Analysis of Algorithms

1-16

O (“big oh”)

Formally:

I g ∈ O(f ) if and only if:

∃C>0 ∃n0>0 ∀n>n0 g(n) ≤ C · f (n).

I Equivalently: g ∈ O(f ) if and only if:

∃C>0 ∃n0>0 ∀n>n0

g(n)

f (n)≤ C .

I Sometimes we write: g = O(f ) rather than g ∈ O(f )

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 47: Analysis of Algorithms

1-16

O (“big oh”)

Formally:

I g ∈ O(f ) if and only if:

∃C>0 ∃n0>0 ∀n>n0 g(n) ≤ C · f (n).

I Equivalently: g ∈ O(f ) if and only if:

∃C>0 ∃n0>0 ∀n>n0

g(n)

f (n)≤ C .

I Sometimes we write: g = O(f ) rather than g ∈ O(f )

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 48: Analysis of Algorithms

1-16

O (“big oh”)

Formally:

I g ∈ O(f ) if and only if:

∃C>0 ∃n0>0 ∀n>n0 g(n) ≤ C · f (n).

I Equivalently: g ∈ O(f ) if and only if:

∃C>0 ∃n0>0 ∀n>n0

g(n)

f (n)≤ C .

I Sometimes we write: g = O(f ) rather than g ∈ O(f )

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 49: Analysis of Algorithms

1-17

Examples of O-notation:

Example 1: f (n) = n, g(n) = 1000n: g ∈ O(f ).

Proof: Let C = 1000. Then g(n) ≤ C · f (n) for all n.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 50: Analysis of Algorithms

1-17

Examples of O-notation:

Example 1: f (n) = n, g(n) = 1000n: g ∈ O(f ).

Proof:

Let C = 1000. Then g(n) ≤ C · f (n) for all n.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 51: Analysis of Algorithms

1-17

Examples of O-notation:

Example 1: f (n) = n, g(n) = 1000n: g ∈ O(f ).

Proof: Let C = 1000. Then g(n) ≤ C · f (n) for all n.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 52: Analysis of Algorithms

1-18

Examples of O-notation:

Example 2: f (n) = n2, g(n) = n3/2: g ∈ O(f ).

Proof: limn→∞g(n)f (n) = 0.

Hence for any C > 0 the ratio is less than C as long as n issufficiently large.(Of course, how large n must be to be“sufficientlylarge” depends on C ).

Alternate Proof: If n ≥ 1, n1/2 ≥ 1, so n3/2 ≤ n2.Hence we can choose C = 1 and n0 = 1.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 53: Analysis of Algorithms

1-18

Examples of O-notation:

Example 2: f (n) = n2, g(n) = n3/2: g ∈ O(f ).

Proof: limn→∞g(n)f (n) = 0.

Hence for any C > 0 the ratio is less than C as long as n issufficiently large.(Of course, how large n must be to be“sufficientlylarge” depends on C ).

Alternate Proof: If n ≥ 1, n1/2 ≥ 1, so n3/2 ≤ n2.Hence we can choose C = 1 and n0 = 1.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 54: Analysis of Algorithms

1-18

Examples of O-notation:

Example 2: f (n) = n2, g(n) = n3/2: g ∈ O(f ).

Proof:

limn→∞g(n)f (n) = 0.

Hence for any C > 0 the ratio is less than C as long as n issufficiently large.(Of course, how large n must be to be“sufficientlylarge” depends on C ).

Alternate Proof: If n ≥ 1, n1/2 ≥ 1, so n3/2 ≤ n2.Hence we can choose C = 1 and n0 = 1.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 55: Analysis of Algorithms

1-18

Examples of O-notation:

Example 2: f (n) = n2, g(n) = n3/2: g ∈ O(f ).

Proof: limn→∞g(n)f (n) = 0.

Hence for any C > 0 the ratio is less than C as long as n issufficiently large.(Of course, how large n must be to be“sufficientlylarge” depends on C ).

Alternate Proof: If n ≥ 1, n1/2 ≥ 1, so n3/2 ≤ n2.Hence we can choose C = 1 and n0 = 1.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 56: Analysis of Algorithms

1-18

Examples of O-notation:

Example 2: f (n) = n2, g(n) = n3/2: g ∈ O(f ).

Proof: limn→∞g(n)f (n) = 0.

Hence for any C > 0 the ratio is less than C as long as n issufficiently large.

(Of course, how large n must be to be“sufficientlylarge” depends on C ).

Alternate Proof: If n ≥ 1, n1/2 ≥ 1, so n3/2 ≤ n2.Hence we can choose C = 1 and n0 = 1.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 57: Analysis of Algorithms

1-18

Examples of O-notation:

Example 2: f (n) = n2, g(n) = n3/2: g ∈ O(f ).

Proof: limn→∞g(n)f (n) = 0.

Hence for any C > 0 the ratio is less than C as long as n issufficiently large.(Of course, how large n must be to be“sufficientlylarge” depends on C ).

Alternate Proof: If n ≥ 1, n1/2 ≥ 1, so n3/2 ≤ n2.Hence we can choose C = 1 and n0 = 1.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 58: Analysis of Algorithms

1-18

Examples of O-notation:

Example 2: f (n) = n2, g(n) = n3/2: g ∈ O(f ).

Proof: limn→∞g(n)f (n) = 0.

Hence for any C > 0 the ratio is less than C as long as n issufficiently large.(Of course, how large n must be to be“sufficientlylarge” depends on C ).

Alternate Proof:

If n ≥ 1, n1/2 ≥ 1, so n3/2 ≤ n2.Hence we can choose C = 1 and n0 = 1.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 59: Analysis of Algorithms

1-18

Examples of O-notation:

Example 2: f (n) = n2, g(n) = n3/2: g ∈ O(f ).

Proof: limn→∞g(n)f (n) = 0.

Hence for any C > 0 the ratio is less than C as long as n issufficiently large.(Of course, how large n must be to be“sufficientlylarge” depends on C ).

Alternate Proof: If n ≥ 1, n1/2 ≥ 1, so n3/2 ≤ n2.

Hence we can choose C = 1 and n0 = 1.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 60: Analysis of Algorithms

1-18

Examples of O-notation:

Example 2: f (n) = n2, g(n) = n3/2: g ∈ O(f ).

Proof: limn→∞g(n)f (n) = 0.

Hence for any C > 0 the ratio is less than C as long as n issufficiently large.(Of course, how large n must be to be“sufficientlylarge” depends on C ).

Alternate Proof: If n ≥ 1, n1/2 ≥ 1, so n3/2 ≤ n2.Hence we can choose C = 1 and n0 = 1.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 61: Analysis of Algorithms

1-19

Examples of O-notation:

Example 3: f (n) = n3, g(n) = n4: g /∈ O(f ).

Proof: limn→∞g(n)f (n) =∞.

Hence there is no C > 0 such that g(n) ≤ C · f (n) for sufficientlylarge n.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 62: Analysis of Algorithms

1-19

Examples of O-notation:

Example 3: f (n) = n3, g(n) = n4: g /∈ O(f ).

Proof: limn→∞g(n)f (n) =∞.

Hence there is no C > 0 such that g(n) ≤ C · f (n) for sufficientlylarge n.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 63: Analysis of Algorithms

1-19

Examples of O-notation:

Example 3: f (n) = n3, g(n) = n4: g /∈ O(f ).

Proof:

limn→∞g(n)f (n) =∞.

Hence there is no C > 0 such that g(n) ≤ C · f (n) for sufficientlylarge n.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 64: Analysis of Algorithms

1-19

Examples of O-notation:

Example 3: f (n) = n3, g(n) = n4: g /∈ O(f ).

Proof: limn→∞g(n)f (n) =∞.

Hence there is no C > 0 such that g(n) ≤ C · f (n) for sufficientlylarge n.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 65: Analysis of Algorithms

1-19

Examples of O-notation:

Example 3: f (n) = n3, g(n) = n4: g /∈ O(f ).

Proof: limn→∞g(n)f (n) =∞.

Hence there is no C > 0 such that g(n) ≤ C · f (n) for sufficientlylarge n.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 66: Analysis of Algorithms

1-20

Examples of O-notation:

Example 4: f (n) = n2, g(n) = 5n2 + 23n + 2: g ∈ O(f ).

Proof: If n ≥ 1, then n ≤ n2 and 1 ≤ n2. Hence:

g(n) = 5n2 + 23n + 2

≤ 5n2 + 23n2 + 2n2

≤ 30n2

= 30f (n)

So we can take C = 30, n0 = 1.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 67: Analysis of Algorithms

1-20

Examples of O-notation:

Example 4: f (n) = n2, g(n) = 5n2 + 23n + 2: g ∈ O(f ).

Proof: If n ≥ 1, then n ≤ n2 and 1 ≤ n2. Hence:

g(n) = 5n2 + 23n + 2

≤ 5n2 + 23n2 + 2n2

≤ 30n2

= 30f (n)

So we can take C = 30, n0 = 1.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 68: Analysis of Algorithms

1-20

Examples of O-notation:

Example 4: f (n) = n2, g(n) = 5n2 + 23n + 2: g ∈ O(f ).

Proof:

If n ≥ 1, then n ≤ n2 and 1 ≤ n2. Hence:

g(n) = 5n2 + 23n + 2

≤ 5n2 + 23n2 + 2n2

≤ 30n2

= 30f (n)

So we can take C = 30, n0 = 1.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 69: Analysis of Algorithms

1-20

Examples of O-notation:

Example 4: f (n) = n2, g(n) = 5n2 + 23n + 2: g ∈ O(f ).

Proof: If n ≥ 1, then n ≤ n2 and 1 ≤ n2.

Hence:

g(n) = 5n2 + 23n + 2

≤ 5n2 + 23n2 + 2n2

≤ 30n2

= 30f (n)

So we can take C = 30, n0 = 1.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 70: Analysis of Algorithms

1-20

Examples of O-notation:

Example 4: f (n) = n2, g(n) = 5n2 + 23n + 2: g ∈ O(f ).

Proof: If n ≥ 1, then n ≤ n2 and 1 ≤ n2. Hence:

g(n) = 5n2 + 23n + 2

≤ 5n2 + 23n2 + 2n2

≤ 30n2

= 30f (n)

So we can take C = 30, n0 = 1.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 71: Analysis of Algorithms

1-20

Examples of O-notation:

Example 4: f (n) = n2, g(n) = 5n2 + 23n + 2: g ∈ O(f ).

Proof: If n ≥ 1, then n ≤ n2 and 1 ≤ n2. Hence:

g(n) = 5n2 + 23n + 2

≤ 5n2 + 23n2 + 2n2

≤ 30n2

= 30f (n)

So we can take C = 30, n0 = 1.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 72: Analysis of Algorithms

1-20

Examples of O-notation:

Example 4: f (n) = n2, g(n) = 5n2 + 23n + 2: g ∈ O(f ).

Proof: If n ≥ 1, then n ≤ n2 and 1 ≤ n2. Hence:

g(n) = 5n2 + 23n + 2

≤ 5n2 + 23n2 + 2n2

≤ 30n2

= 30f (n)

So we can take C = 30, n0 = 1.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 73: Analysis of Algorithms

1-20

Examples of O-notation:

Example 4: f (n) = n2, g(n) = 5n2 + 23n + 2: g ∈ O(f ).

Proof: If n ≥ 1, then n ≤ n2 and 1 ≤ n2. Hence:

g(n) = 5n2 + 23n + 2

≤ 5n2 + 23n2 + 2n2

≤ 30n2

= 30f (n)

So we can take C = 30, n0 = 1.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 74: Analysis of Algorithms

1-20

Examples of O-notation:

Example 4: f (n) = n2, g(n) = 5n2 + 23n + 2: g ∈ O(f ).

Proof: If n ≥ 1, then n ≤ n2 and 1 ≤ n2. Hence:

g(n) = 5n2 + 23n + 2

≤ 5n2 + 23n2 + 2n2

≤ 30n2

= 30f (n)

So we can take C = 30, n0 = 1.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 75: Analysis of Algorithms

1-20

Examples of O-notation:

Example 4: f (n) = n2, g(n) = 5n2 + 23n + 2: g ∈ O(f ).

Proof: If n ≥ 1, then n ≤ n2 and 1 ≤ n2. Hence:

g(n) = 5n2 + 23n + 2

≤ 5n2 + 23n2 + 2n2

≤ 30n2

= 30f (n)

So we can take C = 30, n0 = 1.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 76: Analysis of Algorithms

1-21

More asymptotic notation:o (“little oh”), Ω (“big Omega”)

I o (‘little oh”):

g ∈ o(f ) if and only if limn→∞

g(n)

f (n)= 0.

I Ω (“big Omega”) (or just “Omega”)

g ∈ Ω(f ) if and only if ∃C>0 ∃n0>0 ∀n>n0 g(n) ≥ C · f (n).

Equivalently:

g ∈ Ω(f ) if and only if ∃C>0 ∃n0>0 ∀n>n0

g(n)

f (n)≥ C .

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 77: Analysis of Algorithms

1-21

More asymptotic notation:o (“little oh”), Ω (“big Omega”)

I o (‘little oh”):

g ∈ o(f ) if and only if limn→∞

g(n)

f (n)= 0.

I Ω (“big Omega”) (or just “Omega”)

g ∈ Ω(f ) if and only if ∃C>0 ∃n0>0 ∀n>n0 g(n) ≥ C · f (n).

Equivalently:

g ∈ Ω(f ) if and only if ∃C>0 ∃n0>0 ∀n>n0

g(n)

f (n)≥ C .

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 78: Analysis of Algorithms

1-21

More asymptotic notation:o (“little oh”), Ω (“big Omega”)

I o (‘little oh”):

g ∈ o(f ) if and only if limn→∞

g(n)

f (n)= 0.

I Ω (“big Omega”) (or just “Omega”)

g ∈ Ω(f ) if and only if ∃C>0 ∃n0>0 ∀n>n0 g(n) ≥ C · f (n).

Equivalently:

g ∈ Ω(f ) if and only if ∃C>0 ∃n0>0 ∀n>n0

g(n)

f (n)≥ C .

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 79: Analysis of Algorithms

1-21

More asymptotic notation:o (“little oh”), Ω (“big Omega”)

I o (‘little oh”):

g ∈ o(f ) if and only if limn→∞

g(n)

f (n)= 0.

I Ω (“big Omega”) (or just “Omega”)

g ∈ Ω(f ) if and only if ∃C>0 ∃n0>0 ∀n>n0 g(n) ≥ C · f (n).

Equivalently:

g ∈ Ω(f ) if and only if ∃C>0 ∃n0>0 ∀n>n0

g(n)

f (n)≥ C .

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 80: Analysis of Algorithms

1-21

More asymptotic notation:o (“little oh”), Ω (“big Omega”)

I o (‘little oh”):

g ∈ o(f ) if and only if limn→∞

g(n)

f (n)= 0.

I Ω (“big Omega”) (or just “Omega”)

g ∈ Ω(f ) if and only if ∃C>0 ∃n0>0 ∀n>n0 g(n) ≥ C · f (n).

Equivalently:

g ∈ Ω(f ) if and only if ∃C>0 ∃n0>0 ∀n>n0

g(n)

f (n)≥ C .

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 81: Analysis of Algorithms

1-21

More asymptotic notation:o (“little oh”), Ω (“big Omega”)

I o (‘little oh”):

g ∈ o(f ) if and only if limn→∞

g(n)

f (n)= 0.

I Ω (“big Omega”) (or just “Omega”)

g ∈ Ω(f ) if and only if ∃C>0 ∃n0>0 ∀n>n0 g(n) ≥ C · f (n).

Equivalently:

g ∈ Ω(f ) if and only if ∃C>0 ∃n0>0 ∀n>n0

g(n)

f (n)≥ C .

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 82: Analysis of Algorithms

1-21

More asymptotic notation:o (“little oh”), Ω (“big Omega”)

I o (‘little oh”):

g ∈ o(f ) if and only if limn→∞

g(n)

f (n)= 0.

I Ω (“big Omega”) (or just “Omega”)

g ∈ Ω(f ) if and only if ∃C>0 ∃n0>0 ∀n>n0 g(n) ≥ C · f (n).

Equivalently:

g ∈ Ω(f ) if and only if ∃C>0 ∃n0>0 ∀n>n0

g(n)

f (n)≥ C .

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 83: Analysis of Algorithms

1-22

One more definition:Θ (“Theta”)

I g ∈ Θ(f ) if and only if:

g ∈ O(f ) and g ∈ Ω(f ).

I Equivalently, g ∈ Θ(f ) if and only if:

∃C1>0 ∃C2>0 ∃n0>0 ∀n>n0 C1 ≤g(n)

f (n)≤ C2.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 84: Analysis of Algorithms

1-22

One more definition:Θ (“Theta”)

I g ∈ Θ(f ) if and only if:

g ∈ O(f ) and g ∈ Ω(f ).

I Equivalently, g ∈ Θ(f ) if and only if:

∃C1>0 ∃C2>0 ∃n0>0 ∀n>n0 C1 ≤g(n)

f (n)≤ C2.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 85: Analysis of Algorithms

1-22

One more definition:Θ (“Theta”)

I g ∈ Θ(f ) if and only if:

g ∈ O(f ) and g ∈ Ω(f ).

I Equivalently, g ∈ Θ(f ) if and only if:

∃C1>0 ∃C2>0 ∃n0>0 ∀n>n0 C1 ≤g(n)

f (n)≤ C2.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 86: Analysis of Algorithms

1-23

Examples of Asymptotic notation

Example 1: f (n) = n, g(n) = 1000n.

g ∈ Ω(f ), g ∈ Θ(f )

To see that g ∈ Ω(f ), we can take C = 1.

Then g(n) = 1000 · n > 1 · n = C · f (n).

To see that g ∈ Θ(f ), we could argue that g ∈ O(f ) (shownearlier) and g ∈ Ω(f ) (shown above).

Or we can take C1 = 1, C2 = 1000. Then

C1 ≤g(n)

f (n≤ C2.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 87: Analysis of Algorithms

1-23

Examples of Asymptotic notation

Example 1: f (n) = n, g(n) = 1000n.

g ∈ Ω(f ), g ∈ Θ(f )

To see that g ∈ Ω(f ), we can take C = 1.

Then g(n) = 1000 · n > 1 · n = C · f (n).

To see that g ∈ Θ(f ), we could argue that g ∈ O(f ) (shownearlier) and g ∈ Ω(f ) (shown above).

Or we can take C1 = 1, C2 = 1000. Then

C1 ≤g(n)

f (n≤ C2.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 88: Analysis of Algorithms

1-23

Examples of Asymptotic notation

Example 1: f (n) = n, g(n) = 1000n.

g ∈ Ω(f ), g ∈ Θ(f )

To see that g ∈ Ω(f ), we can take C = 1.

Then g(n) = 1000 · n > 1 · n = C · f (n).

To see that g ∈ Θ(f ), we could argue that g ∈ O(f ) (shownearlier) and g ∈ Ω(f ) (shown above).

Or we can take C1 = 1, C2 = 1000. Then

C1 ≤g(n)

f (n≤ C2.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 89: Analysis of Algorithms

1-23

Examples of Asymptotic notation

Example 1: f (n) = n, g(n) = 1000n.

g ∈ Ω(f ), g ∈ Θ(f )

To see that g ∈ Ω(f ), we can take C = 1.

Then g(n) = 1000 · n > 1 · n = C · f (n).

To see that g ∈ Θ(f ), we could argue that g ∈ O(f ) (shownearlier) and g ∈ Ω(f ) (shown above).

Or we can take C1 = 1, C2 = 1000. Then

C1 ≤g(n)

f (n≤ C2.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 90: Analysis of Algorithms

1-23

Examples of Asymptotic notation

Example 1: f (n) = n, g(n) = 1000n.

g ∈ Ω(f ), g ∈ Θ(f )

To see that g ∈ Ω(f ), we can take C = 1.

Then g(n) = 1000 · n > 1 · n = C · f (n).

To see that g ∈ Θ(f ), we could argue that g ∈ O(f ) (shownearlier) and g ∈ Ω(f ) (shown above).

Or we can take C1 = 1, C2 = 1000. Then

C1 ≤g(n)

f (n≤ C2.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 91: Analysis of Algorithms

1-23

Examples of Asymptotic notation

Example 1: f (n) = n, g(n) = 1000n.

g ∈ Ω(f ), g ∈ Θ(f )

To see that g ∈ Ω(f ), we can take C = 1.

Then g(n) = 1000 · n > 1 · n = C · f (n).

To see that g ∈ Θ(f ), we could argue that g ∈ O(f ) (shownearlier) and g ∈ Ω(f ) (shown above).

Or we can take C1 = 1, C2 = 1000. Then

C1 ≤g(n)

f (n≤ C2.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 92: Analysis of Algorithms

1-23

Examples of Asymptotic notation

Example 1: f (n) = n, g(n) = 1000n.

g ∈ Ω(f ), g ∈ Θ(f )

To see that g ∈ Ω(f ), we can take C = 1.

Then g(n) = 1000 · n > 1 · n = C · f (n).

To see that g ∈ Θ(f ), we could argue that g ∈ O(f ) (shownearlier) and g ∈ Ω(f ) (shown above).

Or we can take C1 = 1, C2 = 1000. Then

C1 ≤g(n)

f (n≤ C2.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 93: Analysis of Algorithms

1-24

Examples of Asymptotic notation

Example 2: f (n) = n2, g(n) = n3/2:

g ∈ o(f )

Because limn→∞g(n)f (n) = 0.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 94: Analysis of Algorithms

1-24

Examples of Asymptotic notation

Example 2: f (n) = n2, g(n) = n3/2:

g ∈ o(f )

Because limn→∞g(n)f (n) = 0.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 95: Analysis of Algorithms

1-24

Examples of Asymptotic notation

Example 2: f (n) = n2, g(n) = n3/2:

g ∈ o(f )

Because limn→∞g(n)f (n) = 0.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 96: Analysis of Algorithms

1-24

Examples of Asymptotic notation

Example 2: f (n) = n2, g(n) = n3/2:

g ∈ o(f )

Because limn→∞g(n)f (n) = 0.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 97: Analysis of Algorithms

1-25

Examples of Asymptotic notation

Example 3: f (n) = n3, g(n) = n4:

g ∈ Ω(f )

Because limn→∞g(n)f (n) =∞, so we can choose any C we want.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 98: Analysis of Algorithms

1-25

Examples of Asymptotic notation

Example 3: f (n) = n3, g(n) = n4:

g ∈ Ω(f )

Because limn→∞g(n)f (n) =∞, so we can choose any C we want.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 99: Analysis of Algorithms

1-25

Examples of Asymptotic notation

Example 3: f (n) = n3, g(n) = n4:

g ∈ Ω(f )

Because limn→∞g(n)f (n) =∞, so we can choose any C we want.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 100: Analysis of Algorithms

1-25

Examples of Asymptotic notation

Example 3: f (n) = n3, g(n) = n4:

g ∈ Ω(f )

Because limn→∞g(n)f (n) =∞, so we can choose any C we want.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 101: Analysis of Algorithms

1-26

Examples of Asymptotic notation

Example 4: f (n) = n2, g(n) = 5n2 − 23n + 2:

g ∈ Ω(f ).

Proof: If n ≥ 23, then 23n ≤ n2. Hence if n ≥ 23:

g(n) = 5n2 − 23n + 2

≥ 5n2 − n2

≥ 4n2

= 4f (n)

So we can take C = 4, n0 = 23.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 102: Analysis of Algorithms

1-26

Examples of Asymptotic notation

Example 4: f (n) = n2, g(n) = 5n2 − 23n + 2:

g ∈ Ω(f ).

Proof: If n ≥ 23, then 23n ≤ n2. Hence if n ≥ 23:

g(n) = 5n2 − 23n + 2

≥ 5n2 − n2

≥ 4n2

= 4f (n)

So we can take C = 4, n0 = 23.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 103: Analysis of Algorithms

1-26

Examples of Asymptotic notation

Example 4: f (n) = n2, g(n) = 5n2 − 23n + 2:

g ∈ Ω(f ).

Proof:

If n ≥ 23, then 23n ≤ n2. Hence if n ≥ 23:

g(n) = 5n2 − 23n + 2

≥ 5n2 − n2

≥ 4n2

= 4f (n)

So we can take C = 4, n0 = 23.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 104: Analysis of Algorithms

1-26

Examples of Asymptotic notation

Example 4: f (n) = n2, g(n) = 5n2 − 23n + 2:

g ∈ Ω(f ).

Proof: If n ≥ 23, then 23n ≤ n2.

Hence if n ≥ 23:

g(n) = 5n2 − 23n + 2

≥ 5n2 − n2

≥ 4n2

= 4f (n)

So we can take C = 4, n0 = 23.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 105: Analysis of Algorithms

1-26

Examples of Asymptotic notation

Example 4: f (n) = n2, g(n) = 5n2 − 23n + 2:

g ∈ Ω(f ).

Proof: If n ≥ 23, then 23n ≤ n2. Hence if n ≥ 23:

g(n) = 5n2 − 23n + 2

≥ 5n2 − n2

≥ 4n2

= 4f (n)

So we can take C = 4, n0 = 23.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 106: Analysis of Algorithms

1-26

Examples of Asymptotic notation

Example 4: f (n) = n2, g(n) = 5n2 − 23n + 2:

g ∈ Ω(f ).

Proof: If n ≥ 23, then 23n ≤ n2. Hence if n ≥ 23:

g(n) = 5n2 − 23n + 2

≥ 5n2 − n2

≥ 4n2

= 4f (n)

So we can take C = 4, n0 = 23.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 107: Analysis of Algorithms

1-26

Examples of Asymptotic notation

Example 4: f (n) = n2, g(n) = 5n2 − 23n + 2:

g ∈ Ω(f ).

Proof: If n ≥ 23, then 23n ≤ n2. Hence if n ≥ 23:

g(n) = 5n2 − 23n + 2

≥ 5n2 − n2

≥ 4n2

= 4f (n)

So we can take C = 4, n0 = 23.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 108: Analysis of Algorithms

1-26

Examples of Asymptotic notation

Example 4: f (n) = n2, g(n) = 5n2 − 23n + 2:

g ∈ Ω(f ).

Proof: If n ≥ 23, then 23n ≤ n2. Hence if n ≥ 23:

g(n) = 5n2 − 23n + 2

≥ 5n2 − n2

≥ 4n2

= 4f (n)

So we can take C = 4, n0 = 23.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 109: Analysis of Algorithms

1-26

Examples of Asymptotic notation

Example 4: f (n) = n2, g(n) = 5n2 − 23n + 2:

g ∈ Ω(f ).

Proof: If n ≥ 23, then 23n ≤ n2. Hence if n ≥ 23:

g(n) = 5n2 − 23n + 2

≥ 5n2 − n2

≥ 4n2

= 4f (n)

So we can take C = 4, n0 = 23.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 110: Analysis of Algorithms

1-26

Examples of Asymptotic notation

Example 4: f (n) = n2, g(n) = 5n2 − 23n + 2:

g ∈ Ω(f ).

Proof: If n ≥ 23, then 23n ≤ n2. Hence if n ≥ 23:

g(n) = 5n2 − 23n + 2

≥ 5n2 − n2

≥ 4n2

= 4f (n)

So we can take C = 4, n0 = 23.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 111: Analysis of Algorithms

1-27

Another Example

Example 5: ln n = o(n)

Proof:

Examine the ratio ln nn as n→∞.

If we try to evaluate the limit directly, we obtain the“indeterminate form” ∞∞ .

We need to apply L’Hopital’s rule (from calculus).

(Continued on next slide)

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 112: Analysis of Algorithms

1-27

Another Example

Example 5: ln n = o(n)

Proof:

Examine the ratio ln nn as n→∞.

If we try to evaluate the limit directly, we obtain the“indeterminate form” ∞∞ .

We need to apply L’Hopital’s rule (from calculus).

(Continued on next slide)

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 113: Analysis of Algorithms

1-27

Another Example

Example 5: ln n = o(n)

Proof:

Examine the ratio ln nn as n→∞.

If we try to evaluate the limit directly, we obtain the“indeterminate form” ∞∞ .

We need to apply L’Hopital’s rule (from calculus).

(Continued on next slide)

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 114: Analysis of Algorithms

1-27

Another Example

Example 5: ln n = o(n)

Proof:

Examine the ratio ln nn as n→∞.

If we try to evaluate the limit directly, we obtain the“indeterminate form” ∞∞ .

We need to apply L’Hopital’s rule (from calculus).

(Continued on next slide)

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 115: Analysis of Algorithms

1-27

Another Example

Example 5: ln n = o(n)

Proof:

Examine the ratio ln nn as n→∞.

If we try to evaluate the limit directly, we obtain the“indeterminate form” ∞∞ .

We need to apply L’Hopital’s rule (from calculus).

(Continued on next slide)

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 116: Analysis of Algorithms

1-27

Another Example

Example 5: ln n = o(n)

Proof:

Examine the ratio ln nn as n→∞.

If we try to evaluate the limit directly, we obtain the“indeterminate form” ∞∞ .

We need to apply L’Hopital’s rule (from calculus).

(Continued on next slide)

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 117: Analysis of Algorithms

1-27

Another Example

Example 5: ln n = o(n)

Proof:

Examine the ratio ln nn as n→∞.

If we try to evaluate the limit directly, we obtain the“indeterminate form” ∞∞ .

We need to apply L’Hopital’s rule (from calculus).

(Continued on next slide)

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 118: Analysis of Algorithms

1-28

Example 5, continued:ln n = o(n)

L’Hopital’s rule: If the ratio of limits

limn→∞ g(n)

limn→∞ f (n)

is an indeterminate form (i.e., ∞/∞ or 0/0), then

limn→∞

g(n)

f (n)= lim

n→∞

g ′(n)

f ′(n)

where f ′ and g ′ are, respectively, the derivatives of f and g .

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 119: Analysis of Algorithms

1-29

Example 5, continued:ln n = o(n)

Let f (n) = n, g(n) = ln n.

Then f ′(n) = 1, g ′(n) = 1/n.

By L’Hopital’s rule:

limn→∞

g(n)

f (n)= lim

n→∞

g ′(n)

f ′(n)

= limn→∞

1/n

1

= limn→∞

1

n

= 0.

Hence g(n) = o (f (n)).

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 120: Analysis of Algorithms

1-29

Example 5, continued:ln n = o(n)

Let f (n) = n, g(n) = ln n.

Then f ′(n) = 1, g ′(n) = 1/n.

By L’Hopital’s rule:

limn→∞

g(n)

f (n)= lim

n→∞

g ′(n)

f ′(n)

= limn→∞

1/n

1

= limn→∞

1

n

= 0.

Hence g(n) = o (f (n)).

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 121: Analysis of Algorithms

1-29

Example 5, continued:ln n = o(n)

Let f (n) = n, g(n) = ln n.

Then f ′(n) = 1, g ′(n) = 1/n.

By L’Hopital’s rule:

limn→∞

g(n)

f (n)= lim

n→∞

g ′(n)

f ′(n)

= limn→∞

1/n

1

= limn→∞

1

n

= 0.

Hence g(n) = o (f (n)).

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 122: Analysis of Algorithms

1-29

Example 5, continued:ln n = o(n)

Let f (n) = n, g(n) = ln n.

Then f ′(n) = 1, g ′(n) = 1/n.

By L’Hopital’s rule:

limn→∞

g(n)

f (n)= lim

n→∞

g ′(n)

f ′(n)

= limn→∞

1/n

1

= limn→∞

1

n

= 0.

Hence g(n) = o (f (n)).

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 123: Analysis of Algorithms

1-29

Example 5, continued:ln n = o(n)

Let f (n) = n, g(n) = ln n.

Then f ′(n) = 1, g ′(n) = 1/n.

By L’Hopital’s rule:

limn→∞

g(n)

f (n)= lim

n→∞

g ′(n)

f ′(n)

= limn→∞

1/n

1

= limn→∞

1

n

= 0.

Hence g(n) = o (f (n)).

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 124: Analysis of Algorithms

1-29

Example 5, continued:ln n = o(n)

Let f (n) = n, g(n) = ln n.

Then f ′(n) = 1, g ′(n) = 1/n.

By L’Hopital’s rule:

limn→∞

g(n)

f (n)= lim

n→∞

g ′(n)

f ′(n)

= limn→∞

1/n

1

= limn→∞

1

n

= 0.

Hence g(n) = o (f (n)).

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 125: Analysis of Algorithms

1-29

Example 5, continued:ln n = o(n)

Let f (n) = n, g(n) = ln n.

Then f ′(n) = 1, g ′(n) = 1/n.

By L’Hopital’s rule:

limn→∞

g(n)

f (n)= lim

n→∞

g ′(n)

f ′(n)

= limn→∞

1/n

1

= limn→∞

1

n

= 0.

Hence g(n) = o (f (n)).

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 126: Analysis of Algorithms

1-29

Example 5, continued:ln n = o(n)

Let f (n) = n, g(n) = ln n.

Then f ′(n) = 1, g ′(n) = 1/n.

By L’Hopital’s rule:

limn→∞

g(n)

f (n)= lim

n→∞

g ′(n)

f ′(n)

= limn→∞

1/n

1

= limn→∞

1

n

= 0.

Hence g(n) = o (f (n)).

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 127: Analysis of Algorithms

1-30

Math background

I Sums, Summations

I Logarithms, Exponents Floors, Ceilings, Harmonic Numbers

I Proof Techniques

I Basic Probability

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 128: Analysis of Algorithms

1-30

Math background

I Sums, Summations

I Logarithms, Exponents Floors, Ceilings, Harmonic Numbers

I Proof Techniques

I Basic Probability

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 129: Analysis of Algorithms

1-30

Math background

I Sums, Summations

I Logarithms, Exponents Floors, Ceilings, Harmonic Numbers

I Proof Techniques

I Basic Probability

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 130: Analysis of Algorithms

1-30

Math background

I Sums, Summations

I Logarithms, Exponents Floors, Ceilings, Harmonic Numbers

I Proof Techniques

I Basic Probability

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 131: Analysis of Algorithms

1-30

Math background

I Sums, Summations

I Logarithms, Exponents Floors, Ceilings, Harmonic Numbers

I Proof Techniques

I Basic Probability

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 132: Analysis of Algorithms

1-31

Sums, Summations

I Summation notation:

b∑i=a

f (i) = f (a) + f (a + 1) + · · ·+ f (b).

I Special cases:I What if a = b? f (a)I What if a > b? 0

I If S = s1, . . . , sn is a finite set:∑x∈S

f (x) = f (s1) + f (s2) + · · ·+ f (sn).

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 133: Analysis of Algorithms

1-31

Sums, Summations

I Summation notation:

b∑i=a

f (i) = f (a) + f (a + 1) + · · ·+ f (b).

I Special cases:I What if a = b? f (a)I What if a > b? 0

I If S = s1, . . . , sn is a finite set:∑x∈S

f (x) = f (s1) + f (s2) + · · ·+ f (sn).

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 134: Analysis of Algorithms

1-31

Sums, Summations

I Summation notation:

b∑i=a

f (i) = f (a) + f (a + 1) + · · ·+ f (b).

I Special cases:I What if a = b? f (a)I What if a > b? 0

I If S = s1, . . . , sn is a finite set:∑x∈S

f (x) = f (s1) + f (s2) + · · ·+ f (sn).

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 135: Analysis of Algorithms

1-31

Sums, Summations

I Summation notation:

b∑i=a

f (i) = f (a) + f (a + 1) + · · ·+ f (b).

I Special cases:

I What if a = b? f (a)I What if a > b? 0

I If S = s1, . . . , sn is a finite set:∑x∈S

f (x) = f (s1) + f (s2) + · · ·+ f (sn).

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 136: Analysis of Algorithms

1-31

Sums, Summations

I Summation notation:

b∑i=a

f (i) = f (a) + f (a + 1) + · · ·+ f (b).

I Special cases:I What if a = b?

f (a)I What if a > b? 0

I If S = s1, . . . , sn is a finite set:∑x∈S

f (x) = f (s1) + f (s2) + · · ·+ f (sn).

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 137: Analysis of Algorithms

1-31

Sums, Summations

I Summation notation:

b∑i=a

f (i) = f (a) + f (a + 1) + · · ·+ f (b).

I Special cases:I What if a = b? f (a)

I What if a > b? 0

I If S = s1, . . . , sn is a finite set:∑x∈S

f (x) = f (s1) + f (s2) + · · ·+ f (sn).

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 138: Analysis of Algorithms

1-31

Sums, Summations

I Summation notation:

b∑i=a

f (i) = f (a) + f (a + 1) + · · ·+ f (b).

I Special cases:I What if a = b? f (a)I What if a > b?

0

I If S = s1, . . . , sn is a finite set:∑x∈S

f (x) = f (s1) + f (s2) + · · ·+ f (sn).

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 139: Analysis of Algorithms

1-31

Sums, Summations

I Summation notation:

b∑i=a

f (i) = f (a) + f (a + 1) + · · ·+ f (b).

I Special cases:I What if a = b? f (a)I What if a > b? 0

I If S = s1, . . . , sn is a finite set:∑x∈S

f (x) = f (s1) + f (s2) + · · ·+ f (sn).

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 140: Analysis of Algorithms

1-31

Sums, Summations

I Summation notation:

b∑i=a

f (i) = f (a) + f (a + 1) + · · ·+ f (b).

I Special cases:I What if a = b? f (a)I What if a > b? 0

I If S = s1, . . . , sn is a finite set:

∑x∈S

f (x) = f (s1) + f (s2) + · · ·+ f (sn).

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 141: Analysis of Algorithms

1-31

Sums, Summations

I Summation notation:

b∑i=a

f (i) = f (a) + f (a + 1) + · · ·+ f (b).

I Special cases:I What if a = b? f (a)I What if a > b? 0

I If S = s1, . . . , sn is a finite set:∑x∈S

f (x) = f (s1) + f (s2) + · · ·+ f (sn).

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 142: Analysis of Algorithms

1-32

Geometric sum

I Geometric sum:

n∑i=0

ai = 1 + a1 + a2 + · · ·+ an =1− an+1

1− a,

provided that a 6= 1.

I Previous formula holds for a = 0 because a0 = 1 even whena = 0.

I Special case of geometric sum:

n∑i=0

2i = 1 + 2 + 4 + 8 + · · ·+ 2n = 2n+1 − 1.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 143: Analysis of Algorithms

1-32

Geometric sum

I Geometric sum:

n∑i=0

ai = 1 + a1 + a2 + · · ·+ an =1− an+1

1− a,

provided that a 6= 1.

I Previous formula holds for a = 0 because a0 = 1 even whena = 0.

I Special case of geometric sum:

n∑i=0

2i = 1 + 2 + 4 + 8 + · · ·+ 2n = 2n+1 − 1.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 144: Analysis of Algorithms

1-32

Geometric sum

I Geometric sum:

n∑i=0

ai = 1 + a1 + a2 + · · ·+ an =1− an+1

1− a,

provided that a 6= 1.

I Previous formula holds for a = 0 because a0 = 1 even whena = 0.

I Special case of geometric sum:

n∑i=0

2i = 1 + 2 + 4 + 8 + · · ·+ 2n = 2n+1 − 1.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 145: Analysis of Algorithms

1-32

Geometric sum

I Geometric sum:

n∑i=0

ai = 1 + a1 + a2 + · · ·+ an =1− an+1

1− a,

provided that a 6= 1.

I Previous formula holds for a = 0 because a0 = 1 even whena = 0.

I Special case of geometric sum:

n∑i=0

2i = 1 + 2 + 4 + 8 + · · ·+ 2n = 2n+1 − 1.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 146: Analysis of Algorithms

1-32

Geometric sum

I Geometric sum:

n∑i=0

ai = 1 + a1 + a2 + · · ·+ an =1− an+1

1− a,

provided that a 6= 1.

I Previous formula holds for a = 0 because a0 = 1 even whena = 0.

I Special case of geometric sum:

n∑i=0

2i = 1 + 2 + 4 + 8 + · · ·+ 2n = 2n+1 − 1.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 147: Analysis of Algorithms

1-32

Geometric sum

I Geometric sum:

n∑i=0

ai = 1 + a1 + a2 + · · ·+ an =1− an+1

1− a,

provided that a 6= 1.

I Previous formula holds for a = 0 because a0 = 1 even whena = 0.

I Special case of geometric sum:

n∑i=0

2i = 1 + 2 + 4 + 8 + · · ·+ 2n = 2n+1 − 1.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 148: Analysis of Algorithms

1-33

Infinite Geometric sum

I From the previous slide:

n∑i=0

ai = 1 + a1 + a2 + · · ·+ an =1− an+1

1− a,

provided that a 6= 1.

I If |a| < 1, we can take the limit as n→∞:

∞∑i=0

ai = 1 + a1 + a2 + · · · =1

1− a,

I Special case of infinite geometric sum:

∞∑i=0

1

2i= 1 +

1

2+

1

4+

1

8+ · · · = 2.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 149: Analysis of Algorithms

1-33

Infinite Geometric sum

I From the previous slide:

n∑i=0

ai = 1 + a1 + a2 + · · ·+ an =1− an+1

1− a,

provided that a 6= 1.

I If |a| < 1, we can take the limit as n→∞:

∞∑i=0

ai = 1 + a1 + a2 + · · · =1

1− a,

I Special case of infinite geometric sum:

∞∑i=0

1

2i= 1 +

1

2+

1

4+

1

8+ · · · = 2.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 150: Analysis of Algorithms

1-33

Infinite Geometric sum

I From the previous slide:

n∑i=0

ai = 1 + a1 + a2 + · · ·+ an =1− an+1

1− a,

provided that a 6= 1.

I If |a| < 1, we can take the limit as n→∞:

∞∑i=0

ai = 1 + a1 + a2 + · · · =1

1− a,

I Special case of infinite geometric sum:

∞∑i=0

1

2i= 1 +

1

2+

1

4+

1

8+ · · · = 2.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 151: Analysis of Algorithms

1-33

Infinite Geometric sum

I From the previous slide:

n∑i=0

ai = 1 + a1 + a2 + · · ·+ an =1− an+1

1− a,

provided that a 6= 1.

I If |a| < 1, we can take the limit as n→∞:

∞∑i=0

ai = 1 + a1 + a2 + · · · =1

1− a,

I Special case of infinite geometric sum:

∞∑i=0

1

2i= 1 +

1

2+

1

4+

1

8+ · · · = 2.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 152: Analysis of Algorithms

1-33

Infinite Geometric sum

I From the previous slide:

n∑i=0

ai = 1 + a1 + a2 + · · ·+ an =1− an+1

1− a,

provided that a 6= 1.

I If |a| < 1, we can take the limit as n→∞:

∞∑i=0

ai = 1 + a1 + a2 + · · · =1

1− a,

I Special case of infinite geometric sum:

∞∑i=0

1

2i= 1 +

1

2+

1

4+

1

8+ · · · = 2.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 153: Analysis of Algorithms

1-33

Infinite Geometric sum

I From the previous slide:

n∑i=0

ai = 1 + a1 + a2 + · · ·+ an =1− an+1

1− a,

provided that a 6= 1.

I If |a| < 1, we can take the limit as n→∞:

∞∑i=0

ai = 1 + a1 + a2 + · · · =1

1− a,

I Special case of infinite geometric sum:

∞∑i=0

1

2i= 1 +

1

2+

1

4+

1

8+ · · · = 2.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 154: Analysis of Algorithms

1-34

Other Summations

I Sum of first n integers

n∑i=1

i = 1 + 2 + 3 + · · ·+ n =n(n + 1)

2= Θ

(n2)

I Sum of first n squares

n∑i=1

i2 = 1 + 4 + 9 + · · ·+ n2 =n(n + 1)(2n + 1)

6= Θ

(n3)

I In general, for any fixed positive integer k:

n∑i=1

ik = 1 + 2k + 3k + · · ·+ nk = Θ(nk+1

)

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 155: Analysis of Algorithms

1-34

Other Summations

I Sum of first n integers

n∑i=1

i = 1 + 2 + 3 + · · ·+ n =n(n + 1)

2= Θ

(n2)

I Sum of first n squares

n∑i=1

i2 = 1 + 4 + 9 + · · ·+ n2 =n(n + 1)(2n + 1)

6= Θ

(n3)

I In general, for any fixed positive integer k:

n∑i=1

ik = 1 + 2k + 3k + · · ·+ nk = Θ(nk+1

)

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 156: Analysis of Algorithms

1-34

Other Summations

I Sum of first n integers

n∑i=1

i = 1 + 2 + 3 + · · ·+ n =n(n + 1)

2= Θ

(n2)

I Sum of first n squares

n∑i=1

i2 = 1 + 4 + 9 + · · ·+ n2 =n(n + 1)(2n + 1)

6= Θ

(n3)

I In general, for any fixed positive integer k:

n∑i=1

ik = 1 + 2k + 3k + · · ·+ nk = Θ(nk+1

)

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 157: Analysis of Algorithms

1-34

Other Summations

I Sum of first n integers

n∑i=1

i = 1 + 2 + 3 + · · ·+ n =n(n + 1)

2= Θ

(n2)

I Sum of first n squares

n∑i=1

i2 = 1 + 4 + 9 + · · ·+ n2 =n(n + 1)(2n + 1)

6= Θ

(n3)

I In general, for any fixed positive integer k:

n∑i=1

ik = 1 + 2k + 3k + · · ·+ nk = Θ(nk+1

)

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 158: Analysis of Algorithms

1-34

Other Summations

I Sum of first n integers

n∑i=1

i = 1 + 2 + 3 + · · ·+ n =n(n + 1)

2= Θ

(n2)

I Sum of first n squares

n∑i=1

i2 = 1 + 4 + 9 + · · ·+ n2 =n(n + 1)(2n + 1)

6= Θ

(n3)

I In general, for any fixed positive integer k:

n∑i=1

ik = 1 + 2k + 3k + · · ·+ nk = Θ(nk+1

)

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 159: Analysis of Algorithms

1-34

Other Summations

I Sum of first n integers

n∑i=1

i = 1 + 2 + 3 + · · ·+ n =n(n + 1)

2= Θ

(n2)

I Sum of first n squares

n∑i=1

i2 = 1 + 4 + 9 + · · ·+ n2 =n(n + 1)(2n + 1)

6= Θ

(n3)

I In general, for any fixed positive integer k:

n∑i=1

ik = 1 + 2k + 3k + · · ·+ nk = Θ(nk+1

)

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 160: Analysis of Algorithms

1-34

Other Summations

I Sum of first n integers

n∑i=1

i = 1 + 2 + 3 + · · ·+ n =n(n + 1)

2= Θ

(n2)

I Sum of first n squares

n∑i=1

i2 = 1 + 4 + 9 + · · ·+ n2 =n(n + 1)(2n + 1)

6= Θ

(n3)

I In general, for any fixed positive integer k:

n∑i=1

ik = 1 + 2k + 3k + · · ·+ nk = Θ(nk+1

)CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 161: Analysis of Algorithms

1-35

Logarithms

Definition: logb x = y if and only if by = x .

Some useful properties:

1. logb 1 = 0.

2. logb ba = a.

3. logb(xy) = logb x + logb y .

4. logb(xa) = a logb x .

5. x logb y = y logb x .

6. logx b = 1logb x

.

7. loga x = logb xlogb a

.

8. loga x = (logb x)(loga b).

Exercise: Prove the above properties.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 162: Analysis of Algorithms

1-35

Logarithms

Definition: logb x = y if and only if by = x .

Some useful properties:

1. logb 1 = 0.

2. logb ba = a.

3. logb(xy) = logb x + logb y .

4. logb(xa) = a logb x .

5. x logb y = y logb x .

6. logx b = 1logb x

.

7. loga x = logb xlogb a

.

8. loga x = (logb x)(loga b).

Exercise: Prove the above properties.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 163: Analysis of Algorithms

1-35

Logarithms

Definition: logb x = y if and only if by = x .

Some useful properties:

1. logb 1 = 0.

2. logb ba = a.

3. logb(xy) = logb x + logb y .

4. logb(xa) = a logb x .

5. x logb y = y logb x .

6. logx b = 1logb x

.

7. loga x = logb xlogb a

.

8. loga x = (logb x)(loga b).

Exercise: Prove the above properties.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 164: Analysis of Algorithms

1-35

Logarithms

Definition: logb x = y if and only if by = x .

Some useful properties:

1. logb 1 = 0.

2. logb ba = a.

3. logb(xy) = logb x + logb y .

4. logb(xa) = a logb x .

5. x logb y = y logb x .

6. logx b = 1logb x

.

7. loga x = logb xlogb a

.

8. loga x = (logb x)(loga b).

Exercise: Prove the above properties.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 165: Analysis of Algorithms

1-35

Logarithms

Definition: logb x = y if and only if by = x .

Some useful properties:

1. logb 1 = 0.

2. logb ba = a.

3. logb(xy) = logb x + logb y .

4. logb(xa) = a logb x .

5. x logb y = y logb x .

6. logx b = 1logb x

.

7. loga x = logb xlogb a

.

8. loga x = (logb x)(loga b).

Exercise: Prove the above properties.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 166: Analysis of Algorithms

1-36

Logarithms

Example (#2): Prove logb ba = a.

Solution: Let y = logb ba

by = ba [by definition of log]

y = a

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 167: Analysis of Algorithms

1-36

Logarithms

Example (#2): Prove logb ba = a.

Solution: Let y = logb ba

by = ba [by definition of log]

y = a

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 168: Analysis of Algorithms

1-36

Logarithms

Example (#2): Prove logb ba = a.

Solution: Let y = logb ba

by = ba [by definition of log]

y = a

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 169: Analysis of Algorithms

1-36

Logarithms

Example (#2): Prove logb ba = a.

Solution: Let y = logb ba

by = ba [by definition of log]

y = a

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 170: Analysis of Algorithms

1-36

Logarithms

Example (#2): Prove logb ba = a.

Solution: Let y = logb ba

by = ba [by definition of log]

y = a

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 171: Analysis of Algorithms

1-37

Logarithms

Special Notations:

I ln x = loge x (e = 2.71828 . . .)

I lg x = log2 x

Some conversions (from Rules #7 and #8 on previous slides):

I ln x = (log2 x)(loge 2) = 0.693 lg x .

I lg x = loge xloge 2

= ln x0.693 = 1.44 ln x .

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 172: Analysis of Algorithms

1-37

Logarithms

Special Notations:

I ln x = loge x (e = 2.71828 . . .)

I lg x = log2 x

Some conversions (from Rules #7 and #8 on previous slides):

I ln x = (log2 x)(loge 2) = 0.693 lg x .

I lg x = loge xloge 2

= ln x0.693 = 1.44 ln x .

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 173: Analysis of Algorithms

1-37

Logarithms

Special Notations:

I ln x = loge x (e = 2.71828 . . .)

I lg x = log2 x

Some conversions (from Rules #7 and #8 on previous slides):

I ln x = (log2 x)(loge 2) = 0.693 lg x .

I lg x = loge xloge 2

= ln x0.693 = 1.44 ln x .

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 174: Analysis of Algorithms

1-37

Logarithms

Special Notations:

I ln x = loge x (e = 2.71828 . . .)

I lg x = log2 x

Some conversions (from Rules #7 and #8 on previous slides):

I ln x = (log2 x)(loge 2) = 0.693 lg x .

I lg x = loge xloge 2

= ln x0.693 = 1.44 ln x .

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 175: Analysis of Algorithms

1-37

Logarithms

Special Notations:

I ln x = loge x (e = 2.71828 . . .)

I lg x = log2 x

Some conversions (from Rules #7 and #8 on previous slides):

I ln x = (log2 x)(loge 2) = 0.693 lg x .

I lg x = loge xloge 2

= ln x0.693 = 1.44 ln x .

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 176: Analysis of Algorithms

1-37

Logarithms

Special Notations:

I ln x = loge x (e = 2.71828 . . .)

I lg x = log2 x

Some conversions (from Rules #7 and #8 on previous slides):

I ln x = (log2 x)(loge 2) = 0.693 lg x .

I lg x = loge xloge 2

= ln x0.693 = 1.44 ln x .

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 177: Analysis of Algorithms

1-38

Floors and ceilings

I bxc = largest integer ≤ x . (Read as Floor of x)

I dxe= smallest integer ≥ x (Read as Ceiling of x)

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 178: Analysis of Algorithms

1-38

Floors and ceilings

I bxc = largest integer ≤ x . (Read as Floor of x)

I dxe= smallest integer ≥ x (Read as Ceiling of x)

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 179: Analysis of Algorithms

1-38

Floors and ceilings

I bxc = largest integer ≤ x . (Read as Floor of x)

I dxe= smallest integer ≥ x (Read as Ceiling of x)

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 180: Analysis of Algorithms

1-39

Factorials

I n! = 1 · 2 · · · n

I n! represents the number of distinct permutations of n objects.

1 2 3

1 3 2

2 1 3

2 3 1

3 1 2

3 2 1

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 181: Analysis of Algorithms

1-39

Factorials

I n! = 1 · 2 · · · nI n! represents the number of distinct permutations of n objects.

1 2 3

1 3 2

2 1 3

2 3 1

3 1 2

3 2 1

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 182: Analysis of Algorithms

1-39

Factorials

I n! = 1 · 2 · · · nI n! represents the number of distinct permutations of n objects.

1 2 3

1 3 2

2 1 3

2 3 1

3 1 2

3 2 1

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 183: Analysis of Algorithms

1-40

Combinations

(nk

)= The number of different ways of choosing k objects

from a collection of n objects. (Pronounced “n choosek”.)

Example:(52

)= 10

1, 2 1, 3 1, 4 1, 5 2, 32, 4 2, 5 3, 4 3, 5 4, 5

Formula:(nk

)= n!

k!(n−k)!

Special cases:(n0

)= 1,

(n1

)= n,

(n2

)= n(n−1)

2

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 184: Analysis of Algorithms

1-40

Combinations

(nk

)= The number of different ways of choosing k objects

from a collection of n objects. (Pronounced “n choosek”.)

Example:(52

)= 10

1, 2 1, 3 1, 4 1, 5 2, 32, 4 2, 5 3, 4 3, 5 4, 5

Formula:(nk

)= n!

k!(n−k)!

Special cases:(n0

)= 1,

(n1

)= n,

(n2

)= n(n−1)

2

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 185: Analysis of Algorithms

1-40

Combinations

(nk

)= The number of different ways of choosing k objects

from a collection of n objects. (Pronounced “n choosek”.)

Example:(52

)= 10

1, 2 1, 3 1, 4 1, 5 2, 32, 4 2, 5 3, 4 3, 5 4, 5

Formula:(nk

)= n!

k!(n−k)!

Special cases:(n0

)= 1,

(n1

)= n,

(n2

)= n(n−1)

2

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 186: Analysis of Algorithms

1-40

Combinations

(nk

)= The number of different ways of choosing k objects

from a collection of n objects. (Pronounced “n choosek”.)

Example:(52

)= 10

1, 2 1, 3 1, 4 1, 5 2, 32, 4 2, 5 3, 4 3, 5 4, 5

Formula:(nk

)= n!

k!(n−k)!

Special cases:(n0

)= 1,

(n1

)= n,

(n2

)= n(n−1)

2

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 187: Analysis of Algorithms

1-40

Combinations

(nk

)= The number of different ways of choosing k objects

from a collection of n objects. (Pronounced “n choosek”.)

Example:(52

)= 10

1, 2 1, 3 1, 4 1, 5 2, 32, 4 2, 5 3, 4 3, 5 4, 5

Formula:(nk

)= n!

k!(n−k)!

Special cases:(n0

)= 1,

(n1

)= n,

(n2

)= n(n−1)

2

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 188: Analysis of Algorithms

1-40

Combinations

(nk

)= The number of different ways of choosing k objects

from a collection of n objects. (Pronounced “n choosek”.)

Example:(52

)= 10

1, 2 1, 3 1, 4 1, 5 2, 32, 4 2, 5 3, 4 3, 5 4, 5

Formula:(nk

)= n!

k!(n−k)!

Special cases:(n0

)= 1,

(n1

)= n,

(n2

)= n(n−1)

2

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 189: Analysis of Algorithms

1-41

Harmonic Numbers

The nth Harmonic number is the sum:

Hn =n∑

i=1

1

i

These numbers go to infinity:

limn→∞

Hn =∞∑i=1

1

i=∞

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 190: Analysis of Algorithms

1-41

Harmonic Numbers

The nth Harmonic number is the sum:

Hn =n∑

i=1

1

i

These numbers go to infinity:

limn→∞

Hn =∞∑i=1

1

i=∞

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 191: Analysis of Algorithms

1-41

Harmonic Numbers

The nth Harmonic number is the sum:

Hn =n∑

i=1

1

i

These numbers go to infinity:

limn→∞

Hn =∞∑i=1

1

i=∞

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 192: Analysis of Algorithms

1-42

Harmonic NumbersThe harmonic numbers are closely related to logs.

Recall:

ln x =

∫ x

1

1

tdt

1 x

y = 1x

We will show that Hn = Θ(log n).

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 193: Analysis of Algorithms

1-42

Harmonic NumbersThe harmonic numbers are closely related to logs. Recall:

ln x =

∫ x

1

1

tdt

1 x

y = 1x

We will show that Hn = Θ(log n).

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 194: Analysis of Algorithms

1-42

Harmonic NumbersThe harmonic numbers are closely related to logs. Recall:

ln x =

∫ x

1

1

tdt

1 x

y = 1x

We will show that Hn = Θ(log n).

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 195: Analysis of Algorithms

1-42

Harmonic NumbersThe harmonic numbers are closely related to logs. Recall:

ln x =

∫ x

1

1

tdt

1 x

y = 1x

We will show that Hn = Θ(log n).CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 196: Analysis of Algorithms

1-43

Harmonic Numbers

y = 1x

1 2 3 4 5

12 + 1

3 + . . .+ 1n < ln n < 1 + 1

2 + . . .+ 1n−1

Hn − 1 < ln n < Hn − 1n

Hence ln n + 1n < Hn < ln n + 1, so Hn = Θ(log n).

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 197: Analysis of Algorithms

1-43

Harmonic Numbers

y = 1x

1 2 3 4 5

12 + 1

3 + . . .+ 1n < ln n < 1 + 1

2 + . . .+ 1n−1

Hn − 1 < ln n < Hn − 1n

Hence ln n + 1n < Hn < ln n + 1, so Hn = Θ(log n).

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 198: Analysis of Algorithms

1-43

Harmonic Numbers

y = 1x

1 2 3 4 5

12 + 1

3 + . . .+ 1n < ln n < 1 + 1

2 + . . .+ 1n−1

Hn − 1 < ln n < Hn − 1n

Hence ln n + 1n < Hn < ln n + 1, so Hn = Θ(log n).

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 199: Analysis of Algorithms

1-43

Harmonic Numbers

y = 1x

1 2 3 4 5

12 + 1

3 + . . .+ 1n < ln n < 1 + 1

2 + . . .+ 1n−1

Hn − 1 < ln n < Hn − 1n

Hence ln n + 1n < Hn < ln n + 1, so Hn = Θ(log n).

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 200: Analysis of Algorithms

1-43

Harmonic Numbers

y = 1x

1 2 3 4 5

12 + 1

3 + . . .+ 1n < ln n < 1 + 1

2 + . . .+ 1n−1

Hn − 1 < ln n < Hn − 1n

Hence ln n + 1n < Hn < ln n + 1, so Hn = Θ(log n).

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 201: Analysis of Algorithms

1-44

Proof/Justification Techniques

I Proof by Example Can be used to proveI A statement of the form “There exists. . . ” is true.I A statement of the form “For all. . . ” is false.I A statement of the form “If P then Q” is false.

I Illustration: Consider the statement:

All numbers of the form 2k − 1 are prime.

This statement is False: 24 − 1 = 15 = 3 · 5I Note: The statement can be rewritten as:

If n is an integer of the form 2k − 1, then n is prime.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 202: Analysis of Algorithms

1-44

Proof/Justification Techniques

I Proof by Example Can be used to prove

I A statement of the form “There exists. . . ” is true.I A statement of the form “For all. . . ” is false.I A statement of the form “If P then Q” is false.

I Illustration: Consider the statement:

All numbers of the form 2k − 1 are prime.

This statement is False: 24 − 1 = 15 = 3 · 5I Note: The statement can be rewritten as:

If n is an integer of the form 2k − 1, then n is prime.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 203: Analysis of Algorithms

1-44

Proof/Justification Techniques

I Proof by Example Can be used to proveI A statement of the form “There exists. . . ” is true.

I A statement of the form “For all. . . ” is false.I A statement of the form “If P then Q” is false.

I Illustration: Consider the statement:

All numbers of the form 2k − 1 are prime.

This statement is False: 24 − 1 = 15 = 3 · 5I Note: The statement can be rewritten as:

If n is an integer of the form 2k − 1, then n is prime.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 204: Analysis of Algorithms

1-44

Proof/Justification Techniques

I Proof by Example Can be used to proveI A statement of the form “There exists. . . ” is true.I A statement of the form “For all. . . ” is false.

I A statement of the form “If P then Q” is false.

I Illustration: Consider the statement:

All numbers of the form 2k − 1 are prime.

This statement is False: 24 − 1 = 15 = 3 · 5I Note: The statement can be rewritten as:

If n is an integer of the form 2k − 1, then n is prime.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 205: Analysis of Algorithms

1-44

Proof/Justification Techniques

I Proof by Example Can be used to proveI A statement of the form “There exists. . . ” is true.I A statement of the form “For all. . . ” is false.I A statement of the form “If P then Q” is false.

I Illustration: Consider the statement:

All numbers of the form 2k − 1 are prime.

This statement is False: 24 − 1 = 15 = 3 · 5I Note: The statement can be rewritten as:

If n is an integer of the form 2k − 1, then n is prime.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 206: Analysis of Algorithms

1-44

Proof/Justification Techniques

I Proof by Example Can be used to proveI A statement of the form “There exists. . . ” is true.I A statement of the form “For all. . . ” is false.I A statement of the form “If P then Q” is false.

I Illustration:

Consider the statement:

All numbers of the form 2k − 1 are prime.

This statement is False: 24 − 1 = 15 = 3 · 5I Note: The statement can be rewritten as:

If n is an integer of the form 2k − 1, then n is prime.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 207: Analysis of Algorithms

1-44

Proof/Justification Techniques

I Proof by Example Can be used to proveI A statement of the form “There exists. . . ” is true.I A statement of the form “For all. . . ” is false.I A statement of the form “If P then Q” is false.

I Illustration: Consider the statement:

All numbers of the form 2k − 1 are prime.

This statement is False: 24 − 1 = 15 = 3 · 5I Note: The statement can be rewritten as:

If n is an integer of the form 2k − 1, then n is prime.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 208: Analysis of Algorithms

1-44

Proof/Justification Techniques

I Proof by Example Can be used to proveI A statement of the form “There exists. . . ” is true.I A statement of the form “For all. . . ” is false.I A statement of the form “If P then Q” is false.

I Illustration: Consider the statement:

All numbers of the form 2k − 1 are prime.

This statement is False: 24 − 1 = 15 = 3 · 5

I Note: The statement can be rewritten as:

If n is an integer of the form 2k − 1, then n is prime.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 209: Analysis of Algorithms

1-44

Proof/Justification Techniques

I Proof by Example Can be used to proveI A statement of the form “There exists. . . ” is true.I A statement of the form “For all. . . ” is false.I A statement of the form “If P then Q” is false.

I Illustration: Consider the statement:

All numbers of the form 2k − 1 are prime.

This statement is False: 24 − 1 = 15 = 3 · 5I Note: The statement can be rewritten as:

If n is an integer of the form 2k − 1, then n is prime.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 210: Analysis of Algorithms

1-45

Proof/Justification Techniques

I Suppose we want to prove a statement of the form “If Pthen Q” is true.

There are three approaches:

1. Direct proof: Assume P is true. Show that Q must be true.2. Indirect proof: Assume Q is false. Show that P must be

false.This is also known as a proof by contraposition.3. Proof by contradiction: Assume P is true and Q is false. Show

that there is a contradiction.

See [GT] Section 1.3.3 for examples.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 211: Analysis of Algorithms

1-45

Proof/Justification Techniques

I Suppose we want to prove a statement of the form “If Pthen Q” is true.There are three approaches:

1. Direct proof: Assume P is true. Show that Q must be true.2. Indirect proof: Assume Q is false. Show that P must be

false.This is also known as a proof by contraposition.3. Proof by contradiction: Assume P is true and Q is false. Show

that there is a contradiction.

See [GT] Section 1.3.3 for examples.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 212: Analysis of Algorithms

1-45

Proof/Justification Techniques

I Suppose we want to prove a statement of the form “If Pthen Q” is true.There are three approaches:

1. Direct proof: Assume P is true. Show that Q must be true.

2. Indirect proof: Assume Q is false. Show that P must befalse.This is also known as a proof by contraposition.

3. Proof by contradiction: Assume P is true and Q is false. Showthat there is a contradiction.

See [GT] Section 1.3.3 for examples.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 213: Analysis of Algorithms

1-45

Proof/Justification Techniques

I Suppose we want to prove a statement of the form “If Pthen Q” is true.There are three approaches:

1. Direct proof: Assume P is true. Show that Q must be true.2. Indirect proof: Assume Q is false. Show that P must be

false.

This is also known as a proof by contraposition.3. Proof by contradiction: Assume P is true and Q is false. Show

that there is a contradiction.

See [GT] Section 1.3.3 for examples.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 214: Analysis of Algorithms

1-45

Proof/Justification Techniques

I Suppose we want to prove a statement of the form “If Pthen Q” is true.There are three approaches:

1. Direct proof: Assume P is true. Show that Q must be true.2. Indirect proof: Assume Q is false. Show that P must be

false.This is also known as a proof by contraposition.3. Proof by contradiction: Assume P is true and Q is false. Show

that there is a contradiction.

See [GT] Section 1.3.3 for examples.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 215: Analysis of Algorithms

1-45

Proof/Justification Techniques

I Suppose we want to prove a statement of the form “If Pthen Q” is true.There are three approaches:

1. Direct proof: Assume P is true. Show that Q must be true.2. Indirect proof: Assume Q is false. Show that P must be

false.This is also known as a proof by contraposition.3. Proof by contradiction: Assume P is true and Q is false. Show

that there is a contradiction.

See [GT] Section 1.3.3 for examples.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 216: Analysis of Algorithms

1-46

Proof/Justification Techniques:Induction

I A technique for proving theorems about the positive (ornonnegative) integers.

I Let P(n) be a statement with an integer parameter, n.Mathematical induction is a technique for proving that P(n) istrue for all integers ≥ some base value b.

I Usually, the base value is 0 or 1.I To show P(n) holds for all n ≥ b, we must show two things:

1. Base Case: P(b) is true (where b is the base value).2. Inductive step: If P(k) is true, then P(k + 1) is true.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 217: Analysis of Algorithms

1-46

Proof/Justification Techniques:Induction

I A technique for proving theorems about the positive (ornonnegative) integers.

I Let P(n) be a statement with an integer parameter, n.Mathematical induction is a technique for proving that P(n) istrue for all integers ≥ some base value b.

I Usually, the base value is 0 or 1.I To show P(n) holds for all n ≥ b, we must show two things:

1. Base Case: P(b) is true (where b is the base value).2. Inductive step: If P(k) is true, then P(k + 1) is true.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 218: Analysis of Algorithms

1-46

Proof/Justification Techniques:Induction

I A technique for proving theorems about the positive (ornonnegative) integers.

I Let P(n) be a statement with an integer parameter, n.Mathematical induction is a technique for proving that P(n) istrue for all integers ≥ some base value b.

I Usually, the base value is 0 or 1.I To show P(n) holds for all n ≥ b, we must show two things:

1. Base Case: P(b) is true (where b is the base value).2. Inductive step: If P(k) is true, then P(k + 1) is true.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 219: Analysis of Algorithms

1-46

Proof/Justification Techniques:Induction

I A technique for proving theorems about the positive (ornonnegative) integers.

I Let P(n) be a statement with an integer parameter, n.Mathematical induction is a technique for proving that P(n) istrue for all integers ≥ some base value b.

I Usually, the base value is 0 or 1.

I To show P(n) holds for all n ≥ b, we must show two things:

1. Base Case: P(b) is true (where b is the base value).2. Inductive step: If P(k) is true, then P(k + 1) is true.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 220: Analysis of Algorithms

1-46

Proof/Justification Techniques:Induction

I A technique for proving theorems about the positive (ornonnegative) integers.

I Let P(n) be a statement with an integer parameter, n.Mathematical induction is a technique for proving that P(n) istrue for all integers ≥ some base value b.

I Usually, the base value is 0 or 1.I To show P(n) holds for all n ≥ b, we must show two things:

1. Base Case: P(b) is true (where b is the base value).2. Inductive step: If P(k) is true, then P(k + 1) is true.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 221: Analysis of Algorithms

1-46

Proof/Justification Techniques:Induction

I A technique for proving theorems about the positive (ornonnegative) integers.

I Let P(n) be a statement with an integer parameter, n.Mathematical induction is a technique for proving that P(n) istrue for all integers ≥ some base value b.

I Usually, the base value is 0 or 1.I To show P(n) holds for all n ≥ b, we must show two things:

1. Base Case: P(b) is true (where b is the base value).

2. Inductive step: If P(k) is true, then P(k + 1) is true.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 222: Analysis of Algorithms

1-46

Proof/Justification Techniques:Induction

I A technique for proving theorems about the positive (ornonnegative) integers.

I Let P(n) be a statement with an integer parameter, n.Mathematical induction is a technique for proving that P(n) istrue for all integers ≥ some base value b.

I Usually, the base value is 0 or 1.I To show P(n) holds for all n ≥ b, we must show two things:

1. Base Case: P(b) is true (where b is the base value).2. Inductive step: If P(k) is true, then P(k + 1) is true.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 223: Analysis of Algorithms

1-47

Induction Example

Example: Show that for all n ≥ 1

n∑i=1

i · 2i = (n − 1) · 2(n+1) + 2

Base Case: (n = 1)

LHS =1∑

i=1

i · 2i = 1 · 21 = 2.

RHS = (1− 1) · 21+1 + 2 = 0 + 2 = 2.

LHS = RHS X

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 224: Analysis of Algorithms

1-47

Induction Example

Example: Show that for all n ≥ 1

n∑i=1

i · 2i = (n − 1) · 2(n+1) + 2

Base Case: (n = 1)

LHS =1∑

i=1

i · 2i = 1 · 21 = 2.

RHS = (1− 1) · 21+1 + 2 = 0 + 2 = 2.

LHS = RHS X

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 225: Analysis of Algorithms

1-47

Induction Example

Example: Show that for all n ≥ 1

n∑i=1

i · 2i = (n − 1) · 2(n+1) + 2

Base Case: (n = 1)

LHS

=1∑

i=1

i · 2i = 1 · 21 = 2.

RHS = (1− 1) · 21+1 + 2 = 0 + 2 = 2.

LHS = RHS X

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 226: Analysis of Algorithms

1-47

Induction Example

Example: Show that for all n ≥ 1

n∑i=1

i · 2i = (n − 1) · 2(n+1) + 2

Base Case: (n = 1)

LHS =1∑

i=1

i · 2i = 1 · 21 = 2.

RHS = (1− 1) · 21+1 + 2 = 0 + 2 = 2.

LHS = RHS X

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 227: Analysis of Algorithms

1-47

Induction Example

Example: Show that for all n ≥ 1

n∑i=1

i · 2i = (n − 1) · 2(n+1) + 2

Base Case: (n = 1)

LHS =1∑

i=1

i · 2i = 1 · 21 = 2.

RHS

= (1− 1) · 21+1 + 2 = 0 + 2 = 2.

LHS = RHS X

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 228: Analysis of Algorithms

1-47

Induction Example

Example: Show that for all n ≥ 1

n∑i=1

i · 2i = (n − 1) · 2(n+1) + 2

Base Case: (n = 1)

LHS =1∑

i=1

i · 2i = 1 · 21 = 2.

RHS = (1− 1) · 21+1 + 2 = 0 + 2 = 2.

LHS = RHS X

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 229: Analysis of Algorithms

1-47

Induction Example

Example: Show that for all n ≥ 1

n∑i=1

i · 2i = (n − 1) · 2(n+1) + 2

Base Case: (n = 1)

LHS =1∑

i=1

i · 2i = 1 · 21 = 2.

RHS = (1− 1) · 21+1 + 2 = 0 + 2 = 2.

LHS

= RHS X

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 230: Analysis of Algorithms

1-47

Induction Example

Example: Show that for all n ≥ 1

n∑i=1

i · 2i = (n − 1) · 2(n+1) + 2

Base Case: (n = 1)

LHS =1∑

i=1

i · 2i = 1 · 21 = 2.

RHS = (1− 1) · 21+1 + 2 = 0 + 2 = 2.

LHS = RHS X

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 231: Analysis of Algorithms

1-48

Induction Example, continued

Inductive Step:

Assume P(k) is true:

k∑i=1

i · 2i = (k − 1) · 2(k+1) + 2.

Show P(k + 1) is true:

k+1∑i=1

i · 2i = k · 2(k+2) + 2.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 232: Analysis of Algorithms

1-48

Induction Example, continued

Inductive Step:

Assume P(k) is true:

k∑i=1

i · 2i = (k − 1) · 2(k+1) + 2.

Show P(k + 1) is true:

k+1∑i=1

i · 2i = k · 2(k+2) + 2.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 233: Analysis of Algorithms

1-48

Induction Example, continued

Inductive Step:

Assume P(k) is true:

k∑i=1

i · 2i = (k − 1) · 2(k+1) + 2.

Show P(k + 1) is true:

k+1∑i=1

i · 2i = k · 2(k+2) + 2.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 234: Analysis of Algorithms

1-49

Induction Example, continued

Assume:k∑

i=1

i · 2i = (k − 1) · 2(k+1) + 2.

Show:k+1∑i=1

i · 2i = k · 2(k+2) + 2.

k+1∑i=1

i · 2i =k∑

i=1

i · 2i + (k + 1) · 2(k+1)

= (k − 1) · 2(k+1) + 2 + (k + 1) · 2(k+1)

= 2k · 2(k+1) + 2

= k · 2(k+2) + 2 QED

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 235: Analysis of Algorithms

1-49

Induction Example, continued

Assume:k∑

i=1

i · 2i = (k − 1) · 2(k+1) + 2.

Show:k+1∑i=1

i · 2i = k · 2(k+2) + 2.

k+1∑i=1

i · 2i

=k∑

i=1

i · 2i + (k + 1) · 2(k+1)

= (k − 1) · 2(k+1) + 2 + (k + 1) · 2(k+1)

= 2k · 2(k+1) + 2

= k · 2(k+2) + 2 QED

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 236: Analysis of Algorithms

1-49

Induction Example, continued

Assume:k∑

i=1

i · 2i = (k − 1) · 2(k+1) + 2.

Show:k+1∑i=1

i · 2i = k · 2(k+2) + 2.

k+1∑i=1

i · 2i =k∑

i=1

i · 2i + (k + 1) · 2(k+1)

= (k − 1) · 2(k+1) + 2 + (k + 1) · 2(k+1)

= 2k · 2(k+1) + 2

= k · 2(k+2) + 2 QED

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 237: Analysis of Algorithms

1-49

Induction Example, continued

Assume:k∑

i=1

i · 2i = (k − 1) · 2(k+1) + 2.

Show:k+1∑i=1

i · 2i = k · 2(k+2) + 2.

k+1∑i=1

i · 2i =k∑

i=1

i · 2i + (k + 1) · 2(k+1)

= (k − 1) · 2(k+1) + 2 + (k + 1) · 2(k+1)

= 2k · 2(k+1) + 2

= k · 2(k+2) + 2 QED

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 238: Analysis of Algorithms

1-49

Induction Example, continued

Assume:k∑

i=1

i · 2i = (k − 1) · 2(k+1) + 2.

Show:k+1∑i=1

i · 2i = k · 2(k+2) + 2.

k+1∑i=1

i · 2i =k∑

i=1

i · 2i + (k + 1) · 2(k+1)

= (k − 1) · 2(k+1) + 2 + (k + 1) · 2(k+1)

= 2k · 2(k+1) + 2

= k · 2(k+2) + 2 QED

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 239: Analysis of Algorithms

1-49

Induction Example, continued

Assume:k∑

i=1

i · 2i = (k − 1) · 2(k+1) + 2.

Show:k+1∑i=1

i · 2i = k · 2(k+2) + 2.

k+1∑i=1

i · 2i =k∑

i=1

i · 2i + (k + 1) · 2(k+1)

= (k − 1) · 2(k+1) + 2 + (k + 1) · 2(k+1)

= 2k · 2(k+1) + 2

= k · 2(k+2) + 2

QED

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 240: Analysis of Algorithms

1-49

Induction Example, continued

Assume:k∑

i=1

i · 2i = (k − 1) · 2(k+1) + 2.

Show:k+1∑i=1

i · 2i = k · 2(k+2) + 2.

k+1∑i=1

i · 2i =k∑

i=1

i · 2i + (k + 1) · 2(k+1)

= (k − 1) · 2(k+1) + 2 + (k + 1) · 2(k+1)

= 2k · 2(k+1) + 2

= k · 2(k+2) + 2 QED

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 241: Analysis of Algorithms

1-50

Probability

I Defined in terms of a sample space, S .

I Sample space consists of a finite set of outcomes, also calledelementary events.

I An event is a subset of the sample space. (So an event is aset of outcomes).

I Sample space can be infinite, even uncountable. In thiscourse, it will generally be finite.

Example: (2-coin example.) Flip two coins.

I Sample space S = HH, HT, TH, TT.I The event “first coin is heads” is the subset HH, HT.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 242: Analysis of Algorithms

1-50

Probability

I Defined in terms of a sample space, S .

I Sample space consists of a finite set of outcomes, also calledelementary events.

I An event is a subset of the sample space. (So an event is aset of outcomes).

I Sample space can be infinite, even uncountable. In thiscourse, it will generally be finite.

Example: (2-coin example.) Flip two coins.

I Sample space S = HH, HT, TH, TT.I The event “first coin is heads” is the subset HH, HT.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 243: Analysis of Algorithms

1-50

Probability

I Defined in terms of a sample space, S .

I Sample space consists of a finite set of outcomes, also calledelementary events.

I An event is a subset of the sample space. (So an event is aset of outcomes).

I Sample space can be infinite, even uncountable. In thiscourse, it will generally be finite.

Example: (2-coin example.) Flip two coins.

I Sample space S = HH, HT, TH, TT.I The event “first coin is heads” is the subset HH, HT.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 244: Analysis of Algorithms

1-50

Probability

I Defined in terms of a sample space, S .

I Sample space consists of a finite set of outcomes, also calledelementary events.

I An event is a subset of the sample space. (So an event is aset of outcomes).

I Sample space can be infinite, even uncountable. In thiscourse, it will generally be finite.

Example: (2-coin example.) Flip two coins.

I Sample space S = HH, HT, TH, TT.I The event “first coin is heads” is the subset HH, HT.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 245: Analysis of Algorithms

1-50

Probability

I Defined in terms of a sample space, S .

I Sample space consists of a finite set of outcomes, also calledelementary events.

I An event is a subset of the sample space. (So an event is aset of outcomes).

I Sample space can be infinite, even uncountable. In thiscourse, it will generally be finite.

Example: (2-coin example.) Flip two coins.

I Sample space S = HH, HT, TH, TT.I The event “first coin is heads” is the subset HH, HT.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 246: Analysis of Algorithms

1-50

Probability

I Defined in terms of a sample space, S .

I Sample space consists of a finite set of outcomes, also calledelementary events.

I An event is a subset of the sample space. (So an event is aset of outcomes).

I Sample space can be infinite, even uncountable. In thiscourse, it will generally be finite.

Example: (2-coin example.) Flip two coins.

I Sample space S = HH, HT, TH, TT.I The event “first coin is heads” is the subset HH, HT.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 247: Analysis of Algorithms

1-50

Probability

I Defined in terms of a sample space, S .

I Sample space consists of a finite set of outcomes, also calledelementary events.

I An event is a subset of the sample space. (So an event is aset of outcomes).

I Sample space can be infinite, even uncountable. In thiscourse, it will generally be finite.

Example: (2-coin example.) Flip two coins.

I Sample space S = HH, HT, TH, TT.

I The event “first coin is heads” is the subset HH, HT.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 248: Analysis of Algorithms

1-50

Probability

I Defined in terms of a sample space, S .

I Sample space consists of a finite set of outcomes, also calledelementary events.

I An event is a subset of the sample space. (So an event is aset of outcomes).

I Sample space can be infinite, even uncountable. In thiscourse, it will generally be finite.

Example: (2-coin example.) Flip two coins.

I Sample space S = HH, HT, TH, TT.I The event “first coin is heads” is the subset HH, HT.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 249: Analysis of Algorithms

1-51

Probability function

I A probability function is a function P(·) that maps events(subsets of the sample space S) to real numbers such that:

1. P(∅) = 0.2. P(S) = 1.3. For every event A, 0 ≤ P(A) ≤ 1.4. If A,B ⊆ S and A ∩ B = ∅, then P(A ∪ B) = P(A) + P(B).

I Note: Property 4 implies that if A ⊆ B then P(A) ≤ P(B).

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 250: Analysis of Algorithms

1-51

Probability function

I A probability function is a function P(·) that maps events(subsets of the sample space S) to real numbers such that:

1. P(∅) = 0.2. P(S) = 1.3. For every event A, 0 ≤ P(A) ≤ 1.4. If A,B ⊆ S and A ∩ B = ∅, then P(A ∪ B) = P(A) + P(B).

I Note: Property 4 implies that if A ⊆ B then P(A) ≤ P(B).

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 251: Analysis of Algorithms

1-51

Probability function

I A probability function is a function P(·) that maps events(subsets of the sample space S) to real numbers such that:

1. P(∅) = 0.

2. P(S) = 1.3. For every event A, 0 ≤ P(A) ≤ 1.4. If A,B ⊆ S and A ∩ B = ∅, then P(A ∪ B) = P(A) + P(B).

I Note: Property 4 implies that if A ⊆ B then P(A) ≤ P(B).

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 252: Analysis of Algorithms

1-51

Probability function

I A probability function is a function P(·) that maps events(subsets of the sample space S) to real numbers such that:

1. P(∅) = 0.2. P(S) = 1.

3. For every event A, 0 ≤ P(A) ≤ 1.4. If A,B ⊆ S and A ∩ B = ∅, then P(A ∪ B) = P(A) + P(B).

I Note: Property 4 implies that if A ⊆ B then P(A) ≤ P(B).

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 253: Analysis of Algorithms

1-51

Probability function

I A probability function is a function P(·) that maps events(subsets of the sample space S) to real numbers such that:

1. P(∅) = 0.2. P(S) = 1.3. For every event A, 0 ≤ P(A) ≤ 1.

4. If A,B ⊆ S and A ∩ B = ∅, then P(A ∪ B) = P(A) + P(B).

I Note: Property 4 implies that if A ⊆ B then P(A) ≤ P(B).

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 254: Analysis of Algorithms

1-51

Probability function

I A probability function is a function P(·) that maps events(subsets of the sample space S) to real numbers such that:

1. P(∅) = 0.2. P(S) = 1.3. For every event A, 0 ≤ P(A) ≤ 1.4. If A,B ⊆ S and A ∩ B = ∅, then P(A ∪ B) = P(A) + P(B).

I Note: Property 4 implies that if A ⊆ B then P(A) ≤ P(B).

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 255: Analysis of Algorithms

1-51

Probability function

I A probability function is a function P(·) that maps events(subsets of the sample space S) to real numbers such that:

1. P(∅) = 0.2. P(S) = 1.3. For every event A, 0 ≤ P(A) ≤ 1.4. If A,B ⊆ S and A ∩ B = ∅, then P(A ∪ B) = P(A) + P(B).

I Note: Property 4 implies that if A ⊆ B then P(A) ≤ P(B).

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 256: Analysis of Algorithms

1-52

Probability function (continued)

For finite sample spaces, this can be simplified:

I Sample space S = s1, . . . , sk,I Each outcome Si is assigned a probability P(si ), with

k∑i=1

P(si ) = 1.

I The probability of an event E ⊆ S is:

P(E ) =∑si∈E

P(si ).

Example: (2-coin example, continued). Define

P(HH) = P(HT) = P(TH) = P(TT) =1

4.

Then

P(first coin is heads) = P(HH) + P(HT) =1

2.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 257: Analysis of Algorithms

1-52

Probability function (continued)For finite sample spaces, this can be simplified:

I Sample space S = s1, . . . , sk,I Each outcome Si is assigned a probability P(si ), with

k∑i=1

P(si ) = 1.

I The probability of an event E ⊆ S is:

P(E ) =∑si∈E

P(si ).

Example: (2-coin example, continued). Define

P(HH) = P(HT) = P(TH) = P(TT) =1

4.

Then

P(first coin is heads) = P(HH) + P(HT) =1

2.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 258: Analysis of Algorithms

1-52

Probability function (continued)For finite sample spaces, this can be simplified:

I Sample space S = s1, . . . , sk,

I Each outcome Si is assigned a probability P(si ), with

k∑i=1

P(si ) = 1.

I The probability of an event E ⊆ S is:

P(E ) =∑si∈E

P(si ).

Example: (2-coin example, continued). Define

P(HH) = P(HT) = P(TH) = P(TT) =1

4.

Then

P(first coin is heads) = P(HH) + P(HT) =1

2.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 259: Analysis of Algorithms

1-52

Probability function (continued)For finite sample spaces, this can be simplified:

I Sample space S = s1, . . . , sk,I Each outcome Si is assigned a probability P(si ), with

k∑i=1

P(si ) = 1.

I The probability of an event E ⊆ S is:

P(E ) =∑si∈E

P(si ).

Example: (2-coin example, continued). Define

P(HH) = P(HT) = P(TH) = P(TT) =1

4.

Then

P(first coin is heads) = P(HH) + P(HT) =1

2.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 260: Analysis of Algorithms

1-52

Probability function (continued)For finite sample spaces, this can be simplified:

I Sample space S = s1, . . . , sk,I Each outcome Si is assigned a probability P(si ), with

k∑i=1

P(si ) = 1.

I The probability of an event E ⊆ S is:

P(E ) =∑si∈E

P(si ).

Example: (2-coin example, continued). Define

P(HH) = P(HT) = P(TH) = P(TT) =1

4.

Then

P(first coin is heads) = P(HH) + P(HT) =1

2.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 261: Analysis of Algorithms

1-52

Probability function (continued)For finite sample spaces, this can be simplified:

I Sample space S = s1, . . . , sk,I Each outcome Si is assigned a probability P(si ), with

k∑i=1

P(si ) = 1.

I The probability of an event E ⊆ S is:

P(E ) =∑si∈E

P(si ).

Example: (2-coin example, continued). Define

P(HH) = P(HT) = P(TH) = P(TT) =1

4.

Then

P(first coin is heads) = P(HH) + P(HT) =1

2.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 262: Analysis of Algorithms

1-52

Probability function (continued)For finite sample spaces, this can be simplified:

I Sample space S = s1, . . . , sk,I Each outcome Si is assigned a probability P(si ), with

k∑i=1

P(si ) = 1.

I The probability of an event E ⊆ S is:

P(E ) =∑si∈E

P(si ).

Example: (2-coin example, continued). Define

P(HH) = P(HT) = P(TH) = P(TT) =1

4.

Then

P(first coin is heads) = P(HH) + P(HT) =1

2.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 263: Analysis of Algorithms

1-53

Random variables

I Intuitive definition: a random variable is a variable whosevalue depends on the outcome of some experiment.

I Formal definition: a random variable is a function that mapsoutcomes in a sample space S to real numbers.

I Special case: An Indicator variable is a random variable that isalways either 0 or 1.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 264: Analysis of Algorithms

1-53

Random variables

I Intuitive definition: a random variable is a variable whosevalue depends on the outcome of some experiment.

I Formal definition: a random variable is a function that mapsoutcomes in a sample space S to real numbers.

I Special case: An Indicator variable is a random variable that isalways either 0 or 1.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 265: Analysis of Algorithms

1-53

Random variables

I Intuitive definition: a random variable is a variable whosevalue depends on the outcome of some experiment.

I Formal definition: a random variable is a function that mapsoutcomes in a sample space S to real numbers.

I Special case: An Indicator variable is a random variable that isalways either 0 or 1.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 266: Analysis of Algorithms

1-53

Random variables

I Intuitive definition: a random variable is a variable whosevalue depends on the outcome of some experiment.

I Formal definition: a random variable is a function that mapsoutcomes in a sample space S to real numbers.

I Special case: An Indicator variable is a random variable that isalways either 0 or 1.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 267: Analysis of Algorithms

1-54

Expectation

I The expected value, or expectation, of a random variable Xrepresents its “average value”.

I Formally: Let X be a random variable with a finite set ofpossible values V = x1, . . . , xk. Then

E (X ) =∑x∈V

x · P(X = x).

Example: (2-coin example, continued). Let X be the number of heads whentwo coins are thrown. Then

E(X ) = 0 · P(X = 0) + 1 · P(X = 1) + 2 · P(X = 2)

= 0 ·(

1

4

)+ 1 ·

(1

2

)+ 2 ·

(1

4

)= 1

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 268: Analysis of Algorithms

1-54

Expectation

I The expected value, or expectation, of a random variable Xrepresents its “average value”.

I Formally: Let X be a random variable with a finite set ofpossible values V = x1, . . . , xk. Then

E (X ) =∑x∈V

x · P(X = x).

Example: (2-coin example, continued). Let X be the number of heads whentwo coins are thrown. Then

E(X ) = 0 · P(X = 0) + 1 · P(X = 1) + 2 · P(X = 2)

= 0 ·(

1

4

)+ 1 ·

(1

2

)+ 2 ·

(1

4

)= 1

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 269: Analysis of Algorithms

1-54

Expectation

I The expected value, or expectation, of a random variable Xrepresents its “average value”.

I Formally: Let X be a random variable with a finite set ofpossible values V = x1, . . . , xk. Then

E (X ) =∑x∈V

x · P(X = x).

Example: (2-coin example, continued). Let X be the number of heads whentwo coins are thrown. Then

E(X ) = 0 · P(X = 0) + 1 · P(X = 1) + 2 · P(X = 2)

= 0 ·(

1

4

)+ 1 ·

(1

2

)+ 2 ·

(1

4

)= 1

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 270: Analysis of Algorithms

1-54

Expectation

I The expected value, or expectation, of a random variable Xrepresents its “average value”.

I Formally: Let X be a random variable with a finite set ofpossible values V = x1, . . . , xk. Then

E (X ) =∑x∈V

x · P(X = x).

Example: (2-coin example, continued). Let X be the number of heads whentwo coins are thrown. Then

E(X ) = 0 · P(X = 0) + 1 · P(X = 1) + 2 · P(X = 2)

= 0 ·(

1

4

)+ 1 ·

(1

2

)+ 2 ·

(1

4

)= 1

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 271: Analysis of Algorithms

1-55

Expectation

Example: Throw a single six-sided die. Assume the die is fair, soeach possible throw has a probability of 1/6.

The expected value of the throw is:

1 · 1

6+ 2 · 1

6+ 3 · 1

6+ 4 · 1

6+ 5 · 1

6+ 6 · 1

6= 3.5

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 272: Analysis of Algorithms

1-55

Expectation

Example: Throw a single six-sided die. Assume the die is fair, soeach possible throw has a probability of 1/6.

The expected value of the throw is:

1 · 1

6+ 2 · 1

6+ 3 · 1

6+ 4 · 1

6+ 5 · 1

6+ 6 · 1

6= 3.5

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 273: Analysis of Algorithms

1-55

Expectation

Example: Throw a single six-sided die. Assume the die is fair, soeach possible throw has a probability of 1/6.

The expected value of the throw is:

1 · 1

6+ 2 · 1

6+ 3 · 1

6+ 4 · 1

6+ 5 · 1

6+ 6 · 1

6= 3.5

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 274: Analysis of Algorithms

1-56

Linearity of Expectation

I For any two random variables X and Y ,

E (X + Y ) = E (X ) + E (Y ).

I Proof: see [GT], 1.3.4

I Very useful, because usually it is easier to compute E (X ) andE (Y ) and apply the formula than to compute E (X + Y )directly.

Example 1: Throw two six-sided dice. Let X be the sum of the values. Then

E(X ) = E(X1 + X2) = E(X1) + E(X2) = 3.5 + 3.5 = 7,

where Xi is the value on die i (i = 1, 2).

Example 2: Throw 100 six-sided dice. Let Y be the sum of the values. Then

E(Y ) = 100 · 3.5 = 350.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 275: Analysis of Algorithms

1-56

Linearity of Expectation

I For any two random variables X and Y ,

E (X + Y ) = E (X ) + E (Y ).

I Proof: see [GT], 1.3.4

I Very useful, because usually it is easier to compute E (X ) andE (Y ) and apply the formula than to compute E (X + Y )directly.

Example 1: Throw two six-sided dice. Let X be the sum of the values. Then

E(X ) = E(X1 + X2) = E(X1) + E(X2) = 3.5 + 3.5 = 7,

where Xi is the value on die i (i = 1, 2).

Example 2: Throw 100 six-sided dice. Let Y be the sum of the values. Then

E(Y ) = 100 · 3.5 = 350.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 276: Analysis of Algorithms

1-56

Linearity of Expectation

I For any two random variables X and Y ,

E (X + Y ) = E (X ) + E (Y ).

I Proof: see [GT], 1.3.4

I Very useful, because usually it is easier to compute E (X ) andE (Y ) and apply the formula than to compute E (X + Y )directly.

Example 1: Throw two six-sided dice. Let X be the sum of the values. Then

E(X ) = E(X1 + X2) = E(X1) + E(X2) = 3.5 + 3.5 = 7,

where Xi is the value on die i (i = 1, 2).

Example 2: Throw 100 six-sided dice. Let Y be the sum of the values. Then

E(Y ) = 100 · 3.5 = 350.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 277: Analysis of Algorithms

1-56

Linearity of Expectation

I For any two random variables X and Y ,

E (X + Y ) = E (X ) + E (Y ).

I Proof: see [GT], 1.3.4

I Very useful, because usually it is easier to compute E (X ) andE (Y ) and apply the formula than to compute E (X + Y )directly.

Example 1: Throw two six-sided dice. Let X be the sum of the values. Then

E(X ) = E(X1 + X2) = E(X1) + E(X2) = 3.5 + 3.5 = 7,

where Xi is the value on die i (i = 1, 2).

Example 2: Throw 100 six-sided dice. Let Y be the sum of the values. Then

E(Y ) = 100 · 3.5 = 350.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 278: Analysis of Algorithms

1-56

Linearity of Expectation

I For any two random variables X and Y ,

E (X + Y ) = E (X ) + E (Y ).

I Proof: see [GT], 1.3.4

I Very useful, because usually it is easier to compute E (X ) andE (Y ) and apply the formula than to compute E (X + Y )directly.

Example 1: Throw two six-sided dice. Let X be the sum of the values. Then

E(X ) = E(X1 + X2) = E(X1) + E(X2) = 3.5 + 3.5 = 7,

where Xi is the value on die i (i = 1, 2).

Example 2: Throw 100 six-sided dice. Let Y be the sum of the values. Then

E(Y ) = 100 · 3.5 = 350.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 279: Analysis of Algorithms

1-56

Linearity of Expectation

I For any two random variables X and Y ,

E (X + Y ) = E (X ) + E (Y ).

I Proof: see [GT], 1.3.4

I Very useful, because usually it is easier to compute E (X ) andE (Y ) and apply the formula than to compute E (X + Y )directly.

Example 1: Throw two six-sided dice. Let X be the sum of the values. Then

E(X ) = E(X1 + X2) = E(X1) + E(X2) = 3.5 + 3.5 = 7,

where Xi is the value on die i (i = 1, 2).

Example 2: Throw 100 six-sided dice. Let Y be the sum of the values. Then

E(Y ) = 100 · 3.5 = 350.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 280: Analysis of Algorithms

1-57

Independent events

I Two events A1 and A2 are independent iff

P(A1 ∩ A2) = P(A1) · P(A2).

Example: (2-coin example, continued). Let

A1 = coin 1 is heads = HH, HTA2 = coin 2 is tails = HT, TT

Then P(A1) = 12, P(A2) = 1

2, and

P(A1 ∩ A2) = P(HT) =1

4= P(A1) · P(A2).

So A1 and A2 are independent.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 281: Analysis of Algorithms

1-57

Independent events

I Two events A1 and A2 are independent iff

P(A1 ∩ A2) = P(A1) · P(A2).

Example: (2-coin example, continued).

Let

A1 = coin 1 is heads = HH, HTA2 = coin 2 is tails = HT, TT

Then P(A1) = 12, P(A2) = 1

2, and

P(A1 ∩ A2) = P(HT) =1

4= P(A1) · P(A2).

So A1 and A2 are independent.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 282: Analysis of Algorithms

1-57

Independent events

I Two events A1 and A2 are independent iff

P(A1 ∩ A2) = P(A1) · P(A2).

Example: (2-coin example, continued). Let

A1 = coin 1 is heads = HH, HTA2 = coin 2 is tails = HT, TT

Then P(A1) = 12, P(A2) = 1

2, and

P(A1 ∩ A2) = P(HT) =1

4= P(A1) · P(A2).

So A1 and A2 are independent.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 283: Analysis of Algorithms

1-57

Independent events

I Two events A1 and A2 are independent iff

P(A1 ∩ A2) = P(A1) · P(A2).

Example: (2-coin example, continued). Let

A1 = coin 1 is heads = HH, HTA2 = coin 2 is tails = HT, TT

Then P(A1) = 12, P(A2) = 1

2, and

P(A1 ∩ A2) = P(HT) =1

4= P(A1) · P(A2).

So A1 and A2 are independent.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 284: Analysis of Algorithms

1-58

Independent events

A collection of n events C = A1,A2, . . . ,An is mutuallyindependent (or simply independent) if:

For every subset Ai1 ,Ai2 , . . .Aik ⊆ C:

P(Ai1 ∩ Ai2 ∩ . . . ∩ Aik ) = P(Ai1) · P(Ai2) · · ·P(Aik ).

Example: Suppose we flip 10 coins. Suppose the flips are fair(P(H) = P(T) = 1/2) and independent. Then the probability of anyparticular sequence of flips (e.g., HHTTTHTHTH) is 1/(210).

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 285: Analysis of Algorithms

1-58

Independent events

A collection of n events C = A1,A2, . . . ,An is mutuallyindependent (or simply independent) if:

For every subset Ai1 ,Ai2 , . . .Aik ⊆ C:

P(Ai1 ∩ Ai2 ∩ . . . ∩ Aik ) = P(Ai1) · P(Ai2) · · ·P(Aik ).

Example: Suppose we flip 10 coins. Suppose the flips are fair(P(H) = P(T) = 1/2) and independent. Then the probability of anyparticular sequence of flips (e.g., HHTTTHTHTH) is 1/(210).

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 286: Analysis of Algorithms

1-58

Independent events

A collection of n events C = A1,A2, . . . ,An is mutuallyindependent (or simply independent) if:

For every subset Ai1 ,Ai2 , . . .Aik ⊆ C:

P(Ai1 ∩ Ai2 ∩ . . . ∩ Aik ) = P(Ai1) · P(Ai2) · · ·P(Aik ).

Example: Suppose we flip 10 coins. Suppose the flips are fair(P(H) = P(T) = 1/2) and independent. Then the probability of anyparticular sequence of flips (e.g., HHTTTHTHTH) is 1/(210).

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 287: Analysis of Algorithms

1-59

Example: Probability and counting

Example: Suppose we flip a coin 10 times. Suppose the flips are fair andindependent. What is the probability of getting exactly 7 heads out ofthe 10 flips?

Solution:

I The outcomes consist of the set of possible sequences of 10 flips(e.g., HHTTTHTHTH).

I The probability of each outcome is 1/(210).

I The number of successful outcomes is(107

).

I Hence the probability of getting exactly 7 heads is:(107

)210

=120

1024= 0.117.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 288: Analysis of Algorithms

1-59

Example: Probability and counting

Example: Suppose we flip a coin 10 times. Suppose the flips are fair andindependent. What is the probability of getting exactly 7 heads out ofthe 10 flips?

Solution:

I The outcomes consist of the set of possible sequences of 10 flips(e.g., HHTTTHTHTH).

I The probability of each outcome is 1/(210).

I The number of successful outcomes is(107

).

I Hence the probability of getting exactly 7 heads is:(107

)210

=120

1024= 0.117.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 289: Analysis of Algorithms

1-59

Example: Probability and counting

Example: Suppose we flip a coin 10 times. Suppose the flips are fair andindependent. What is the probability of getting exactly 7 heads out ofthe 10 flips?

Solution:

I The outcomes consist of the set of possible sequences of 10 flips(e.g., HHTTTHTHTH).

I The probability of each outcome is 1/(210).

I The number of successful outcomes is(107

).

I Hence the probability of getting exactly 7 heads is:(107

)210

=120

1024= 0.117.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 290: Analysis of Algorithms

1-59

Example: Probability and counting

Example: Suppose we flip a coin 10 times. Suppose the flips are fair andindependent. What is the probability of getting exactly 7 heads out ofthe 10 flips?

Solution:

I The outcomes consist of the set of possible sequences of 10 flips(e.g., HHTTTHTHTH).

I The probability of each outcome is 1/(210).

I The number of successful outcomes is(107

).

I Hence the probability of getting exactly 7 heads is:(107

)210

=120

1024= 0.117.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 291: Analysis of Algorithms

1-59

Example: Probability and counting

Example: Suppose we flip a coin 10 times. Suppose the flips are fair andindependent. What is the probability of getting exactly 7 heads out ofthe 10 flips?

Solution:

I The outcomes consist of the set of possible sequences of 10 flips(e.g., HHTTTHTHTH).

I The probability of each outcome is 1/(210).

I The number of successful outcomes is(107

).

I Hence the probability of getting exactly 7 heads is:(107

)210

=120

1024= 0.117.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 292: Analysis of Algorithms

1-59

Example: Probability and counting

Example: Suppose we flip a coin 10 times. Suppose the flips are fair andindependent. What is the probability of getting exactly 7 heads out ofthe 10 flips?

Solution:

I The outcomes consist of the set of possible sequences of 10 flips(e.g., HHTTTHTHTH).

I The probability of each outcome is 1/(210).

I The number of successful outcomes is(107

).

I Hence the probability of getting exactly 7 heads is:(107

)210

=120

1024= 0.117.

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 293: Analysis of Algorithms

1-60

An average-case result about finding the maximum

v = -∞for i = 0 to n-1:

if A[i] > v:

v = A[i]

return v

I Worst-case number of comparisons is n.

I This can be reduced to n − 1I How many times is the running maximum updated?

I In the worst case n.I What about the average case? . . .

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 294: Analysis of Algorithms

1-60

An average-case result about finding the maximum

v = -∞for i = 0 to n-1:

if A[i] > v:

v = A[i]

return v

I Worst-case number of comparisons is n.

I This can be reduced to n − 1I How many times is the running maximum updated?

I In the worst case n.I What about the average case? . . .

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 295: Analysis of Algorithms

1-60

An average-case result about finding the maximum

v = -∞for i = 0 to n-1:

if A[i] > v:

v = A[i]

return v

I Worst-case number of comparisons is n.

I This can be reduced to n − 1I How many times is the running maximum updated?

I In the worst case n.I What about the average case? . . .

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 296: Analysis of Algorithms

1-60

An average-case result about finding the maximum

v = -∞for i = 0 to n-1:

if A[i] > v:

v = A[i]

return v

I Worst-case number of comparisons is n.

I This can be reduced to n − 1

I How many times is the running maximum updated?I In the worst case n.I What about the average case? . . .

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 297: Analysis of Algorithms

1-60

An average-case result about finding the maximum

v = -∞for i = 0 to n-1:

if A[i] > v:

v = A[i]

return v

I Worst-case number of comparisons is n.

I This can be reduced to n − 1I How many times is the running maximum updated?

I In the worst case n.I What about the average case? . . .

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 298: Analysis of Algorithms

1-60

An average-case result about finding the maximum

v = -∞for i = 0 to n-1:

if A[i] > v:

v = A[i]

return v

I Worst-case number of comparisons is n.

I This can be reduced to n − 1I How many times is the running maximum updated?

I In the worst case n.

I What about the average case? . . .

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 299: Analysis of Algorithms

1-60

An average-case result about finding the maximum

v = -∞for i = 0 to n-1:

if A[i] > v:

v = A[i]

return v

I Worst-case number of comparisons is n.

I This can be reduced to n − 1I How many times is the running maximum updated?

I In the worst case n.I What about the average case? . . .

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 300: Analysis of Algorithms

1-61

Average number of updates to the running maximum

I Assume

I all possible orderings (permutations) of A are equally likelyI all n elements of A are distinct.

I The running maximum gets updated on iteration i of the loop iffmaxA[0], . . . ,A[i ] = A[i ].

I The probability of this happening is 1/(i + 1).

I Define indicator variables Xi :

Xi =

1 if v gets updated on iteration #i

0 if v does not get updated on iteration #i

Then E (Xi ) = 1i+1

I The total number of times that v gets updated is:

X =n−1∑i=0

Xi

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 301: Analysis of Algorithms

1-61

Average number of updates to the running maximumI Assume

I all possible orderings (permutations) of A are equally likelyI all n elements of A are distinct.

I The running maximum gets updated on iteration i of the loop iffmaxA[0], . . . ,A[i ] = A[i ].

I The probability of this happening is 1/(i + 1).

I Define indicator variables Xi :

Xi =

1 if v gets updated on iteration #i

0 if v does not get updated on iteration #i

Then E (Xi ) = 1i+1

I The total number of times that v gets updated is:

X =n−1∑i=0

Xi

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 302: Analysis of Algorithms

1-61

Average number of updates to the running maximumI Assume

I all possible orderings (permutations) of A are equally likelyI all n elements of A are distinct.

I The running maximum gets updated on iteration i of the loop iffmaxA[0], . . . ,A[i ] = A[i ].

I The probability of this happening is 1/(i + 1).

I Define indicator variables Xi :

Xi =

1 if v gets updated on iteration #i

0 if v does not get updated on iteration #i

Then E (Xi ) = 1i+1

I The total number of times that v gets updated is:

X =n−1∑i=0

Xi

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 303: Analysis of Algorithms

1-61

Average number of updates to the running maximumI Assume

I all possible orderings (permutations) of A are equally likelyI all n elements of A are distinct.

I The running maximum gets updated on iteration i of the loop iffmaxA[0], . . . ,A[i ] = A[i ].

I The probability of this happening is 1/(i + 1).

I Define indicator variables Xi :

Xi =

1 if v gets updated on iteration #i

0 if v does not get updated on iteration #i

Then E (Xi ) = 1i+1

I The total number of times that v gets updated is:

X =n−1∑i=0

Xi

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 304: Analysis of Algorithms

1-61

Average number of updates to the running maximumI Assume

I all possible orderings (permutations) of A are equally likelyI all n elements of A are distinct.

I The running maximum gets updated on iteration i of the loop iffmaxA[0], . . . ,A[i ] = A[i ].

I The probability of this happening is 1/(i + 1).

I Define indicator variables Xi :

Xi =

1 if v gets updated on iteration #i

0 if v does not get updated on iteration #i

Then E (Xi ) = 1i+1

I The total number of times that v gets updated is:

X =n−1∑i=0

Xi

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 305: Analysis of Algorithms

1-61

Average number of updates to the running maximumI Assume

I all possible orderings (permutations) of A are equally likelyI all n elements of A are distinct.

I The running maximum gets updated on iteration i of the loop iffmaxA[0], . . . ,A[i ] = A[i ].

I The probability of this happening is 1/(i + 1).

I Define indicator variables Xi :

Xi =

1 if v gets updated on iteration #i

0 if v does not get updated on iteration #i

Then E (Xi ) = 1i+1

I The total number of times that v gets updated is:

X =n−1∑i=0

Xi

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 306: Analysis of Algorithms

1-61

Average number of updates to the running maximumI Assume

I all possible orderings (permutations) of A are equally likelyI all n elements of A are distinct.

I The running maximum gets updated on iteration i of the loop iffmaxA[0], . . . ,A[i ] = A[i ].

I The probability of this happening is 1/(i + 1).

I Define indicator variables Xi :

Xi =

1 if v gets updated on iteration #i

0 if v does not get updated on iteration #i

Then E (Xi ) = 1i+1

I The total number of times that v gets updated is:

X =n−1∑i=0

Xi

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 307: Analysis of Algorithms

1-62

Average number of updates to the running maximum(continued)

The expected total number of times that v gets updated is:

E (X ) = E

(n−1∑i=0

Xi

)=

n−1∑i=0

E (Xi ) =n−1∑i=0

1

i + 1=

n∑i=1

1

i= Hn = O(log n)

It can be shown that

Hn = ln n + γ + o(1), where γ = 0.5772157 . . .

If there are 30,000 elements in the list, the expected update count isabout 10.9

If there are 3,000,000,000 elements in the list, the expected update countis about 22.4

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 308: Analysis of Algorithms

1-62

Average number of updates to the running maximum(continued)

The expected total number of times that v gets updated is:

E (X )

= E

(n−1∑i=0

Xi

)=

n−1∑i=0

E (Xi ) =n−1∑i=0

1

i + 1=

n∑i=1

1

i= Hn = O(log n)

It can be shown that

Hn = ln n + γ + o(1), where γ = 0.5772157 . . .

If there are 30,000 elements in the list, the expected update count isabout 10.9

If there are 3,000,000,000 elements in the list, the expected update countis about 22.4

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 309: Analysis of Algorithms

1-62

Average number of updates to the running maximum(continued)

The expected total number of times that v gets updated is:

E (X ) = E

(n−1∑i=0

Xi

)

=n−1∑i=0

E (Xi ) =n−1∑i=0

1

i + 1=

n∑i=1

1

i= Hn = O(log n)

It can be shown that

Hn = ln n + γ + o(1), where γ = 0.5772157 . . .

If there are 30,000 elements in the list, the expected update count isabout 10.9

If there are 3,000,000,000 elements in the list, the expected update countis about 22.4

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 310: Analysis of Algorithms

1-62

Average number of updates to the running maximum(continued)

The expected total number of times that v gets updated is:

E (X ) = E

(n−1∑i=0

Xi

)=

n−1∑i=0

E (Xi )

=n−1∑i=0

1

i + 1=

n∑i=1

1

i= Hn = O(log n)

It can be shown that

Hn = ln n + γ + o(1), where γ = 0.5772157 . . .

If there are 30,000 elements in the list, the expected update count isabout 10.9

If there are 3,000,000,000 elements in the list, the expected update countis about 22.4

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 311: Analysis of Algorithms

1-62

Average number of updates to the running maximum(continued)

The expected total number of times that v gets updated is:

E (X ) = E

(n−1∑i=0

Xi

)=

n−1∑i=0

E (Xi ) =n−1∑i=0

1

i + 1

=n∑

i=1

1

i= Hn = O(log n)

It can be shown that

Hn = ln n + γ + o(1), where γ = 0.5772157 . . .

If there are 30,000 elements in the list, the expected update count isabout 10.9

If there are 3,000,000,000 elements in the list, the expected update countis about 22.4

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 312: Analysis of Algorithms

1-62

Average number of updates to the running maximum(continued)

The expected total number of times that v gets updated is:

E (X ) = E

(n−1∑i=0

Xi

)=

n−1∑i=0

E (Xi ) =n−1∑i=0

1

i + 1=

n∑i=1

1

i

= Hn = O(log n)

It can be shown that

Hn = ln n + γ + o(1), where γ = 0.5772157 . . .

If there are 30,000 elements in the list, the expected update count isabout 10.9

If there are 3,000,000,000 elements in the list, the expected update countis about 22.4

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 313: Analysis of Algorithms

1-62

Average number of updates to the running maximum(continued)

The expected total number of times that v gets updated is:

E (X ) = E

(n−1∑i=0

Xi

)=

n−1∑i=0

E (Xi ) =n−1∑i=0

1

i + 1=

n∑i=1

1

i= Hn

= O(log n)

It can be shown that

Hn = ln n + γ + o(1), where γ = 0.5772157 . . .

If there are 30,000 elements in the list, the expected update count isabout 10.9

If there are 3,000,000,000 elements in the list, the expected update countis about 22.4

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 314: Analysis of Algorithms

1-62

Average number of updates to the running maximum(continued)

The expected total number of times that v gets updated is:

E (X ) = E

(n−1∑i=0

Xi

)=

n−1∑i=0

E (Xi ) =n−1∑i=0

1

i + 1=

n∑i=1

1

i= Hn = O(log n)

It can be shown that

Hn = ln n + γ + o(1), where γ = 0.5772157 . . .

If there are 30,000 elements in the list, the expected update count isabout 10.9

If there are 3,000,000,000 elements in the list, the expected update countis about 22.4

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 315: Analysis of Algorithms

1-62

Average number of updates to the running maximum(continued)

The expected total number of times that v gets updated is:

E (X ) = E

(n−1∑i=0

Xi

)=

n−1∑i=0

E (Xi ) =n−1∑i=0

1

i + 1=

n∑i=1

1

i= Hn = O(log n)

It can be shown that

Hn = ln n + γ + o(1), where γ = 0.5772157 . . .

If there are 30,000 elements in the list, the expected update count isabout 10.9

If there are 3,000,000,000 elements in the list, the expected update countis about 22.4

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 316: Analysis of Algorithms

1-62

Average number of updates to the running maximum(continued)

The expected total number of times that v gets updated is:

E (X ) = E

(n−1∑i=0

Xi

)=

n−1∑i=0

E (Xi ) =n−1∑i=0

1

i + 1=

n∑i=1

1

i= Hn = O(log n)

It can be shown that

Hn = ln n + γ + o(1), where γ = 0.5772157 . . .

If there are 30,000 elements in the list, the expected update count isabout 10.9

If there are 3,000,000,000 elements in the list, the expected update countis about 22.4

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 317: Analysis of Algorithms

1-62

Average number of updates to the running maximum(continued)

The expected total number of times that v gets updated is:

E (X ) = E

(n−1∑i=0

Xi

)=

n−1∑i=0

E (Xi ) =n−1∑i=0

1

i + 1=

n∑i=1

1

i= Hn = O(log n)

It can be shown that

Hn = ln n + γ + o(1), where γ = 0.5772157 . . .

If there are 30,000 elements in the list, the expected update count isabout 10.9

If there are 3,000,000,000 elements in the list, the expected update countis about 22.4

CompSci 161—Spring 2021— c©M. B. Dillencourt—University of California, Irvine

Page 318: Analysis of Algorithms

Amortization

q The amortized running time of an operation within a series of operations is the worst-case running time of the series of operations divided by the number of operations.

q Example: A growable array, S. When needing to grow:a. Allocate a new array B of larger capacity.b. Copy A[i] to B[i], for i = 0, . . . , n − 1, where n is size of A.c. Let A = B, that is, we use B as the array now supporting A.

© 2015 Goodrich and Tamassia Analysis of Algorithms 39

Page 319: Analysis of Algorithms

Analysis of Algorithms 40

Growable Array Descriptionq Let add(e) be the operation

that adds element e at the end of the array

q When the array is full, we replace the array with a larger one

q But how large should the new array be?n Incremental strategy: increase

the size by a constant cn Doubling strategy: double the

size

Algorithm add(e)if t = A.length - 1

thenB ¬ new array of

size …for i ¬ 0 to n-1 do

B[i] ¬ A[i]A ¬ B

n ¬ n + 1A[n-1] ¬ e

© 2015 Goodrich and Tamassia

Page 320: Analysis of Algorithms

Analysis of Algorithms 41

Comparison of the Strategies

q We compare the incremental strategy and the doubling strategy by analyzing the total time T(n) needed to perform a series of nadd operations

q We assume that we start with an empty list represented by a growable array of size 1

q We call amortized time of an add operation the average time taken by an add operation over the series of operations, i.e., T(n)/n

© 2015 Goodrich and Tamassia

Page 321: Analysis of Algorithms

Analysis of Algorithms 42

Incremental Strategy Analysis q Over n add operations, we replace the array k = n/c

times, where c is a constantq The total time T(n) of a series of n add operations is

proportional ton + c + 2c + 3c + 4c + … + kc =

n + c(1 + 2 + 3 + … + k) =n + ck(k + 1)/2

q Since c is a constant, T(n) is O(n + k2), i.e., O(n2)q Thus, the amortized time of an add operation is

O(n)© 2015 Goodrich and Tamassia

Page 322: Analysis of Algorithms

Analysis of Algorithms 43

Doubling Strategy Analysisq We replace the array k = log2 n

timesq The total time T(n) of a series of n

push operations is proportional ton + 1 + 2 + 4 + 8 + …+ 2k =n + 2k + 1 - 1 = 3n - 1

q T(n) is O(n)q The amortized time of an add

operation is O(1)

geometric series

1

2

14

8

© 2015 Goodrich and Tamassia

Page 323: Analysis of Algorithms

Accounting Method Proof for the Doubling Strategy

q We view the computer as a coin-operated appliance that requires the payment of 1 cyber-dollar for a constant amount of computing time.

q We shall charge each add operation 3 cyber-dollars, that is, it will have an amortized O(1) amortized running time.n We over-charge each add operation not causing an overflow 2 cyber-dollars.n Think of the 2 cyber-dollars profited in an insertion that does not grow the

array as being “stored” at the element inserted. n An overflow occurs when the array A has 2i elements.n Thus, doubling the size of the array will require 2i cyber-dollars. n These cyber-dollars are at the elements stored in cells 2i−1 through 2i−1.

© 2015 Goodrich and Tamassia Analysis of Algorithms 44