66
CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC 341, and textbook

CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

CMSC 341

Lecture 5 Asymptotic Analysis

Based on slides from Prof; Gibson, previous iterations of CMSC 341, and textbook

Page 2: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Today’s Topics

Review

Mathematical properties

Proof by induction

Program complexity

Growth functions

Big O notation

UMBC CMSC 341 Asymptotic Analysis 2

Page 3: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Mathematical Properties

3

Page 4: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Why Review Mathematical Properties?

You will be solving complex problems

That use division and power

These mathematical properties will help you

solve these problems more quickly

Exponents

Logarithms

Summations

Mathematical Series

UMBC CMSC 341 Asymptotic Analysis 4

Page 5: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Exponents

Shorthand for multiplying a number by itself

Several times

Used in identifying sizes of memory

Help to determine the most efficient way to

write a program

UMBC CMSC 341 Asymptotic Analysis 5

Page 6: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Exponent Identities

xaxb = x(a+b)

xaya = (xy)a

(xa)b = x(ab)

x(a-b) = (xa)/(xb)

x(-a) = 1/(xa)

x(a/b) = (xa) = √xa

UMBC CMSC 341 Asymptotic Analysis 6

1

b b___

Page 7: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Exponent Identities

xaxb = x(a+b)

xaya = (xy)a

(xa)b = x(ab)

x(a-b) = (xa)/(xb)

x(-a) = 1/(xa)

x(a/b) = (xa) = √xa

UMBC CMSC 341 Asymptotic Analysis 7

1

b b___

Page 8: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Logarithms

ALWAYS base 2 in Computer Science

Unless stated otherwise

Used for:

Conversion between numbering systems

Determining the mathematical power needed

Definition:

n = logax if and only if an = x

UMBC CMSC 341 Asymptotic Analysis 8

Page 9: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Logarithm Identities

logb(1) = 0

logb(b) = 1

logb(x*y) = logb(x) + logb(y)

logb(x/y) = logb(x) - logb(y)

logb(xn) = n*logb(x)

logb(x) = logb(c) * logc(x)

= logc(x) / logc(b)

UMBC CMSC 341 Asymptotic Analysis 9

Page 10: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Logarithm Identities

logb(1) = 0

logb(b) = 1

logb(x*y) = logb(x) + logb(y)

logb(x/y) = logb(x) - logb(y)

logb(xn) = n*logb(x)

logb(x) = logb(c) * logc(x)

= logc(x) / logc(b)

UMBC CMSC 341 Asymptotic Analysis 10

Page 11: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Summations

The addition of a sequence of numbers

Result is their sum or total

Can break a function into several summations

UMBC CMSC 341 Asymptotic Analysis 11

Page 12: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Proof by Induction

12

Page 13: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Proof by Induction

A proof by induction is just like an ordinary

proof

In which every step must be justified

However, it employs a neat trick:

You can prove a statement about an arbitrary

number n by first proving

It is true when n is 1 and then

Assuming it is true for n=k and

Showing it is true for n=k+1

UMBC CMSC 341 Asymptotic Analysis 13

Page 14: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Proof by Induction Example

Let’s say you want to show that you can

climb to the nth floor of a fire escape

With induction, need to show that:

They can climb the ladder up to the fire

escape (n = 0)

They can climb the first flight of stairs (n = 1)

Then we can show that you can climb the

stairs from any level of the fire escape

(n = k) to the next level (n = k + 1)

UMBC CMSC 341 Asymptotic Analysis 14

Page 15: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Program Complexity

15

Page 16: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

What is Complexity?

How many resources will it take to solve a

problem of a given size?

Time (ms, seconds, minutes, years)

Space (kB, MB, GB, TB, PB)

Expressed as a function of problem size

(beyond some minimum size)

UMBC CMSC 341 Asymptotic Analysis 16

Page 17: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Increasing Complexity

How do requirements grow as size grows?

Size of the problem

Number of elements to be handled

Size of thing to be operated on

UMBC CMSC 341 Asymptotic Analysis 17

Page 18: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Determining Complexity: Experimental

Write a program implementing the algorithm

Run the program with inputs of varying size

and composition

Use a method like clock()

to get an accurate

measure of the actual

running time

Plot the results

UMBC CMSC 341 Asymptotic Analysis 18

Page 19: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Limitations of Experimental Method

What are some limitations of this approach?

Must implement algorithm to be tested

May be difficult

Results may not apply to all possible inputs

Only applies to inputs explicitly tested

Comparing two algorithms is difficult

Requires same hardware and software

UMBC CMSC 341 Asymptotic Analysis 19

Page 20: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Determining Complexity: Analysis

Theoretical analysis solves these problems

Use a high-level description of the algorithm

Instead of an implementation

Run time is a function of the input size, n

Take into account all possible inputs

Evaluation is independent of specific

hardware or software

Including compiler optimization

UMBC CMSC 341 Asymptotic Analysis 20

Page 21: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Using Asymptotic Analysis

For an algorithm:

With input size n

Define the run time as T(n)

Purpose of asymptotic analysis is to

examine:

The rate of growth of T(n)

As n grows larger and larger

UMBC CMSC 341 Asymptotic Analysis 21

Page 22: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Growth Functions

22

Page 23: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Seven Important Functions

Constant 1

Logarithmic log n

Linear n

N-Log-N n log n

Quadratic n2

Cubic n3

Exponential 2n

UMBC CMSC 341 Asymptotic Analysis 23

Page 24: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Constant and Linear

Constant

T(n) = c

Getting array element at known location

Any simple C++ statement (e.g. assignment)

Linear

T(n) = cn [+ any lower order terms]

Finding particular element in array of size n

Sequential search

Trying on all of your n shirts

UMBC CMSC 341 Asymptotic Analysis 24

“c” is a constant value, like 1

Page 25: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Quadratic and Polynomial

Quadratic

T(n) = cn2 [ + any lower order terms]

Sorting an array using bubble sort

Trying all your n shirts with all your n pants

Polynomial

T(n) = cnk [ + any lower order terms]

Finding the largest element of a k-dimensional array

Looking for maximum substrings in array

UMBC CMSC 341 Asymptotic Analysis 25

Page 26: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Exponential and Logarithmic

Exponential

T(n) = cn [ + any lower order terms]

Constructing all possible orders of array elements

Towers of Hanoi (2n)

Recursively calculating nth Fibonacci number (2n)

Logarithmic

T(n) = lg n [ + any lower order terms]

Finding a particular array element (binary search)

Algorithms that continually divide a problem in half

UMBC CMSC 341 Asymptotic Analysis 26

Page 27: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Graph of Growth Functions

UMBC CMSC 341 Asymptotic Analysis 27

Page 28: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Graph of Growth Functions

UMBC CMSC 341 Asymptotic Analysis 28

logarithmic

linear quadratic

n-log-n cubic

exponential

Page 29: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Expanded Growth Functions Graph

UMBC CMSC 341 Asymptotic Analysis 29

Page 30: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Asymptotic Analysis

30

Page 31: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Simplification

We are only interested in the growth rate as

an “order of magnitude”

As the problem grows really, really, really large

We are not concerned with the fine details

Constant multipliers are dropped

If T(n) = c*2n, we reduce it to T(n) = 2n

Lower order terms are dropped

If T(n) = n4 + n2, we reduce it to T(n) = n4

UMBC CMSC 341 Asymptotic Analysis 31

Page 32: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Three Cases of Analysis

Best case

When input data minimizes the run time

An array that needs to be sorted is already in order

Average case

The “run time efficiency” over all possible inputs

Worst case

When input data maximizes the run time

Most adversarial data possible

UMBC CMSC 341 Asymptotic Analysis 32

Page 33: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Analysis Example: Mileage

How much gas does it take to go 20 miles?

Best case

Straight downhill, wind at your back

Average case

“Average” terrain

Worst case

Winding uphill gravel road, inclement weather

UMBC CMSC 341 Asymptotic Analysis 33

Page 34: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Analysis Example: Sequential Search

Consider sequential search on an unsorted

array of length n, what is the time complexity?

Best case

Worst case

Average case

UMBC CMSC 341 Asymptotic Analysis 34

Page 35: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Comparison of Two Algorithms

Insertion sort:

(n2)/4

Merge sort:

2nlgn

n = 1,000,000

Million ops per second

Merge takes 40 secs

Insert takes 70 hours

UMBC CMSC 341 Asymptotic Analysis 35

Source: Matt Stallmann, Goodrich and Tamassia slides

Page 36: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Big O Notation

36

Page 37: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

What is Big O Notation?

Big O notation has a special meaning in

Computer Science

Used to describe the complexity (or

performance) of an algorithm

Big O describes the worst-case scenario

Big Omega (Ω) describes the best-case

Big Theta (Θ) is used when the best and

worst case scenarios are the same

UMBC CMSC 341 Asymptotic Analysis 37

Page 38: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Big O Definition

We say that f(n) is O(g(n)) if

There is a real constant c > 0

And an integer constant n0 ≥ 1

Such that

f(n) ≤ c*g(n), for n ≥ n0

Let’s do an example

Taken from https://youtu.be/ei-A_wy5Yxw

UMBC CMSC 341 Asymptotic Analysis 38

Page 39: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Big O: Example – n4

We have f(n) = 4n2 + 16n + 2

Let’s test if f(n) is O(n4)

Remember, we want to see f(n) ≤ c*g(n), for n ≥ n0

We’ll start with c = 1

UMBC CMSC 341 Asymptotic Analysis 39

n0 4n2 + 16n + 2 ≤ c*n4

0 2 > 0

1 22 > 1

2 50 > 16

3 86 > 81

4 130 < 256

Page 40: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Big O: Example – n4

We have f(n) = 4n2 + 16n + 2

Let’s test if f(n) is O(n4)

Remember, we want to see f(n) ≤ c*g(n), for n ≥ n0

We’ll start with c = 1

UMBC CMSC 341 Asymptotic Analysis 40

n0 4n2 + 16n + 2 ≤ c*n4

0 2 > 0

1 22 > 1

2 50 > 16

3 86 > 81

4 130 < 256

Page 41: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Big O: Example

So we can say that

f(n) = 4n2 + 16n + 2 is O(n4)

Big O is an upper bound

The worst the algorithm could perform

Does n4 seem high to you?

UMBC CMSC 341 Asymptotic Analysis 41

Page 42: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Big O: Example – n2

We have f(n) = 4n2 + 16n + 2

Let’s test if f(n) is O(n2)

Remember, we want to see f(n) ≤ c*g(n), for n ≥ n0

Let’s start with c = 10

UMBC CMSC 341 Asymptotic Analysis 42

n0 4n2 + 16n + 2 ≤ c*n2

0 2 > 0

1 22 > 10

2 50 > 40

3 86 > 90

Page 43: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Big O: Example – n2

We have f(n) = 4n2 + 16n + 2

Let’s test if f(n) is O(n2)

Remember, we want to see f(n) ≤ c*g(n), for n ≥ n0

Let’s start with c = 10

UMBC CMSC 341 Asymptotic Analysis 43

n0 4n2 + 16n + 2 ≤ c*n2

0 2 > 0

1 22 > 10

2 50 > 40

3 86 < 90

Page 44: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Big O: Example

So we can more accurately say that

f(n) = 4n2 + 16n + 2 is O(n2)

Could f(n) = 4n2 + 16n + 2 is O(n) ever be true?

Why not?

UMBC CMSC 341 Asymptotic Analysis 44

Page 45: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Big O:

Practice Examples

45

Page 46: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Big O: Example 1

Code:

a = b;

++sum;

int y = Mystery( 42 );

Complexity:

Constant – O(c)

UMBC CMSC 341 Asymptotic Analysis 46

Page 47: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Big O: Example 2

Code:sum = 0;

for (i = 1; i <= n; i++) {

sum += n;

}

Complexity:

Linear – O(n)

UMBC CMSC 341 Asymptotic Analysis 47

Page 48: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Big O: Example 3

Code:sum1 = 0;

for (i = 1; i <= n; i++) {

for (j = 1; j <= n; j++) {

sum1++;

}

}

Complexity:

Quadratic – O(n2)

UMBC CMSC 341 Asymptotic Analysis 48

Page 49: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Big O: Example 4

Code:sum2 = 0;

for (i = 1; i <= n; i++) {

for (j = 1; j <= i; j++) {

sum2++;

}

}

Complexity:

Quadratic – O(n2)

UMBC CMSC 341 Asymptotic Analysis 49

how many times do we execute this

statement?

1 + 2 + 3 + 4 + … + n-2 + n-1 + n

Page 50: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Expressing as a summation

Can we express this as a summation?

Yes!

Does this have a known formula?

Yes!

What does this formula multiply out to?

(n2 + n) / 2

or O(n2)

UMBC CMSC 341 Asymptotic Analysis 50

n

ii

1 2

)1(

nn

Page 51: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Other Geometric Formulas

O(n3)

O(n4)

O(cn)

UMBC CMSC 341 Asymptotic Analysis 51

, where c ≠ 1

6

)12)(1(2

1

nnni

n

i

4

)1( 223

1

nni

n

i

c

cc

nin

i

1

1 )1(

0

Page 52: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Big O: Example 5

Code:sum3 = 0;

for (i = 1; i <= n; i++) {

for (j = 1; j <= i; j++) {

sum3++; }

}

for (k = 0; k < n; k++) {

a[ k ] = k;

}

Complexity:

Quadratic – O(n2)

UMBC CMSC 341 Asymptotic Analysis 52

Page 53: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Big O: Example 6

Code:sum4 = 0;

for (k = 1; k <= n; k *= 2)

for (j = 1; j <= n; j++) {

sum4++;

}

Complexity:

O(n log n)

UMBC CMSC 341 Asymptotic Analysis 53

Page 54: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Big O: More Examples

Square each element of an N x N matrix

Printing the first and last row of an N x N

matrix

Finding the smallest element in a sorted array

of N integers

Printing all permutations of N distinct

elements

UMBC CMSC 341 Asymptotic Analysis 54

Page 55: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Big Omega (Ω) and Big Theta(Θ)

55

Page 56: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

“Big” Notation (words)

Big O describes an asymptotic upper bound

The worst possible performance we can expect

Big Ω describes an asymptotic lower bound

The best possible performance we can expect

Big Θ describes an asymptotically tight bound

The best and worst running times can be

expressed with the same equation

UMBC CMSC 341 Asymptotic Analysis 56

Page 57: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

“Big” Notation (equations)

Big O describes an asymptotic upper bound

f(n) is asymptotically less than or equal to g(n)

Big Ω describes an asymptotic lower bound

f(n) is asymptotically greater than or equal to g(n)

Big Θ describes an asymptotically tight bound

f(n) is asymptotically equal to g(n)

UMBC CMSC 341 Asymptotic Analysis 57

Page 58: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Big O and Big Omega Example

UMBC CMSC 341 Asymptotic Analysis 58

f(n)4x2+16x+2

g(n)x4

4*g(n)x2

O(n4) Ω(n2)

Page 59: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Big Theta Example

UMBC CMSC 341 Asymptotic Analysis 59

f(n)4x2+16x+2

10*g(n)x2

4*g(n)x2

O(n2) Ω(n2)

Θ(n2)

Page 60: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

A Simple Example

Say we write an algorithm that takes in an

array of numbers and returns the highest one

What is the absolute fastest it can run?

Linear time – Ω(n)

What is the absolute slowest it can run?

Linear time – O(n)

Can this algorithm be tightly asymptotically bound?

YES – so we can also say it’s Θ(n)

UMBC CMSC 341 Asymptotic Analysis 60

Page 61: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Proof by Induction

61

Page 62: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Proof by Induction

The only way to prove that Big O will work

As n becomes larger and larger numbers

To prove F(n) for any positive integer n

1. Base case: prove F(1) is true

2. Hypothesis: Assume F(k) is true for any

k >= 1

3. Inductive: Prove the if F(k) is true, then

F(k+1) is true

UMBC CMSC 341 Asymptotic Analysis 62

Page 63: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Induction Example (Step 1)

Show that for all n ≥ 1 :

1. Base case:

n = 1

(This is our n0)

UMBC CMSC 341 Asymptotic Analysis 63

6

)12)(1(2

1

nnni

n

i

6

)1)1(2)(11(121

1

i

i

6

)3)(2(121

1 i

i

6

621

1 i

i

121

1 i

i

Page 64: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Induction Example (Step 2)

Show that for all n ≥ 1 :

2. Hypothesis:

Assume that

holds for any n ≥ 1

UMBC CMSC 341 Asymptotic Analysis 64

6

)12)(1(2

1

nnni

n

i

6

)12)(1(2

1

nnni

n

i

Page 65: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Induction Example (Step 3)

Show that for all n ≥ 1 :

3. Inductive:

Prove that if F(k) is true (assumed),

the F(k+1) is also true

We’ve already proved F(1) is true

So proving this step will prove F(2) from F(1),

and F(3) from F(2), …, and F(k+1) from F(k)

UMBC CMSC 341 Asymptotic Analysis 65

6

)12)(1(2

1

nnni

n

i

Page 66: CMSC 341 Lecture 5 Asymptotic Analysischang/cs341.s17/park/L05-Asymptotic... · CMSC 341 Lecture 5 Asymptotic Analysis Based on slides from Prof; Gibson, previous iterations of CMSC

Induction Example (Step 3)

UMBC CMSC 341 Asymptotic Analysis 66

6

)12)(1(2

1

nnni

n

i

22

1

21

1)1(

kii

k

i

k

i

221

1)1(

6

)12)(1(

k

kkki

k

i

6

))1(6)12()(1(21

1

kkkki

k

i

6

)672)(1( 221

1

kkki

k

i

6

)32)(2)(1(21

1

kkki

k

i 6

)1)1(2)(1)1)((1(21

1

kkki

k

i