The Towers of Hanoi and Traveling Salesman problemsTwo other examples of problems that can be modeled by algorithms but are not tractable when they are scaled up, because they take an inordinate amount of time, are the Towers of Hanoi and the Traveling Salesman problem.Towers of Hanoi"...is a mathematical game or puzzle. It consists of three rods, and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top, thus making a conical shape.
The image below shows how disks are moved from the left peg to the right one. The number of moves is 2 n - 1, where n is the number of disks. The table below shows the moves for various numbers of discs. As can be seen the number of moves increases exponentially as n increases. Imagine that you could create a program that calculated a move every second, then for 40 discs the program would take 34,841 years (1,099,511,627,776 /60 /60 /24 /365.25). This makes the problem intractable for a large number of disks because it simply takes too long to run.The objective of the puzzle is to move the entire stack to another rod, obeying the following rules:
From http://en.wikipedia.org/wiki/Tower_of_Hanoi
Following is an algorithm or method to move the disks from a left-most peg (A) to a right-most peg (C), using these pegs and a middle one (B). In other words using a three peg arrangement (A, B, and C).
Alternating between the smallest and the next-smallest disks, follow the steps for the appropriate case:
The following link points to a Flash game where you can attempt to solve the puzzle for various numbers of disks: Owen's World Traveling Salesman problem (TSP)
|
Combination |
Then return to |
ABC | A |
ACB | A |
BAC | B |
CBA | C |
BCA | B |
CAB | C |
A 'brute force' algorithm would work out all the possible combinations, adding up the distances for each. The smallest distance would then give the best route. The algorithm is of a type called brute force because it contains no thoughtful logic. It is relying on the processing power of the computer to work out all the combinations of cities to visit just once on a particular journey. Even with a powerful computer the task quickly becomes intractable as the number of cities grow. For instance, for 20 cities the number of possible routes is 20! or 2,432,902,008,176,640,000.
Attached to the bottom of this page is a BYOB 3.0.9 program that simulates the TSP. A recursive function is used to work out all the possible permutations for a given number of nodes. When the program is run, the user can see that as the number of nodes (lettered A, B, C, etc.) increases the time to compute all possible routes, starting and finishing at node s, increases sharply. For 6 nodes the time on a slowish computer is around 3 minutes, for 7 it is about 21.
However, more efficient algorithms have been devised. Click on this link to go to a site that features them, complete with demonstration Java applets:
TSP Algorithms in Action
Visit this website to see how, for instance, the TSP problem for the cities of Sweden has been solved (24,978 cities. Tour has length approximately 72,500 kilometers). The Traveling Salesman Problem
Finally, there are many TSP type problems in the real world. For example, how do you optimally load a container ship? How does a courier work out the best route to deliver their parcels? Programs have been devised to solve these problems.
This link points to some TSP games that can help get a better idea of the problem.
Big O notation
Note: a knowledge of Big O notation does not necessarily need to be known by students (or teachers!) for DT 2.44, but it is useful.
Additional Resources
- The following link points to a Word document that gives an excellent rundown on Complexity and Big O: Complexity by Joe Carthy
- The following link points to a web page/website set up by Lero in Ireland. It contains excellent resources to do with complexity. You will need to obtain a password to access Module 7, which is the one dealing with complexity. Be sure to check out the rest of the website which contains an excellent complete Computer Science course. It is recommended for students 15-16, but in my opinion some parts could be used with a younger age group.
Link: http://www.scratch.ie/module7
Screenshot of the relevant web page.