Examples Class Lectures
Examples Class Lectures
Examples Class Lectures
=
= +
0 r
r r
! r
t f h
) h t ( f
Finite Difference Methods
October 2011 CATAM Irena Borzym
12:19 PM 17/10/2011 Page 13 of 27 immb100@damtp.cam.ac.uk
In the 1A Analysis a real analytic function was defined as a function which had a
convergent Taylor series.
( )
( )
=
= +
0 r
r r
! r
t f h
) h t ( f
From this we know that a real analytic function on the real line can be completely
specified either by giving all the values on the line or by giving its Taylor series at a
point. We can approximate the value of the function by truncating the series to
obtain,
( )
( )
( ) h , t R
! r
t f h
) h t ( f
N
N
0 r
r r
+ = +
=
Where the Lagrange form of the remainder is
( )
( )
( )
h t t
! 1 N
f h
) h , t ( R
1 N 1 N
N
+ < <
+
=
+ +
Finite difference methods are numerical methods for approximating the solutions to
differential equations by using finite difference equations to approximate derivatives.
In the numerical analysis the real line is replaced by a grid which is often, but not
always, equally spaced. The parameter h is called the step length and the derivatives
in the Taylor series can be approximated and replaced by finite differences between
points on the grid.
The ERROR in a method's solution is defined as the difference between its
approximation and the exact analytical solution.
In finite difference methods there are two sources of error
1. Rounding Error
This is the loss of precision due to computer rounding of decimal quantities.
2. Truncation Error
This is the difference between the exact solution of the finite difference
equation and the exact quantity ignoring the rounding error.
The LOCAL TRUNCATION ERROR is defined by
k k
f ) t ( f where ) t ( f
k
is the exact
value of the function at the point
k
t t = and
k
f is the numerical approximation
at
k
t t = .
This error is often expressed using the big O notation. Recall the definition
October 2011 CATAM Irena Borzym
12:19 PM 17/10/2011 Page 14 of 27 immb100@damtp.cam.ac.uk
( ) ( ) t g O ) t ( f = as a t iff ( ) ( ) t g M t f : 0 M , s > o - for o < a t
If the function g(t) is non zero in a neighbourhood then we have an equivalent form
( ) ( ) t g O ) t ( f = as a t iff
( )
( )
<
t g
t f
sup lim
a t
for o < a t
The remainder term of a truncated Taylor series is convenient for analyzing the local
truncation error.
Notation and terminology
http://en.wikipedia.org/wiki/Forward_difference
A FORWARD DIFFERENCE has the form | | ( ) ( ) ( ) x f h x f x f
h
+ = A
A BACKWARD DIFFERENCE has the form | | ( ) ( ) ( ) h x f x f x f
h
= V
A CENTRAL DIFFERENCE has the form | | ( )
|
.
|
\
|
|
.
|
\
|
+ = o
2
h
x f
2
h
x f x f
h
Depending on the application, the spacing h may be variable or constant.
Relation with derivatives
The right derivative is defined by
( )
( )
( ) ( )
h
x f h x f
lim x f
0 h
1
+
=
Therefore the forward difference approximates the right derivative when h is small.
( ) ( )
| | ( )
h
x f
h
x f h x f
h
A
=
+
The error in this approximation can be derived from Taylor's theorem,
| | ( )
( )
( ) ( ) 0 h h O x f
h
x f
1
h
=
A
as
The left derivative is defined by
( )
( )
( ) ( )
h
h x f x f
lim x f
0 h
1
=
Therefore the backward difference approximates the left derivative when h is small.
( ) ( )
| | ( )
h
x f
h
h x f x f
h
V
=
The error in this approximation can be derived from Taylor's theorem,
October 2011 CATAM Irena Borzym
12:19 PM 17/10/2011 Page 15 of 27 immb100@damtp.cam.ac.uk
| | ( )
( )
( ) ( ) 0 h h O x f
h
x f
1
h
=
V
as
The central difference yields a more accurate approximation, with an error
proportional to square of the spacing.
| | ( )
( )
( ) ( ) 0 h h O x f
h
x f
2 1
h
=
o
as
A problem with the central difference method, however, is that oscillating functions
can yield zero derivative.
Higher-order differences
We can similarly obtain finite difference approximations to higher order derivatives.
2nd Order Central
Apply a central difference formula for the derivative of
( )
( ) x f
1
at x with a spacing of
2
h
, and then substitute into this the central difference formula for the derivative.
| | ( )
( ) ( )
( ) ( ) ( )
2 2
1 1
2
h
2
h
h x f x f 2 h x f
h
2
h
x f
2
h
x f
h
x f
+ +
=
|
.
|
\
|
|
.
|
\
|
+
=
o
( )
( )
| | ( )
( ) ( ) ( )
2 2
h
2
h
h x f x f 2 h x f
h
x f
x f
+ +
=
o
~
Similarly we can apply other difference formulae in a recursive manner.
2nd Order Forward
( )
( )
| | ( )
( ) ( ) ( )
2 2
h
2
2
h
x f h x f 2 h 2 x f
h
x f
x f
+ + +
=
A
~
The n
th
-order forward, backward, and central differences are
October 2011 CATAM Irena Borzym
12:19 PM 17/10/2011 Page 16 of 27 immb100@damtp.cam.ac.uk
( )
( )
| | ( )
( ) ( ) ( ) h j n x f
j
n
1
h
1
h
x f
) h ( O x f
n
0 j
j
n n
h
n
n
+
|
|
.
|
\
|
=
A
= +
=
( )
( )
| | ( )
( ) ( ) jh x f
j
n
1
h
1
h
x f
) h ( O x f
n
0 j
j
n n
h
n
n
|
|
.
|
\
|
=
V
= +
=
( )
( )
| | ( )
( ) |
.
|
\
|
|
.
|
\
|
+
|
|
.
|
\
|
=
o
= +
=
h j
2
n
x f
j
n
1
h
1
h
x f
) h ( O x f
n
0 j
j
n n
h
n
2 n
In the central difference formula the odd n terms have non integer multiples of h.
This changes the grid size. We can get around this by taking averages of
| |
|
.
|
\
|
o
2
h
x f
n
and | |
|
.
|
\
|
+ o
2
h
x f
n
.
Higher-order differences can also be used to construct better approximations. The
finite difference can be centered about any point by mixing forward, backward, and
central differences.
Using Taylor Series we can make approximations, which sample an arbitrary number
of points to the left and a possibly different number of points to the right of the center
point, for any order of derivative. This is useful for differentiating a function near a
grid edge.
This means that we have an infinite supply of numerical approximations to our
original differential equation. In CATAM you are usually told which method to use
but if you are asked to compare two different methods then remember that if you were
choosing a method for yourself then you would be taking into account the following
factors.
1. The required accuracy of the approximation.
2. Special features of the differential equation and how they would be reflected
in a given numerical approximation.
3. The type of solution you expect.
Are you trying to model the surface of the Grand Canyon or the Cambridge fenlands?
Are you interested in small scale or large scale features?
Is your data in the form of large amounts of height above sea level measurements or is
it in the form of derivative and gradient information.
Using derivative data rather than functional values in your algorithm is often useful is
in numerical minimisation of functions. As usual there is a trade off. The algorithms
with derivative data are faster but more complicated in general. You need to use your
judgement in each particular case. In a CATAM project you might want to compare
run times for two such algorithms and comment on the results.
October 2011 CATAM Irena Borzym
12:19 PM 17/10/2011 Page 17 of 27 immb100@damtp.cam.ac.uk
A Naive Example Problem
When presented with a problem one might think that a numerical
approximation can be obtained from the Taylor Series.
For example suppose you are given the equation
0 k t 0 0 ) t ( Y k
dt
) t ( Y d
2
2
2
> < s =
kA
dt
dY
A ) 0 ( Y
0 t
= =
=
We can numerically approximate this by having a grid on which we give values of our
function or a combination of values and derivatives.
( )
( )
( )
( )
( )
3
2 2
1
h O
! 2
t f h
t hf ) t ( f ) h t ( f + + + = +
( )
( )
( )
( )
( )
3
2 2
1
h O
! 2
t f h
t hf ) t ( f ) h t ( f + + =
So we have
( )
( ) ( ) h O
h
) t ( f 2 ) h t ( f ) h t ( f
t f
2
2
+
+ +
=
and for the first derivative we are forced to use only forward terms so
( )
( ) ( )
2 1
h O
h
) t ( f ) h t ( f
t f +
+
=
Hence with step length h and ( )
n n
t Y Y = and h t t
n 1 n
+ =
+
( ) 0 k t 0 0 Y Y k h 2 Y
1 n n
2 2
1 n
> < s = + +
+
A Y
0
=
and Ak
dt
dY
0 t
=
=
so
( )
( )
2 n 1 n
n
1
h O
h
Y Y
Y +
=
+
and
( )
h
Y Y
Y
0 1
0
1
=
Solving ( ) 0 1 k h 2
2 2 2
= + + so
( ) ( )
2
4 k h 2 k h 2
2
2 2 2 2
+ +
=
|
|
.
|
\
|
+
|
|
.
|
\
|
+ =
4
k h
1 hk
2
k h
1
2 2 2 2
October 2011 CATAM Irena Borzym
12:19 PM 17/10/2011 Page 18 of 27 immb100@damtp.cam.ac.uk
n
2 2 2 2
n
2 2 2 2
n
4
k h
1 hk
2
k h
1 b
4
k h
1 hk
2
k h
1 a Y
|
|
.
|
\
|
|
|
.
|
\
|
+
|
|
.
|
\
|
+ +
|
|
.
|
\
|
|
|
.
|
\
|
+ +
|
|
.
|
\
|
+ =
b a A Y
0
+ = =
( )
h
Y Y
kA Y
0 1
0
1
= =
( )
|
|
.
|
\
|
+
|
|
.
|
\
|
+
|
|
.
|
\
|
+ +
=
4
k h
1 hk 2
4
k h
1 Ahk
2
k h
1 A Ah 1 k
b
2 2
2 2 2 2
( )
|
|
.
|
\
|
+
|
|
.
|
\
|
+ +
|
|
.
|
\
|
+ +
=
4
k h
1 hk 2
4
k h
1 Ahk
2
k h
1 A Ah 1 k
a
2 2
2 2 2 2
You now have to worry about the stability of this method before you can do anything
with it.
Numerical Stability
If we ignore truncation and rounding errors then numerical algorithms are constructed
to approach the correct solution in the limit when the step length goes to zero and the
number of steps to infinity.
Depending on the computational method chosen the errors can be magnified or
damped. If they are damped then the method is called NUMERICALLY STABLE. There
are different definitions of numerical stability depending on the application.
Sometimes a calculation which can be carried out in several ways, all of which are
algebraically equivalent in terms of ideal numbers, but which yield different results
when carried out on computers.
Reducing the step size h reduces the truncation error of the difference approximation.
As we have seen this error decreases more rapidly for central difference
approximations and more slowly for forward and backward difference
approximations. When h becomes too small, the difference approximations are taken
for almost equal values of f(x) at the two points. Any rounding error of computations
of f(x) is magnified by a factor of 1/h. As a result, the rounding error grows with h
for very small values of h. An optimal step size h = h
opt
can be computed from
minimization of the sum of truncation and rounding errors.
October 2011 CATAM Irena Borzym
12:19 PM 17/10/2011 Page 19 of 27 immb100@damtp.cam.ac.uk
It is also possible to identify an optimal range of values for h in any given problem
just by numerical trials. It then makes sense to choose a convenient h within the
given range. Note that for practical purposes it is sometimes useful to choose h to be
of a specific form, for example a multiple of 7 because that is convenient for the grid
being used.
We will look at an example a little later which gives you three different difference
methods for solving the heat equation. In each case there are advantages and
disadvantages to using the specified method.
Explicit methods calculate the state of a system at a later time from the state of the
system at the current time. We solve an equation of the form
( ) ( ) ( ) x f G h x f = +
Implicit methods find a solution by solving an equation involving both the current
state of the system and the later one. We solve an equation of the form
( ) ( ) ( ) 0 h x f , x f G = + to find ( ) h x f +
Implicit methods require an extra computation and they can be much harder to
implement. Implicit methods are used because there are many problems arising in
practice for which the use of an explicit method requires impractically small time
steps to keep the error in the result bounded. (For example see stiff probelms.) For
such problems, to achieve given accuracy, it takes much less computational time to
use an implicit method with larger time steps. The choice of explicit or implicit
method depends upon the problem to be solved.
Example 1:
http://en.wikipedia.org/wiki/Implicit_method
Consider the ordinary differential equation
| | a , 0 y y
dt
dy
2
c = with initial Condition ( ) 1 0 y =
We can write down discrete versions of this equation. The simplest explicit method is
the Forward Euler and the simplest implicit is the Backward Euler method.
Equi-spaced grid for t:
n
ak
t
k
= with step length
n
a
h = and n k 0 s s
The numerical approximation of the analytic function ) t ( y
k
at the grid point
k
t t =
will be denoted
k
y .
The Forward Euler method
The forward Euler method is the explicit method.
October 2011 CATAM Irena Borzym
12:19 PM 17/10/2011 Page 20 of 27 immb100@damtp.cam.ac.uk
| | ( )
2
k
k 1 k
h
t t
y
h
y y
h
y y
dt
dy
k
=
=
A
~
+
=
Gives
2
k k 1 k
hy y y =
+
for each n ,..., 0 k =
Backward Euler method
The Backward Euler method is the implicit method
| | ( )
2
1 k
k 1 k
h
t t
y
h
y y
h
y y
dt
dy
k
+
+
=
=
=
V
~
Gives
k
2
1 k 1 k
y hy y = +
+ +
for
1 k
y
+
This is a quadratic equation, having one negative and one positive root. The positive
root is picked because in the original equation the initial condition is positive, and
then y at the next time step is given by
h 2
hy 4 1 1
y
k
1 k
+ +
=
+
In the vast majority of cases, the equation to be solved when using an implicit scheme
is much more complicated than a quadratic equation, and no exact solution exists.
Then one uses root-finding algorithms, such as Newton's method.
Example 2: The heat equation in one dimension with homogeneous
Dirichlet boundary conditions
http://en.wikipedia.org/wiki/Finite_difference_method
Differential Equation:
( ) ( )
2
2
x
t , x U
t
t , x U
c
c
=
c
c
Boundary Condition: ( ) 0 t , 1 U ) t , 0 ( U = =
Initial Condition: ( ) x U ) 0 , x ( U
0
=
METHOD (1) Explicit method solves
Equi-spaced grid for x:
J 0
x ,..., x with step length h
Equi-spaced grid for t:
N 0
t ,..., t with step length k
The numerical approximation of the analytic function ) t , x ( U
n j
at the grid point
) t , x (
n j
will be denoted
n
j
U .
October 2011 CATAM Irena Borzym
12:19 PM 17/10/2011 Page 21 of 27 immb100@damtp.cam.ac.uk
Use a forward difference at time t
n
Use second-order central difference for the space derivative at position x
j
("FTCS")
We get the recurrence equation:
2
n
1 j
n
j
n
1 j
n
j
1 n
j
h
U U 2 U
k
U U
+
+
+
=
To apply this method we rewrite it in the form,
n
1 j
2
n
1 j
2
n
j
2
1 n
j
U
h
k
U
h
k
U
h
k
2 1 U
+
+
+ +
|
.
|
\
|
=
0 U
n
0
= and 0 U
n
J
= are the boundary conditions.
So given the values at a time n t = on the right hand side we can evolve to the next
time step.
Numerically stable and convergent for
2
1
h
k
2
s with numerical errors
( ) ) h ( O k O U
2
+ = A
METHOD (2) Implicit method
Use a backward difference at time t
n
Use second-order central difference for the space derivative at position x
j
We get the recurrence equation:
2
1 n
1 j
1 n
j
1 n
1 j
n
j
1 n
j
h
U U 2 U
k
U U
+
+ +
+
+
+
=
To apply this method we rewrite it in the form,
1 n
1 j
2
1 n
1 j
2
1 n
j
2
n
j
U
h
k
U
h
k
U
h
k
2 1 U
+
+
+
+
+
|
.
|
\
|
+ =
0 U
n
0
= and 0 U
n
J
= are the boundary conditions.
We solve this linear system of equations for
n
j
U .
October 2011 CATAM Irena Borzym
12:19 PM 17/10/2011 Page 22 of 27 immb100@damtp.cam.ac.uk
Numerically stable and convergent for
2
1
h
k
2
s with numerical errors
( ) ) h ( O k O U
2
+ = A
METHOD (3) CrankNicolson Method
Use a central difference at time t
n+1/2
Use second-order central difference for the space derivative at position x
j
|
|
.
|
\
|
+
+
+
=
+
+
+ +
+
+
2
n
1 j
n
j
n
1 j
2
1 n
1 j
1 n
j
1 n
1 j
n
j
1 n
j
h
U U 2 U
h
U U 2 U
2
1
k
U U
We can obtain
1 n
j
U
+
from solving a system of linear equations
n
1 j
2
n
1 j
2
n
j
2
1 n
1 j
2
1 n
1 j
2
1 n
j
2
U
h
k
U
h
k
U
h
k
2 2 U
h
k
U
h
k
U
h
k
2 2
+
+
+
+
+
+ +
|
.
|
\
|
=
|
.
|
\
|
+
The scheme numerically stable and convergent.
The error is ( ) ) h ( O k O U
4 2
+ = A away from the bhoundaries but may rise to
( ) ) h ( O k O U
2 2
+ = A near the boundaries.
However, near the boundaries, the error is often O(h
2
) instead of O(h
4
).
Usually the CrankNicolson scheme is the most accurate scheme for small time steps.
The explicit scheme is the least accurate and can be unstable, but is also the easiest to
implement and the least numerically intensive. The implicit scheme works the best for
large time steps.
Sources of Error
1. Your original model includes assumptions. These introduce errors. In
CATAM projects we are given the model so with the exception of some part 2
projects which explicitly ask for comments on the difficulties or advantages of
the model you can ignore these issues.
2. The next step might be turning a continuous system into a discrete system. I
hope we have covered enough material about discrete methods for you to
realise that these are a source of error and also that each problem must be
considered on its own merits. Many CATAM projects ask questions which
invite you to comment about the choice of numerical approximation or to
compare different numerical approximations. If you check sources then you
October 2011 CATAM Irena Borzym
12:19 PM 17/10/2011 Page 23 of 27 immb100@damtp.cam.ac.uk
will be able to say much more. In principle it is easy to take a Taylor series
and use it to find a discrete analogue of a differential equation. It is harder to
show that the resulting approximation actually works. You can always find
discussion of the accuracy, stability and key features of the most commonly
used algorithms in CATAM. You can quote these results but you can also
verify that they are consistent with your data in your particular project.
3 This might be relevant in some Part 2 CATAM projects were you are
explicitly making comparisons between different numerical algorithms. Ask
yourself would a different approach be better.
COMPLEXITY
Some of you are conscientious enough to have gone online or looked in
books or talked to compsci friends about the definition of complexity and you may
now be very confused. CATAM takes a very simple minded approach to complexity
and you really only need to follow instructions in each project and in the CATAM
lectures which were given last term.
The CATAM definition of complexity is the number of arithmetic operations needed
to carry out an algorithm. All the operations are given equal weight. This is hugely
oversimplified but provides a good starting point from which to develop your
experience. Do not be confused by things which you may hear from Computer
Science students or read on the web. For the purposes of CATAM you are only
interested in arithmetic operations and you give them all equal weight as instructed in
the manual. You may find that the instructions for complexity calculations differ
slightly from project to project always follow instructions.
I am going to give you a little background so that you have some idea of the issues
involved and also being ignored. The first thing to recognise is that complexity is
defined in different ways by different disciplines and therefore you need to be careful
when talking to strangers!
The complexity analysis of numerical algorithms is an important part of the broader
subject of computational complexity theory. Computational complexity provides
theoretical estimates for the resources needed by any algorithm to solve a given
computational problem.
The COMPUTATIONAL TIME COMPLEXITY of a problem is defined as the number of
steps that it takes to solve a function as a function of the size of the input, which is
usually measured in bits, using the most efficient algorithm.
The COMPUTATIONAL SPACE COMPLEXITY of a problem is defined as the volume of
the memory used by the algorithm required to solve an instance of the problem as a
function of the size of the input measured in bits and using the most efficient
algorithm.
An axiomatic approach to these two ideas exists and allows the classification of
problems into complexity classes such as polynomial P and non polynomial NP time.
October 2011 CATAM Irena Borzym
12:19 PM 17/10/2011 Page 24 of 27 immb100@damtp.cam.ac.uk
- The travelling salesman problem can be solved in time O(n
2
2
n
), where n is
the size of the network to visit. As the size of the network of cities grows, the
time needed to find the route grows more than exponentially.
- The binary search is said to run in a number of steps proportional to the
logarithm of the length of the list being searched, O(log(n)).
In theoretical analysis of algorithms it is common to estimate their complexity
asymptotically. Asymptotic estimates are used because different implementations of
the same algorithm may differ in efficiency. However the efficiencies of any two
"reasonable" implementations of a given algorithm are related by a constant
multiplicative factor called a HIDDEN CONSTANT.
All this seems and in fact is well defined. Clearly with a nice linear algorithm things
will work well but we need to consider cases where the algorithm is a very
complicated tree.
Intuitively we clearly need to define complexity as the average time taken through the
algorithm. There are two things we can mean by time.
- The actual time taken to complete the calculation.
- The number of operations, steps, required to complete the algorithm.
Clearly the first of these is very dependent on the computer chip and is only really
useful as a tool to compare different algorithms on the same computer. Many
computers and programs have a function which will track the time and print out the
result for you. This can be very useful provided you keep in mind the limitations. It
is an excellent way of comparing different versions of an algorithm on the same
machine. It is clearly useless as a comparison of different algorithms across different
machines.
The second method is better because it is less dependent on the platform chosen.
Here the difficulty is to assign weights to the operations and decide which operations
to include. The operations of multiplication and division take much longer than
simple addition and subtraction. In more detailed work you might find that operations
have weights assigned to them and that a weighted sum is taken as the total.
Remember that different disciplines will have a different focus. For a computational
scientist, looking a large data set it would make sense to add the time taken to write to
external disc as an operation since this can be slow. Whereas if the data was held
inside the computer then writing the data into an array held internally would be quick
and would not need to be counted as an operation. The computer scientist is
interested in the final step when the algorithm is implemented on a given computer.
The size and nature of the data set would be a consideration. A mathematician would
concentrate on the properties of the algorithm in the abstract and before a choice of
computer is made. (Different steps in our solution process.)
An algorithm can be very linear and then the time taken through it is easy to calculate.
Many algorithms have more of a tangled tree structure and then we are forced to talk
about mean times through the algorithm. In such cases it often pays to look at worst
October 2011 CATAM Irena Borzym
12:19 PM 17/10/2011 Page 25 of 27 immb100@damtp.cam.ac.uk
and best case scenarios. For example if you are given a list to sort and it is already in
alphabetical order then the sort program to put it into alphabetical order will drop
straight through and finish in a short time. If you are given the same list, maximally
disordered with respect to your sorting algorithm, then it will take a lot longer for the
program to run and sort the list. In some CATAM projects you are asked to consider
worst and best case scenarios. It obviously makes sense to worry about the worst case
if that might cause the program to fail on a particular computer. It is therefore in your
interest to ask yourself in any particular project whether the structure of your
algorithm is sufficiently complicated for it to be worth looking at the worst and best
cases. As always in a given CATAM project you should use your common sense. Do
not shoot off along a tangent just because worst and best cases were mentioned today
but do make a comment if the problem merits a remark.
When you are calculating complexity it is often not necessary to count every detailed
step. Most algorithms have a few start and finish steps and then some loops. If you
know that the loops will be run many times within the algorithms then there is no
point in counting the steps which are only carried out once. Since you are interested
in large n behaviour you can afford to ignore all but the dominant contribution. In
your answer you should give the asymptotic form, that is the leading term only. I
would include the hidden constant in the term unless your specific CATAM
instructions tell you to ignore it but very often people do not include this constant
because it is compiler dependent. Whichever you decide to do you must explain in
your write-up why you have made this choice. In a CATAM calculation of
complexity you should explain how you count the operations. If you are not sure
whether to include an operation then ask the Catam Helpline. If that fails then ask
around to find out how long the operation takes. This may not be easy to find out!
Once you know then you can say in your write up that, for example, you are including
checking for equality as an arithmetic operation because it takes a time which is
comparable or greater than the standard operations of addition and subtraction. If you
do not know then make an executive decision and justify it as best you can in your
write up.
One further level of complication can arise, and this is one reason why I might
include the hidden constant in my estimate of complexity. If you are inverting an n
by n matrix once then you will have a contribution of
3
n from the Gauss-Jordan
elimination due to the size of the matrix. If you are repeating the operation a million
times in your implemented algorithm, which in real life is not an uncommon situation,
then you will need to take
3 6
n 10 which is a big difference if 1000 n = . In other
words you may have more than one n and you will need to string the contributions
together. In this case we have
3
mn . This final consideration should not arise in
CATAM.
REF: For nice list of complexities.
http://en.wikipedia.org/wiki/Computational_complexity_of_mathematical_operations
REF: Example calculation source material, modified.
http://en.wikipedia.org/wiki/Analysis_of_algorithms
October 2011 CATAM Irena Borzym
12:19 PM 17/10/2011 Page 26 of 27 immb100@damtp.cam.ac.uk
EXAMPLE: Evaluating run-time complexity
The run-time complexity for the worst-case scenario of a given algorithm can
sometimes be evaluated by examining the structure of the algorithm and making some
simplifying assumptions. Consider the following code.
Step 1 Read the first number n from the input vector
Step 2 If the number is greater than ten
Step 3 then print Large
Step 4 For i = 1 to n
Step 5 For j = 1 to i
Step 6 Print i*j
Step 7 Print Finished.
In the algorithm above, steps 1, 2 and 7 will only be run once. For a worst-case
evaluation, it should be assumed that step 3 will be run as well. Thus the total amount
of time to run steps 1-3 and step 7 is:
We still need to evaluate the loops in steps 4, 5 and 6.
The outer loop test in step 4 will execute ( n + 1 ) times. The extra step is needed to
terminate the for loop. This takes T
4
( n + 1 ) time.
The inner loop, on the other hand, is governed by the value of i, which iterates from 1
to n. On the first pass through the outer loop, j iterates from 1 to 1. The inner loop
makes one pass, so running the inner loop body, step 6, takes T
6
time, and the inner
loop test, step 5, takes 2T
5
time. During the next pass through the outer loop, j iterates
from 1 to 2. The inner loop makes two passes, so running the inner loop body, step 6,
consumes 2T
6
time, and the inner loop test, step 5, consumes 3T
5
time.
Altogether, the total time required to run the inner loop body can be expressed as an
The total time required to run the inner loop test can be evaluated similarly.
Therefore the total running time for this algorithm is:
October 2011 CATAM Irena Borzym
12:19 PM 17/10/2011 Page 27 of 27 immb100@damtp.cam.ac.uk
Usually we can assume that the highest-order term in any given function dominates its
rate of growth and thus defines its run-time order. In this example, n is the highest-
order term, so we conclude that f(n) = O(n).
CLAIM:
Proof
For positive integer n we have
Let k be a constant greater than or equal to [T
1
..T
7
]
= 12kn
2
Therefore
for c = 12k, n
0
= 1
One final word of advice.
CATAM is about using your common sense as well as your mathematical training to
understand a given project. You only have to demonstrate you thinking to do well.
Good Luck