Download presentation
Presentation is loading. Please wait.
Published byClemence Perry Modified over 8 years ago
1
Intro to Analysis of Algorithms
2
Algorithm “A sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate input in a finite amount of time.” Named for Al Khwarizmi, who laid out basic procedures for arithmetic functions. (Read about him!)
3
Analysis of Algorithms Correctness Generality Optimality Simplicity Time Efficiency Space Efficiency
4
Measuring Efficiency What is basic unit to measure input size? (n) What is basic unit of resource? – Time: basic unit operation – Space: memory units Best, worst, or average case? Find its efficiency class
5
Why do we care? Let’s look at Fibonacci numbers: 1, 1, 2, 3, 5, 8, 13, …
6
Fibonacci Sequence We want to compute the nth number in the sequence. (F 3 = 2, for example.)
7
This definition can be translated directly into code – a recursive method. How many additions does it take to compute F n ?
8
Which is better? function fib2(n) create an array f[0...n] f[0] = 0, f[1] = 1 for i = 2...n: f[i] = f[i-1] + f[i-2] return f[n] function fib1(n) if n = 0: return 0 if n = 1: return 1 return fib1(n-1) + fib1(n-2)
9
Consider calculating F 200. The fib1 method takes over 2 138 steps. Computers can do several billion instructions per second. Suppose we have a supercomputer that does 40 trillion instructions per second.
10
Consider calculating F 200. The fib1 method takes over 2 138 steps. Computers can do several billion instructions per second. Suppose we have a supercomputer that does 40 trillion instructions per second. Even on this machine, fib1(200) takes at least 2 92 seconds, or 10 18 centuries, long after the expected end of our sun!!!
11
Consider calculating F 200. The fib1 method takes over 2 138 steps. Computers can do several billion instructions per second. Suppose we have a supercomputer that does 40 trillion instructions per second. Even on this machine, fib1(200) takes at least 2 92 seconds, or 10 18 centuries, long after the expected end of our sun!!! But, fib2(200) would take less than a billionth of a second to compute!!!
12
10 6 instructions/sec, runtimes N O(log N)O(N)O(N log N)O(N 2 ) 10 0.0000030.000010.0000330.0001 100 0.0000070.000100.0006640.1000 1,000 0.0000100.001000.0100001.0 10,000 0.0000130.010000.1329001.7 min 100,000 0.0000170.100001.6610002.78 hr 1,000,000 0.0000201.019.911.6 day 1,000,000,000 0.00003016.7 min18.3 hr318 centuries
13
Basics of Efficiency Big-oh – upper bound on the efficiency class Efficiency classes don’t worry with constants A cubic is worse than a quadratic, quartic worse than cubic… Getting big-oh analysis of non-recursive code is pretty easy
14
The algorithm is O(3n+2), which is O(n). We only care about the efficiency class. Why? At some point, every parabola (n 2 ) overtakes any line (n). We only really care about large input.
15
Know the shapes constant logarithmic linear quadratic exponential
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.