Escolar Documentos
Profissional Documentos
Cultura Documentos
Outline
Defined
Big-Oh Notation
Complexity Analysis
Complexity
Rate at which storage or time grows
as a function of the problem size
Asymptotic Analysis
Examples
Access element in an array
Retrieve the first element in a list
Code Comparison
Put_line(“Enter an integer:”) ;
Get(N) ;
Sum = 0 ;
For I in 1..N do
Sum += I ;
Put_line(sum) ;
Code Comparison
Put_line(“Enter an integer:”) ;
Get(N) ;
Sum = (N* (N+1)) / 2 ;
Put_line(sum) ;
O(N)
Examples
Searching for element in a list that
does not exist.
Searching through a Binary Tree of
size N where a value does not exist.
O(log N)
Examples
Example, a full balanced Binary
Search Tree
Can eliminate half of the BST every
time the search
Any algorithm that eliminates a large
portion of the data set at each
iteration is generalized into (OlogN)
O(NM)
O(MN)
Big-O
Big-O
Big-O Examples
Big-O Examples
Big-O Simplifications
Big-O Simplifications
Big-O Simplifications
O: Upper Bounding Function
Def
f(n)= O(g(n)) if ∃ c >0 and n0 > 0 such
that 0 ≤ f(n) ≤ cg(n) for all n ≥ n0.
Intuition: f(n) “≤ ” g(n) when we
ignore constant multiples and small
values of n.
O: Upper Bounding Function
O: Upper Bounding Function
Def
f(n)= O(g(n)) if ∃ c >0 and n0 > 0 such
that 0 ≤ f(n) ≤ cg(n) for all n ≥ n0.
Intuition: f(n) “≤ ” g(n) when we
ignore constant multiples and small
values of n.
Ω : Lower Bounding Function
Def
f(n)= O(g(n)) if ∃ c >0 and n0 > 0 such
that 0 ≤ f(n) ≤ cg(n) for all n ≥ n0.
Intuition: f(n) “≤ ” g(n) when we
ignore constant multiples and small
values of n.
Ω : Lower Bounding Function
θ: Tightly Bounding Function
Def
f(n)= θ(g(n)) if ∃ c1, c2 >0 and n0 > 0
such that 0 ≤ c1g(n) ≤ f(n) ≤ c2 g(n)
for all n ≥ n0.
Intuition: f(n) “ = ” g(n) when we
ignore constant multiples and small
values of n.
θ: Tightly Bounding Function
Binary Search
.
Max. in an Array
Max. in an Array
Growth Rate
Constant Factors
Big – O Notation
Big – O Notation
Big – O Examples
Big – O and Growth Rate
Big – O Rules
Computing Prefix Averages
Computing Prefix Averages
Computing Prefix Averages
Computing Prefix Averages
Big-Oh
Big-Oh
Big-Oh
N
Calculate
∑ i 3
i =1
1
1
2
2N+2
3 4N
4 1
N2 / 2 – 3N = O(N2)
1 + 4N = O(N)
7N2 + 10N + 3 = O(N2) = O(N3)
log10 N = log2 N / log2 10 = O(log2 N) =
O(log N)
sin N = O(1); 10 = O(1), 1010 = O(1)
Big-Oh Examples
log N + N = O(N)
logk N = O(N) for any constant k
N = O(2N), but 2N is not O(N)
210N is not O(2N)
Big-Oh Examples
Consecutive Statements
y = 3x 3+5x 2 –9
y=x 3
y=x 2
y=x
Big-Oh Examples
Find k so that
3x 3 + 5x 2 – 9 ≤ 5x 3
for x > k
Collect terms: 5x 2 ≤ 2x 3 + 9
What k will make 5x 2 ≤ x 3 for x > k ?
k=5!
So for x > 5, 5x 2 ≤ x 3 ≤ 2x 3 + 9
Solution: C = 5, k = 5 (not unique!)