25
CS-2852 Data Structures Week 10, Class 1 Lab 8 notes Big-O revisited CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick 1

CS-2852 Data Structures Week 10, Class 1 Lab 8 notes Big-O revisited CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick 1

Embed Size (px)

Citation preview

Page 1: CS-2852 Data Structures Week 10, Class 1 Lab 8 notes Big-O revisited CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick 1

CS-2852Data Structures

Week 10, Class 1 Lab 8 notes Big-O revisited

CS-2852Dr. Josiah Yoder

Slide style: Dr. Hornick1

Page 2: CS-2852 Data Structures Week 10, Class 1 Lab 8 notes Big-O revisited CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick 1

Running time

CS-2852Dr. Josiah Yoder

Slide style: Dr. HornickContent: Dr. Hasker

2

f(n) f(50) f(100) f(100)/f(50)

1 1 1 1

log2 n 5.64 6.64 1.18

n 50 100 2

n2 2500 10,000 4

n3 12,500 100,000 8

2n 1e15 1e30 1e15

n! 3e64 9e157 3e93

Page 3: CS-2852 Data Structures Week 10, Class 1 Lab 8 notes Big-O revisited CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick 1

Big-O Motivation

Want to ignore minor details Focus on what really makes the difference as

n grows Each function on the previous slide is in a

class of its own Want to find that class Multiplication by scalar doesn’t matter Addition of lower-order operations doesn’t

matterCS-2852

Dr. Josiah YoderSlide style: Dr. Hornick

3

Page 4: CS-2852 Data Structures Week 10, Class 1 Lab 8 notes Big-O revisited CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick 1

Big-O Definition

T(n) = O(f(n)

if and only if

There exist n0 and c such that

T(n) ≤ cf(n) for all n > n0

CS-2852Dr. Josiah Yoder

Slide style: Dr. Hornick4

Page 5: CS-2852 Data Structures Week 10, Class 1 Lab 8 notes Big-O revisited CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick 1

Simplifying Big-O expressions Addition

O(1) < O(log n) < O(nk) < O(kn) < O(n!)

e.g. O(nk+n!) = O(n!) e.g. O(log n + n2) = O(n2) e.g. O(n log n + n2) = O(n2) e.g. O(n log n + n) = O(n log n

CS-2852Dr. Josiah Yoder

Slide style: Dr. Hornick5

Page 6: CS-2852 Data Structures Week 10, Class 1 Lab 8 notes Big-O revisited CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick 1

Simplifying Big-O expresionsMultiplication by scalar

O(kf(n)) = O(f(n)) for any fixed k

e.g. O(5) = O(1) e.g. O(2 log2 n + 5) = O(log n)

CS-2852Dr. Josiah Yoder

Slide style: Dr. Hornick6

Page 7: CS-2852 Data Structures Week 10, Class 1 Lab 8 notes Big-O revisited CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick 1

Strategies for determining Big-O

Analysis of Code Intuition based on

Structure of data Analysis of Code

CS-2852Dr. Josiah Yoder

Slide style: Dr. Hornick7

Page 8: CS-2852 Data Structures Week 10, Class 1 Lab 8 notes Big-O revisited CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick 1

Big-O based on analysis of Code

public void f() {

if(isG()) {

h();

x++;

} else {

for(int i=0;i<j();i++) {

k();

for(A a: list) {

m();

}

}

} // end of f

max – sequence of simple expressions

max – different if clauses

prod – number of iterations over loop * contents of loop

subs – method calls

CS-2852Dr. Josiah Yoder

Slide style: Dr. Hornick8

Page 9: CS-2852 Data Structures Week 10, Class 1 Lab 8 notes Big-O revisited CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick 1

Big-O based analysis of a Recursive algorithm

Each recursive call should be O(1) except for method calls No loops

So order is number of recursive calls needed Number of recursive calls can be exponential

e.g. simple implementation of Fibbonaci Often, number of recursive calls is O(n) or

even O(log n)

CS-2852Dr. Josiah Yoder

Slide style: Dr. Hornick9

Page 10: CS-2852 Data Structures Week 10, Class 1 Lab 8 notes Big-O revisited CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick 1

Example

Suppose binary search of an array is implemented recursively. What is the Big-O running time?

CS-2852Dr. Josiah Yoder

Slide style: Dr. Hornick10

Page 11: CS-2852 Data Structures Week 10, Class 1 Lab 8 notes Big-O revisited CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick 1

Example

Suppose binary search of a Red-Black tree is implement recursively. What is the Big-O running time?

CS-2852Dr. Josiah Yoder

Slide style: Dr. Hornick11

Page 12: CS-2852 Data Structures Week 10, Class 1 Lab 8 notes Big-O revisited CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick 1

Example

Suppose binary search of a simple BinarySearchTree is implemented recursively. What is the Big-O running time?

CS-2852Dr. Josiah Yoder

Slide style: Dr. Hornick12

Page 13: CS-2852 Data Structures Week 10, Class 1 Lab 8 notes Big-O revisited CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick 1

Example

Suppose binary search of a simple BinarySearchTree is implemented iteratively (with a loop). What is the Big-O running time?

CS-2852Dr. Josiah Yoder

Slide style: Dr. Hornick13

Page 14: CS-2852 Data Structures Week 10, Class 1 Lab 8 notes Big-O revisited CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick 1

Example

Suppose we insert n items into an ArrayList using add(E). What is the Big-O running time?

CS-2852Dr. Josiah Yoder

Slide style: Dr. Hornick14

Page 15: CS-2852 Data Structures Week 10, Class 1 Lab 8 notes Big-O revisited CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick 1

Example

Suppose we insert n items into an ArrayList using add(0, E). What is the Big-O running time?

CS-2852Dr. Josiah Yoder

Slide style: Dr. Hornick15

Page 16: CS-2852 Data Structures Week 10, Class 1 Lab 8 notes Big-O revisited CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick 1

Example

Suppose we insert n items into a LinkedList using add(0, E). What is the Big-O running time?

CS-2852Dr. Josiah Yoder

Slide style: Dr. Hornick16

Page 17: CS-2852 Data Structures Week 10, Class 1 Lab 8 notes Big-O revisited CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick 1

Example

Suppose we insert n items into a LinkedList using add(E). What is the Big-O running time?

CS-2852Dr. Josiah Yoder

Slide style: Dr. Hornick17

Page 18: CS-2852 Data Structures Week 10, Class 1 Lab 8 notes Big-O revisited CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick 1

Example

Suppose we insert just 1 item into a LinkedList using add(n/2, e). What is the Big-O running time?

CS-2852Dr. Josiah Yoder

Slide style: Dr. Hornick18

Page 19: CS-2852 Data Structures Week 10, Class 1 Lab 8 notes Big-O revisited CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick 1

Example

Suppose we insert one item into an empty hash-table.

What is the Big(O) running time?

CS-2852Dr. Josiah Yoder

Slide style: Dr. Hornick19

Page 20: CS-2852 Data Structures Week 10, Class 1 Lab 8 notes Big-O revisited CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick 1

Example

Suppose we insert n items into an empty hash-table, and then remove them

What is the Big(O) running time?

Assume: No collisions occur

CS-2852Dr. Josiah Yoder

Slide style: Dr. Hornick20

Page 21: CS-2852 Data Structures Week 10, Class 1 Lab 8 notes Big-O revisited CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick 1

Example

Suppose we insert n items into an empty hash-table, and then remove them

What is the Big(O) running time?

Assume: Collisions always occur

CS-2852Dr. Josiah Yoder

Slide style: Dr. Hornick21

Page 22: CS-2852 Data Structures Week 10, Class 1 Lab 8 notes Big-O revisited CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick 1

Example

Suppose we insert an item into a properly-implemented stack.

What is the Big-O running time?

CS-2852Dr. Josiah Yoder

Slide style: Dr. Hornick22

Page 23: CS-2852 Data Structures Week 10, Class 1 Lab 8 notes Big-O revisited CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick 1

Example

Suppose we remove an item from a properly-implemented circular queue (the wrap-around array implementation)

What is the Big-O running time?

CS-2852Dr. Josiah Yoder

Slide style: Dr. Hornick23

Page 24: CS-2852 Data Structures Week 10, Class 1 Lab 8 notes Big-O revisited CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick 1

Example

What is the Big-O running time of the word search lab?

(in terms of the size of the grid)

CS-2852Dr. Josiah Yoder

Slide style: Dr. Hornick24

Page 25: CS-2852 Data Structures Week 10, Class 1 Lab 8 notes Big-O revisited CS-2852 Dr. Josiah Yoder Slide style: Dr. Hornick 1

CS-2852 Dr. Josiah Yoder

Slide style: Dr. Hornick25