Академический Документы
Профессиональный Документы
Культура Документы
Lecture 24:
Polynomials Functions Algorithmic Complexity
Module Home Page
Page 1 of 11
Back
Full Screen
Close
Quit
24.1. Algorithm Complexity Examples
Title Page
Problem 24.1.
Parameters: An array a[1 . . . n] of integers.
JJ II Returns: An array b[1 . . . n] of doubles such that b[i]
is the average of a[1] . . . a[i].
J I
Example
Page 2 of 11
1 2 3 4
a 3 1 2 6
Back
b 3.0 2.0 2.0 3.0
Full Screen
This is a problem that arises in, e.g., financial software. E.g. given the year-by-year
returns of an investment, you might want to plot the average returns over the lifetime
Close
of the investment.
Quit
24.1.2. Simple Algorithm
• Here is the simplest algorithm.
J I
The first line is in English. Beware of such lines! They may hide huge quantities of
work. In this case, it’s fairly benign: all algorithms that solve this problem will have
Page 3 of 11
to contain this piece of work, and it doesn’t hide any additions and divisions.
• Class Exercise
Back
Give a formula for t(n), in terms of n, that defines its worst-case time complexity,
counting only the additions and divisions used to compute the averages.
Full Screen
Quit
b[i − 1] = (a[1] + a[2] + . . . + a[i − 1]) /(i − 1)
b[i] = (a[1] + a[2] + . . . + a[i − 1] + a[i]) /i
If we keep track of the total so far, we can easily compute the next average:
Algorithm Complexity . . .
Polynomials Functions – add the next number to the total
– divide
Module Home Page
Algorithm: prefixAverages2(a)
Title Page
create array b[1 . . . n]
sum := 0.0;
for i := 1 upto n
JJ II { sum := sum + a[i];
b[i] := sum/i;
}
J I return b;
Page 4 of 11
• Class Exercise
Back
Give a formula for t(n), in terms of n, that defines its worst-case time complexity,
again counting only the additions and divisions.
Full Screen
Close
Quit
1000
900
800
t(n)
500
300
Title Page
200
100
PrefixAverages1
JJ II PrefixAverages2
0
0 20 40 60 80 100
Input size, n
J I
Page 5 of 11
Back
Full Screen
Close
Quit
24.2. Polynomials Functions
• In algebra, a polynomial function, or polynomial, is a function of the form:
f (n) =def 2n + 3
Back
Close
– degree 3 are called cubic functions, e.g.:
f (n) =def n3 + 3
Quit
• A root of a polynomial f (n) is a number r such that f (r) = 0
• To determine the roots of polynomials, i.e. to ‘solve algebraic equations’:
– approximate them using, e.g., Newton’s method
Algorithm Complexity . . .
– use a formula, where known, e.g. the quadratic formula for quadratic equation
Polynomials Functions
an2 + bn + c = 0 √
−b ± b2 − 4ac
n=
Module Home Page 2a
• Here are the graphs of a few very simple polynomials.
Title Page 1000
tA(n) = n
tB(n) = n^2
900 tC(n) = n^3
JJ II 800
700
J I 600
t(n)
500
Page 7 of 11
400
300
Back
200
100
Full Screen
0
0 5 10 15 20 25 30 35 40 45 50
Input size, n
Close
• In general, of course, coefficients will not always be one or zero, as they were in the
previous graph. So let’s see what happens when we have some slightly more interest-
Quit
ing polynomials. Suppose algorithm A’s worst-case time complexity tA (n) =def 100n,
and algorithm B’s worst-case time complexity tB (n) =def 2n2 .
20000
14000
Module Home Page
12000
t(n)
10000
Title Page
8000
6000
JJ II
4000
2000
J I tA(n) = 100n
tB(n) = 2n^2
0
0 20 40 60 80 100
Input size, n
Page 8 of 11
The lines cross at n = 50. So when inputs of are of size less than 50, algorithm B
is the faster; when input sizes exceed 50, algorithm A is the faster. What’s more,
Back
from that point on, the larger the input, the bigger the advantage A has over B. If
n = 100, A is twice as fast as B; if n = 1000, A is 20 times as fast.
Full Screen
• Those of you who have a bit of algebra under your belts can compare algorithms
without graphing them.
Close • To find out where the graphs for A and B cross, solve
100n = 2n2
Quit
Rearranging:
2n2 − 100n = 0
To solve an2 + bn + c, we can use
√
Algorithm Complexity . . . −b ± b2 − 4ac
n=
Polynomials Functions 2a
In this case, √
Module Home Page −(−100) ± −1002 − 4 × 2 × 0
n=
2×2
So n = 0 and n = 50 are the roots of this equation.
Title Page
• We can find out the maximum input size that can be handled within a certain time
period.
JJ II
– To find out how much work A can do in 1000 milliseconds (1 second), assuming
each operation takes 1 millisecond, solve
J I
100n = 1000
Page 9 of 11 So n = 10.
– To find out how much B can do in 1000 milliseconds, solve
Back 2n2 = 1000
• Obviously, the algebra gets harder if the functions that describe the complexities of
Module Home Page the algorithms are not as simple as these ones.
The prefix averages problem and its two algorithms come from [GT02]. Some of the
JJ II
discussion of polynomial functions is based on [AU92].
Clip Art (of head with bomb) licensed from the Clip Art Gallery on DiscoverySchool.com.
J I
Page 10 of 11
Back
Full Screen
Close
Quit
References
[AU92] A. V. Aho and J. D. Ullman. Foundations of Computer Science. W.H. Freeman,
1992.
Algorithm Complexity . . .
[GT02] M. T. Goodrich and R. Tamassia. Algorithm Design: Foundations, Analysis, and
Polynomials Functions
Internet Examples. Wiley, 2002.
Title Page
JJ II
J I
Page 11 of 11
Back
Full Screen
Close
Quit