390 likes | 566 Vues
Chapter 4. Analysis Tools. Why we Analyze Algorithms. We try to save computer resources such as : Memory space used to store data structures, Space Complexity CPU time , which is reflected by Algorithm Run Time Complexity. 4.1 Seven Functions. The Constant Function: f(n)= c,
E N D
Chapter 4 Analysis Tools Algorithm Analysis
Why we Analyze Algorithms We try to save computer resources such as: • Memory space used to store data structures, Space Complexity • CPU time, which is reflected by Algorithm Run Time Complexity. Algorithm Analysis
4.1 Seven Functions • The Constant Function:f(n)= c, wherec is some fixed constant, c=5, c=100, or c=210, so we let g(n)=1, so f(n)=cg(n) • The Logarith Function:f(n)= logbn x = logbn iff bx=n , (see Proposition 4.1), the most common base for logarithmic function in computer science is 2. • Linear Function:f(n)= n , important for algorithms on vectors (one dim. Arrays). Algorithm Analysis
The N-Log-N Function:f(n)= nlogn This function grows a little faster than linear function and a lot slower than quadratic function. • The Quadratic Function:f(n)= n2 It’s important for algorithms of nested loops. • The Cubic Function (Polynomial): f(n)= n3 f(n)=a0+a1n+ a2n2+…+adnd, is a polynomial of degree d, where a0 , a1 ,…., ad are constants. • The Exponential Function:f(n)= bn Algorithm Analysis
We define a set of primitive operations such as the following: • Assigning a value to a variable • Calling a method • Performing an arithmetic operation (for example, adding two numbers) • Comparing two numbers • Indexing into an array • Following an object reference • Returning from a method. Algorithm Analysis
Primitive Operations Algorithm Analysis
Counting Primitive Operations Algorithm Analysis
4.2.3 Asymptotic Notation Algorithm Analysis
Example Algorithm Analysis
Properties of Big-Oh Notation Algorithm Analysis
Big-Omega and Big-Theta Algorithm Analysis
Big-Omega • Just as Big-Oh notation provides us a way of saying that a function f(n) is “less than or equal to” another function, • Big-Omega notation provides us a way of saying that a function f(n) is “greater than or equal to” another function. • Let f(n) and g(n) be functions mapping non-negative integers: We say that f(n) is Ω(g(n)) if g(n) is O(f(n)) • That’s there is a real constant c>0 and an integer constant n0 ≥ 1 such that: f(n) ≥ cg(n), for n ≥ n0 Algorithm Analysis
Big-Theta • In addition, there is a notation that allows us to say that two functions grow at the same rate , up to a constant factor. • We say that f(n) is Θ(g(n)) if f(n) is O(g(n)) and f(n) is Ω(g(n)), • That’s, there are real constants c’>0 and c”>0 and an integer constant n0 ≥ 1 such that: c’g(n) ≤ f(n) ≥ c”g(n), for n ≥ n0 . Algorithm Analysis
Example • f(n) = 3nlog n + 4n + 5log n isΘ(nlog n) • Since 3nlog n + 4n + 5log n ≥ 3nlog n for n ≥2 • So, f(n) is Ω(nlog n) • Since 3nlog n + 4n + 5log n ≤ (3+4+5)nlog n for n ≥2 • So, f(n) is O(nlog n) • Thus, f(n) is Θ(nlog n) Algorithm Analysis