CSC 282: Design & Analysis of Efficient Algorithms Graph Algorithms Shortest Path Algorithms
Analysis of Algorithms CPS212 Gordon College. Measuring the efficiency of algorithms There are 2...
-
date post
30-Jan-2016 -
Category
Documents
-
view
215 -
download
0
Transcript of Analysis of Algorithms CPS212 Gordon College. Measuring the efficiency of algorithms There are 2...
![Page 1: Analysis of Algorithms CPS212 Gordon College. Measuring the efficiency of algorithms There are 2 algorithms: algo1 and algo2 that produce the same results.](https://reader035.fdocuments.us/reader035/viewer/2022062500/56649d815503460f94a658dc/html5/thumbnails/1.jpg)
Analysis of Algorithms
CPS212
Gordon College
![Page 2: Analysis of Algorithms CPS212 Gordon College. Measuring the efficiency of algorithms There are 2 algorithms: algo1 and algo2 that produce the same results.](https://reader035.fdocuments.us/reader035/viewer/2022062500/56649d815503460f94a658dc/html5/thumbnails/2.jpg)
Measuring the efficiency of algorithms
There are 2 algorithms: algo1 and algo2 that produce the same results.
How do you choose which algorithm to keep. Algorithm Efficiency
![Page 3: Analysis of Algorithms CPS212 Gordon College. Measuring the efficiency of algorithms There are 2 algorithms: algo1 and algo2 that produce the same results.](https://reader035.fdocuments.us/reader035/viewer/2022062500/56649d815503460f94a658dc/html5/thumbnails/3.jpg)
Measuring the efficiency of algorithms
How do you determine an algorithm’s inherent efficiency? Code them up and compare their running times on
a lab machine. How were they coded? We want to compare the
algorithms - not their implementations What computer do we use? What else is running? What data is a fair test of an algorithm’s efficiency.
![Page 4: Analysis of Algorithms CPS212 Gordon College. Measuring the efficiency of algorithms There are 2 algorithms: algo1 and algo2 that produce the same results.](https://reader035.fdocuments.us/reader035/viewer/2022062500/56649d815503460f94a658dc/html5/thumbnails/4.jpg)
Measuring the efficiency of algorithms
Goal: Measure the efficiency of an algorithm -
independent of implementation, hardware, or data. Solution:
Analyze and represent the number of operations the algorithm will perform as a function of input.
Example: Copying an array with n elements requires x
operations. What is x?
![Page 5: Analysis of Algorithms CPS212 Gordon College. Measuring the efficiency of algorithms There are 2 algorithms: algo1 and algo2 that produce the same results.](https://reader035.fdocuments.us/reader035/viewer/2022062500/56649d815503460f94a658dc/html5/thumbnails/5.jpg)
Growth rates Algo1 requires n2 / 2 operations to solve
a problem with input size n Algo2 requires 5n + 10 operations
Which is the better algorithm for the job?
![Page 6: Analysis of Algorithms CPS212 Gordon College. Measuring the efficiency of algorithms There are 2 algorithms: algo1 and algo2 that produce the same results.](https://reader035.fdocuments.us/reader035/viewer/2022062500/56649d815503460f94a658dc/html5/thumbnails/6.jpg)
Order of magnitude growth analysis A function f(x) is O(g(x)) if and only if
there exist 2 positive constants, c and n, such that | f(x) |≤ cg(x) for all x > n
size of input set
number ofoperations
cg(x)
f(x)
![Page 7: Analysis of Algorithms CPS212 Gordon College. Measuring the efficiency of algorithms There are 2 algorithms: algo1 and algo2 that produce the same results.](https://reader035.fdocuments.us/reader035/viewer/2022062500/56649d815503460f94a658dc/html5/thumbnails/7.jpg)
Order of magnitude growth analysis Asymptotic growth - as the growth approaches infinity An asymptote of a real-valued function y = f(x) is a
curve which describes the behavior of f as either x or y tends to infinity.
size of input set
number ofoperations
cg(x)
f(x)
![Page 8: Analysis of Algorithms CPS212 Gordon College. Measuring the efficiency of algorithms There are 2 algorithms: algo1 and algo2 that produce the same results.](https://reader035.fdocuments.us/reader035/viewer/2022062500/56649d815503460f94a658dc/html5/thumbnails/8.jpg)
Order of magnitude growth analysis Important points:
Focus only on the growth Shape of g(x) is essential As the input set grows large (ignore the shape for small x)
size of input set
number ofoperations
cg(x)
f(x)
![Page 9: Analysis of Algorithms CPS212 Gordon College. Measuring the efficiency of algorithms There are 2 algorithms: algo1 and algo2 that produce the same results.](https://reader035.fdocuments.us/reader035/viewer/2022062500/56649d815503460f94a658dc/html5/thumbnails/9.jpg)
Standard function shapes: constant O(1)
Examples?
size of input set
number ofoperations
= cg(x)f(x)
![Page 10: Analysis of Algorithms CPS212 Gordon College. Measuring the efficiency of algorithms There are 2 algorithms: algo1 and algo2 that produce the same results.](https://reader035.fdocuments.us/reader035/viewer/2022062500/56649d815503460f94a658dc/html5/thumbnails/10.jpg)
Standard function shapes: linear O(x)
Examples?
size of input set
number ofoperations
cg(x)
f(x)=ax+b
![Page 11: Analysis of Algorithms CPS212 Gordon College. Measuring the efficiency of algorithms There are 2 algorithms: algo1 and algo2 that produce the same results.](https://reader035.fdocuments.us/reader035/viewer/2022062500/56649d815503460f94a658dc/html5/thumbnails/11.jpg)
Standard function shapes: logarithmic O(log x) (base 2)
QuickTime™ and a decompressor
are needed to see this picture.
bc = a logba = c 23 = 8 log28 = 3
log(x*y) = log x + log y
log(xa) = a log x
Examples?
![Page 12: Analysis of Algorithms CPS212 Gordon College. Measuring the efficiency of algorithms There are 2 algorithms: algo1 and algo2 that produce the same results.](https://reader035.fdocuments.us/reader035/viewer/2022062500/56649d815503460f94a658dc/html5/thumbnails/12.jpg)
Standard function shapes: Polynomial O(x2)
Examples?
![Page 13: Analysis of Algorithms CPS212 Gordon College. Measuring the efficiency of algorithms There are 2 algorithms: algo1 and algo2 that produce the same results.](https://reader035.fdocuments.us/reader035/viewer/2022062500/56649d815503460f94a658dc/html5/thumbnails/13.jpg)
Polynomial vs. Linear
![Page 14: Analysis of Algorithms CPS212 Gordon College. Measuring the efficiency of algorithms There are 2 algorithms: algo1 and algo2 that produce the same results.](https://reader035.fdocuments.us/reader035/viewer/2022062500/56649d815503460f94a658dc/html5/thumbnails/14.jpg)
Polynomial vs. Linear
Example: 50n + 20 & n2
At what point will n2 surpass 50n+20?
n2 = 50n+20 Solve for x…quadratic formula
n2 - 50n - 20 = 0
n = 101/2 = 50.5 n = -1/2
0
500
1000
1500
2000
2500
3000
3500
4000
0 10 20 30 40 50 60 70
![Page 15: Analysis of Algorithms CPS212 Gordon College. Measuring the efficiency of algorithms There are 2 algorithms: algo1 and algo2 that produce the same results.](https://reader035.fdocuments.us/reader035/viewer/2022062500/56649d815503460f94a658dc/html5/thumbnails/15.jpg)
Standard function shapes: Exponential O(cx)
Examples?Hamiltonian CircuitTraveling SalesmanOptimization problems
Solution:Limit to small input setsIsolate special casesFind approximate solution
(near optimal)
![Page 16: Analysis of Algorithms CPS212 Gordon College. Measuring the efficiency of algorithms There are 2 algorithms: algo1 and algo2 that produce the same results.](https://reader035.fdocuments.us/reader035/viewer/2022062500/56649d815503460f94a658dc/html5/thumbnails/16.jpg)
Complexity in action
QuickTime™ and a decompressor
are needed to see this picture.
![Page 17: Analysis of Algorithms CPS212 Gordon College. Measuring the efficiency of algorithms There are 2 algorithms: algo1 and algo2 that produce the same results.](https://reader035.fdocuments.us/reader035/viewer/2022062500/56649d815503460f94a658dc/html5/thumbnails/17.jpg)
Real Examples Searching (sequential)
Unit of work: comparisons Best case: O(1) [theta] Worst case: O(n) [theta] Average Case: O(n) [theta]
![Page 18: Analysis of Algorithms CPS212 Gordon College. Measuring the efficiency of algorithms There are 2 algorithms: algo1 and algo2 that produce the same results.](https://reader035.fdocuments.us/reader035/viewer/2022062500/56649d815503460f94a658dc/html5/thumbnails/18.jpg)
Real Examples Sort (selection)
Unit of work: comparisons and exchanges Best case: O(n2) [theta] Worst case: O(n2) [theta] Average Case: O(n2) [theta]
![Page 19: Analysis of Algorithms CPS212 Gordon College. Measuring the efficiency of algorithms There are 2 algorithms: algo1 and algo2 that produce the same results.](https://reader035.fdocuments.us/reader035/viewer/2022062500/56649d815503460f94a658dc/html5/thumbnails/19.jpg)
Real Examples Search (binary)
Unit of work: comparisons Best case: O(1) [theta] Worst case: O(log n) [theta] Average Case: O(log n) [theta]