Great Big House in New Orleans and the Josephus problem
Six years ago, when I was a freshman in college, I wrote a series of three posts on this blog about a game that I used to play in elementary school music class called "Great Big House in New Orleans". In those posts, I attempted to write a program in Java and Python that would calculate the winning position of the game given the number of players.
- "Great Big House in New Orleans" (published Monday, November 06, 2017)
- "Great Big House in New Orleans: Improved Code" (published Thursday, November 09, 2017)
- "Great Big House in New Orleans: The Special Case" (published Friday, February 02, 2018)
At the time, I had less than a year of experience in computer programming and had only recently decided that I would study computer science. Because of my naivete, the posts have many problems, and they are a little embarrassing to look back on. The posts ramble excessively about basic programming principles and assume without proof that a pattern will hold for all numbers of players. My most glaring oversight: I did not know it at the time, but the whole "Great Big House in New Orleans" game is in fact identical to a famous problem in mathematics and computer science called the Josephus problem. In this post, I would like to relay everything I have learned since then.
Problem
The original "Great Big House in New Orleans" game
The rules of the game are simple. All players gather in a circle, and one of the them starts holding a ball. The players then sing the following song while passing the ball around the circle on every beat:
Great big house in New Orleans,
Forty stories high,
Every room that I've been in,
Filled with pumpkin pie.
The player that ends with the ball on the last beat of the song must exit the circle, and the game repeats until there is only one player remaining with the ball. Effectively, it means that every sixteenth player must exit the circle.
In my original "Great Big House in New Orleans" post, I incorrectly stated that two players exit the circle during one repetition of the song—the players on the second beat of the words high and pie. After speaking with my old friend who actually used to run through the entire game in advance and work out exactly where he would need to sit in the circle to win the game, I realized I had misremembered. The actual rules of the game, as we played it in elementary school, were that only the player who has the ball on the very last beat of the song (the second beat of the word pie) needs to exit the circle. The player who has the ball on the word high does not need to leave.
The Josephus problem
The Josephus problem is a generalized version of "Great Big House in New Orleans". According to a first century man named Flavius Josephus, the namesake of the problem, he and his fellow soldiers during a war were facing imminent defeat, and instead of letting the enemy capture them, they decided to commit suicide. To determine the order in which they would kill themselves, the soldiers all lined up in a circle, and starting with one soldier, they would start counting numbers around in a circle. Every k-th soldier would have to kill themselves (e.g. every third soldier or every seventh soldier—Josephus did not make it clear which it was). By a stroke of luck, Josephus and another man were the last two standing, and they agreed to surrender and live rather than die.
The YouTube channel Numberphile made a video discussing the Josephus problem in October 2016, a little more than one year before I wrote my original 2017 blog post. Stumbling across this video in my junior year of college was actually how I found out about the Josephus problem. Imagine the look on my face as I realized this was "Great Big House in New Orleans". If every k-th soldier must die, then "Great Big House in New Orleans" is simply the Josephus problem where k = 16. (In my misremembered version where players also get out on the word high, it is the Josephus problem where k = 8.)
Solution
k = 2
The Numberphile video focused exclusively on the simplest case, where every soldier must kill the soldier standing directly next to them (i.e. k = 2; every second soldier must die).
The solution to this case is quite elegant. In the first pass around the circle, all of the players in the even positions are eliminated immediately. If the total number of players, n, is even, then that means the first pass eliminates players 2, 4, 6, ..., all the way up to and including player n. The game then restarts from player 1 with exactly half as many players. If n is not only even but also a power of two (e.g. 1, 2, 4, 8, 16, 32, 64, etc.), then the total number of players in each subsequent pass will also be a power of two. For example, here is a simulation of a game with 16 players (24), with the bolded positions being the ones eliminated in that round:
- Starting positions (16 players): 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
- Remaining after first round (8 players): 1, 3, 5, 7, 9, 11, 13, 15
- Remaining after second round (4 players): 1, 5, 9, 13
- Remaining after third round (2 players): 1, 9
- Remaining after fourth round (1 player): 1
Because the game will always cycle back to player 1 if n is a power of two, the player in position 1 will always win if n is a power of 2.
If n is not a power of 2, then to find the winning position, you should first find the largest power of two less than the total number of players (n), then calculate the difference between that n and that power of two. For example, if there are 21 players, then the largest power of two less than 21 is 16, and the difference between 21 and 16 is 5. You then need to eliminate 5 players, as follows:
- Starting positions (21 players): 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21
- Remaining after eliminating 5 players (16 players): 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 1, 3, 5, 7, 9
That next surviving player—position 11 in the original circle—can be thought of as position 1 in the game of the remaining 16 players, and we already know that if the total number of players is a power of two, then position 1 wins. Indeed, position 11 wins in a game with 21 players.
General solution
If k can be any number, things get much more complicated. At first glance, you might guess that the pattern where position 1 wins if n is a power of 2 could be extended: position 1 wins if n is a power of k. Unfortunately, such a pattern does not hold.
The Josephus problem where k ≠ 2 can be solved using a technique called dynamic programming. It revolves around the observation that, after the game eliminates its first person, it becomes a smaller version of the same game. If there are n players in the game, then the winner of the game is the same person who wins the game with n - 1 players starting from the player right after the first eliminated player. The base case of the problem is when n is 1, in which case, that player is the winner by default. As is commonly the case for obscure programming questions, I found a concise general solution on the website StackOverflow: see this question.
Honestly speaking, I have to wonder if there is an even better solution to the general case than dynamic programming. I feel like my "improved code" post was actually onto something, but without some kind of mathematical proof that the algorithm holds for all class sizes (n), it is all just conjecture. I do not really have the time these days to investigate this further. Perhaps someday in the future I will write yet another blog post on this topic.
