23
David Luebke 1 06/21/2 CS 332: Algorithms Asymptotic Performance

David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance

Embed Size (px)

Citation preview

Page 1: David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance

David Luebke 1 04/21/23

CS 332: Algorithms

Asymptotic Performance

Page 2: David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance

David Luebke 2 04/21/23

Review: Asymptotic Performance

• Asymptotic performance: How does algorithm behave as the problem size gets very large?

o Running time

o Memory/storage requirements

Remember that we use the RAM model:o All memory equally expensive to access

o No concurrent operations

o All reasonable instructions take unit time Except, of course, function calls

o Constant word size Unless we are explicitly manipulating bits

Page 3: David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance

David Luebke 3 04/21/23

Review: Running Time

• Number of primitive steps that are executed Except for time of executing a function call most

statements roughly require the same amount of time

We can be more exact if need be

• Worst case vs. average case

Page 4: David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance

David Luebke 4 04/21/23

An Example: Insertion Sort

InsertionSort(A, n) {for i = 2 to n {

key = A[i]j = i - 1;while (j > 0) and (A[j] > key) {

A[j+1] = A[j]j = j - 1

}A[j+1] = key

}

}

Page 5: David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance

David Luebke 5 04/21/23

An Example: Insertion Sort

InsertionSort(A, n) {for i = 2 to n {

key = A[i]j = i - 1;while (j > 0) and (A[j] > key) {

A[j+1] = A[j]j = j - 1

}A[j+1] = key

}

}

10 20 30 40

1 2 3 4

i = 4 j = 1 key = 20A[j] = 10 A[j+1] = 20

Page 6: David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance

David Luebke 6 04/21/23

Insertion Sort

InsertionSort(A, n) {for i = 2 to n {

key = A[i]j = i - 1;while (j > 0) and (A[j] > key) {

A[j+1] = A[j]j = j - 1

}A[j+1] = key

}

}

What is the preconditionfor this loop?

Page 7: David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance

David Luebke 7 04/21/23

Insertion Sort

InsertionSort(A, n) {for i = 2 to n {

key = A[i]j = i - 1;while (j > 0) and (A[j] > key) {

A[j+1] = A[j]j = j - 1

}A[j+1] = key

}

}How many times will this loop execute?

Page 8: David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance

David Luebke 8 04/21/23

Rules

• Rule of sums

• Rule of products

Page 9: David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance

David Luebke 9 04/21/23

Insertion Sort

Statement EffortInsertionSort(A, n) {

for i = 2 to n { c1n (why?)

key = A[i] c2(n-1)

j = i - 1; c3(n-1)

while (j > 0) and (A[j] > key) { c4T

A[j+1] = A[j] c5(T-(n-1))

j = j - 1 c6(T-(n-1))

} 0

A[j+1] = key c7(n-1)

} 0

}

T = t2 + t3 + … + tn where ti is number of while expression evaluations for the ith for loop iteration

Page 10: David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance

David Luebke 10 04/21/23

Analyzing Insertion Sort

• T(n) = c1n + c2(n-1) + c3(n-1) + c4T + c5(T - (n-1)) + c6(T - (n-1)) + c7(n-1)

= c8T + c9n + c10

• What can T be? Best case -- inner loop body never executed

o ti = 1 T(n) is a linear function Worst case -- inner loop body executed for all

previous elementso ti = i T(n) is a quadratic function

Average caseo ???

Page 11: David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance

David Luebke 11 04/21/23

Analysis

• Simplifications Ignore actual and abstract statement costs Order of growth is the interesting measure:

o Highest-order term is what counts Remember, we are doing asymptotic analysis As the input size grows larger it is the high order term that

dominates

Page 12: David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance

David Luebke 12 04/21/23

Upper Bound Notation

• We say InsertionSort’s run time is O(n2) Properly we should say run time is in O(n2) Read O as “Big-O” (you’ll also hear it as “order”)

• In general a function f(n) is O(g(n)) if there exist positive constants c and n0

such that f(n) c g(n) for all n n0

• Formally O(g(n)) = { f(n): positive constants c and n0 such that

f(n) c g(n) n n0

Page 13: David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance

David Luebke 13 04/21/23

Insertion Sort Is O(n2)

• Proof Suppose runtime is an2 + bn + c

o If any of a, b, and c < 0 then a=|a|, .. an2 + bn + c (a + b + c)n2 + (a + b + c)n + (a + b + c) 3(a + b + c)n2 for n 1 Let c’ = 3(a + b + c) and let n0 = 1

• Questions Is InsertionSort O(n3)? Is InsertionSort O(n)?

Page 14: David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance

David Luebke 14 04/21/23

Big O Fact

• A polynomial of degree k is O(nk)

• Proof: Suppose f(n) = bknk + bk-1nk-1 + … + b1n + b0

o Let ai = | bi |

f(n) aknk + ak-1nk-1 + … + a1n + a0

ki

kk

i

ik cnan

n

nan

Page 15: David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance

David Luebke 15 04/21/23

Lower Bound Notation

• We say InsertionSort’s run time is (n)

• In general a function f(n) is (g(n)) if positive constants c and n0 such

that 0 cg(n) f(n) n n0

• Proof: Suppose run time is an + b (a,b constants)

o Assume a and b are positive (what if b is negative?)

an an + b

Page 16: David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance

David Luebke 16 04/21/23

Asymptotic Tight Bound

• A function f(n) is (g(n)) if positive constants c1, c2, and n0 such that

c1 g(n) f(n) c2 g(n) n n0

• Theorem f(n) is (g(n)) iff f(n) is both O(g(n)) and (g(n)) Proof:from definitions

Page 17: David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance

David Luebke 17 04/21/23

Practical Complexity

0

250

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

f(n) = n

f(n) = log(n)

f(n) = n log(n)

f(n) = n 2̂

f(n) = n 3̂

f(n) = 2 n̂

Page 18: David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance

David Luebke 18 04/21/23

Practical Complexity

0

500

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

f(n) = n

f(n) = log(n)

f(n) = n log(n)

f(n) = n 2̂

f(n) = n 3̂

f(n) = 2 n̂

Page 19: David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance

David Luebke 19 04/21/23

Practical Complexity

0

1000

1 3 5 7 9 11 13 15 17 19

f(n) = n

f(n) = log(n)

f(n) = n log(n)

f(n) = n 2̂

f(n) = n 3̂

f(n) = 2 n̂

Page 20: David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance

David Luebke 20 04/21/23

Practical Complexity

0

1000

2000

3000

4000

5000

1 3 5 7 9 11 13 15 17 19

f(n) = n

f(n) = log(n)

f(n) = n log(n)

f(n) = n 2̂

f(n) = n 3̂

f(n) = 2 n̂

Page 21: David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance

David Luebke 21 04/21/23

Practical Complexity

1

10

100

1000

10000

100000

1000000

10000000

1 4 16 64 256 1024 4096 16384 65536

Page 22: David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance

David Luebke 22 04/21/23

Other Asymptotic Notations

• A function f(n) is o(g(n)) if positive constants c and n0 such that

f(n) < c g(n) n n0

• A function f(n) is (g(n)) if positive constants c and n0 such that

c g(n) < f(n) n n0

• Intuitively, o() is like < O() is like

() is like > () is like

() is like =

Page 23: David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance

David Luebke 23 04/21/23

Deriving O(g(n)) for T(n)

• Solving recurrences Substitution method Master theorem (advanced course)