Document 7662695

Download Report

Transcript Document 7662695

Department of Computer and Information Science,
School of Science, IUPUI
CSCI 240
Analysis of Algorithms
Growth Rates
Dale Roberts, Lecturer
Computer Science, IUPUI
E-mail: [email protected]
Dale Roberts
Choosing n in t(n)
The analysis of algorithms attempts to optimize some
critical resource. The critical resource usually chosen is
time, so t(n) represents a time function.
t(n) is influenced by hardware and compiler.
Other
factors include the algorithm used and input to the
algorithm.
Parameter n, usually referring to number of data items
processed, affects running time most significantly.
n may be degree of polynomial, size of file to be sorted or searched,
number of nodes in a graph, etc.
Dale Roberts
Best Case, Worse Case and Average Case Defined
Best case is amount of time program would take
with best possible input configuration
best case is found for input and easier to find; not usually the metric
chosen
Worst case is amount of time program would
take with worst possible input configuration
worst case is found for input and easier to find; usually the metric
chosen
Average case is amount of time a program is
expected to take using "typical" input data
definition of "average" can affect results
average case is much harder to compute
Dale Roberts
Average Case and Worse Case
An algorithm may run faster on certain data sets than on others.
Finding the average case can be very difficult, so typically algorithms
are measured by the worst-case time complexity.
Also, in certain application domains (e.g., air traffic control, surgery,
IP lookup) knowing the worst-case time complexity is of crucial
importance.
worst-case
5 ms
}
4 ms
average-case?
3 ms
best-case
2 ms
1 ms
A
B
C
D
Input
E
F
G
Dale Roberts
Comparison of Growth Rates
Functions in Increasing Order
Function
Name
c
Constant
log N
Logarithmic
log2 N
Log-squared
N
Linear
N log N
N log N
N2
Quadratic
N3
Cubic
2N
Exponential
Dale Roberts
Comparison of Growth Rates (cont)
250
f(n) = n
f(n) = log(n)
f(n) = n log(n)
f(n) = n^2
f(n) = n^3
f(n) = 2^n
0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Dale Roberts
Comparison of Growth Rates (cont)
500
f(n) = n
f(n) = log(n)
f(n) = n log(n)
f(n) = n^2
f(n) = n^3
f(n) = 2^n
0
1 2 3
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Dale Roberts
Comparison of Growth Rates (cont)
1000
f(n) = n
f(n) = log(n)
f(n) = n log(n)
f(n) = n^2
f(n) = n^3
f(n) = 2^n
0
1
3
5
7
9
11
13
15
17
19
Dale Roberts
Comparison of Growth Rates (cont)
5000
4000
f(n) = n
f(n) = log(n)
3000
f(n) = n log(n)
f(n) = n^2
2000
f(n) = n^3
f(n) = 2^n
1000
0
1
3
5
7
9
11
13
15
17
19
Dale Roberts
Comparison of Growth Rates (cont)
Dale Roberts
Acknowledgements
Philadephia University, Jordan
Nilagupta, Pradondet
Dale Roberts