{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

07-algorithm_analysis

07-algorithm_analysis - Computer Science 136 Elementary...

This preview shows pages 1–5. Sign up to view the full content.

Computer Science 136: David R. Cheriton School of Computer Science Elementary Algorithm Design and Data Abstraction Winter 2012 Lecture 7 – Analysis of Algorithms We explore the idea of efficiency of algorithms. What does it mean for one algorithm to be faster than another? How do we compare the cost of two different algorithms? What techniques are available for figuring out this cost? CS 136 – Winter 2012 07 – Algorithm Analysis 1/1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Playing Time for Simon How long does it for a person to play simple-simon? Recurrence: P s ( 1 ) = 1 , P s ( n ) = 1 + P s ( n - 1 ) How long does it take for a person to play full-simon? Recurrence: P f ( 1 ) = 1 , P f ( n ) = n + P f ( n - 1 ) P f ( n ) = n · ( n + 1 ) / 2 n 2 / 2 6 P f ( n ) 6 n 2 CS 136 – Winter 2012 07 – Algorithm Analysis 2/1
Playing Time for Guess-a-number I’m thinking of a number between 1 and n ... How many guesses do you need? I’ll tell you if you’re too high, too low, or you win. Think about G ( n ) , number of guesses to win in the worst case. Depends on your strategy! Guess 1, guess 2, ...? For this strategy, G ( n ) = n CS 136 – Winter 2012 07 – Algorithm Analysis 3/1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Playing Time for Guess-a-number (2) Smarter approach: Guess in the middle of the range you know the number is in. This divides the size of game in two! n 1 2 3 ... 7 ... 15 G ( n ) 1 2 2 ... 3 ... 4 For “Nice” values of n , G ( n ) = 1 + G (( n - 1 ) / 2 ) Turn it around: With m guesses, can solve a game of size 2 m - 1 n = 2 G ( n ) - 1 2 G ( n ) = n + 1 G ( n ) = log 2 ( n + 1 ) for “nice” n of the form n = 2 k - 1 .
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 15

07-algorithm_analysis - Computer Science 136 Elementary...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online