cisc220 spring 2010 james atlas lecture 06: linked lists (2), big o notation

29
CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation

Post on 20-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation

CISC220Spring 2010James Atlas

Lecture 06: Linked Lists (2),

Big O Notation

Page 2: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation

Objectives for Today

• Introduce Big-O notation

• Reading - K+W Chap 4.1-4.5, 2.6

Page 3: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation

Collection

• add(x)

• remove(x)

• member(x)

• size()

• first()

Page 4: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation

Single Linked List

Page 5: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation

Class Exercise

• Implement add, member, first

Page 6: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation

Efficiency of Algorithms

• An operation for our ADT can be thought of as a “problem”

• An algorithm solves the “problem”– A series of steps

– Each step has a cost• Time

• Space

– Efficiency is a measurement of this cost

Page 7: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation

Example 2.14/2.16

Page 8: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation

Big-O notation

• Describes the relationship between input size and execution time

• If we double the number of inputs, n, and the execution time is approximately doubled– Linear growth rate– Growth rate has an order of n– O(n)

Page 9: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation

Example 2.14/2.16 Analysis

• 2.14– execution time proportional to x_length– O(n)

• 2.16– execution time proportional to (x_length)2

– O(n2)

Page 10: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation

Let’s count individual instructionsfor (int i = 0; i < n; i++) {

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

Simple Statement

}

}

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

Simple Statement 1

Simple Statement 2

Simple Statement 3

Simple Statement 4

Simple Statement 5

}

Simple Statement 1

Simple Statement 2

...

Simple Statement 25

Page 11: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation

Let’s count individual instructionsfor (int i = 0; i < n; i++) {

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

Simple Statement

}

}

n2 executions

Page 12: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation

Let’s count individual instructionsfor (int i = 0; i < n; i++) {

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

Simple Statement

}

}

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

Simple Statement 1

Simple Statement 2

Simple Statement 3

Simple Statement 4

Simple Statement 5

}

n executions,5 statements per= 5n

Page 13: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation

Let’s count individual instructionsfor (int i = 0; i < n; i++) {

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

Simple Statement

}

}

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

Simple Statement 1

Simple Statement 2

Simple Statement 3

Simple Statement 4

Simple Statement 5

}

Simple Statement 1

Simple Statement 2

...

Simple Statement 25

1 execution,25 statements = 25

Page 14: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation

Let’s count individual instructionsfor (int i = 0; i < n; i++) {

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

Simple Statement

}

}

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

Simple Statement 1

Simple Statement 2

Simple Statement 3

Simple Statement 4

Simple Statement 5

}

Simple Statement 1

Simple Statement 2

...

Simple Statement 25

T(n) = total execution time as a function of n

T(n) = n2 + 5n + 25

Page 15: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation

Formal Big-O Definition

T(n) = n2 + 5n + 25

T(n) = O(f(n)) means that there exists a function, f(n), that for sufficiently large n and some constant c:

cf(n) T(n)

Page 16: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation

n2 + 25n + 25 vs. 3n2

Page 17: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation

Common Big-O Runtimes

• Constant -- O(1)

• Logarithmic -- O(log n)

• Fractional -- O(sqrt n)

• Linear -- O(n)

• Log-Linear-- O(n log n)

• Quadratic -- O(n2)

• Cubic -- O(n3)

• Exponential -- O(2n)

• Factorial -- O(n!)

Page 18: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation

Various Runtimes

Page 19: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation

Powers of n (Order of the polynomial)

Page 20: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation

Multiplicative Constants

Page 21: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation

Multiplicative Constants (cont’)

Page 22: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation

Dominant Terms (1)

Page 23: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation

Dominant Terms (2)

Page 24: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation

Dominant Terms (3)

Page 25: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation

Dominant Terms Comparison

Page 26: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation

Dominant Terms Comparison (cont’)

Page 27: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation

Class Exercise

• Implement last on our Linked List

Page 28: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation

Group Exercise

• Implement last in O(1)

Page 29: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation