Algorithmic Puzzles Anany Levitin Villanova University
Transcription
Algorithmic Puzzles Anany Levitin Villanova University
Algorithmic Puzzles Anany Levitin Villanova University anany.levitin@villanova.edu Plan of the Talk What is an algorithmic puzzle? Two taxonomies of algorithmic puzzles Algorithmic puzzles in research Algorithmic problem solving and puzzles in education Puzzles in job interviews (?) Q&A What is an algorithmic puzzle? An algorithm is a sequence of unambiguous instructions for solving a problem. A puzzle is a problem that challenges ingenuity. An algorithmic puzzle is a puzzle that involves the design or analysis of an algorithm. Birth of the term “algorithmic puzzle” oldest algorithmic puzzles are more than 1,200 years old (Alcuin of York’s river crossing puzzles c. 800 CE) just recently recognized as a special category of puzzles algopuzzles A. K. Dewdney, Scientific American, June 1987 cyberpuzzles D. Shasha, Doctor Ecco's Cyberpuzzles, 2002 algorithmic puzzles P. Winkler, Mathematical Puzzles: a Connoisseur’s Collection, 2004 first large collection of algorithmic puzzles A. Levitin and M. Levitin Algorithmic Puzzles, Oxford, 2011 30-page tutorial with 22 puzzles as examples and 150 other algorithmic puzzles divided into three difficulty levels Two Taxonomies of Algorithmic Puzzles Algorithmic puzzles can be classified by puzzle types algorithmic questions posed Some Types of Algorithmic Puzzles river crossing and similar problems weighing, measuring, and cutting puzzles moving counter problems state changing puzzles (coin turning, pancake frying) chessboard problems (8 queens, knight’s tours) Some Types of Algorithmic Puzzles (cont.) route and tracing puzzles tiling problems magic squares and related problems sliding piece puzzles 1-person games (Chinese Rings, Tower of Hanoi) 5 Types of Algorithmic Questions find, for a given input, the output of a given algorithm find an input yielding a required output by a given algorithm find the number of steps needed to solve a puzzle by a given algorithm design an algorithm for solving a given puzzle (often, in a minimum number of moves) show that a puzzle has no solution with operations allowed by the puzzle Find an algorithm’s output Fibonacci’s rabbit problem (1202) A man put a pair of rabbits in a place surrounded on all sides by a wall. How many pairs of rabbits can be produced from that pair in a year if it is supposed that every month each pair begets a new pair which from the second month on becomes productive? Find an algorithm’s input Josephus Problem There are n people numbered 1 to n standing in a circle. Starting the count with person number 1, every second person is eliminated until only one person is left. Where in the circle should a person stand to remain the last person standing? Infected Chessboard A virus spreads through squares of an nn chessboard infecting squares that have two infected neighbors (horizontally, vertically, but not diagonally). What is a smallest subset of unit squares that need to be infected initially for the virus to spread to the entire board? Find an algorithm’s number of steps Single-elimination tournament How many matches, in total, does it take to determine a winner among n players? How many rounds need to be played? A more interesting question is about the number of byes if n ≠ 2k Design an algorithm for a puzzle given Most algorithmic puzzles can be theoretically solved by exhaustive search (generate and check all potential solutions) or its more intelligent version called backtracking but a more efficient solution is usually expected. Algorithm design puzzles can be classified by the solution’s design strategy (divide-and-conquer, greedy, dynamic programming, etc.) General Algorithm Design Strategies brute force (w/exhaustive search as special case) backtracking and branch-and-bound decrease-and-conquer divide-and-conquer transform-and-conquer greedy approach iterative improvement dynamic programming Divide-and-Conquer Strategy 1. Divide a given problem into two or more smaller subproblems 2. Solve the smaller subproblems 3. Combine the solutions to the subproblems to get a solution to the problem given Divide-and-Conquer Example Tromino Puzzle Cover any 2n 2n chessboard with one missing square with right trominoes (L-shaped tiles of 3 adjacent squares) Invented by Solomon Golomb, then a math graduate student at Harvard, in the early 1950s Solution to the Tromino Puzzle Place one tromino at the center of the board to cover three central unit squares that are not in the 2n-1 2n-1 subboard with the missing square. This reduces the problem to tiling the four 2n-1 2n-1 subboards each with one missing square, which can be done by the same method (i.e. recursively). Real-Life “Application” Puzzles with no solution 7 Bridges of Königsberg (Leonhard Euler, 1736) Find a walk through the city that would cross each bridge once and only once. Puzzles with no solution The Fifteen Puzzle (a precursor was invented by Noyes Chapman, a postmaster in Canastota, NY; the instance below was popularized by Sam Loyd, 1870s) Slide the numbered tiles from the configuration on the left to the configuration on the right. Puzzles with no solution Tiling a mutulated chessboard with dominoes (Martin Gardner’s 1957 column in “Scientific American”) Tile (i.e., cover exactly with no overlaps) an 88 board without two diagonally opposite corners with dominoes (21 rectangles). Puzzles in Research Two historically important algorithmic puzzles: Problem Fibonacci numbers 7 Bridges of Königsberg topology, graph theory Fibonacci’s Rabbits Traveling Salesman Problem/Puzzle Find a shortest tour through n cities that visits every city once before returning to the tour’s starting city Can TSP be solved in polynomial time, i.e. significantly faster than exhaustive search? (P NP conjecture, with a $1 million prize from the Clay Mathematics Institute for an answer) Puzzles in Research (cont.) Optimal solutions to some puzzles 20-move solution for any Rubik’s Cube configuration (2010) Most research is in puzzle complexity (e.g., E. Demaine of MIT) Puzzles in Education – Why? Help developing problem-solving skills and creativity Attract more interest on the part of students, making them work harder on the problems assigned to them May seem less intimidating to less prepared students Force students to think about algorithms on a more abstract level, divorced from programming and computer language minutiae Are excellent subjects for project assignments and independent research Puzzles in Education (cont.) very old idea Propositiones ad acuendos juvenes (Problems to sharpen the young) attributed to Alcuin of York, c.800 CE, contained three river-crossing puzzles the wolf-goat-cabbage puzzle “three jealous husbands” the two adults and the two children, where the children weigh half as much as the adults Puzzles in Education – a modern quote “There is no better way to relieve the tedium than by injecting recreational topics into a course, topics strongly tinged with elements of play, humor, beauty, and surprise.” Martin Gardner (1914--2010) an American writer, best known for his "Mathematical Games" column in Scientific American and books on recreational mathematics (voted one of the ten most influential mathematicians of XX century, although he had no formal math training beyond a high school) Puzzle-Based Learning (PBL) Z. Michalewicz and M. Michalewicz Puzzle-Based Learning Hybrid Publishers, 2008 general problem solving oriented http://www.youtube.com/watch?v=GaKuB1lrqtw PBL Puzzles in Modern Higher Education A few schools run puzzle-based courses Carnegie Mellon Univ. of Vermont UNC-Charlotte Univ. of Colorado-Colorado Springs UCSB Virginia Tech a few foreign schools (Australia, Israel, Japan, Poland) Villanova – fall 2012 Two types of the courses 3-credit course 1-credit (in introductory engineering or problem solving courses) Algorithmic Puzzles and Main CS Ideas Notion of an algorithm (e.g., river crossing puzzles, detecting a lighter fake coin with a balance, or the following more sophisticated example) Catching a Spy In a computer game, a spy is located on a onedimensional line. At time 0, the spy is at location a. With each time interval, the spy moves b units to the right if b ≥ 0 and |b| units to the left if b < 0; a and b are fixed integers, but they are unknown to you. Your goal is to identify the spy's location by asking at each time interval (starting at time 0) whether the spy is currently at some location of your choosing. For example, you can ask whether the spy is currently at location 19, to which you will receive a truthful yes/no answer. If the answer is "yes," you reach your goal; if the answer is "no," you can ask the next time whether the spy is at the same or another location of your choice. Devise an algorithm that will find the spy after a finite number of questions. Catching a Spy (Algorithmic Puzzles, #136) How to catch the spy? -4 -3 -2 -1 0 1 2 3 4 Algorithmic Puzzles and Main CS Ideas Tower of Hanoi (Édouard Lucas, 1883) There are n disks of different sizes and three pegs. Initially, all the disks are on the first peg in order of size, the largest on the bottom and the smallest on top. The objective is to transfer all the disks to another peg by a sequence of moves. Only one disk can be moved at a time, and it is forbidden to place a larger disk on top of a smaller one. Algorithmic Puzzles and Main CS Ideas All 9 general algorithm design strategies can be nicely illustrated by algorithmic puzzles, showing applicability of the strategies to problems outside traditional computing domain See: A. Levitin, Introduction to the Design and Analysis of Algorithms, 3rd ed., 2011 A. Levitin and M. Levitin, Algorithmic Puzzles, 2011 A. Levitin and M.-A. Papalaskari, Proc. of SIGCSE’02 Algorithmic Puzzles and Main CS Ideas Insolvability of some problems and the idea of an invariant: a property that doesn’t change by any of the operations allowed in the problem Parity (e.g. 7 Bridges of Königsberg, the Fifteen Puzzle) Coloring (e.g. many tiling and chessboard tour problems) Algorithmic Puzzles and Main CS Ideas Algorithm efficiency False Coin Detection Find a lighter fake among n coins with a balance scale without any weights. Brute force: weigh coin pairs (1 vs. 1) Divide-by-half: divide into two halves (with 1 aside if n is odd) and compare their weights Divide into 3 about equal subsets is better than dividing into two Schweik’s Puzzle (Algorithmic Puzzles, #50) Note: The puzzle alludes to the hero of the novel The Good Soldier Schweik by the Czech writer Yaroslav Hašek (1883--1923). In this satirical novel, Schweik is depicted as a simple minded man who appears to be eager to execute orders but does it in a manner that, in fact, contradicts their intended goal. The good soldier Schweik had been ordered to line up a band of new recruits. The desired line was required to minimize the average difference in height of adjacent men. Schweik put the tallest recruit first, the shortest one last, and let the remaining men stand between them in random order. Did Schweik execute his order as stated? Instance of Schweik’s Puzzle Is the average difference in height of adjacent men minimized in this line? What mathematical fact, which is occasionally useful for algorithm analysis, does the puzzle demonstrate? Algorithmic Thinking For Other Majors Increasing importance of algorithmic thinking for all Computational thinking is a problem solving approach named for its extensive use of computer science techniques. The term was first used by Seymour Papert (of the Logo fame) in 1996. Basic skills: 3R’s (Reading, wRiting, aRithmetic) + computational thinking Jeannette Wing, Head of CS Dept. at CMU, CACM, March 2006 Algorithmic thinking thinking is the main component of computational Importance of Algorithmic Thinking (cont.) Increasing importance of algorithmic methods in sciences from biology to sociology “The Algorithm's coming-of-age as the new language of science promises to be the most disruptive scientific development since quantum mechanics.” Bernard Chazelle http://www.cs.princeton.edu/~chazelle/pubs/algorithm.html XVII-XX centuries XXI century math CS equations algorithms General Problem Solving Strategies by a famous physicist by a prominent mathematician by CS (algorithmics) General Problem Solving Strategies (cont.) Richard Feynman (1918-1988), Nobel Prize winner (1965) A fellow physicist joked that Feynman solved problems by what has become known as The Feynman Problem-Solving Algorithm: 1. write down the problem 2. think very hard 3. write down the answer General Problem Solving Strategies (cont.) George Pólya (1887-1985), prominent Hungarian mathematician, lived in the U.S. since 1940 Author of the most well-known book on problem solving How to Solve It, published first in 1945 and still in print! The four-step plan: 1. Understand the problem 2. Devise a plan 3. Carry out the plan 4. Look back The plan is further elaborated by 67 (!) heuristics General Problem Solving Strategies (cont.) Computer Science can offer several general problem solving strategies: brute force (w/exhaustive search as special case) backtracking and branch-and-bound decrease-and-conquer divide-and-conquer transform-and-conquer greedy approach iterative improvement dynamic programming This is what CS should be “selling” to the world! Algorithmic Puzzle-Based Course for Other Majors Algorithmic puzzles can help teaching algorithmic thinking without traditional CS background and computer programming CS ≠ programming Major objectives appreciation of the concept of algorithm including that of recursive algorithm familiarity with major algorithm design strategies some familiarity with importance of algorithm efficiency recognition of problems that have no algorithmic solution Algorithmic Puzzles in K–12 Education "Most (U.S.) students are not exposed to computer science in the same way they are to biology and physics. We have to incorporate computer science at the K-12 level. It's not easy to do, but this is what is needed." Jeannette Wing, quoted in The Pittsburgh Tribune-Review, 2/27/2012 Algorithmic puzzles are particularly attractive for the pre-college exposure to algorithmic problem solving Algorithmic Puzzles in K–12 Education Some chapters for a 2007 supplement to the Russian course on informatics (grades 5–6) Patterns Sorting 1-to-1 correspondence Problems about liars River crossing problems Decanting problems Weighing problems Combinatorial problems Numeral systems Game strategies Summary Algorithmic puzzles is an interesting puzzle genre still underappreciated exceptionally promising for growth in importance Problem solving is increasingly understood as algorithmic problem solving Algorithmic puzzles can illustrate main ideas of CS In particular, they show that algorithm design strategies are general problem solving tools with applications outside traditional CS domain In addition to all the utilitarian benefits of algorithmic puzzles, the best of them are remarkable and inspiring products of human ingenuity and wit. Puzzles in Technical Interviews Until recently, puzzles were a standard component of job interviews at leading software companies and Wall Street Influenced (but not originated) by Microsoft W. Poundstone, How Would You Move Mount Fuji? Microsoft’s Cult of the Puzzle: How the World’s Smartest Companies Select the Most Creative Thinkers, 2004 Are You Smart Enough to Work at Google? 2012 The practice remains controversial Arguments against interview puzzles Solving puzzles is irrelevant to specific skills required for the job in question Typical interview puzzles are too difficult for the short time allotted to an interview Puzzles can be vague or based on tricks, with some of them not even having a clear-cut correct answer There is no convincing documented evidence that persons hired after successful puzzle-solving prove to be better employees than candidates turned down after such interviews After many interview puzzles and their solutions have been published in books and on the Internet, their value has all but disappeared because there is no way to verify whether the interviewee knew the puzzle’s solution before the interview Arguments for interview puzzles Interview puzzles seek to identify good general problem solvers rather than specific skills such as coding Interviewers might not necessarily be interested in a puzzle’s solution, but rather in the way the applicant has arrived at it Interviews involving puzzles should be evaluated against traditional interviews, which have been found deficient in several documented studies It’s possible to alleviate the difficulty caused by the wide availability of a rather limited set of interview puzzles 3 Types of Interview Puzzles Not all interview puzzles are created equal! One should distinguish among 3 types of puzzles: “puzzling questions” logic puzzles algorithmic puzzles Examples of Puzzling Questions How long would it take to move Mount Fuji? Why are manhole covers round rather than square? If you could remove any of the fifty U.S. states, which would it be? Example of a Logic Puzzle There are three boxes. One of them has red balls only, one has blue balls only, and the third has a mixture of red and blue balls. The boxes are labeled with labels “red,” “blue,” and “mixed” but all the labels are wrong. Is it possible to pick one box and pick only one ball from it and then correctly label all the three boxes? Example of an Algorithmic Puzzle Locker door problem You are at one end of a hallway lined with n closed lockers. You make n passes along the lockers returning to the starting point after each pass. On the first pass, you open all the lockers. On the second pass, you close every second locker. In general, on the i-th pass, you toggle every i-th locker, i.e., open it if it was closed and close it if it was open. What lockers will be open after the last pass and how many such lockers will be there? Advantages of Algorithmic Puzzles for Interviews Unlike the other types of puzzles, nobody can claim that solving such puzzles is unrelated to the job skills needed for success in the software industry. They test the algorithmic problem-solving skills of an applicant in a way divorced from programming skills, which should be tested separately. Unlike logic puzzles, many algorithmic puzzles have reasonable alternative solutions. This allows for evaluation the interviewee’s performance on a more nuanced scale. Are puzzles still asked in interviews? Microsoft – no Google – sometimes? Others – yes Q&A