340 likes | 663 Views
Lecture 2 Analysis of Algorithms. How to estimate time complexity? Analysis of algorithms Techniques based on Recursions. ACKNOWLEDGEMENTS : Some contents in this lecture source from slides of Yuxi Fu. ROADMAP. How to estimate time complexity? Analysis of algorithms
E N D
Lecture 2Analysis of Algorithms • How to estimate time complexity? • Analysis of algorithms • Techniques based on Recursions ACKNOWLEDGEMENTS: Some contents in this lecture source from slides of Yuxi Fu
ROADMAP • How to estimate time complexity? • Analysis of algorithms • worst case, best case? • Big-O notation Xiaojuan Cai
Observation • System dependent effects. • Hardware: CPU, memory, cache, … • Software: compiler, interpreter, garbage collector, … • System: operating system, network, other applications, … • Easy, butdifficult to get precise measurements. Xiaojuan Cai
% 1-sum int count = 0; for (int i = 0; i < N; i++) if (a[i] == 0) count++; Mathematical model Total running time =sum of cost × frequency for all operations. • Cost depends on machine, compiler. • Frequency depends on algorithm, input data. Xiaojuan Cai
Mathematical model • Relative rather than Absolute. • Machine independent. • About large input instance • Time complexity of an algorithm is a function of the size of the input n. Xiaojuan Cai
Input size -- convention • Sorting and searching: the size of the array • Graph: the number of the vertices and edges • Computational geometry: the number points or line segments • Matrix operation: the dimensions of the input matrices • Number theory and Cryptography: the number of bits of the input. Xiaojuan Cai
Where are we? • How to estimate time complexity? • Analysis of algorithms • worst case, best case? • Big-O notation Xiaojuan Cai
Searching • Problem: Searching • Input: An integer array A[1...n], and an integer x • Output: Does x exist in A? Does 45 exist in: 9, 8, 9, 6, 2, 56, 12, 5, 4, 30, 67, 93, 25, 44, 96 2, 4, 5, 6, 8, 9, 9, 12, 25, 30, 44, 56, 67, 93, 96 Linear searching v.s. Binary searching Xiaojuan Cai
Binary searching 2, 4, 5, 6, 8, 9, 9, 12, 25, 30, 44, 56, 67, 93, 96 mid low high Xiaojuan Cai
Quiz • A. 1 B.logn. C. n D. none of above Xiaojuan Cai
Computational Complexity Xiaojuan Cai
Where are we? • How to estimate time complexity? • Analysis of algorithms • worst case, best case? • Big-O notation Xiaojuan Cai
Definition (O-notation) Let f(n) and g(n) : f(n) is said to be O(g(n)), written f (n) = O(g(n)),if∃c>0.∃n0.∀n ≥ n0.f (n) ≤ cg(n) Big-O Notation • The O-notation provides an upper bound of the running time; • f grows no faster than some constant times g. Xiaojuan Cai
Examples • 10n2 + 20n = O(n2). • logn2 =O(logn). • log(n!) = O(nlogn). • Hanoi: T(n) = O(2n), n is the input size. • Searching: T(n) = O(n) Xiaojuan Cai
Definition (Ω-notation) Let f(n) and g(n) : f(n) is said to be Ω(g(n)), written f (n) = Ω(g(n)), if∃c>0.∃n0.∀n ≥ n0.f (n) ≥cg(n) Big-Ω Notation • The Ω-notation provides an lower bound of the running time; • f(n) = O(g(n)) iff g(n) = Ω(f(n)). Xiaojuan Cai
Does there exist f,g, such that f = O(g) and f = Ω(g)? Examples • log nk = Ω(logn). • log n! = Ω(nlogn). • n! = Ω(2n). • Searching: T(n) = Ω(1) Xiaojuan Cai
Big-Θ notation • log n! = Θ(nlogn). • 10n2 + 20 n = Θ(n2) Definition (Θ-notation) f(n)=Θ(g(n)), if bothf(n)=O(g(n)) and f(n) = Ω (g(n)). Xiaojuan Cai
Definition (o-notation) Let f(n) and g(n) : f(n) is said to be o(g(n)), written f (n) = o(g(n)), if∀c.∃n0.∀n ≥ n0.f (n) < cg(n) Small o-notation • log n! = o(nlogn)? • 10n2 + 20 n = o(n2)? • nlog n = o(n2)? Xiaojuan Cai
Definition (ω-notation) Let f(n) and g(n) : f(n) is said to be ω(g(n)), written f (n) = ω(g(n)), if∀c.∃n0.∀n ≥ n0.f (n) > cg(n) Small ω-notation Xiaojuan Cai
Quiz • A. O B.Ω. C. Θ D. none of above Xiaojuan Cai
Approximation by Integration Xiaojuan Cai
Suppose exists In terms of limits Xiaojuan Cai
Definition (Complexity class) An equivalence relation R on the set of complexity functions is defined as follows: f Rg if and only if f (n) = Θ(g (n)). Complexity class Xiaojuan Cai
Order of growth Assume the computer consumes 10-6s per instruction Xiaojuan Cai
Order of growth Xiaojuan Cai
Quiz • A. Θ(n2) B.Θ(n). C. Θ(nlogn) D. none of above Xiaojuan Cai
Quiz • A. Θ(n2) B.Θ(n). C. Θ(nlogn) D. none of above Xiaojuan Cai
Quiz • A. Θ(n2) B.Θ(n). C. Θ(nlogn) D. none of above Xiaojuan Cai
Quiz • A. Θ(n2) B.Θ(n). C. Θ(nlogn) D. none of above Xiaojuan Cai
Quiz • A. Θ(n2) B.Θ(n). C. Θ(nlogn) D. none of above Xiaojuan Cai
Quiz • θ(nloglogn) • A. Θ(n2) B.Θ(n). C. Θ(nlogn) D. none of above Xiaojuan Cai
Amortized analysis The total number of append is n. The total number of delete is between 0 and n-1. So the time complexity is O(n). Xiaojuan Cai
Memory • Time: Time complexity • faster, better. • Memory: Space complexity • less, better. • Space complexity <= Time complexity Xiaojuan Cai
Conclusion • How to estimate time complexity? • Analysis of algorithms • worst case, best case? • Big-O notation Xiaojuan Cai