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.
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 ; 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 to , Step 4 replace by , Step 5 repeat while . For , takes 14, 11, 8, 5, 2 and then , and . The loop test is applied after changes, so the final is not added.
Adding or counting the value that makes the loop test fail. Track exactly when the test is applied.
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 items the middle item is in position if is odd and if is even. So gives the 5th item and gives the 4th item. Note that for an even the middle item is the one just after the true centre. Example: for 10 items the middle is position . 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.
Using position for an even list. The glossary rule gives .
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 rounded up. For files of total size 73 MB on 20 MB sticks, , so at least 4 sticks are needed. If an algorithm uses exactly the lower bound, the packing is optimal.
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 , , , , , which is 5 bins. Sorting to 13, 12, 11, 10, 9, 7, 6, 5 gives , , , , which is 4 bins and meets the lower bound.
Putting an item in the bin with most room instead of the first bin that fits.
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 the remainder of , then and until : for , 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
- An algorithm is described as follows.
Step 1: Input a number .
Step 2: Set equal to 0.
Step 3: Add to .
Step 4: Replace by .
Step 5: If , go to Step 3.
Step 6: Print .Find the value printed when the input is .2 marks
- 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
- An algorithm is described as follows.
Step 1: Input two positive integers and , with .
Step 2: Let be the remainder when is divided by .
Step 3: If , print and stop.
Step 4: Replace by and by , then go to Step 2.Apply the algorithm to the inputs and , showing the values of , and each time Step 2 is performed.3 marks
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).