Computational complexity The same problem can frequently be solved with different algorithms which differ in efficiency. Computational complexity is a measure of the degree of difficulty of an algorithm and it is used to compare the efficiency of algorithms. Computational complexity indicates how much effort is needed to apply an algorithm or how costly it is. Data Structures and Algorithms in Java, Third EditionCh02 – 1
The efficiency of execution of an algorithm depends on the hardware, programming language used, compiler, programming skill, etc. To evaluate an algorithm’s efficiency, logical time units that express a relationship between the size of an input and the amount of time and space required to process the input should be used. Computational complexity Data Structures and Algorithms in Java, Third EditionCh02 – 2
Big-O notation: definition f(n) is O(g(n)) ↔ there exist numbers c, N > 0 such that for each n ≥ N f(n) ≤ c∙g(n) The meaning: f(n) is larger than g(n) only for finite number of n’s a constant c and a value N can be found so that for every value of n ≥ N: f(n) ≤ c∙g(n) f(n) does not grow more than a constant factor faster than g(n) Data Structures and Algorithms in Java, Third EditionCh02 – 3
Big-O notation: illustration N c∙g(n)c∙g(n) f(n)f(n) n Data Structures and Algorithms in Java, Third EditionCh02 – 4
Big-O notation: properties 1.(transitivity) If f(n) is O(g(n)) and g(n) is O(h(n)), then f(n) is O(h(n)) (i.e., f(n) = O(g(n)) = O(O(h(n))) = O(h(n)). 2.If f(n) is O(h(n)) and g(n) is O(h(n)), then f(n) + g(n) is O(h(n)). 3.an k is O(n k ). 4.n k is O(n k+j ) for any j > 0. 5.If f(n) = cg(n), then f(n) = O(g(n)). 6.log a n = O(log b n) for any numbers a, b > 1. 7.log a n is O(log 2 n) for any positive a ≠ 1 Data Structures and Algorithms in Java, Third EditionCh02 – 5
Big-O notation: examples These functions are O(n 2 ): 3n n n 3n 2 + log 10 n These functions are not O(n 2 ): 3n n 3 Data Structures and Algorithms in Java, Third EditionCh02 – 6
Θ notation: definition f(n) is Θ(g(n)) ↔ there exist numbers c 1, c 2, N > 0 such that for each n ≥ N c 1 ∙g(n) ≤ f(n) ≤ c 2 ∙g(n) big-O notationbig-Ω notation Data Structures and Algorithms in Java, Third EditionCh02 – 7
Θ notation: illustration c2∙g(n)c2∙g(n) f(n)f(n) c1∙g(n)c1∙g(n) N n Data Structures and Algorithms in Java, Third EditionCh02 – 8
Θ notation: examples These functions are Θ (n 2 ): 3n n n 2 + log 10 n These functions are not Θ (n 2 ): 3n 3n n 3 it is O(n 2 ) they are Ω(n 2 ) Data Structures and Algorithms in Java, Third EditionCh02 – 9