Big O: What does it really mean? In CReicursive fb of IIrah ......Big O: What does it really mean?...
Transcript of Big O: What does it really mean? In CReicursive fb of IIrah ......Big O: What does it really mean?...
Big O: What does it really mean?
ofIn CReicursive f b
primitive ioperations
IIrahve fitin
RunningTime n size ofthe input
Big O is an upper limit on the running
time of an algorithm as n gets large
f n 5 NZ log N t NI 110 00z 0 N'log N
re
n
f n z 5 N'logN 1 Nat 1000g
S 5N logN 1N'logntweign
7 N'log Ningen
flu O Neocon
A more precise definition of Big-O• f(n) and g(n): running times of two algorithms on inputs of size n. • f(n) and g(n) map positive integer inputs to positive reals.
We say f = O(g) if there is a constant c > 0 and k>0 such that f(n) ≤ c · g(n) for all n >= k.
f = O(g) means that “f grows no faster than g”
C
I fCnlike
According to the definition on the previous
slide if f O g f Och for
any h gSo if f 0 N logN then technically
2 0 Ws
f a 0 Wh and so on
But in practice when doing Big o
analysis we look for the lowest
older function that satisfies the
definition ofBig ohthe tightest upper
bound to fCn
Big-Omega• f(n) and g(n): running times of two algorithms on inputs of size n. • f(n) and g(n) map positive integer inputs to positive reals.
We say f = Ω(g) if there are constants c > 0, k>0 such that c · g(n) ≤ f(n) for n >= k
f = Ω(g)means that “f grows at least as fast as g”
Big-Theta• f(n) and g(n): running times of two algorithms on inputs of size n. • f(n) and g(n) map positive integer inputs to positive reals.
We say f = Θ(g) if there are constants c1, c2 , k such that 0 ≤ c1g(n) ≤ f(n) ≤ c2g(n), for n >=k
Running time
Problem Size (n)
What is the Big-O running time of algoX?• Assume dataA is some data structure that supports the following operations
with the given running times, where N is the number of keys stored in the data structure:
• insert: O(log N) • min: O(1) • delete: O(log N)
void algoX(int arr[], int N) { dataA ds;//ds contains no keys for(int i=0; i < N; i=i++) ds.insert(arr[i]); for(int i=0; i < N; i=i++) arr[i] = ds.min(); ds.delete(arr[i]); }
A. O(N2) B. O(N logN) C. O(N) D. O(log N) E. Not enough information to
compute
An
n is the no ofKeys in dsn Dn p
input E
Bad
Dam g
3
Reasons
Each insert takes adifferent
Running time ofamount of time
because
less than the running time depends
this loop is on the number ofkeys
C N togN already in ds
The first insert takesthe
least time the last one
takes the most
Although we don't know
Running time ofthis the exact numberofCoop is less than operations for
each
insert we can findN tag logN an upper limit
Specifically the
Overall running running timeofeach
insert is less thantime is a log N
G Nlog Nt CzN t GNlogN
OCNlogW