Beruflich Dokumente
Kultur Dokumente
Algorithms
What is an algorithm?
An algorithm is sequence of instructions that will solve
a particular problem.
There can be no ambiguity in the instructions.
There must be a clause that can terminate the
algorithm.
Algorithms can be shown in a number of ways;
A list of instructions
Pseudo code (computer language)
Flow charts
Zeller`s Algorithm
Do you know which day you were born on?
Zellers Algorithm
Let day number = D
Month number = M
And year = Y
If M is 1 or 2 replace M with M
add 12 and replace Y with Y
subtract 1.
Let C be the first two digits of Y
and Y` be the last to digits of Y.
Add together the integer parts of
(2.6M - 5.39), (Y`/4), (C/4).
Now add on D and Y` and
subtract 2C.
Find the remainder when the
answer is divided by 7.
0 = Sunday
1 = Monday
etc
D = 06
M = 02
Y = 1977
D = 06
M = 14
Y = 1976
C = 19
Y` = 76
(2.6 x 14 5.39 = 31)
76/4 = 19
19/4 = 4
31 + 19 + 4 = 54
54 + 6 + 76 2 x 19 = 98
98/7 = 14 r 0
Sunday
Is fair and wise and good and gay.
Now try and see what day you were born on.
numbers to be multiplied.
Step 2 Beneath the left number
write down double that number.
Beneath the right number write
down half that number
Step 3 Repeat step 2 until the right
number is 1.
Step 4 Delete those rows where
the number in the right column is
even.
Step 5 Add up the remaining
numbers in the left column.
So 24 x 163 = 3912
24
48
96
192
384
768
1536
3072
3912
163
81
40
20
10
5
2
1
163
326
652
1304
2608
3912
24
12
6
3
1
76
152
304
608
1216
2432
4028
53
26
13
6
3
1
28 x 405
28 405
56 202
112 101
224 50
448 25
896 12
1792
3584
7168
11340
6
3
Decision Maths
Algorithms: Flow charts
Flow Charts
A diamond indicates a
question or choice to be
made.
A rectangle is an
instruction box, it has one
entrance and one exit.
A circle (oval) indicates a
start or stop.
Flow diagrams
Below is an algorithm expressed in psuedo code.
Step 1 - Let M = b2 4ac.
Step 2 - If M < 0 print no real solutions and go
to step 5.
Step 3 - Let x1 = (-b + M)/(2a)
Step 5 -
Stop
equations?
Yes
Print
no real Solutions
x>y?
Yes
Subtract y from
x to get new
value of x
Yes
Subtract x from
y to get new
value of y
No
x<y?
No
No
x=y?
Yes
Output x
Can you draw a flow chart that would represent an algorithm for
Tea Algorithm
The example we have just looked at was a
Complexity
There is often more than one way of solving a
Complexity
To understand complexity we are going to look at two examples
can see that b2 4ac will be evaluated twice and hence is less
efficient.
b b 2 4ac
x1
2a
b b 2 4ac
x2
2a
Complexity
Lets now consider the evaluation of the
Complexity
Now let us extend this further to cubic polynomials.
f() = 43 + 32 + 2 + 1
f(4) = 4 x 4 x 4 x 4 + 3 x 4 x 4 + 2 x 4 + 1 = 313
This has 6 multiplications and 3 additions
Again we can re-arrange f()
f() = 43 + 32 + 2 + 1
= (42 + 31 + 2) + 1
= ((4 + 3) + 2) + 1
Now ((4 x 4 + 3) x 4 + 2) x 4 + 1 = 313
This has 3 multiplications and 3 additions.
Hopefully you can see that the second way is more
efficient.
Complexity
Lets generalise this for any polynomial
ann + an-1n-1 + + a1 + a0
We can use our previous results to help, and make a
Degree of
polynomial
Method 1
(Straight forward)
Method 2
(Nested)
Multiplications
Additions
Multiplications
Additions
10
(n/2)(n+1)
You can see from the table that the nested method for
Complexity
Because there is an n2 term in the number of