Documente Academic
Documente Profesional
Documente Cultură
big-O session
TA Li Xin
Review Big-O, Omega-O
– To describe how long the algorithm takes using big-O and
Omega-O.
T(N)=O(f(N)): the growth rate of T(N)<= that of f(N)
– T(N)<=c*f(N)
T(N)=Ω(g(N)):the growth rate of T(N)<=that of g(N)
– T(N)>=c*f(N)
When we say that T (N)=O(f(N)), we are guaranteeing that the
function T(N) grows at a rate no faster than f(N). Thus, f(N) is
an upper bound on T(N).
Similarly, when T(N)=Ω(g(N)), we say g(N) is a lower bound on
T(N).
2
Review Big-O, Omega-O
4
ωnotation
Definition: A theoretical measure of the execution of
an algorithm, usually the time or memory needed,
given the problem size n, which is usually the number
of items. Informally, saying some equation f(n) = ω
(g(n)) means g(n) becomes insignificant relative to f(n)
as n goes to infinity.
Formal Definition: f(n) = ω (g(n)) means that for any
positive constant c, there exists a constant k, such that
0 ≤ cg(n) < f(n) for all n ≥ k. The value of k must not
depend on n, but may depend on c.
5
Examples
1. What is the big-O of 2 n2 + 1000n + 5 ?
2. T(n) = 2 n2 + 1000n + 5; f(n) = n2
3. T(n) <= c f(n) ? 2 n2 + 1000n + 5 <= c n2 ?
4. for c = 3, n0 = 1001,check: 2(10012) + 1000(1001) + 5
= 3005007;3(10012) = 3006003
5. answer: O(n2)
6. An algorithm actually takes n5 + n3 + 7, we say its
complexity is O(n5).
7. This is only for addition! Obviously if the algorithm
takes n5 * n3 its complexity is O(n8)!
6
Question
If an algorithm reads a whole list the length of
which is n, it will takes n time.
– What is its complexity?
If a modified algorithm reads through the whole
list twice.
– What is its complexity?
Answer:
Both are O(n)
For T(N)<=c*f(N), here c=2, f(N)=n
7
Class O(1)
Function/order
– Constant time
Examples
– Find the ith element in an array.
– A[i]
Remarks
– The running time of the algorithm doesn't depend on
the value of n.
8
Class O(logan)
Function/order
– Logarithmic time
Examples
– binary search
Remarks
– Typically achieved by dividing the problem into smaller
segments and only looking at one input element in each
segment.
– Binary Search: Every time you go through the recursion
(binary search uses recursion), the problem is reduced in half.
So the most amount of times you can go through the recursion
is log2n.
9
Class O(n)
Function/order
– Linear time
Examples
– Find the minimum element, printing, listing
Remarks
– Typically achieved by examining each element in
the input once.
– The running time of the algorithm is proportional to
the value of n.
10
Consider below algorithm
11
Class O(n * log(n))
Examples
– heapsort, mergesort
Remarks
– Typically achieved by dividing the problem into subproblems, solving
the subproblems independently, and then combining the results.
Unlike the log N algorithms, each element in the subproblems must
be examined.
12
Class O(n2)
Function/order
– Quadratic time
Examples
– bubblesort, insertion sort
Remarks
– Typically achieved by examining all pairs of data elements
– Why is insertion sort quadratic?
– Comparisons :
(1 + 2 + ... + n) = n(n + 1)/2 = n2/2 + n/2 = O(n2)
13
Class O(n!)
Function/order
– Factorial time
Examples
– TSP
Remarks
– TSP is the Travelling Salesman Problem. Let's say you are a
salesperson and you have to visit each of n cities to sell your
product. You want to visit all the cities and return to the city you
came from. You want to find the shortest path possible, visiting
all the cities.Obviously, it would be naive to try all the
permutations. There are (n-1)! permutations, and each
permutation requires computation worth n (add n distances), for
a total of n!. Many tried their hand on this problem, but few have
14 found a good solution for it.
Figure showing
15
Algorithm Analysis(1)
sum = 0;
for (i=0; i<3; i++)
for (j=0; j<n; j++)
sum++;
16
Algorithm Analysis(2)
19
Exercises
1. Classify each of the following functions as O(1), O(n),
O(n^2), O(log n), or O(nlog n), O(1)
(a) 5n + 7
(b) n^2− 5n
(c) 5log n + 10
(d) n(log n + n)
(e) n(log n + 1)
(f) 3 + sinnπ
20
Three algorithms for the same problem have complexity
functions as follows:
Algorithm A: 4n + 10
Algorithm B: 2n + 40
Algorithm C: n^2+ 5
Illustrate the comparative performance of the three algorithms by
sketching their complexity functions together in a graph, and
hence determine, for each algorithm, the range of values of n for
which it performs more efficiently than the other two algorithms.
21