Class 19: Analyzing Algorithms

18
Class 19: Analyzing Algorithms cs1120 Fall 2011 David Evans 5 October 2011

description

XOR MachinesProof by ConstructionAlgorithm AnalysisTuring Machine

Transcript of Class 19: Analyzing Algorithms

Page 1: Class 19: Analyzing Algorithms

Class 19: Analyzing Algorithms

cs1120 Fall 2011David Evans5 October 2011

Page 2: Class 19: Analyzing Algorithms

2

Plan

Asymptotic Operators RecapAnalyzing biggerHow Bletchley Park Really Broke Lorenz

(maybe not until Friday)

Assistant Coaches’ Review Sessions for Exam 1:Tuesday (tomorrow), 6:30pm, Rice 442

Wednesday, 7:30pm, Rice 442

Page 3: Class 19: Analyzing Algorithms

3

XOR MachineA B

FALSE

TRUE

TRUE

Design by Alex Shin

Page 4: Class 19: Analyzing Algorithms

4

XOR Machine

NOT from book

FloatWine

Design by Anthony Teate

FloatWine

WineFloatWine

Page 5: Class 19: Analyzing Algorithms

5

XOR Machine

NOT from book

FloatWine

Design by Anthony Teate

FloatWine

WineFloatWineFloatWine

WineWine

Page 6: Class 19: Analyzing Algorithms

6

Measuring Cost

Machines vary, so we want to understand how the cost to execute a procedure scales with the size of the input

We use the asymptotic operators to abstract away all the details

Page 7: Class 19: Analyzing Algorithms

7

Recap• Big-O: functions that grow no faster than f

• Omega (): functions that grow no slower than f

• Theta (): functions that grow as fast as f

Page 8: Class 19: Analyzing Algorithms

8

Example

Prove: 73n2 + n (n2)

Page 9: Class 19: Analyzing Algorithms

9

Real CSists

Page 10: Class 19: Analyzing Algorithms

Proofs are Not Scary!

• Theorem: There exists a polygon with four sides.

• Proof:

What kind of proof is this?

Page 11: Class 19: Analyzing Algorithms

Proof by Construction

We can prove a “there exists an X with property Y” theorem, by showing an X that has property Y

O(f) means “there are positive constants c and n0 such that g(n) cf(n) for all n n0

So, to prove g is in O(f) we need to find c and n0 and show that g(n) cf(n) for all n n0

Page 12: Class 19: Analyzing Algorithms

Dis-Proof by Construction

• To prove g is not in O(f):• O(f) means: there are positive constants c and n0 such that g(n) cf(n) for all n n0

• So, to prove g is not in O(f) we need to find a way given any c and n0, to find an n n0 such that g(n) > cf(n).

Page 13: Class 19: Analyzing Algorithms

13

Example

Prove: 73n2 + n (n2)

Page 14: Class 19: Analyzing Algorithms

14

Algorithm Analysis

What is the asymptotic running time of the Scheme procedure below:

(define (bigger a b) (if (> a b) a b))

Page 15: Class 19: Analyzing Algorithms

15

Algorithm Analysis

What is the asymptotic running time of (> a b):

(define (bigger a b) (if (> a b) a b))

Page 16: Class 19: Analyzing Algorithms

16

What are we really measuring?

Input size: number of tape squares it takes to write down the input

Running time: number of steps it takes before TM enters a final state

Input size for (bigger a b) = nNumber of tape squares to represent input (not its

magnitude)

Why doesn’t the alphabet size matter?

Page 17: Class 19: Analyzing Algorithms

17

Page 18: Class 19: Analyzing Algorithms

18

Next Class

What if the input is binary instead of unary?

a>b# 0# if a > b, 1# if a bwhere a, b are the numbers a, b

represented in binary