analysis of alg. efficiencyposition evaluation in chess -playing algs. in many algs., we can isolate...

61
Ch.2 Analysis of Alg. Efficiency 2.1 The Analysis Framework

Upload: others

Post on 20-Mar-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

Ch.2Analysis of Alg. Efficiency

2.1 The Analysis Framework

Page 2: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

How to measure the size of the input

It depends on the problem! Examples: degree of a polynomial nr. of elements of an array the two dimensions of a matrix, either separately (m, n), or as a

product m·n If matrix is square , we can measure only n

the maximum depth of a binary tree the number of bits in the binary representation of a (large)

integer n: Verify this formula with

pencil and paper for small integers!

Page 3: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

How to measure running time

Measure the actual time (hrs, min, s, ms, etc.) Problem: The time depends on many factors that are not algorithm-related: the hardware and OS of the underlying computer the compiler/bytecode generator/interpreter today’s computers usually run dozens/hundreds of processes

“in parallel” (!)

Page 4: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

How to measure running time

Identify one or more basic operations of the alg., i.e. the ones that take the most time. Examples: key comparisons in searching and sorting algs. divisions, multiplications and add/sub in numerical algs. position evaluation in chess-playing algs.In many algs., we can isolate one basic op.

Count how many times the basic operation is executed as a function of the size of the input n.

Page 5: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function
Page 6: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function
Page 7: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

ApplicationLet C(n) be the nr. of times the basic operation is executed as a function of the size of the input n.Let cop be the time for the basic oper. to run on a given computer (a better notation would be top).Then we can estimate the running time:

Since we’re estimating anyway, we can retain only the most significant parts of the function C(n). Lower-order terms can be ignored:

Page 8: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

ApplicationActually, the estimate becomes more accurate if we refer to a standard size of the input, for which the time was accurately measured:

QUIZ

Page 9: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

QUIZ: How many times longer would it take for the matrix multiplication algorithm?

Page 10: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

ApplicationActually, the estimate becomes more accurate if we refer to a standard size of the input, for which the time was accurately measured:

Under this scenario, the constant factors can also be ignored!We are left with the order of growth of the function C(n).

Page 11: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

Order of Growth

Note: The base of the logarithm “does not matter”, because changing the base amounts to multiplying by a constant:

…"Linearithmic" function

Page 12: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

Important caveat!

This ordering of the functions does nothold for all values n of the input, but only from a certain value on - see this example:

Page 13: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

QUIZ: How does the time (nr. ops.) increase when n doubles?

Page 14: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

Worst, best and average-caseRunning time often depends not only on an input size but also on the specifics of a particular input.

Cworst(n) = n Cbest(n) = 1 Cave(n) = (n+1)/2 (?)

Page 15: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

Average-case for sequential

search

p is the probability to find the key in the array:

What does this formula become if the key is almost always found? Almost always not found? Found about half the time?

Page 16: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

2.1 Individual work for next time (in notebook!)

End-of-section exercises 1, 8, 10.

Page 17: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

Average-case for sequential

search

p is the probability to find the key in the array:

What does this formula become if the key is almost always found? Almost always not found? Found about half the time?

Page 18: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

QUIZ: Orders of growth

On an input size n = 1000, your algorithms takes 10 min. to complete.You have analyzed it, and concluded that its order of growth is n2∙sqrt(n)∙log2(n).How long would it take to complete with input of size 100,000? Should you attempt to run it?

Page 19: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

QUIZ: Orders of growth

Page 20: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

2.2 Asymptotic Notations

We say that "Big-Oh" is an upper

bound

We say that "Big-Omega"

is a lower bound

We say that theta is a tight bound

Page 21: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

2.2 Asymptotic Notations –formal definitions

The constants n0, c, c1, c2 are for us to choose, depending on the problem. As long as we can find one combination of such constants, the proof is complete! Example on next slide.

t “is smaller than” g

t “is larger than” g

t “is the same as” g

Page 22: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function
Page 23: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

Proof on p.56 – read and understand!

Page 24: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

Using limits to determinethe order of growth

Although less general than the actual definitions, limits are very useful in practice because they allow us to leverage the power of calculus, in particular L’Hopital’s rule and Stirling’s formula!Examples on the next slides.

Page 25: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

Using limits to determinethe order of growth

Page 26: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

Using limits to determinethe order of growth

Page 27: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

Basic efficiency classesEven though asymptotic notation glosses over lower-order terms and constant factors, there are still infinitely many classes of growth (in the theta sense).For example, in between the logarithmic and linear functions from Table 2.1, there are a lot of roots, and neither two of them are thetaof each other:

log n … n1/5 n1/4 n1/3 n1/2 n2/3 n3/4 … n

Page 28: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

Nevertheless, practical algorithms usually fall within one of these basic classes:

Page 29: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

2.2 Individual work for next time (in notebook!)

End-of-section exercise 2

Page 30: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

QUIZ: Orders of growth

Explain Big-Oh, Big-Omega and theta informally, in your own words.

Page 31: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

t “is smaller than” g

t “is larger than” g

t “is the same as” g

Page 32: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

QUIZ: Orders of growth

If t(n) is in O(g(n)), does it follow that g(n) is in Ω(t(n))? Prove it, using the definitions!

Page 33: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

If t(n) is in O(g(n)), does it follow that g(n) is in Ω(t(n))?

Page 34: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

Order of GrowthConsider the functions 𝑛𝑛 and nsin(n). Decide and then prove if either one is Big Oh, Big Omega or Theta of each other.

• Hint: Start by comparing 𝑛𝑛and n.

• Hint: Sketch the functions! Use, for example https://www.desmos.com/calculator

Page 35: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

Order of GrowthConsider the functions 𝑛𝑛 and nsin(n). Decide and then prove if either one is Big Oh, Big Omega or Theta of each other.

𝐴𝐴𝑛𝑛𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴:𝑛𝑛 is O(n), n is Ω( 𝑛𝑛), and

neither is Θ of the other

𝑛𝑛 and nsin(n) are neither O, Ωor Θ of each other!

Page 36: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

2.3 Analysis of Non-recursive Algorithms

a. How to measure the input size?b. What are the basic operations (that get executed the

most)?c. Should we consider them both, or are there grounds for

preferring one over the other?d. Best, worst, average cases?

Page 37: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function
Page 38: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

The plan (meta-algorithm? )

See App. A for other useful formulas!

Page 39: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

a. How to measure the input size? b. What is the basic operation (that gets executed the most)?c. Best, worst, average cases?

a. How to measure the input size? nb. What is the basic operation (that gets executed the most)?

Comparisonc. Best, worst, average cases? See next slide.

Page 40: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

Worst case: Either no early return, or return triggered for the last pair of elements examined.

Page 41: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

Read and take notes

Example 3/p.64:Multiplying two square matricesExample 4/p.66: Finding number of bits to represent an integer.

2.3 Individual work for next time (in notebook!)

End-of-section exercises 1, 4.

Page 42: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

2.4 Analysis of Recursive algs.

a. How to measure the input size? b. What is the basic operation (that gets executed the most)?c. Best, worst, average cases?

Page 43: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

Idea: Since the algorithm itself is recursive, let us express its efficiency recursively as well!

Recurrence relation

What is missing here?

+ or *? Do not confuse the recurrence in the alg. with the recurrence in the analysis of the alg.!

We solve by back-substitution:

Page 44: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

QUIZ: Back-substitution

Page 45: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

The plan

Page 46: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

Individual work for next time (in notebook!)

End-of-section exercise 1 c, d, e. Use back-substitution!

Page 47: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

2.4 Analysis of Recursive algs. - ToH

a. How to measure the input size? b. What is the basic operation (that gets executed the most)?c. Best, worst, average cases?

We’re only examining the best case (optimal solution) this time, since the input changes only with n, the # of disks.

Page 48: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

ToH – Recurrence relation

Solve by backward substitution:

Page 49: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

ToH – Visualizing the tree of

recursive calls

Page 50: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

Assigned reading for next time (take notes in notebook!)

Example 3/pp.75-76

Page 51: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

2.5 Computing nth Fibonacci Number

Back-substitution doesn’t work directly!

We are going to deploy a powerful “machine” for solving recurrence relations: Formula (Theorem) for Linear Recurrences with Constant Coefficients

Page 52: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

Solving a Linear Recurrence w/Constant Coefficients

The Theorem (See App.B) says that the solution to the recurrence is a linear combination of powers of the solutions of the associated characteristic equation:

ax2 + bx + c = 0

x2 - x - 1 = 0

Page 53: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

x2 - x - 1 = 0

Solve quadratic characteristic equation

The recurrence solution must be of the form

Find the coefficients p and q using the boundary conditions:

Binet’s formula

Golden Ratio ≈ 1.618

≈ -0.618

Page 54: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

Binet’s formula

Since the second root is <1 in magnitude, the second term tends to ? when n→ꝏ, so we have

What is the efficiency/order of growth of this algorithm?

Page 55: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

2.5 Computing nth Fibonacci Number

A second algorithm:

Page 56: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

Note that this is an inhomogeneous recurrence!

Do not confuse the recurrence in the alg. with the recurrence

in the analysis of the alg.!

Page 57: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

This is simply the Fibonacci sequence shifted by one!

Page 58: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

Algorithm using the non-recursive (iterative) definition:

A(n) = n – 1 ∈ Θ(n)

Page 59: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

2.6 Empirical Analysis of Algs.We use it when the mathematical analysis is too difficult (or inexistent!)

Page 60: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

Assigned reading for next time (take notes in notebook!)

Read the entire Sec. 2.6.

Think about what we have been doing in the lab with the “gloves” puzzle!

Page 61: Analysis of Alg. Efficiencyposition evaluation in chess -playing algs. In many algs., we can isolate one basic op. Count how many times the basic operation is executed as a function

Homework for Ch.2- due next Wed., Feb.5 -

• Ex. 2.1 (p.21): 7, 9abc (only a, b, and c)– Note: For prob.9, use the limit technique from p.57 of our text!

• Ex. 2.2 (p.60): 5, 6ab– Note: No proofs are required for prob.5, simply write down the list of

functions. Prob. 6 requires proofs!

• Ex. 2.3 (p.67): 2, 5• Ex. 2.4 (p.76): 3, 4• Ex. 2.5 (p.83): 2, 6