Upload
damian-davidson
View
215
Download
0
Embed Size (px)
Citation preview
CS-2852Data Structures
Week 10, Class 1 Lab 8 notes Big-O revisited
CS-2852Dr. Josiah Yoder
Slide style: Dr. Hornick1
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
CS-2852 Dr. Josiah Yoder
Slide style: Dr. Hornick25