cisc220 spring 2010 james atlas lecture 06: linked lists (2), big o notation
Post on 20-Dec-2015
215 views
TRANSCRIPT
![Page 1: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d4b5503460f94a28635/html5/thumbnails/1.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d4b5503460f94a28635/html5/thumbnails/2.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d4b5503460f94a28635/html5/thumbnails/3.jpg)
Collection
• add(x)
• remove(x)
• member(x)
• size()
• first()
![Page 4: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d4b5503460f94a28635/html5/thumbnails/4.jpg)
Single Linked List
![Page 5: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d4b5503460f94a28635/html5/thumbnails/5.jpg)
Class Exercise
• Implement add, member, first
![Page 6: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d4b5503460f94a28635/html5/thumbnails/6.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d4b5503460f94a28635/html5/thumbnails/7.jpg)
Example 2.14/2.16
![Page 8: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d4b5503460f94a28635/html5/thumbnails/8.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d4b5503460f94a28635/html5/thumbnails/9.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d4b5503460f94a28635/html5/thumbnails/10.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d4b5503460f94a28635/html5/thumbnails/11.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d4b5503460f94a28635/html5/thumbnails/12.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d4b5503460f94a28635/html5/thumbnails/13.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d4b5503460f94a28635/html5/thumbnails/14.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d4b5503460f94a28635/html5/thumbnails/15.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d4b5503460f94a28635/html5/thumbnails/16.jpg)
n2 + 25n + 25 vs. 3n2
![Page 17: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d4b5503460f94a28635/html5/thumbnails/17.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d4b5503460f94a28635/html5/thumbnails/18.jpg)
Various Runtimes
![Page 19: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d4b5503460f94a28635/html5/thumbnails/19.jpg)
Powers of n (Order of the polynomial)
![Page 20: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d4b5503460f94a28635/html5/thumbnails/20.jpg)
Multiplicative Constants
![Page 21: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d4b5503460f94a28635/html5/thumbnails/21.jpg)
Multiplicative Constants (cont’)
![Page 22: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d4b5503460f94a28635/html5/thumbnails/22.jpg)
Dominant Terms (1)
![Page 23: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d4b5503460f94a28635/html5/thumbnails/23.jpg)
Dominant Terms (2)
![Page 24: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d4b5503460f94a28635/html5/thumbnails/24.jpg)
Dominant Terms (3)
![Page 25: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d4b5503460f94a28635/html5/thumbnails/25.jpg)
Dominant Terms Comparison
![Page 26: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d4b5503460f94a28635/html5/thumbnails/26.jpg)
Dominant Terms Comparison (cont’)
![Page 27: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d4b5503460f94a28635/html5/thumbnails/27.jpg)
Class Exercise
• Implement last on our Linked List
![Page 28: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d4b5503460f94a28635/html5/thumbnails/28.jpg)
Group Exercise
• Implement last in O(1)
![Page 29: CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d4b5503460f94a28635/html5/thumbnails/29.jpg)