View
215
Download
0
Tags:
Embed Size (px)
Citation preview
CISC220Spring 2010James 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
Collection
• add(x)
• remove(x)
• member(x)
• size()
• first()
Single Linked List
Class Exercise
• Implement add, member, first
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
Example 2.14/2.16
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)
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)
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
Let’s count individual instructionsfor (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
Simple Statement
}
}
n2 executions
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
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
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
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)
n2 + 25n + 25 vs. 3n2
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!)
Various Runtimes
Powers of n (Order of the polynomial)
Multiplicative Constants
Multiplicative Constants (cont’)
Dominant Terms (1)
Dominant Terms (2)
Dominant Terms (3)
Dominant Terms Comparison
Dominant Terms Comparison (cont’)
Class Exercise
• Implement last on our Linked List
Group Exercise
• Implement last in O(1)