COMP2230 Introduction To Algorithmics: Lecture Overview
COMP2230 Introduction To Algorithmics: Lecture Overview
Lecture Overview
Maths Review - examples Analysis of Algorithms - Text, Section 2.3 Recurrence Relations Text, Section 2.4
Binomial Coefficients
n k n! (n-k)!k! 0
n(n-1)(n-2) . . . (n-k+1) k! 1
1 2 3 4 5
2
12 13 23 14 24 34 15 25 35 45
3
123 124 134 234 125 135 235 145 245 345
4
1234 1235 1245 1345 2345
5
12345
Knigsberg Bridges
(solved by the Swiss mathematician Leonard Euler (1707-1783) in 1736)
Can you cross all the bridges exactly once and return to the starting point?
Analysis of Algorithms
Analysis of algorithms refers to estimating the time and space required to execute the algorithm. We shall be mostly concerned with time complexity. Problems for which there exist polynomial time algorithms are considered to have an efficient solution.
Analysis of Algorithms
Problems can be:
Unsolvable
These problems are so hard that there does not exist an algorithm that can solve them. Example is the famous Halting problem.
Intractable
These problems can be solved, but for some instances the time required is exponential and thus these problems are not always solvable in practice, even for small sizes of input.
Analysis of Algorithms
Problems can be (continued):
Problems of unknown complexity
These are, for example, NP -complete problems; for these problems neither there is a known polynomial time algorithm, nor they have been shown to be intractable.
Analysis of Algorithms
We shall not try to calculate the exact time needed to execute an algorithm; this would be very difficult to do, and it would depend on the implementation, platform, etc. We shall only be estimating the time needed for algorithm execution, as a function of the size of the input. We shall estimate the running time by counting some dominant instructions. A barometer instruction is an instruction that is executed at least as often as any other instruction in the algorithm.
Analysis of Algorithms
Worst-case time is the maximum time needed to execute the algorithm, taken over all inputs of size n. Average-case time is the average time needed to execute the algorithm, taken over all inputs of size n. Best-case time is the minimum time needed to execute the algorithm, taken over all inputs of size n.
Analysis of Algorithms
It is usually harder to analyse the average-case behaviour of the algorithm than the best and the worst case. Behaviour of the algorithm that is the most important depends on the application. For example, for algorithm that is controlling a nuclear reactor, the worst case analysis is clearly very important. On the other hand, for an algorithm that is used in a noncritical application and runs on a variety of different inputs, the average case may be more appropriate.
Example 2.3.1 Finding the Maximum Value in an Array Using a While Loop
This algorithm finds the largest number in the array s[1], s[2], ... , s[ n]. Input Parameter: s Output Parameters: None array_max_ver1(s) { large = s [1] i = 2 while (i = s. last ) { if (s [i] > large) large = s [i] // larger value found i = i + 1 } return large }
The input is an array of size n. What can be used as a barometer ? The number of iterations of while loop appears to be a reasonable estimate of execution time - The loop is always executed n-1 times. Well use the comparison i = s.last as a barometer. The worst-case and the average-case (as well as the best case!) times are the same and equal to n.
Suppose that the worst-case time of an algorithm is (for input of size n) t(n) = 60n2 + 5n + 1 For large n, t(n) grows as 60n2: n 10 100 1000 T(n)=60n2 + 5n + 1 6,051 600,501 60,005,001 60n2 6,000 600,000 60,000,000 6,000,000,000
10,000 6,000,050,001
The constant 60 is actually not important as it does not affect the growth of t(n) with n. We say that t(n) is of order n2, and we write it as t(n) = (n2) (t(n) is theta of n 2)
Let f and g be nonnegative functions on the positive integers. Asymptotic upper bound: f(n) =O(g(n)) if there exist constants C1 > 0 and N1 such that f(n) C1 g(n) for all n N1 . We read f(n) =O(g(n)) as f(n) is big oh of g(n)) or f(n) is of order at most g(n).
C1g(n) f(n)
N1
Asymptotic lower bound: f(n) = (g(n)) if there exist constants C2 > 0 and N2 such that f(n) C2 g(n) for all n N2 . We read f(n) = (g(n)) as f(n) is of order at least g(n) or f(n) is omega of g(n)).
f(n) C2g(n)
N2
Asymptotic tight bound: f(n) =(g(n)) if there exist constants C1 ,C2 > 0 and N such that C2 g(n) f(n) C1 g(n) for all n N. Equivalently, f(n) =(g(n)) if f(n) =O(g(n)) and f(n) =(g(n)) ). We read f(n) =(g(n)) as f(n) is of order g(n) or f(n) is theta of g(n)) Note that n=O(2n) but n (2n).
C1g(n) f(n) C2g(n)
Example
Let us now formally show that if t(n) = 60n2 + 5n + 1 then t(n) = (n2 ). Since t(n) = 60n2 + 5n + 1 60n2 + 5n2 + n2 = 66n2 for all n 1 it follows that t(n) = 60n2 + 5n + 1 = O(n2 ). Since t(n) = 60n2 + 5n + 1 60n2 for all n 1 it follows that t(n) = 60n2 + 5n + 1 = (n2 ). Since t(n) = 60n2 + 5n + 1 = O(n2 ) and t(n) = 60n2 + 5n + 1 = (n2 ) it follows that t(n) = 60n2 + 5n + 1 = (n2 ).
Name
Constant Log log Log Sublinear Linear n log n Quadratic Cubic Polynomial Exponential Factorial
The running time of different algorithms on a processor performing one million high level instructions per second (from Algorithm Design, by J. Kleinberg and E Tardos).
n
10 30 50 100 1,000 10,000 100,000
n
< 1 sec < 1 sec < 1 sec < 1 sec < 1 sec < 1 sec < 1 sec
nlogn
< 1 sec < 1 sec < 1 sec < 1 sec < 1 sec < 1 sec 2 sec 20 sec
n2
< 1 sec < 1 sec < 1 sec < 1 sec 1 sec 2 min
n3
< 1 sec < 1 sec < 1 sec 1 sec 18 min 12 days
1.5n
< 1 sec < 1 sec 11 min 12,892 y. >1025 y. >1025 y. >1025 y. >1025 y.
2n
< 1 sec 18 min 36 years 1017 y. >1025 y. >1025 y. >1025 y. >1025 y.
n!
4 sec 1025 y. >1025 y. >1025 y. >1025 y. >1025 y. >1025 y. >1025 y.
1,000,000 1 sec
Properties of Big Oh
If f(x) = O(g(x)) and h(x) = O(g(x)) then f(x)+h(x) = O( max { g(x),g(x) } ) and f(x) h(x) = O( g(x) g(x) )
If
Examples
1. 5 n2 1000 n + 8 = O(n2) ? 2. 5 n2 1000 n + 8 = O(n3) ? 3. 5 n2 + 1000 n + 8 = O(n) ? 4. nn = O(2n) ? 5. If f(n) = (g(n)), then 2f(n) = (2g(n)) ? 6. If f(n) = O(g(n)), then g(n) = (f(n)) ?
Examples
1. 2. 3. 5 n2 1000 n + 8 = O(n2) ? True 5 n2 1000 n + 8 = O(n3) ? True 5 n2 + 1000 n + 8 = O(n) ? False n2 grows faster than n nn = O(2n) ? False nn grows faster than 2n If f(n) = (g(n)), then 2f(n) = (2g(n)) ? False here constants matter, as they become exponents !!! E.g., f(n) = 3n, g(n) = n, 23n (2 n) If f(n) = O(g(n)), then g(n) = (f(n)) ? True if f(n) Cg(n) for n N, then g(n) f(n)/C for n N.
4.
5.
6.
Examples
7. 8. 9. If f(n) = (g(n)), and g(n) = (h(n)), then f(n) + g(n) = (h(n)) ? If f(n) = O(g(n)), then g(n) = O(f(n)) ? If f(n) = (g(n)), then g(n) = (f(n)) ?
10. f(n) + g(n) = (h(n)) where h(n) = max {f(n), g(n)} ? 11. f(n) + g(n) = (h(n)) where h(n) = min {f(n), g(n)} ?
Examples
7. If f(n) = (g(n)), and g(n) = (h(n)), then f(n) + g(n) = (h(n)) ? True If f(n) = O(g(n)), then g(n) = O(f(n)) ? False e.g., f(n) = n, g(n) = n2 If f(n) = (g(n)), then g(n) = (f(n)) ? True
8.
9.
10. f(n) + g(n) = (h(n)) where h(n) = max {f(n), g(n)} ? True 11. f(n) + g(n) = (h(n)) where h(n) = min {f(n), g(n)} ? False f(n) = n, g(n) = n2 , h(n) n
Recurrence Relations
A recurrence relation for the sequence a0 , a1, is an equation that relates an to certain of its predecessors a0 , a1 , , an-1 . Initial conditions for the sequence a0 , a1 , are explicitly given values for the finite number of terms of the sequence. Example: Fibonacci sequence f n = fn-1 + fn-2 , n 3
Initial conditions: f1 = f2 = 1.
Or, equivalently,
t0 = 0 tn = 2tn-1 + 1, n > 0
Example 2.4.3
example(n ) { if (n == 1) return for i = 1 to n x = x + 1 example(n /2) } Recurrence relation: C1 = 0, Cn = n + Cn/2, n>1