Practical Activities for teaching Decision Mathematics MEI Conference 29th July 2009 Jeff Trim - jefftrim@fmnetwork.org.uk Graph Theory Word Maze A puzzle activity for reinforcing Graph Theory vocabulary A Handful of Mathematicians Short biographies of 5 pioneers of Decision Mathematics Graph Theory Vocabulary – Hexagon Puzzle Tarsia puzzle for revising definitions of common Graph Theory terms Instant Insanity This activity demonstrates the power of Graph Theory to solve problems Bin Packing Exercise A practical activity which practises bin-packing but demonstrates that the algorithms do not necessarily lead to an optimal solution Algorithms for Sorting The differences between four sorting algorithms are emphasised by using numbered playing cards and a practical approach Sprouts A pen-and-paper game using nodes/vertices and arcs/edges which can be easily analysed using familiar concepts There are 20 words associated with Graph Theory hidden in the square below. The Start and Finish squares are indicated and the words lie in a continuous path from one to the other. All moves are horizontal or vertical (not diagonal.). The initial letter of each word and the order in which they appear are given to stop you getting lost! Start A E D D E G R E N E C R C G E A R T E N O T 1. A 2. E 3. P 4. C 5. T 6. D 7. L 8. T 9. A 10. B 11. R 12. W 13. D 14. C 15. V 16. C 17. E 18. N 19. S 20. H L P E E I L P L X C E A C L R D A O O E E D N Y E T J L E R T U L E C E C A P T E O N E B T N A L M E V D N R I O U W K O I S E A I P R T E D C M A M I L A E T G I H P H N O T R T I R A P L E I A N Finish Start A E D D E G R E N E C R C G E A R T E N O T L P E E I L P L X C E A C L R D A O O E E D N Y E T J L E R T U L E C E C A P T E O N E 1. A R C 2. E D G E 3. P L A N E 4. C Y C L E 5. T R E E 6. D E G R 7. L O O P 8. T R A I L 9. A D J A C E N T 10. B I P A R T I T 11. R O U T E 12. W A L K 13. D I G R A P H 14. C O M P L E T E 15. V E R T E X 16. C O N N E C T E 17. E U L E R I A N 18. N O D E 19. S I M P L E 20. H A M I L T O N E B T N A L M E V D N R I O U W K O I S E A I P R T E D C M A M I L A E T G I H P H N O T R T I R A P L E I A N E E D I A N Finish A HANDFUL OF MATHEMATICIANS Short biographies of five of the mathematicians who have contributed to Decision Mathematics and Graph Theory Leonhard Euler Prolific 18th Century mathematician and founder of modern graph theory, after whom is named the Eulerian Cycle Sir William Rowan Hamilton Childhood mathematical prodigy and later Astronomer Royal of Ireland, after whom is named the Hamiltonian Cycle Robert Prim Developer of Prim’s Algorithm for finding a minimum spanning tree Joseph Kruskal Inventor of Kruskal’s Algorithm for finding a minimum spanning tree Edsger Dijkstra Computer scientist best known for the Dijkstra Algorithm to find a shortest path between two vertices EULER Leonhard Euler (pronounced ‘Oyler’) (1707-1783) b. Switzerland; lived and worked in Russia and Germany Euler was an extremely prolific mathematician, contributing to the understanding of such diverse areas as calculus, topology, optics and astronomy. He popularised the use of the specific symbols i (for √ -1), e and π. In particular, whilst the base for the natural logarithm had been discussed earlier by mathematicians with the symbol b, it was Euler who first used e to represent this constant in his book ‘Mechanica’ of 1736. Although e is sometimes referred to as Euler’s Number, his innate modesty precludes the thought that the letter was chosen because it was his initial! His name is given to the Eulerian Cycle; a closed path which traverses every edge in a graph exactly once. A graph which contains such a path is called an Eulerian Graph and the simple test is that the graph must be connected and have no vertices of odd degree. If there are two odd nodes, the graph will be semi-Eulerian, where the path starts and finishes at different points and so cannot be a cycle. The concept of an Eulerian Cycle relates back to a puzzle that Euler proved could not be solved concerning the seven bridges of Kőnigsberg. A feature of this Prussian city (now called Kaliningrad) was the seven bridges across the river Pregel and its tributaries. The challenge was to find a route whereby a citizen could cross every bridge once and return home again. In creating the mathematics to show that such a cycle was impossible, Euler became the founding father of topology. Euler lost the sight in his right eye after a life-threatening fever, although he blamed the loss on over-work. It is at this stage in his life that the well-known portrait (seen above) was painted. The sight in his left eye was subsequently lost due to a cataract. The fact that he continued to work, using his two sons as amanuenses, is testimony to his photographic memory and phenomenal skills of mental calculation. Amongst a range of other results named after him, is Euler’s Identity: eiπ + 1 = 0. This remarkable brief statement combines the five most important mathematical numbers using the four basic operations of addition, multiplication, exponentiation, and equality, each used exactly once. HAMILTON Sir William Rowan Hamilton (1805-1865) b. Dublin, Ireland; lived in Ireland William Hamilton was the son of a Dublin solicitor and his intellectually gifted wife, although both parents had died by the time William was 14. His prodigious talent was evident very early on and from the age of 3 he was sent to live with his uncle, Reverend James Hamilton in the village of Trim (about 20 miles from Dublin). This uncle, an accomplished linguist and polymath, instructed William, who at the age of 13 could claim that he had mastered a language for each year he had lived. Even prior to his graduation, at age 22 he was appointed Professor of Astronomy at Trinity College, Dublin, Director of the Dunsink Observatory and Astronomer Royal for Ireland. He was knighted at 30 and in 1837 became President of the Royal Irish Academy. He is considered to be Ireland’s greatest man of Science. Hamilton made valuable contributions to various different fields of science; algebra, optics, dynamics and quantum mechanics. Of interest to pure mathematicians is his pioneering work in the discovery of Quaternions, involving the extension of the concept of complex numbers into four dimensions. He may also be considered as the inventor of the ‘cross product’ and ‘dot product’ of vector algebra. In graph theory, a cycle which visits every vertex in a graph exactly once is known as a Hamiltonian Cycle. The still-unsolved Travelling Salesman Problem is a variation on this principle, where a tour of minimum length must be found which visits each vertex at least once. In 1857, in his spare time Hamilton invented The Icosian Game, based on the twenty vertices of an Icosahedron. The simple nature of the game was that the player, given the first five vertices, had to complete the route through the remaining fifteen vertices without repeats. The game was subsequently sold to a London games dealer, John Jaques & Sons, who marketed it in two forms – flat for parlour use and hand-held for journeys – under the name “Around the World”. The game was not a commercial success and Hamilton, who was paid £25 outright, had the better part of the bargain! PRIM Robert Clay Prim (1921- ) b. Sweetwater, Texas, USA Robert Prim was a graduate of Princeton University, where he studied Electrical Engineering and subsequently (after World War II) also Mathematics, also remaining for a couple of years as a research associate. During the war years, he had worked as an engineer and been employed by the United States Naval Ordnance Laboratory. Robert Prim spent the late 1950’s and early 1960’s working at Bell (Telephone) Laboratories and then moved on to become Vice President of Research at Sandia National Laboratories. It was at Bell Laboratories, where he was Director of Mathematics research, that he developed Prim’s Algorithm for finding a Minimum Spanning Tree. It is for this that he is best known in Decision Mathematics. Together with his co-worker, Joseph Kruskal, two different methods were developed. In fact Prim’s algorithm had previously been discovered in 1930 by Vojtech Jarnik, was independently found by Prim in 1957 and subsequently rediscovered by Edsger Dijkstra in 1959. It is sometimes therefore called the DJP Algorithm! In contrast to Kruskal’s Algorithm, which focuses on the systematic selection of edges, Prim’s Algorithm builds up a set of connected vertices by progressively adding in the vertex least distant from the existing set, whilst ensuring none are revisited, thus avoiding cycles. The edges used form the minimum spanning tree of the graph. This algorithm was first published in the ‘Bell System Technical Journal’ in 1957. KRUSKAL Joseph Bernard Kruskal (1928- ) b. New York, USA Joseph Kruskal is a graduate of the University of Chicago with a PhD from Princeton University. He is a fellow of the American Statistical Association and former President of the Psychometric Society. He has also campaigned for civil rights and fair housing. Joseph Kruskal worked with Robert Prim at the Bell Laboratories where they developed two different approaches to the same problem. Formerly know as the Bell Telephone Laboratories, their practical application was focussed on communications networks. These can be represented as graphs with weighted edges. Kruskal’s Algorithm, also known as the ‘Greedy algorithm’, is a method for finding a Minimum Spanning Tree for a given network by choosing in turn the edges with the lowest weights whilst avoiding the creation of cycles. In fact the term greedy algorithm is a more general expression meaning a process which chooses at a given stage during each iteration the best option at that point. Kruskal’s Algorithm was first published in the ‘American Mathematical Society Proceedings’ in 1956. Kruskal later said that he regretted that the term minimum spanning tree has taken hold, claiming that ‘minimum’ is too vague; minimum in what way? He stated: “I always think of the concept as ‘shortest spanning subtree’ and hope someday to see SST replace MST.” Joseph Kruskal also had two brothers who were renowned in different areas of mathematics and physics; Martin Kruskal, the co-inventor of ‘solitons’ and ‘surreal numbers’ and William Kruskal, who developed the Kruskal-Wallis one-way analysis of variance. DIJKSTRA Edsger Wybe Dijkstra (pronounced ‘Deekstrah’) (1930-2002) b. Rotterdam, Netherlands; worked in Amsterdam and also Texas, USA Edsger Dijkstra studied Theoretical Physics at the University of Leiden before moving into the field of Computer Science. He worked for the Mathematisch Centrum in Amsterdam and then the Burroughs Corporation, which had progressed from making adding machines to typewriters and computers. He later held the Schlumberger Centennial Chair in Computer Sciences at the University of Texas. He wrote prolifically on programming and observed that “programming is so inherently difficult and complex that programmers need to harness every trick and abstraction possible in hopes of managing the complexity of it successfully”. He was renowned for working carefully in fountain pen and then photocopying and distributing these manuscripts, each numbered with EWD as the pre-fix. More than 1300 of these hand-written articles are known to exist. In Graph Theory, he is known best for Dijkstra’s Algorithm for finding the Shortest Path between two points in a network, which he developed in 1959. This is a labelling procedure which identifies the smallest total path length to each vertex from the starting point. Tracing back from the finish vertex finds the shortest path itself. It is an example of a greedy algorithm because of the step during each iteration where the vertex with the lowest working value is chosen as the best and is then permanently labelled. He received the 1972 Turing Award (after Alan Turing, the British mathematician who helped break the German Enigma code in WWII) for fundamental contributions in the area of programming languages. Despite a career in programming, Dijkstra only owned one computer in his life. Even then it was after pressure from his colleagues and just used for email and internet browsing. This is in keeping with his quoted opinion that: “Computer Science is no more about computers than astronomy is about telescopes.” A subset of the vertices and edges of a larger graph o hn wit ges alk ed A w eated rep h p a r g i D Wa lk rk o tw e N Gr dir aph w ect ion ith al e dg K5 h rap Simple Graph alu e A Trail with no repeated vertices nG ria ule No de V Line from vertex to vertex −E um m ni Mi t ed a i c sso a ber edge m u n e n it h a h T w een etw ts b x is es h e rt ic pat v e e a r of her pai ... w any i Sem ree T g Point where edges meet nin n a Sp ... can be drawn without edges crossing Bipartite Graph Mo re t he t han sam one e p edg air e of bet w v er tic een es es nec t io ns ... t edg rav e e e rses xa ctl ev er yo y nce Complete Graph Gr a p of h w h v e by ertice re tw an y sa od c om re no istinc mo t jo t s e n edg ined ts es con ex t r Ve Path Eulerian Cycle x rte Ve Trail Co wit nnec te h n o o d gr d d n aph o des e4 Odd node Arc he dg wit hw ap Gr ho it h dd no deg ree ... connects all vertices with the least total edge length wit Cy cle e e r T Weight Complete graph with five vertices No de de o N Co on nnec e p ted air G of rap od d v h wit ert h ice s les cyc h wit s s h n rap ect io g ped conn a h s x nt ert e e r v fe e Dif sam t he ian ler Eu aph Gr ... v is it ex a s ev e ct l ry v yo nce ertex Isomorphic Graphs Gr of aph c arc on s a sist nd i nod ng es V a le n c y Planar Graph h pat sed clo A sequence of consecutive edges h p a r g b Su ... a ge Ed De g r ee o fa ve r t ex e g d e d e t a e Rep ... h or as r epe no lo o a t ed ps e dg es Point where edges meet Va len cy Connected Graph f o ex t r Ve 4 e re g e d ... where every vertex is joined to every other vertex Hamiltonian Cycle Ed ge wh ere edg es m eet The number associated with an edge um nim Mi gT nin an Sp ree h wit es t h rt ic leng e v e ll s a edg ect t al onn st t o ... c e lea th N o de int tex v er Po fa V Co on nnec e p t ed air G of rap od d v h wit ert h ice s rom x rt e ve Ed ge h rap s te g ertice e l v mp Co h fiv e wit ef Lin o xt rte ve K5 eo gre De ncy e l a e Tre Arc ge Ed Pla nar G rap h n edg be dr es aw cro n w ssi ng it hou t h Sem i− E ule r i a nG rap h ... ca es ycl oc hn Graph where two distinct sets of vertices are not joined by any common edges x rt e Ve o Pa th Gr ap h ... or has n rep o l eat oop ed s edg es s ice aph ert gr e v rger h t of a la set of ub es A s edg d an Co nn ect ed N e alu V e od Sim p wit More than one edge between the same pair of vertices le G rap h Gr ap ph h ap Gr Repeated edge 4 ree g e fd ra bg Su . ns Walk .. w h er an e a p y p at h air ex o f v ists ert bet ice we s en cy len Va AT rep rail ea w i t e d v th no ert ice s t io nec ... visits every vertex exactly once Trail con C o wit nnect h n ed g o o dd raph no des Bipartite Graph Vertex with odd degree Different shaped graphs with the same vertex connections e4 ler ian x rt e e V Eu a ... ler ian Cy cle th pa d e s clo Ne t wo rk ... t edg rav e e e rse s x a ctl ev er yo y nce edg Eu cle Cy ith Weight A sequence of consecutive edges et me Isomorphic Graphs Graph with directional edges es dg w de A walk with no repeated edges ... where every vertex is joined to every other vertex ee her No Hamiltonian Cycle G r of aph c arc on s s a nd ist ing no des w int Po Odd node Complete Graph Digraph Variations of this puzzle have been published and marketed under various titles over the years, but all have the same underlying design. The challenge is to stack four cubes such that each side of the stack shows four different images. The puzzle can be very frustrating to solve by trial and error, but yields very quickly to the approach described below. It is an excellent way to introduce applications of graph theory without straying into the common algorithms on the syllabus such as minimum spanning trees and shortest paths. In particular, it gives students a chance to use vocabulary such as vertex, edge, and sub-graph. Preparation: Nets for the puzzle can be found here: http://nrich.maths.org/public/viewer.php?rss=1&obj_id=443&part=index Symbols can be used for the cube faces instead of colours. Duplicate the puzzle sheet onto four different pale shades of paper and roughly cut out the nets. Distribute so that each student has each of A, B, C and D in a different colour. Paper is ideal for this (rather than card) and glue is not required. Scissors and coloured pencils to match the paper shades are needed. It really benefits the finished cubes if the folds are creased firmly first! Method: When students have made up the cubes and tried the problem, introduce the idea of using a graph to represent the symbols on opposite sides of each cube. Prepare a graph with four vertices. Assign one symbol to each vertex. For each cube, add three colour-coded edges to the graph. Each of these three arcs will connect the vertices corresponding to the symbols on each pair of opposite faces. The resulting graph will contain twelve arcs, three of each colour. Sub-graphs: The students need to find and draw two distinct sub-graphs, each of which contains one arc of each colour and in which each of the four vertices has degree two (i.e. is a ‘two-node’). The sub-graph need not be connected, but I have chosen two connected sub-graphs for my own solution. One nice outcome is that students will have a graph specific to their combinations of colours and therefore probably different to the person next to them! However, if the vertices have been labelled in the same way, at least the graphs will be isomorphic, ignoring the colours. Interpreting the Solution: Label one sub-graph “Left-Right” and the other “Front-Back”. Label the clockwise direction by each graph. Left – Right Front - Back The arcs of each colour, read clockwise, indicate how each cube is to be arranged in the tower. E.g. a green arc from flower to circle (as you read clockwise) on the Left-Right sub-graph means that on the green cube, the pair of opposite faces that are the flower and circle need to be arranged with the flower on the left and the circle on the right (from the student’s point of view!). It is often necessary to demonstrate how to do this for at least one cube! The solution omits one arc from each cube used in the original graph. These arcs correspond to the top and bottom faces that are concealed in the tower. The solution shown here is based on the four cubes having the following colours: A – Green B – Purple C – Orange D – Blue Graph: Sub-graphs: Left – Right Front - Back [See also p. 13-14, AEB Discrete Mathematics, Heinemann, 1992.] Where objects of varying sizes must be placed into containers of a fixed capacity, the problem is described as ‘bin packing’. The name is used for any problem of this general type, whether to do with objects, lengths, times or whatever the scenario. Two algorithms are commonly used to attempt to solve these problems: First-Fit Bin Packing: Number the bins, then always place the next item in the lowest numbered bin which can take that item. First-Fit Decreasing Bin Packing: Reorder the items into decreasing order of size. Number the bins, then always place the next item in the lowest numbered bin which can take that item. Activity A builder uses piping of standard length 12 metres. The following sections of varying lengths are required for a particular job: Section Length (metres) A B C D E F G H I J K L 2 2 3 3 3 3 4 4 4 6 7 7 Explore how many standard 12 m lengths of pipe will be required if each of the following methods is used: (a) First-fit bin packing (b) first-fit decreasing bin packing (c) trial and improvement [Adapted from p. 209-210, AEB Discrete Mathematics, Heinemann, 1992] Length of standard pipe section (metres) Length of standard pipe section (metres) Bins 12 11 10 9 8 7 6 5 4 3 2 1 12 11 10 9 8 7 6 5 4 3 2 1 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 Pipe Sections A(2) B(2) C(3) E(3) D(3) K(7) L(7) K(7) L(7) F(3) G(4) J(6) H(4) I(4) A(2) B(2) C(3) E(3) D(3) F(3) G(4) J(6) H(4) I(4) (a) First-fit bin packing 12 Length of standard pipe section (metres) 11 10 9 D(3) G(4) 8 7 6 I(4) C(3) 5 F(3) 4 3 B(2) L(7) 5 6 7 8 6 7 8 H(4) 2 1 K(7) J(6) E(3) A(2) 1 2 3 4 (b) First-fit decreasing bin packing 12 A(2) Length of standard pipe section (metres) 11 F(3) 10 9 G(4) H(4) I(4) 8 E(3) 7 6 5 4 D(3) K(7) L(7) J(6) 3 2 C(3) B(2) 1 1 2 3 4 5 (c) Trial and improvement 12 Length of standard pipe section (metres) 11 A(2) B(2) F(3) I(4) 10 9 C(3) D(3) 8 E(3) 7 H(4) 6 5 4 K(7) L(7) J(6) 3 G(4) 2 1 1 2 3 4 5 6 7 8 ALGORITHMS FOR SORTING BUBBLE SORT Step 1: Compare the first two numbers. Step 2: If the first number is larger than the second, exchange the numbers. Step 3: Repeat steps 1 and 2 for all pairs of numbers until you reach the end of the list. Step 4: Repeat steps 1 to 3 until no more exchanges are made. SHUTTLE SORT The Shuttle Sort works by comparing pairs of numbers and exchanging them if necessary. Step 1: Compare the first two numbers and exchange if necessary. Step 2: Compare the second and third numbers and exchange if necessary, then compare the second and first numbers and exchange if necessary. Step 3: Compare the third and fourth numbers and exchange if necessary, compare the second and third numbers and exchange if necessary, compare the second and first numbers and exchange if necessary. Step 4: For a list of length n, continue until n-1 passes have been performed. SHELL SORT The shell Sort differs from the Bubble and Shuttle methods as it compares, and possibly exchanges, non-adjacent elements. The set of elements is split into subsets. The number of subsets for the first pass is INT(n/2), that is, the number of elements, divided by two and ignoring any remainder. QUICK SORT Step 1: Choose any number x from the list L, (usually the number at the mid-point – but for AQA, choose the FIRST). Step 2: Write all the numbers smaller than x to the left of x, reading the original list from left to right. These form a new list L1. Write all of the numbers larger than x to the right of x reading the original list. These numbers form a new list L2. Step 3: Apply steps 1 and 2 to each separate list until all of the lists contain only one number. Step 4: The original list is now in ascending order. This is a simple pen-and-paper game for two players which involves arcs and nodes. It was invented in 1967 by Professor John H Conway and Michael S Paterson and has been described and analysed in books and on the internet. (Enter the words sprouts and Conway into a search engine for more information.) Sprouts with three spots: Draw three spots anywhere on the paper. Each player in turn draws a line joining one spot to another spot (or itself) and places a new spot somewhere on this line. No lines may cross, No spot may have more than three lines coming out of it (i.e. we would say that the degree of any vertex cannot exceed three). The game continues until no further moves are possible and the player to make the final move is the winner. A possible three spot game: Analysis: Providing students have met the fact that for any graph the node-sum is twice the number of edges (from the hand-shake lemma), then it is quite easy to prove that a three spot game of sprouts can never exceed 8 moves. The game begins with 3 vertices and no edges. Every move adds to the network 2 edges and one vertex. Therefore after m moves, the network will have gained 2m edges and m vertices. So the final graph contains 2m edges and m + 3 vertices. Since the winning move will leave at least one vertex of degree 2, the maximum number of 3-nodes is m + 2. Therefore node sum ≤ (m + 2) x 3 + 2 and since node sum = 2 x (no. of edges) we have so and so 2 x (2m) ≤ (m + 2) x 3 + 2 4m ≤ 3m + 8 m ≤ 8 Thus no game of three spot sprouts can exceed 8 moves. Sprouts can be played with any number of spots and it can be shown that an n spot game will never exceed 3n – 1 moves. Brussels Sprouts: A similar game can be played with crosses instead of spots, where each cross represents a vertex of maximum degree 4. A move joins two ‘branches’ of existing crosses (or a cross to itself) and a cross is placed on the new line. The start of a possible two cross game: In fact in Brussels Sprouts the first player always wins if the number of crosses is odd and the second player always wins where the number of crosses is even!