All revision notes topics

Algorithms, flow charts and bin packingEdexcel International A Level Maths: Revision notes

Section 1

Algorithms

An algorithm is a finite sequence of precise, unambiguous steps that solves a problem and always stops. It can be written as numbered text instructions or drawn as a flow chart. To use an algorithm, follow the steps exactly. Inputs are the values you start with, a loop repeats steps (here by going back to an earlier step) and a decision chooses between two routes (Yes or No). You are not expected to work out the order (efficiency class) of an algorithm.

Key termsalgorithmloopinput

Section 2

Flow charts and trace tables

In a flow chart: an oval is Start or Stop; a parallelogram is Input or Print; a rectangle is a process such as S=S+NS=S+N; a diamond is a decision with Yes and No branches; arrows show the order. Apply an algorithm by writing a trace table with one column for each variable and a new row each time a value changes. Example: Step 3 add NN to SS, Step 4 replace NN by N−3N-3, Step 5 repeat while N>0N>0. For N=14N=14, NN takes 14, 11, 8, 5, 2 and then −1-1, and S=14+11+8+5+2=40S=14+11+8+5+2=40. The loop test is applied after NN changes, so the final N=−1N=-1 is not added.

Key termsflow charttrace table
Common mistake

Adding or counting the value that makes the loop test fail. Track exactly when the test is applied.

Exam tip

Show every pass of a loop in your working so that you earn method marks even if the final value is wrong.

Section 3

Finding the middle item

Whenever an algorithm needs the middle item of a list, use the glossary method. In a list of NN items the middle item is in position N+12\frac{N+1}{2} if NN is odd and N+22\frac{N+2}{2} if NN is even. So N=9N=9 gives the 5th item and N=6N=6 gives the 4th item. Note that for an even NN the middle item is the one just after the true centre. Example: for 10 items the middle is position 10+22=6\frac{10+2}{2}=6. If a search deletes the middle item and the items beyond it, the remaining list is renumbered from 1 before the rule is applied again.

Key termsmiddle item
Common mistake

Using position N2\frac N2 for an even list. The glossary rule gives N+22\frac{N+2}{2}.

Section 4

Bin packing: the problem

Bin packing asks for the items to be placed into as few bins of a fixed capacity as possible. Every item must fit in a bin, and the total in each bin cannot exceed the capacity. The algorithms below give a good solution, not always the best one, so they are heuristic. A lower bound on the number of bins is ⌈total size of all itemsbin capacity⌉,\left\lceil\frac{\text{total size of all items}}{\text{bin capacity}}\right\rceil, rounded up. For files of total size 73 MB on 20 MB sticks, 7320=3.65\frac{73}{20}=3.65, so at least 4 sticks are needed. If an algorithm uses exactly the lower bound, the packing is optimal.

Key termsbin packinglower bound

Section 5

First-fit and first-fit decreasing

First-fit: take the items in the order given. Place each item in the first bin that has room, starting a new bin only when no earlier bin can take it. First-fit decreasing: first sort the items into decreasing order, then apply first-fit. It usually uses fewer bins. Full-bin: look for combinations of items that fill a bin exactly, pack these first, then pack the rest by first-fit. Example, capacity 20, items 9, 7, 11, 6, 5, 13, 12, 10: first-fit gives {9,7}\{9,7\}, {11,6}\{11,6\}, {5,13}\{5,13\}, {12}\{12\}, {10}\{10\}, which is 5 bins. Sorting to 13, 12, 11, 10, 9, 7, 6, 5 gives {13,7}\{13,7\}, {12,6}\{12,6\}, {11,9}\{11,9\}, {10,5}\{10,5\}, which is 4 bins and meets the lower bound.

Key termsfirst-fitfirst-fit decreasingfull-bin
Common mistake

Putting an item in the bin with most room instead of the first bin that fits.

Exam tip

List the contents and running total of every bin so the examiner can see each decision.

Section 6

Showing how an algorithm is applied

Show clearly how an algorithm has been applied: write the successive values, the item chosen and the resulting list or bins at each stage. For a text algorithm such as Euclid's, with RR the remainder of A÷BA\div B, then A←BA\leftarrow B and B←RB\leftarrow R until R=0R=0: for A=252A=252, B=105B=105 the remainders are 42, 21 and 0, and the output is 21, the highest common factor. State the purpose of an algorithm only after tracing it, and quote the output with its context.

That's the notes covered.

Carry on to the next subtopic.

Exam questions on Algorithms, flow charts and bin packing

  1. An algorithm is described as follows. Step 1: Input a number NN. Step 2: Set SS equal to 0. Step 3: Add NN to SS. Step 4: Replace NN by N−3N-3. Step 5: If N>0N>0, go to Step 3. Step 6: Print SS.
    Find the value printed when the input is N=20N=20.2 marks
  2. Eight files of sizes 9, 7, 11, 6, 5, 13, 12 and 10 megabytes (MB) are to be stored on memory sticks. Each memory stick can hold at most 20 MB.
    Use the first-fit decreasing algorithm to pack the files, and write down the contents of each memory stick.2 marks
  3. An algorithm is described as follows. Step 1: Input two positive integers AA and BB, with A>BA>B. Step 2: Let RR be the remainder when AA is divided by BB. Step 3: If R=0R=0, print BB and stop. Step 4: Replace AA by BB and BB by RR, then go to Step 2.
    Apply the algorithm to the inputs A=252A=252 and B=105B=105, showing the values of AA, BB and RR each time Step 2 is performed.3 marks
See the full worksheet

Written by the Exaim team, led by Shaun Daswani (Head of Upper Secondary, Improve ME Institute; MSc Financial Mathematics, Imperial College London; BSc, UCL) and Jason Daswani (operational lead, Improve ME Institute; LSE).