0% found this document useful (0 votes)
22 views

Using_puzzles_in_teaching_algorithms

Uploaded by

Jorge Ruiz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Using_puzzles_in_teaching_algorithms

Uploaded by

Jorge Ruiz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

See discussions, stats, and author profiles for this publication at: https://www.researchgate.

net/publication/221536674

Using puzzles in teaching algorithms

Conference Paper in ACM SIGCSE Bulletin · March 2002


DOI: 10.1145/563340.563456 · Source: DBLP

CITATIONS READS
29 739

2 authors:

Anany Levitin Mary-Angela Papalaskari


Villanova University Villanova University
22 PUBLICATIONS 778 CITATIONS 24 PUBLICATIONS 193 CITATIONS

SEE PROFILE SEE PROFILE

All content following this page was uploaded by Anany Levitin on 04 June 2014.

The user has requested enhancement of the downloaded file.


Using Puzzles in Teaching Algorithms

Anany Levitin Mary-Angela Papalaskari


Department of Computing Sciences
Villanova University
Villanova, PA 19085
[email protected]

Abstract
This paper advocates a wider use of puzzles and puzzle-like • Solving puzzles helps in developing creativity and
problems in teaching design and analysis o f algorithms. It problem solving skills - - the qualities any future CS
discusses a variety o f puzzles and classifies them according professional should strive to acquire.
to the general algorithm design techniques. Pedagogic • Puzzles usually attract more interest on the part o f
issues are explored. students, making them work harder on the problems
assigned to them.
1 Introduction • Puzzles can be used in a variety o f ways in an
Traditionally, most important concepts related to design algorithms course:
and analysis o f algorithms have been illustrated b y a) to introduce a new approach to algorithm design;
problems o f mathematical nature. This has been true even
b) as exercises to hone students' skills in a particular
for textbooks organized around algorithm design
approach to algorithmic thinking;
techniques [3, 7, 12]. We believe that it would be very
desirable to illustrate the principal ideas o f algorithmics on c) as a theme for a programming project.
other material as well. In particular, we believe that puzzles As mentioned earlier, there are a few puzzles that are a
and puzzle-like problems remain an untapped source o f standard feature o f algorithm textbooks but the number o f
such enrichment. them is very small. O f these, the most widely used puzzle
The idea o f using puzzles to illustrate or reinforce key is arguably the Tower-of-Hanoi problem. It provides a
concepts is not new for computer science education. There natural and convenient vehicle for illustrating the idea o f a
has been no attempt, however, to collect and systematically recursive algorithm, for showing how a recursive algorithm
catalogue puzzles illustrating each algorithm design can be analyzed by setting up and solving a recurrence
technique. relation, and even for proving an algorithm's optimality.
Other standard examples include the K6nigsberg bridge
What are advantages o f using puzzles and puzzle-like
puzzle (to introduce Euler circuits), mazes (in conjunction
problems for teaching design and analysis o f algorithms? In
with depth-first search), and the n-queens problem (for
our view, there are several:
illustration o f backtracking).
• Puzzles force students to think about algorithms on a
Why is this list so short? In addition to the unavoidable
more abstract level, divorced from programming and
inertia o f textbook writing, there is, o f course, a deeper
computer language minutiae.
reason for the paucity o f puzzles in algorithm textbooks.
• Puzzles show that algorithm design strategies can be Many puzzles are based on exploiting idiosyncrasies o f
looked upon as general problem-solving tools that problems, whereas algorithms typically seek to solve
might be useful in areas far removed from computer problems with a very large i f not infinite variety o f inputs.
science. Despite this principal obstacle, we believe that more
puzzles useful for teaching algorithms can be found. In
support o f this view, we quote below several puzzles that
Permission to make digital or hard copies of all or part of this work for illustrate general algorithm design techniques. Many o f
personal or classroom use is granted withoutfee providedthat copies are these puzzles are included in the forthcoming textbook b y
not rnadc or distributed for profit or commercial advantage and that one o f this paper's authors [11].
copies bear this notice and the filll citation on the first page. To copy
otherwise, or republish, to post on servers or to redistribute to lists,
requires prior specificpermission and/ors fee.
SIGCSE'02, February27- March 3, 2002, Covington,Kentucky,USA.
Copyright2002 ACM 1-58113-473-8/02/0002...$5.00.

292
2 Puzzles for illustrating design techniques are "L"-shaped tiles formed by three adjacent squares o f a
We will use the hierarchical taxonomy of algorithm design chessboard. (A missing square can be any o f the board
squares. Triominos should cover all the squares except the
techniques proposed by Levitin [10]. It divides the
strategies into two groups. The group of most general missing one, with no overlaps.)
paradigms includes four strategies: brute force, divide-and- The other problem is the nuts-and-bolts problem [16]:
conquer, decrease-and-conquer, and transform-and- Given a collection of n bolts o f different widths and n
conquer. The group of less general ones consists o f the corresponding nuts, match each bolt to its nut. One is
greedy approach, dynamic programming, backtracking, and allowed to try a nut and bolt together, from which one can
branch-and-bound. We will start the discussion with the determine whether the nut is larger than the bolt, smaller
most general strategies. For each of these four strategies than the bolt, or matches the bolt exactly. There is no way,
and their varieties, we will provide examples o f puzzles however, to compare two nuts together or two bolts
whose algorithmic solutions illustrate the strategy in together.
question. Another divide-and-conquer algorithm for solving a puzzle
is an algorithm for the knight's tour problem suggested by
Brute Force This strategy is defined as a straightforward Parberry [ 15].
approach to solving a problem, usually directly based on
the problem's statement and definitions o f the concepts Decrease-and-conquer Solving a problem by reducing a
involved. Its important special case is exhaustive search: a given instance to a smaller one, solving the latter
brute-force approach to solving problems that involve - - recursively, and then extending the obtained solution to get
explicitly or implicitly - - combinatorial objects such as a solution to the original instance is, o f course, a well-
permutations, combinations, and subsets o f a given set. It known algorithm design approach. Though this approach is
suggests generating all the elements of the problem's considered by some to be a special case of divide-and-
domain and then finding a desired element (e.g., the one conquer, it is better to consider them as distinct design
that optimizes a given objective function). strategies. The crucial difference between the two lies in
For obvious reasons, interesting puzzles don't usually lend the number of smaller subproblems that need to be solved:
themselves to solutions by brute force in general and by several (usually, two) in divide-and-conquer algorithms and
exhaustive search in particular. In fact, many puzzles can just one in decrease-and-conquer algorithms. It is further
provide good examples o f problems that either cannot be useful, both from the design and the analysis perspectives,
solved by brute force at all, or for which this strategy yields to distinguish three varieties o f this strategy.
a very clumsy and unsatisfactory solution. To make this
point, one can assign, for example, the famous puzzle of
crossing nine dots in the plane by four straight lines The decrease-by-a-constant variety suggests decreasing a
without lifting a pen o f f the paper (e.g., [8]) - the problem's size by a constant, typically one. Here is an old
quintessential "'thinking outside the box" puzzle. and very simple puzzle illustrating this strategy. A
detachment of n soldiers must cross a wide and deep river
As for puzzles that can be solved by brute force, one can with no bridge in sight. They notice two boys playing in a
suggest, for example, getting the 3-by-3 magic square by rowboat by the shore. The boat is so tiny, however, that it
exhaustive search. It provides a good illustration o f the can only hold two boys or one soldier. How can the
limitations o f exhaustive search and the usefulness o f soldiers get across the river and leave the boys in joint
knowing an algorithm's efficiency class. Thus, students possession of the boat? How many times does the boat
can be asked to run an experiment to determine the largest need to pass from shore to shore? This puzzle is very good
magic square that the exhaustive search algorithm can find for our purposes. First, it is at the right level of difficulty
in one minute o f computer time and then estimate the order because the answer is not immediately obvious, but, after
of the largest magic square that can be found in one year or some thought, students can fairly reliably arrive at the
in one hundred years. answer. Second, it is not a very widely known puzzle,
giving students the opportunity to actually be engaged by it
Divide-and-conquer Divide-and-conquer is based on and to successfully solve it (the "aha" experience). Third, it
partitioning a problem into several smaller subproblems can also be used as springboard for a discussion o f the
(usually o f the same kind and ideally o f about the same relationship between the recursive nature o f some strategies
size), solving each of them recursively, and then combining and their ultimate implementations in specific situations.
their solutions to get a solution to the original problem. Another puzzle illustrating the decrease-by-a-constant
Surprisingly, there seem to be few puzzles solvable by the strategy is a version of the nuts-and-bolts problem in which
divide-and-conquer approach. Here are two examples that the goal is just to find the smallest bolt and the matching
are rather well known. The first one is the triomino puzzle nut [13, #612].
(e.g., [13, #49]). The object is to cover any 2"-by-2"
chessboard with one missing square with triominos, which

293
Decrease-by-a-constant-factor, the second variety o f the compare any bolt with any nut but also can also compare
decrease-and-conquer strategy, suggests decreasing a nut sizes with each other, and bolt sizes with each other.
problem's size b y a constant factor, most often two. Since The goal is to match each bolt to its nut.
the principal example o f the decrease-by-half technique is
binary search, any puzzle or game (like 20 questions) based
on this algorithm's idea can be used to illustrate the The second variety o f the transform-and-conquer strategy
strategy. - - called representation change ~ is based on a
transformation o f a problem's input to a different
A more interesting and useful puzzle is determining a fake representation, which is more conducive to an efficient
coin with a balance scale. An easier version o f the problem algorithmic solution. Examples from traditional computer
assumes that it is known that the fake coin is, say, lighter science include search trees, hashing, H o m e r ' s rule, FFT,
than the genuine one. The attractiveness o f this puzzle lies and heaps. As to unorthodox problems, one can point out
in the fact that it can be solved either by dividing coins into an impressive algorithm for finding all sets o f anagrams
two groups o f coins or into three groups o f coins. The first (e.g., "eat," "'ate," and "'tea" belong to the same set) in a
approach is most obvious, and its analysis is almost dictionary o f English words [2]. The algorithm first
identical to that o f binary search. The division-into-three is assigns each word a signature obtained by sorting its letters
less obvious, provides a rather rare example o f dividing a (it is a representation change) and then sorts the dictionary
problem into three subproblems, and is, in fact, more in alphabetical order o f the signatures (it is a presorting
efficient (asymptotically, by a constant factor) than the idea) to put anagrams next to each other.
divide-into-two algorithm. A comparative study o f these
two algorithms can be particularly instructive for students. As very exotic (but still making the point) examples o f
representation change, we can mention two items from
A m u c h more challenging version o f this problem does not computer science folklore (see, e.g., [4]). The first one is
assume any additional information about relative weights the so-called "spaghetti sort." Numbers to be sorted are
o f the fake and genuine coins. It is famous among puzzle represented b y lengths o f uncooked spaghetti rods that are
lovers but would be difficult for most students. Its solution placed vertically on the table to immediately identify the
can be found, for example, in [17]. Brassard and Bratley longest one in the bunch. Note that this provides even a
[3] use this puzzle to demonstrate the interplay between better illustration o f an efficient implementation o f the
algorithm design and decision trees. priority queue. The second item is solving the shortest path
As one more nontraditional decrease-by-half example - - problem with a string model o f the graph in question.
though it can hardly be called a puzzle - - we can mention
the Josephus Problem. A good analysis o f it can be found
in [6]. The third variety o f the transformation strategy is problem
reduction, in which an instance o f a given problem is
transformed into an instance o f a different problem
Variable-size decrease algorithms is the last variety o f the altogether. To illustrate, several puzzles can be reduced to
decrease-and-conquer strategy. In such algorithms, the size questions about graphs. The K6nigsberg bridge puzzle is
reduction varies irregularly from iteration to iteration. one such example. There are also several well-known
Euclid's algorithm, the partition-based algofithm for the ferrying puzzles such as the problem o f h o w a peasant can
selection problem, and binary tree search are mathematical ferry himself, a wolf, a goat, and a cabbage head over a
examples o f variable-size decrease algorithms. Some river (e.g., [8]). The problem can be represented b y a graph
versions o f the game o f N i m can be used to illustrate this whose vertices specify possible states o f the problem and
strategy. Here is a typical example. There is a pile o f n edges correspond to legitimate fiver crossings. This
matches off a table. Two players take turns removing 1,2, reduces the problem to the question about a path (say, with
3, or 4 matches. The winner is the player who removes the a m i n i m u m number o f edges) from the vertex representing
last match. Design a winning strategy, i f it exists, for the the initial situation to the vertex representing the final one.
player making the first move.
Less general strategies One should not expect a rich
Transform-and-conquer The last most general technique bounty from a hunt for puzzles solvable b y the greedy
is based on the idea o f transformation. Its first variety - - approach. The reason is the same as for the brute-force
called instance simplification - - solves a problem by first strategy: good puzzles are usually too "tricky" to be
transforming an instance given to another instance o f the solvable in a straightforward fashion. We found one
same problem (and o f the same size) with some special notable exception in Huffrnan trees, which are usually
property which makes the problem easier to solve. A good introduced for constructing optimal prefix-free codes and
example o f this technique is presorting (e.g., for finding whose construction is universally considered to be a greedy
equal elements in a list). algorithm. Less known are their applications to decision
The same idea can also be illustrated b y an easier version trees mentioned b y K_nuth [9, p. 402]. Gardner [5, p. 30]
o f the nuts-and-bolts problem in which one can not only provides a useful puzzle - - a version o f the twenty

294
questions with unequal probabilities o f item s e l e c t i o n - makes it harder for them to think on the more abstract level
that takes advantage o f this idea. Also, Parberry [14] has necessary for a successful mastery of algorithm design and
suggested an algorithm for the sliding tiles puzzle, which is analysis techniques. Puzzles provide an indispensable tool
based on a combination o f the greedy and divide-and- for loosing this unfortunate grip of computing minutiae.
conquer strategies. As to specific ways to introduce puzzles, we have used
most o f them through a systematic inclusion in homeworks,
Algorithm textbooks contain a rather standard set o f as examples' o f applying specific algorithm design
examples illustrating dynamic programming, which do not strategies. A few o f them can be used in a lecture to
include puzzles. The book by Richard Bellman, the introduce a particular design technique. In addition to the
method's inventor, and Stuart Dreyfus [1], does contain two canonical introduction o f backtracking via the n-queens
such applications. The first one deals with the so-called problem, we can mention again the fake-coin identification
"missionaries and cannibals" puzzle (pp. 97-99); the problem. One may argue that this puzzle provides the best
second one deals with a very difficult problem o f detecting way to introduce the decrease-by-a constant factor strategy
two fake coins with a balance scale (pp. 16g-178). These because of the natural opportunity to discuss the size
examples can be useful for discussing limitations o f reduction by both a factor of two and a factor o f three.
dynamic programming. Some puzzles - - such as the magic square construction
already mentioned above - - can be used as projects
involving an algorithm timing and empirical investigation
We have already mentioned above the n-queens problem of relative efficiency of several algorithms for the same
algorithm based on the backtracking technique. This and problem. Assigning puzzles as projects has an additional
several other similar puzzles are standard topics in advantage o f leading often to less straightforward data
Artificial Intelligence (AI). From the AI perspective, structures and objects. Games, o f course, can provide even
backtracking is just one o f several approaches to state- better vehicles for the latter purpose.
space search which can also be adapted to solve some
optimization problems. The "missionaries and cannibals" Though our principal goal was to find puzzles for
problem and the sliding tiles puzzle are in fact very illustrating algorithm design strategies, they can also be
commonly used as illustrations in this connection (see, for used for teaching algorithm analysis techniques. For
example, [18]). State-space search can also be adapted to example, the same fake-coin identification problem can
the analysis o f two-person games; the minimax algorithm serve this purpose as well as the often-used Tower-of-
Hanoi puzzle.
with tx-I~ pruning is the premier approach to adversary
search. This, in turn, can be used as an illustration o f the
branch-and-bound strategy.
In this discussion we have intentionally expanded our 4 Conclusion
puzzle domain to include some games. From our
perspective, the use of games offers all the pedagogical Puzzles can be very helpful for teaching different aspects o f
advantages mentioned in the introduction. algorithmics. In this paper, we quoted several puzzles and
puzzle-like problems that can be used for illustrating most
general algorithm design techniques: brute force, divide-
and-conquer, decrease-and-conquer, and transform-and-
conquer and less general techniques: greedy, dynamic
3 Pedagogical Comments
programming, backtracking, and branch-and-bound. We
What courses would benefit from a more extensive usage hope that more puzzles suitable for teaching design and
o f puzzles? We believe that any course dealing with analysis o f algorithms will be found in existing collections
algorithms would. This includes introductory or specifically designed for this worthy purpose. Some
programming courses and courses on computer problem promising sources of such puzzles are references [19-22],
solving for non-majors. But it is the course on design and which primarily deal with problems related to computer
analysis of algorithms that, in our view, should benefit science. There are also a few Web sites that provide links
from such an introduction most. We are in agreement with to puzzle-related repositories (see, for example, links at
a widely shared opinion that such a course should be http ://h°me'sundse'ch/pglaus/puzzlinks'htm)"
organized around algorithm design techniques rather than
specific problem types. Such organization of the course
makes puzzles indispensable for showcasing fundamental
algorithm design strategies as general problem solving References
paradigms. In our experience, many students come to a
course on design and analysis o f algorithms prone to [I] Bellman, R.E. and Dreyfus, S.E. Applied Dynamic
thinking in terms o f a specific programming language Programming. Princeton University Press, 1962.
taught in their introductory programming sequence. This

295
[2] Bentley, J. Programming Pearls. 2nd ed., Addison-
Wesley, 2000.
[3] Brassard, G. and Bratley, P. Fundamentals of Algo-
rithmics. Prentice-Hall, 1996.
[4] Dewdney, A.K. The (New) Turing Omnibus: 66
Excursions in Computer Science. Computer Science
Press, 1992.
[5] Gardner, M. My Best Mathematical and Logical
Puzzles. Dover, i 994.
[6] Graham, R.L., Knuth, D.E., and Potashnik, O. Concrete
Mathematics. Addison-Wesley, 1988.
[7] Horowitz, E., Sahni, S., and Rajasekaran, S. Computer
Algorithms. Computer Science Press, New York, 1996.
[8] Kordemsky, B. The Moscow Puzzles: 359 Mathematical
Recreations. Dover, 1992.
[9] Knuth, D.E. The Art o f Computer Programming,
Volume I: Fundamental Algorithms, 3rd ed., Addison-
Wesley, 1997.
[10] Levitin, A. Do we teach the right algorithm design
techniques? in Proceedings o f SIGCSE '99 (March
1999), 179-183.
[l l] Levitin, A. Introduction to Design and Analysis o f
Algorithms. Addison-Wesley, 2002 (to appear).
[12] Neapolitan, R.E. and Naimipour, K. Foundations of
Algorithms. Jones and Bartlett Publishers, 1996.
[13] Parberry, I. Problems on Algorithms. Prentice-Hall,
1995.
[14] Parberry, I. A real-time algorithm for the (n 2 - 1)-
puzzle. Information Processing Letters 56 (1995),
23-28.
[15] Parberry, I. An efficient algorithm for the knight's
tour problem. Discrete Applied Mathematics 73
(1997), 251-260.
[16] Rawlins, G.J.E. Compared to What? An Introduction
to the Analysis o f Algorithms. Computer Science
Press, 1991.
[17] Reingold, E.M., Nievergelt, J., and Deo, N.
Combinatorial Algorithms: Theory and Practice.
Prentice-Hall, 1977.
[18] Russell, S. and Norvig, P. Artificial lntelligeace: A
Modern Approach. 2nd ed., Prentice-Hall, 2001,
[19] Shasha, D.E. Codes, Puzzles, and Conspiracy. W.H.
Freeman and Co., 1992.
[20] Shasha, D.E. The Puzzling Adventures of Doctor Ecco.
Dover, 1998.
[21] Shasha, D.E. "Dr. Ecco's Omniheurist Corner," the
column in the Dr. Dobb's Journal.
[22] Shasha, D.E. "Puzzling Adventures," the column in
the Scientific American.

296

View publication stats

You might also like