algorithm analysis

24
Algorithm Analysis

Upload: elsu

Post on 14-Jan-2016

24 views

Category:

Documents


0 download

DESCRIPTION

Algorithm Analysis. Math Review – 1.2. Exponents X A X B = X A+B X A /X B =X A-B (X A ) B = X AB X N +X N = 2X N ≠ X 2N 2 N+ 2 N = 2 N+1 Logarithms X A =B iff log X B=A log A B =log C B/log C A; A,B,C > 0, A ≠ 1 logAB = logA + logB; A, B > 0 Series N ∑ 2 i = 2 N+1 -1 i=0 N - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Algorithm Analysis

Algorithm Analysis

Page 2: Algorithm Analysis

Math Review – 1.2• Exponents

– XAXB = XA+B

– XA/XB=XA-B

– (XA)B = XAB

– XN+XN = 2XN ≠ X2N

– 2N+2N = 2N+1

• Logarithms– XA=B iff logXB=A– logAB =logCB/logCA; A,B,C > 0, A ≠ 1– logAB = logA + logB; A, B > 0

• Series N

– ∑ 2i = 2N+1-1 i=0

N

– ∑ Ai = AN+1-1/A-1 i=0

N

– ∑ i = N(N+1)/2 i=0

Page 3: Algorithm Analysis

Running Time

• Why do we need to analyze the running time of a program?

• Option 1: Run the program and time it– Why is this option bad?– What can we do about it?

Page 4: Algorithm Analysis

Pseudo-Code

• Used to specify algorithms

• Part English, part code

Algorithm (arrayMax(A, n))

curMax = A[0]

for i=1 i<n i++

if curMax < A[i]

curMax = A[i]

return curMax

Page 5: Algorithm Analysis

Counting Operations

Algorithm (arrayMax(A, n))curMax = A[0]for i=1 i<n i++ if curMax < A[i] curMax = A[i]return curMax

• Best case – 5n • Worst case – 7n-2 • Average case – hard to analyze

Page 6: Algorithm Analysis

Asymptotic Notation

• 7n-2

• n=5 -> 33

• n=100 -> 698

• n=1,000,000 -> 6,999,998

• Running time grows proportionally to n

• What happens as n gets large?

Page 7: Algorithm Analysis

Big-Oh

• T(N) is O(f(N)) if there are positive constants c and n0such that T(N) <= cf(N) when N>=n0

• 7n2-2 is O(n2) n0>=1 c=8

• Upper bound

Page 8: Algorithm Analysis

Omega

• T(N) is Ω(g(N)) if there are positive constants c and n0such that T(N) >= cg(N) when N>=n0

• 7n2-2 is Ω(n2) n0>=1 c=1

• Lower bound

Page 9: Algorithm Analysis

Theta

• T(N) is Θ(h(N)) if and only if T(N) = Oh(N) and T(N) = Ωh(N)

• 7n2-2 is Θ(n2)

• Tightest result possible

Page 10: Algorithm Analysis

Little-Oh

• T(N) is o(p(N)) if T(N) = O(p(N)) and T(N) ≠ Θ(p(N))

• 7n2-2 is o(n3)– O when n0>=8 c=1

• Growth rate of T(N) is smaller than growth rate of p(N)

Page 11: Algorithm Analysis

Rules

• Rule 1– If T1(N) = O(f(N) and T2(N) = O(g(N)), then

• T1(N) + T2(N) = max(O(f(N)), O(g(N)))

• T1(N)* T2(N) = O(f(N)*g(N))

• Rule 2– If T(N) is a polynomial of degree k, then

T(N) = Θ(Nk)

• Rule 3– logkN = O(N) for any constant k. This tells us that

logs grow very slowly.

Page 12: Algorithm Analysis

Examples

• 87n4 + 7n

• 3nlogn + 12logn

• 4n4 + 7n3logn

Page 13: Algorithm Analysis

Terminology

• Logarithmic – O(logn)

• Linear – O(n)

• Quadratic – O(n2)

• Polynomial – O(nk) k>=1

• Exponential – O(an) a>1

Page 14: Algorithm Analysis

Rule 1 – for loops

• The running time of a for loop is at most the running time of the statements inside the for loop (including tests) times the number of iterations.

for(i=0; i<5; i++)

cout << “hello\n”;

Page 15: Algorithm Analysis

Rule 2 – nested loops

• Analyze these inside out. The total running time of a statement inside a group of nested loops is the running time of the statement multiplied by the product of the sizes if all of the loops.

for(i=0; i<n; i++) for(j=0; j<n; j++) cout << “hello\n”;

Page 16: Algorithm Analysis

Rule 3 – consecutive statements

• These just add (which means that the max is the one that counts)

for(i=0; i<n; i++)

a[i] = 0;

for(i=0; i<n; i++)

for(j=0; j<n; j++)

a[i]+=a[j]+i+j;

Page 17: Algorithm Analysis

Rule 4 – if/else

• The running time of an if/else statement is never more than the running time of the test plus the larger of the running times of the statements,

if(condition)

S1

else

S2

Page 18: Algorithm Analysis

Example

• Find maximum number in nxn matrix

• Algorithm:

6 4 … 2

12 3 … 9

… … … …

5 8 … 1

0

n-1

n-10

Page 19: Algorithm Analysis

Example

• What is the big-oh running time of this algorithm?

Algorithm:Input: A, ncurMax = A[0][0]for i=0 i<n i++for j=0 j<n j++

if curMax < A[i][j]curMax = A[i][j]

return curMax

Page 20: Algorithm Analysis

Another Example

• Determine how many elements of array 1 match elements of array 2

• Algorithm?

2 4 … 6n-10

6 8 … 3n-10

Page 21: Algorithm Analysis

Another Example

Algorithm:Input: A, B, n

for i=0 i<n i++for j=0 j<n j++

if A[i] == A[j]matches++break

• What is the running time of the algorithm?

2 4 … 6n-10

6 8 … 3n-10

Page 22: Algorithm Analysis

Logs in the Running Time

• An algorithm is O(logN) if it takes constant time to cut the problem size by a fraction.

• Binary search– Given an integer X and integers A0, A1, …,

AN-1, which are presorted and already in memory, find I such that Ai=X, or return i = -1 if X is not in the input.

Page 23: Algorithm Analysis

Binary Search

• Algorithm?

• Running time is at most ceil(log(N-1))+2 which is O(logN)

Page 24: Algorithm Analysis

Example

• 1 – (N-1) - 1

• 2 – (N-1)/2 - 2

• 3 – (N-1)/4 - 4

• 4 – (N-1)/8 - 8

• i – (N-1)/2i-1 - 2i-1 = N-1– difference between high and low is 1

• difference between high and low is 0