Decision Maths 1 Chapter 1 Algorithms
Decision Maths 1 Chapter 1 Algorithms
Andrew.Blackett@UTCSouthDurham.org
now = datetime.now()
mm = str(now.month)
dd = str(now.day)
yyyy = str(now.year)
hour = str(now.hour)
mi = str(now.minute)
ss = str(now.second)
print mm + "/" + dd + "/" + yyyy + "
" + hour + ":" + mi + ":" + ss
1) Buy cake
2) Eat cake
1.1 Examples of algorithms
The ‘peasant’ algorithm on two numbers is as follows:
Make a table with two columns, A and B.
1) Write A and B
2) Let A = half A (ignoring remainders), Let B = double B.
3) Write A and B.
4) Repeat step 2 & 3 until A = 1.
5) Delete all the rows where A is even
6) Find the sum of the non deleted Bs, let this equal p.
7) Write p = ….
A B C D
2 3 0 0
2 3
4 6
6
Print 6
?
Exercise 1A
Pearson Decision 1, Page 5
1.2 Flow Charts
There are three shapes of boxes which are used in the examination:
As with an algorithm written in words, you need to follow each step in order.
Example 4
n E Decision
Let n = 0
0
Let n = n + 1
1 2 No
Let E = 2n 2 4 No
Print E
3 6 No
4 8 No
Is No
n > 9?
5 10 No
Yes 6 12 No
Stop
7 14 No
8 16 No
9 18 No
10 20 Yes
Example 5
This flowchart can be used to find roots of an equation of
the form 𝑎𝑥 2 + 𝑏𝑥 + 𝑐 = 0.
𝑎) 6𝑥 2 − 5𝑥 − 11 = 0 a)
𝑏) 𝑥 2 − 6𝑥 + 9 = 0
𝒂 𝒃 𝒄 𝒅 𝒅<𝟎 𝒅=𝟎
𝑐) 4𝑥 2 + 3𝑥 + 8 = 0
6 -5 -11 289 No No
11
Roots are 𝑥1 ?
= , 𝑥2 = −1
6
b)
𝒂 𝒃 𝒄 𝒅 𝒅<𝟎 𝒅=𝟎
1 -6 9 0 No Yes
c)
? 𝑥=3
Equal roots
𝒂𝒂 𝒃𝒃 𝒄𝒄 𝒅𝒅 𝒅𝒅 <
< 𝟎𝟎 𝒅𝒅== 𝟎𝟎
4 3 8 -119 yes
No real roots?
Test your understanding – The Euclidian algorithm
Input various values for A=16 and B=12, use a trace table to show
your results and work out what the Euclidian Algorithm is doing.
1-3 16 12 no Yes
6 4
2-5 8 no no
2-5 4 no no
2-5 0 no no
2 yes
Print 4 ?
Applications:
Click to
31 19 55 42 2 112 67 Animate
19 31 42 55 2 67 112 Click to
Animate
After the second pass, we know that the 67 is now in the right place. We keep doing
passes, ignoring one extra item at the end each time.
We can stop if there are no swaps
? on a pass.
Analysis of Bubble Sort
! Bubble Sort Exam Tip: Bubble
Sort should not be
1. On each pass, look at each pair of terms left-to-right, confused with a
swapping if in the wrong order. ‘Bubble Snort’,
which should be
2. Keep doing further passes, ignoring one extra value at the avoided at all costs.
end each time.
3. We are done if there are no swaps on a pass.
Pros Cons
Use a bubble sort to arrange these letters into alphabetical order, showing
the state of the list after each pass.
A L G O R I T H M
1st pass A G L O I? R H M T
2nd pass A G L I O? H M R T
3rd pass A G I L H? M O R T
4th pass A G I H L? M O R T
5th pass A G H I L? M O R T
6th pass A G H I L? M O R T
Test your understanding – past exam question
?
Grumpy examiner’s report – is he talking about you?
Further test your understanding
?
Bubble sort - quiz
1. Which number is definitely in its correct position at the end of the
first pass if sorting a list into ascending order?
The largest number is in the correct position if sorting left to right. If
? will be in the correct position.
sorting right to left, the smallest number
2. How does the number of comparisons required change as the pass
number increases?
Quicksort is a divide-and-conquer algorithm. It works by selecting a 'pivot' element from the array and
partitioning the other elements into two sub-arrays, according to whether they are less than or greater
than the pivot. The sub-arrays are then sorted recursively. This can be done in-place, requiring small
additional amounts of memory to perform the sorting.
https://en.wikipedia.org/wiki/Quicksort
Quick sort algorithm
You select a pivot and then split the items into two sub-lists – those less than the pivot
and those greater than the pivot:
1. Choose the item at the mid-point of the list to be the first pivot (if there an even
number of items, choose right of the middle – choosing left is also ok)
2. Write down all the items that are less than the pivot, keeping their order, in a sub-list
3. Write down the pivot
4. Write down the remaining items (those greater than the pivot) in a sub list
5. Apply steps 1 to 4 to each sub-list
6. When all items have been chosen as pivots, stop
18 20 11 7 17 15 14 21 23 16 9
The list of numbers shown above is to be sorted into ascending order. Apply
quick sort to obtain the sorted list. You must make your pivots clear.
Test your understanding
Test your understanding
Challenge time!
Challenge 2 – can you arrange the numbers 1-7, so every item has
been chosen as a pivot after 7 passes.
7 5 3 1 2 4 6
Exercise 1D
Pearson Decision 1, Page 15, Q2,5,6
1.5 Bin Packing
In the bin packing problem, items of different volumes must be packed into a finite
number of bins or containers each of a fixed given volume in a way that minimizes the
number of bins used. In computational complexity theory, it is a combinatorial NP-
hard problem. The decision problem (deciding if items will fit into a specified number of
bins) is NP-complete.
There are many variations of this problem, such as 2D packing, linear packing, packing by
weight, packing by cost, and so on. They have many applications, such as filling up
containers, loading trucks with weight capacity constraints, creating file backups in media
and technology mapping in field-programmable gate array semiconductor chip design.
For your course you need to know how to implement three bin-
packing algorithms.
• First-fit
• First-fit decreasing
• Full-bin
1d pin-backing
3d bin-packing
Lower Bounds
The lower bound:
Nine boxes of fixed cross section have heights, in metres, as follows;
0.3, 0.7, 0.8, 0.8, 1.0, 1.1, 1.1, 1.2, 1.5
They are to be placed into bins with the same fixed cross section and
height 2m. Determine the lower bound for the number of bins
needed.
To find the lower bound.
0.3 + 0.7 + 0.8 + 0.8 + 1.0 + 1.1 + 1.1 + 1.2 + 1.5 = 8.5m
• Sum all the items.
8.5 ÷ 2 = 4.25 bins • Divide by the size of
the bin
So a minimum of 5 bins will be needed.
• Always round up!
Advantage:
It is quick to do.
Disadvantage:
It is not likely to lead to a good solution.
First-fit algorithm - example
Use the first-fit algorithm to pack the following items into bins of size 20 (the
numbers in brackets are the size of the item).
• State the amount of bins used and the amount of wasted space.
This used 6 bins and there are 26 = (2 + 5 + 7 + 12) units of wasted space.
First-fit decreasing algorithm
Advantages:
You usually get a fairly good solution.
It is easy to do.
Disadvantage:
You may not get an optimal solution.
First-fit decreasing algorithm - example
A(8) B(7) C(14) D(9) E(6) F(9) G(5) H(15) I(6) J(7) K(8)
First – rearrange into decreasing order
H(15) C(14) D(9) F(9) A(8) K(8) B(7) J(7) E(6) I(6) G(5)
Advantage:
You usually get a fairly good solution.
Disadvantage:
It is difficult to do, especially when the numbers are plentiful and awkward.
Full Bin Packing - example
A(8) B(7) C(10) D(11) E(13) F(17) G(4) H(6) I(12) J(14) K(9)
Listen in….
Determining the order of an algorithm could lead to you winning $1,000,000. The problem
known as the P vs NP Problem is one of the 7 Millennium Prize Problems, it asks if every
problem has an algorithm which has polynomial order. Worth paying attention this lesson ay?
https://en.wikipedia.org/wiki/P_versus_NP_problem
1 algorithm
Order of an algorithm……
1.6 Order of an algorithm
• If an algorithm has order n2, what will be the effect of increasing the size of
the problem from 10 to 20?
202
= 4 so the run time will increase by approximately a factor of 4
102
• If an algorithm has order 𝑛3 , what will be the effect of increasing the size of
the problem from 10 to 20?
203
= 8 so the run time will increase by approximately a factor of 8
103
Order of bubble sort algorithm
For the bubble sort, most of the steps are to do with making
comparisons between pairs of numbers.
• If a list has n items the first pass will require (n-1) comparisons.
• The second pass will require (n-2) comparisons.
• In the worst case this process will continue so that (n-3) are needed for the
3rd pass, and so on right down to 1 comparison in the final pass.
• The number of comparisons would then be.
Some quadratic order algorithms
could be much quicker to run
• 1 + 2 + 3 + … + (n-2) + (n-1) than others, if the order of an
algorithm was 100𝑛2 it would
obviously have a longer run time
1 1 2 1 than one with order 0.5𝑛2
• = 𝑛−1 𝑛 = 𝑛 − 𝑛
2 2 2
• Since this is a quadratic expression, the bubble sort has quadratic order.
Example 17 – determine the order from a flowchart
No
1250 𝑡∝𝑛
= 2.5
500 𝑡 = 𝑘𝑛
0.036 × 2.5 = 0.09
𝑡 = 0.036, 𝑛 = 500
Approximately 0.09 seconds
0.036
= 𝑘 = 0.000072
500
𝑡 = 0.000072𝑛
480,000 𝑡 ∝ 𝑛2
= 300
1600 𝑡 = 𝑘𝑛2
0.2 × 3002 = 18,000
Approximately 18000 seconds 𝑡 = 0.2, 𝑛 = 1600
or 5 hours to sort 480,000
numbers. 0.2 −8
= 𝑘 = 7.8125 × 10
16002
𝑡 = (7.8125 × 10−8 )𝑛2
• Choose a starting vertex and list all possible routes which satisfy the
problem.
• Determine the order of the brute force search.
EABCD, EABDC, EACBD, EACDB, EADBC, EADCB,
EBACD, EBADC, EBCAD, EBCDA, EBDAC, EBDCA,
ECABD, ECADB, ECBAD, ECBDA, ECDAB, ECDBA,
EDABC, EDACB, EDBAC, EDBCA, EDCAB, EDCBA
22.5 × 106 𝑡 ∝ √𝑛
=9
2.5 × 106 𝑡 = 𝑘√𝑛
12 × 9 = 36 seconds
𝑡 = 12 𝑛 = 2.5 × 106
12
= 𝑘 = 7.589466 … × 10−3
√2.5 × 106
The exact run time will
depend on the initial 𝑡 = (7.589466 … × 10−3 )√𝑛
ordering of the items.
𝑡 = 7.589466 … × 10−3 × (22.5 × 106 = 36𝑠
Exercise 1F
Pearson Decision 1, Page 24,
2)The grid method of multiplication can multiply an n digit, by another n digit number. Consider the number of individual multiplications required for
multiplying 2 such numbers. What is the order of this algorithm?
3)A search algorithm is written to find the closest 3 dimensional integer coordinates to a point in 3 dimensional space.
The algorithm considers every integer coordinates in a 𝑛 × 𝑛 × 𝑛 cube and computes (using Pythagoras) the distance from that coordinate to a known
point.
What is the order of this algorithm?
4) If there are n people in a room who all want to shake hands with each other, what is the expression in terms of n, for the total number of handshakes
required? What is the order of this expression?