Graduate School of Information Sciences, Tohoku University, [email protected]://orcid.org/0000-0002-9912-6898Partially supported by JSPS KAKENHI Grant Numbers JP19K11814 and JP20H05793, Japan.Kyoto University, [email protected] Kyoto University, Japan and https://www.lab2.kuis.kyoto-u.ac.jp/minato/[email protected]://orcid.org/0000-0002-1397-1020 Nagoya University, Japan and https://www.math.mi.i.nagoya-u.ac.jp/~otachi/ [email protected]://orcid.org/0000-0002-0087-853X JSPS KAKENHI Grant Numbers JP18K11168, JP18K11169, JP20H05793, JP21K11752. Kyushu Institute of Technology, [email protected] Graduate School of Information Sciences, Tohoku University, Japan and http://www.ecei.tohoku.ac.jp/alg/suzuki/[email protected]://orcid.org/0000-0002-5212-0202Partially supported by JSPS KAKENHI Grant Numbers JP20K11666 and JP20H05794, Japan.Japan Advanced Institute of Science and Technology, Japan and http://www.jaist.ac.jp/~uehara[email protected]://orcid.org/0000-0003-0895-3765JSPS KAKENHI Grant Numbers JP20H05961, JP20H05964, JP20K11673. National Institute of Informatics, [email protected] Iwate University, Japan and http://www.kono.cis.iwate-u.ac.jp/~yamanaka/[email protected]://orcid.org/0000-0002-4333-8680Partially supported by JSPS KAKENHI Grant Numbers 19K11812, Japan.Graduate School of Information Sciences, Tohoku University, [email protected]://orcid.org/0000-0002-5175-465X \CopyrightTakehiro Ito, Jun Kawahara, Shin-ichi Minato, Yota Otachi, Toshiki Saitoh, Akira Suzuki, Ryuhei Uehara, Takeaki Uno, Katsuhisa Yamanaka, and Ryo Yoshinaka \ccsdesc[100]Mathematics of computing Discrete mathematics Combinatorics Combinatorial algorithms \fundingThis research is partially supported by JSPS Kakenhi Grant Number 18H04091. \EventEditorsJohn Q. Open and Joan R. Access \EventNoEds2 \EventLongTitle42nd Conference on Very Important Topics (CVIT 2016) \EventShortTitleCVIT 2016 \EventAcronymCVIT \EventYear2016 \EventDateDecember 24–27, 2016 \EventLocationLittle Whinging, United Kingdom \EventLogo \SeriesVolume42 \ArticleNo23
Sorting Balls and Water: Equivalence and Computational Complexity
Abstract
Various forms of sorting problems have been studied over the years. Recently, two kinds of sorting puzzle apps are popularized. In these puzzles, we are given a set of bins filled with colored units, balls or water, and some empty bins. These puzzles allow us to move colored units from a bin to another when the colors involved match in some way or the target bin is empty. The goal of these puzzles is to sort all the color units in order. We investigate computational complexities of these puzzles. We first show that these two puzzles are essentially the same from the viewpoint of solvability. That is, an instance is sortable by ball-moves if and only if it is sortable by water-moves. We also show that every yes-instance has a solution of polynomial length, which implies that these puzzles belong to NP. We then show that these puzzles are NP-complete. For some special cases, we give polynomial-time algorithms. We finally consider the number of empty bins sufficient for making all instances solvable and give non-trivial upper and lower bounds in terms of the number of filled bins and the capacity of bins.
keywords:
Ball sort puzzle, recreational mathematics, sorting pairs in bins, water sort puzzlecategory:
\relatedversion1 Introduction
The ball sort puzzle and the water sort puzzle are popularized recently via smartphone apps.111These sort puzzles are released by several different developers. As far as the authors checked, it seems that the first one is released by IEC Global Pty Ltd in January, 2020. Both puzzles involve bins filled with some colored units (balls or water), and the goal is to somehow sort them. The most significant feature of these puzzles is that each bin works as a stack. That is, the items in a bin have to follow the “last-in first-out” (LIFO) rule.

In the ball sort puzzle, we are given colored balls in bins of capacity and additional empty bins. For a given (unsorted) initial configuration, the goal of this puzzle is to arrange the balls in order; that is, to make each bin either empty or full with balls of the same color. (The ordering of bins does not matter in this puzzle.) The rule of this puzzle is simple: (0) Each bin works like a stack, that is, we can pick up the top ball in the bin. (1) We can move the top ball of a bin to the top of another bin if the second bin is empty or it is not full and its top ball before the move and the moved ball have the same color. An example with , , and is given in Figure 1.

The water sort puzzle is similar to the ball sort puzzle. Each ball is replaced by colored water of a unit volume in the water sort puzzle. In the water sort puzzle, the rules (0) and (1) are the same as the ball sort puzzle except one liquid property: Colored water units are merged when they have the same color and they are consecutive in a bin. When we pick up a source bin and move the top water unit(s) to a target bin, the quantity of the colored water on the top of the bin to be moved varies according to the following conditions (Figure 2). If the target bin has enough margin, all the water of the same color moves to the target bin. On the other hand, a part of the water of the same color moves up to the limit of the target bin if the target bin does not have enough margin.
In this paper, we investigate computational complexities of the ball and water sort puzzles. We are given bins of capacity . In an initial configuration, bins are full (i.e., filled with units) and bins are empty, where each unit has a color in the color set . Our task is to fill bins monochromatically, that is, we need to fill each of them with units of the same color. We define and as the problems of deciding whether a given initial configuration can be reconfigured to a sorted configuration by a sequence of ball moves and water moves, respectively. We assume that instances are encoded in a reasonable way, which takes bits. Without loss of generality, we assume that each color occurs exactly times for some positive integer in any instance since otherwise the instance is a trivial no-instance. (This implies that is at most .)
1.1 Our results
We first prove that the problems and are actually equivalent. By their definitions, a yes-instance of is a yes-instance of as well. Thus our technical contribution here is to prove the opposite direction. As a byproduct, we show that a yes-instance of the problems admits a reconfiguration sequence of length polynomial in as a yes-certificate. This implies that the problems belong to NP. We also show that and are solvable in time .
We next show that and are indeed NP-complete. By slightly modifying this proof, we also show that even for some kind of trivial yes-instances of , it is NP-complete to find a shortest reconfiguration to a sorted configuration.
We show that if the capacity is and the number of colors is , then and are polynomial-time solvable. In this case, we present an algorithm that finds shortest reconfiguration sequences for yes-instances.
We also consider the following question: how many empty bins do we need to ensure that all initial configurations can reach a sorted configuration? Observe that and are trivial if . It is not difficult to see that is also sufficient by using an idea based on bucket sort. By improving this idea, we show that is sufficient for all instances. We also show that some instances need empty bins.
1.2 Related studies
Various forms of sorting problems have been studied over the years. For example, sorting by reversals is well investigated in the contexts of sorting network [6, Sect. 5.2.2], pancake sort [2], and ladder lotteries [8]. There is another extension of sorting problem from the viewpoint of recreational mathematics defined as follows. For each , there are balls labeled . These balls are given in bins in which each bin is of capacity . In one step, we are allowed to swap any pair of balls. Then how many swaps do we need to sort the balls? This sorting problem was first posed by Peter Winker for the case in 2004 [7], and an almost optimal algorithm for that case was given by Ito et al. in 2012 [4].
The ball/water sorting puzzles are interesting also from the viewpoints of not only just puzzles but also combinatorial reconfiguration. Recently, the reconfiguration problems attracted the attention in theoretical computer science (see, e.g., [5]). These problems arise when we need to find a step-by-step transformation between two feasible solutions of a problem such that all intermediate results are also feasible and each step abides by a fixed reconfiguration rule, that is, an adjacency relation defined on feasible solutions of the original problem. In this sense, the ball/water sort puzzles can be seen as typical implementations of the framework of reconfiguration problems, while their reconfiguration rules are non-standard. In most reconfiguration problems, representative reconfiguration rules are reversible; that is, if a feasible solution can be reconfigured to another feasible solution , then can be reconfigured to as well (see e.g., the puzzles in [3]). In the ball sort puzzle, we can reverse the last move if we have two or more balls of the same color at the top of the source bin, or there is only one ball in the source bin. Otherwise, we cannot reverse the last move. That is, some moves are reversible while some moves are one-way in these puzzles. (In Figure 1, only the last move is reversible.) In the water sort puzzle, basically, we cannot separate units of water of the same color once we merge them, however, there are some exceptional cases. An example is given in Figure 2; the move from configuration (2) to configuration (3) is reversible, but the other moves are not.
2 Equivalence of balls and water
This section presents fundamental properties of and , from which we will conclude that
-
•
a configuration is a yes-instance of if and only if it is a yes-instance of , and
-
•
and both belong to .
2.1 Notation used in this section
Let and be finite sets of bins and colors, respectively. The set of sequences of colors of length at most is denoted by . A sequence of of length is called monochrome and denoted by . The empty sequence is denoted as , which is, in our terminology, also called monochrome. A configuration is a map . An instance of and is a configuration such that for all . A bin is sorted under if for some . A configuration is sorted or goal if all bins are sorted under . Sometimes we identify with when is clear from the context.
The th element of a sequence is denoted by for . The top color of a color sequence is if is not empty. In case is empty, is defined to be . A border in is a non-negative integer such that either and or , and the set of borders of is denoted by . We count non-trivial borders under as . For example, in Figure 2, the values of are in (0), (1), (2), (3), respectively.
We now turn to define a move of the sort puzzles with the terminologies introduced above. Intuitively, we pick up two bins and from , and pour the top item(s) from to if is empty or the top item of has the same color. The quantity of the items are different in the cases of balls and water. In the ball case, a unit is moved if possible. On the other hand, two or more units of water of the same color move at once until all units are moved or becomes full. We define the move more precisely below.
A ball-move can modify a configuration into , denoted as , if there are and such that
-
•
, , , and
-
•
for all .
A water-move can modify into , denoted as , if there are , and such that
-
•
, , and ,
-
•
either or , and
-
•
for all .
The reflexive and transitive closures of and are denoted as and , respectively. Clearly any single water-move can be simulated by some number of ball-moves. Thus we have .
2.2 Fundamental Theorems of and
Hereafter we fix an arbitrary initial configuration . Some notions introduced below are relative to , though it may not be explicit. The top-border table of a configuration is a map defined as .
The main object of this section is to observe that top-border tables play an essential role in analyzing the solvability of an instance. Note that if and only if is monochrome. Once we have reached a configuration with for all , we can achieve a goal configuration by trivial moves. Figure 3 illustrates some notions introduced in this section.
By the definition of moves, one can easily observe the following properties. {observation} If , the following holds for all bins :
-
1.
Moves monotonically remove borders from top to bottom:
-
2.
The contents below the top borders of do not change:
-
3.
If is not monochrome in , the colors of all the units above the border in are the color of the unit just above that border in :
We first give a necessary condition for .
Recall that throughout the game play, the total number of units of each color does not change, i.e., if , it holds for every color ,
Since the units under the top border of each bin have not been moved (Observation 2.2-2), the total numbers of units of each color above the borders of coincide in and . We count those units of color as
Those units may have been moved, but units of color can go only to empty bins or bins whose top color is in . Let us partition the bins into groups with respect to , where
-
•
: monochrome bins,
-
•
: non-monochrome bins with the top color .
Note that if , the top color of in is . Since each bin may have at most units of color above the border , there are
units of color can be on the top layer of non-monochrome bins. Thus, we must have at least
monochrome bins in which have some units of color . Therefore, it is necessary that
(1) |
We remark that the values of the functions introduced above, namely , , , are all determined by the top-border table . Those values coincide for and if . Let us say that is consistent with if and only if satisfies Eq. (1). Moreover, is tight if has exactly monochrome bins with at least one unit of every color .
Lemma 2.1.
Suppose is consistent with . There is a tight configuration such that and .
Proof 2.2.
Recall that it is necessary that has at least monochrome bins of each color . If has more than that, one can move all the units of any of the monochrome bins of color to other non-empty bins. The definition of guarantees that we can repeat this until we have just monochrome bins of color .
Of course being consistent with does not imply . Suppose with , where has one less border than by moving a unit above the top border of some bin to some other bins. Note that is not monochrome. During the move, the bin can keep units below its top border only. Therefore, the number of necessary monochrome bins of each color for this move is
and it is necessary that
Those conditions depend on top-border configuration of . For two top-border tables and and a bin , we write if
-
•
for all ,
-
•
and ,
-
•
.
We write if for some . Figure 4 may help intuitive understanding the above argument.
Theorem 2.3.
Suppose a configuration is consistent with . Then, there is a configuration such that if and only if , where is the reflexive and transitive closure of .
Proof 2.4.
Suppose and . Then and must satisfy the condition for .
Suppose . We assume without loss of generality that is tight by Lemma 2.1. The condition implies that units of color in concern can be distributed to bins other than . If , one can move the units in above to other bins whose top color is . If , one can move those units to an empty bin, which must be available in , since is tight.
Corollary 2.5.
An initial configuration is a yes-instance of if and only if it is a yes-instance of .
Proof 2.6.
Since any water-move can be simulated by some ball-moves, it is enough to show the converse. We claim that, for any configurations and such that , if , then there is such that and .
If , from the values and , one can compute and in constant time, by preprocessing , using the following equations:
Corollary 2.7.
and belong to .
Proof 2.8.
By Theorem 2.3, is a yes-instance if and only if there is a bin sequence with () that admits a top-border table sequence such that and for all . Each is uniquely determined by and and one can verify in constant time, by maintaining the values of and (or just ).
A solution for an instance is essentially an order of borders of the instance to remove.
Corollary 2.9.
and can be solved in time.
Proof 2.10.
There are at most distinct top-border tables.
Corollary 2.11.
For every yes-instance of , there exists a sequence of ball-moves to a goal configuration of length at most .
Proof 2.12.
In , ball-moves are enough to eliminate a border from a tight configuration, if there is a way to eliminate the border. To make the obtained configuration tight takes at most moves, since the obtained configuration may have at most one monochrome bin to empty when the previous configuration is tight. Since there can be at most borders to eliminate, every yes-instance has a solution consisting of at most ball-moves.
Corollary 2.13.
For every yes-instance of , there exists a sequence of water-moves to a goal configuration of length at most where .
Proof 2.14.
In , water-moves are enough to eliminate a border from a tight configuration, if there is a way to eliminate the border. To make the obtained configuration tight, it takes again moves. To see this, observe that the obtained configuration may have at most one monochrome bin to empty as in the ball case. If one water-move does not empty the source monochrome bin, it must make the target bin full. Since one cannot make more than full bins, to empty a monochrome bin takes at most water-moves. Therefore, every yes-instance has a solution consisting of at most water-moves.
3 NP-completeness
In this section, we show that and are NP-complete even with two colors. By Corollaries 2.5 and 2.7, it suffices to show that with two colors is NP-hard. By slightly modifying the proof, we also show that given a trivial yes-instance of , it is NP-complete to find a shortest sequence of water-moves to a sorted configuration, where an instance is trivial in the sense that it contains many (say ) empty bins.
Theorem 3.1.
and are NP-complete even with two colors.
Proof 3.2.
As mentioned above, it suffices to show that with two colors is NP-hard. We prove it by a reduction from the following problem 3-Partition, which is known to be NP-complete even if is bounded from above by some polynomial in [1].
3-Partition
-
Input:
Positive integers such that for some positive integer and for .
-
Question:
Is there a partition of into subsets such that for ?
In the reduction, we use two colors red and blue. A non-empty bin is red (blue) if it contains red (blue, resp.) units only. A non-empty bin is red-blue if its top units are red and the other units are blue. From an instance of 3-Partition, we define an instance of as follows (see Figure 5):
-
•
the capacity of each bin is ;
-
•
for each , it contains a full red-blue bin with red units and blue units;
-
•
it contains one empty bin.
We show that is a yes-instance of if and only if is a yes-instance of 3-Partition.

To show the if direction, assume that is a yes-instance of 3-Partition. We can construct a sequence of water-moves from to a sorted configuration as follows. Let be a partition of such that for . Using one empty bin, we can sort with as follows (see Figure 5). We first move all red units to the empty bin. Since the number of red units is , the bin becomes full. Now , , and are blue bins containing blue units in total. We move the units in to and . After that, and become full and is empty. Using this new empty bin, we can continue and sort all bins.
To show the only-if direction, we prove the following slightly modified statement. (What we need is the case of .)
Let and be non-negative integers, and be the instance of obtained from by adding full red bins and full blue bins. If is a yes-instance of the decision problem, then is a yes-instance of 3-Partition.
Assume that there is a sorting sequence , where is a sorted configuration. We use induction on . (We need the dummy bins for the induction step.) The case of is trivial since all instances of 3-Partition with are yes-instances. Assume that and that the statement holds for strictly smaller instances of 3-Partition.
Observe that contains bins and units of water ( red units and blue units). Since the capacity of bins is , every contains at most one empty bin, and if there is one, then the other bins are full. Observe also that each bin in each is either red, blue, red-blue, or empty.
We say that a move opens a bin if the move makes red units free for the first time. Observe that each red unit in has to move at most once since otherwise non-monochromatic bins will remain. Thus, all will eventually be opened. Let , , and be the first three bins opened in the sorting sequence in this order.
Assume that is opened by the move from to . Let .
Claim 1.
satisfies the following conditions.
-
1.
There are red bins, and they contain at least units.
-
2.
There are blue bins, and they contain units.
-
3.
There are red-blue bins, and they contain blue units and at most red units.
-
4.
There is no empty bin. (This follows for free from the other conditions.)
Proof 3.3.
Observe that we cannot create a new red-blue bin by any sequence of moves. Thus the number of red-blue bins is . Since each red-blue bin contains exactly blue units and at most red units, the red-blue bins contain exactly blue units and at most red units in total.
The blue bins contain exactly units. Since contains exactly units, the other blue bins contain units, and thus the number of blue bins is at least , where the inequality holds as .
Since the red bins contain at least units, the number of red bins is at least . Recall that the total number of bins is . Thus, the number of red bins is exactly and the number of blue bins is exactly .
Claim 2.
.
Proof 3.4.
In , the red bins contain at least units. This implies that . Suppose to the contrary that . We show that under this assumption, the conditions in 1 cannot be violated by any sequence of moves. This contradicts that the final state does not contain any red-blue bin.
Let be a configuration satisfying the conditions in 1 and be a configuration obtained from by one move. It suffices to show that still satisfies the conditions in 1. Assume that the move is from a bin to another bin .
First consider the case where we moved blue units. In this case, and have to be blue bins. Hence, to show that all properties are satisfied, it suffices to show that is not empty after the move. Indeed, if becomes empty after the move, then blue bins contain units in total. This contradicts the capacity of bins.
Next consider the case where we moved red units. The bins and are red or red-blue. If becomes empty (when it was red) or blue (when it was red-blue), then the total number of red bins and red-blue bins becomes but they still contain units ( red and blue). This contradicts the capacity of bins. Thus, we know that the type of does not change by the move. If the move is either from red to red, from red-blue to red-blue, or from red-blue to red, then the all conditions are satisfied. Assume that the move is from red to red-blue and that the red-blue bins contain more than red units after the move. Now the red-blue bins contain more than units. This again contradicts the capacity of bins.
By the claims above and the capacity of bins, satisfies the following conditions.
-
1.
There are full red bins.
-
2.
There are blue bins, and they contain units.
-
3.
There are full red-blue bins, and they contain blue units and red units.
-
4.
There is no empty bin.
Observe that each red-blue bin in is not opened so far, and thus contains blue units (and red units as it is full).
Let us take a look at the sorting sequence from to . Since there is no empty bin and all bins containing red units are full in , we need to move blue units in blue bins first. Unless we make an empty bin, the situation does not change. Let be the first configuration after that contains an empty bin. By the capacity of bins and the discussion so far, satisfies the following conditions.
-
1.
There are full red bins.
-
2.
There are full blue bins.
-
3.
For each , the bin is a full red-blue bin that contains red units and blue units.
-
4.
There is one empty bin.
Without loss of generality, assume that . Let be the instance of obtained from by the reduction above, and be the one obtained from by adding full red bins and full blue bins. Observe that can be obtained from by renaming the bins. Thus the sorting sequence from to can be applied to by appropriately renaming the bins. Therefore, we can apply the induction hypothesis to and thus is a yes-instance of 3-Partition. Since , the instance is also a yes-instance of 3-Partition.
Corollaries 2.11, 2.13 and 3.1 imply that given an integer and a configuration , it is NP-complete to decide whether there is a sequence of length at most from to a sorted configuration under both settings in and . We observe a slightly stronger result for .
Corollary 3.5.
Given an integer and an instance of , it is NP-complete to decide whether there is a sorting sequence for with length at most even if is guaranteed to be a yes-instance.
Proof 3.6.
From an instance of 3-Partition, we first construct an instance of as described in the proof of Theorem 3.1, and then add a sufficient number of empty bins to guarantee that the resultant instance is a yes-instance. This is always possible with a polynomial number of empty bins as we see in Section 5.222It is not difficult to show that this instance only needs a constant number of bins. In fact, two empty bins are enough to perform a greedy algorithm for sorting. Let denote the constructed instance. We set
The proof of Theorem 3.1 implies that if is a yes-instance, then admits a sorting sequence of length . (See Figure 5.)
Conversely, assume that admits a sorting sequence of length . Recall that each of the red-blue bins in contains red units at the top and blue units at the bottom for some . Since each full blue bin in the final configuration contains units from at least two original bins, we need at least one move for it. Thus we need at least moves to make full blue bins. This implies that we have at most moves that involve red units. Actually, the number of such moves is exactly since each red unit has to move at least once. Since for all , each of the full red bins in the final configuration contains units from at least three original bins, and thus it needs at least three moves. If some red bin contains units from more than three original bins, then it needs at least four moves. This contradicts the assumption that we have at most moves for red units. Thus we can conclude that each red bin in the final configuration contains units from exactly three original bins. This gives a solution to the instance of 3-Partition as the capacity of the bins is .
4 Polynomial-time algorithms when and
In this section, we focus on the special case of and . In popular apps, it is often the case that is a small constant and . The case is the first nontrivial case in this setting. Under this setting, every ball-move is a water-move and vice versa, except for moving (a) unit(s) from a bin with two units of the same color to an empty bin, which is a vacuous move. Therefore, in this section we do not distinguish water-moves and ball-moves and simply call them moves. We prove that in this setting, all instances with are yes-instances. We also show that we can find a shortest sorting sequence in time (if any exists).
We say that a bin of capacity is a full bin if it contains two units, and a half bin if it contains one unit.
Theorem 4.1.
If , , and , then all instances of are yes-instances. Moreover, a shortest sorting sequence can be found in time.
Proof 4.2.
For the first claim of the theorem, it is enough to consider the case . Under a configuration , we say a color is sorted if the two units of color are in the same bin. For a configuration , we define a directed multigraph as follows (see Figure 6). The vertex set is the color set . We add one directed edge from to for each full bin that contains a unit of color at the bottom and a unit of color at the top. That is, consists of for all full bins . (We may add self-loops here.) For the directed multigraph , we denote its underlying multigraph by , where is a multiset obtained from by ignoring the directions. Since , each color appears twice in . When consists of full bins, is a 2-regular graph with edges, which means that is a set of cycles. We call a self-loop a trivial cycle. If also contains half bins, then is a disjoint union of cycles and paths.
Let denote the number of nontrivial directed cycles in , the number of vertices of indegree 2 in , and the number of vertices with no self-loop, i.e., the number of unsorted colors. We prove that if has either two empty bins or one empty and two half bins, then a shortest sorting sequence has length . Clearly the initial configuration, which has two empty bins, satisfies the condition.
We first prove that there exists a sorting sequence of length . We use an induction on . When , all colors are sorted and thus is a goal. Now we turn to the inductive step, which consists of four cases.
(Case 1) Suppose has no half bins and . Note that when has no half bins, it has two empty bins. Then one can move the top unit of an arbitrary bin to an empty bin. This eliminates a directed cycle in the graph. Then we have one empty and two half bins. The claim follows from the induction hypothesis.
(Case 2) Suppose has no half bins and there is a vertex whose indegree is 2. By moving the two units of to an empty bin, we use two moves, while decreasing both and . Then we have one empty and two half bins. The claim follows from the induction hypothesis.
(Case 3) Suppose has two half bins one of which has a color of outdegree 0. Then, the other unit of that color is not below another unit in some bin. By moving that unit on top of the half bin, we can sort the color by one move. This decreases by one, and does not change . After this move, still we have two half bins. The claim follows from the induction hypothesis.
(Case 4) Otherwise, has two half bins and and both colors in the half bins have outdegree 1. Let be the color in and the sequence of vertices such that for all where has outdegree 0. By the assumption, is not the color of the unit of . Therefore, has indegree 2, i.e., both units of appear as the top units of full bins. We sort the color using the empty bin. It takes two moves and decreases both and by one. We then sort in this order by moves, which decreases by , where we require no additional empty bins and finally the bin will be empty. We have one empty and two half bins. The claim follows from the induction hypothesis.
We next prove that there exists no sorting sequence of length less than with regardless of the number of empty bins. Note that if all colors are sorted, . We show that any move decreases the potential by at most one.
(Case 1) If the color of the moved unit belongs to a directed cycle, this reduces by one but not . The other unit of the same color has a unit of another color on its top. Therefore this cannot reduce .
(Case 2) If the color of the moved unit has indegree 2, this reduces by one but none of or .
(Case 3) Otherwise, any other kind of moves cannot reduce or . Clearly one move cannot reduce by two.
Since , we can sort any instance in moves. Each feasible move can be found in time, which completes the proof.
Theorem 4.3.
If , , and , then can be solved in time. For a yes-instance, a shortest sorting sequence can be found in time.
Proof 4.4.
For a configuration , we again use the directed multigraph and its underlying multigraph by defined in the proof of Theorem 4.1. Let be a given instance of with , , and . To simplify, we assume that has no full bin that contains two units of the same color without loss of generality. (Hereafter, when we have a full bin that contains two units of the same color, we remove it from the configuration and never touch. In terms of , has no self-loop, and once produces a vertex with a self-loop, we remove it from .) Observe that if a configuration consists of full bins and one empty bin, then is a 2-regular multigraph with edges. That is, is a set of cycles of size at least 2 since we remove vertices with self-loops.
Now we focus on a cycle in . When is a directed cycle in (as in Figure 6), it is easy to remove the vertices in from by using one empty bin. When contains exactly one vertex of outdegree in , contains another vertex of indegree . In this case, we first move two units of color to the empty bin, and we can sort the other bins using two half bins. Lastly, we can get two units of color together, and obtain an empty bin again. Now we consider the case that contains (at least) two vertices and of outdegree in (as in Figure 6). In this case, we can observe that we eventually get stuck with two half bins of colors and . That is, is a no-instance if and only if contains at least one cycle that has at least two vertices of outdegree in .
In summary, we can conclude that the original instance can be sorted if and only if every cycle in contains at most one vertex of outdegree in . The construction of and can be done in time, and this condition can be checked in time. Moreover, it is easy to observe that sorting items in each cycle of length in requires moves, and moves are sufficient. Therefore, the optimal sorting requires moves, where is the number of cycles in , which can be computed in time.
Corollary 4.5.
If and , any yes-instance of has a solution of length .
5 The number of sufficient bins
In this section, we consider the minimum number for and such that all instances of with full bins of capacity with empty bins are yes-instances. We can easily see that such a number exists and that as follows. Let be an instance with full and empty bins of capacity such that . For each color used in , let be the positive integer such that contains units of color . We assign empty bins to each color . Since , we can easily “bucket sort” by moving water units in the bins to empty bins following the assignment of the empty bins to the colors. On the other hand, as shown in Theorem 4.3, we have no-instances when even if (see Section 6 for a larger no-instance).
In the following, we improve the upper bound of and show a lower bound.
Theorem 5.1.
.
Proof 5.2.
It suffices to show the lemma for the case where . We refine the bucket-sort base algorithm described above. See Figure 7. Suppose that we have at least empty bins. Then, using those empty bins, one can make other bins monochrome, since . Now we have monochrome bins. When some of those bins have the same color, we merge them. Then we can dedicate different bins to pairwise different colors, and we can use them to sort the remaining bins by the bucket sort.
We note that when , which coincides with the naive bucket sort algorithm.
Theorem 5.3.
.
Proof 5.4.
We show a configuration of bins where every bin has the same contents . In the case where , one can add extra bins whose contents are already sorted. In the case where , one can arbitrarily pad the non-empty bins of the above configuration with extra units of each color at the bottom.
Suppose that it is solvable with empty bins. We consider the very first moment when a unit of color has been moved. Figure 8 shows an example configuration with and . Note that any initially empty bin will always be monochrome. Let be the set of colors such that at least one unit of the color occupies a bin which was initially empty, where , and be the number of units of color which have been removed from the initial location. Particularly . Then we have for all colors , since a unit of color can be removed only after all the units above have been removed. Moreover, if , units of color which can be put only on units of the same color. That is, from both the source and target bins involved in the move, the unit of color must have been removed. Each of the target bins accepts at most units of water color on top of the unit initially located there. Therefore, for , (see Figure 9), i.e.,
(2) |
We will give a lower bound of the size of that admits a sequence of integers with that satisfies Eq. (2) for . Suppose that satisfies the condition with and . Then, one can easily see that admits a sequence with that satisfies Eq. (2). Therefore, we may and will assume that .
For , and implies . Hence, for ,
implies . Hence, for ,
implies . Hence, for ,
implies . On the other hand, . That is, implies , i.e., .
6 Concluding remarks
In this paper, we investigate the problems of solvability of ball and water sort puzzles. We show that both problems are equivalent and NP-complete. We also show that even for trivial instances of the water sort puzzle, it is NP-complete to find a shortest sorting sequence.
As discussed in Section 5, any instance can be sorted when is large enough. Especially, as discussed in Section 4, is enough for , while we have a no-instance for and . On the other hand, there exist no-instances with for and for (Figure 10). We constructed them by hand and verified that they are no-instances by using a computer program. It is interesting to give the boundary of for a given , especially, , that allows us to sort any input. Does depend on both and , or is it independent from ?
Recently, a Japanese puzzle maker produces a commercial product of the ball sort puzzle.333https://www.hanayamatoys.co.jp/product/products/product-cat/puzzle/ They extend the ball sort puzzle by introducing three different aspects as follows. (1) It contains three different capacities of bins. That is, it allows to use different size of bins. (2) The balls are not only colored, but also numbered. In some problems, the puzzle asks us to not only arrange the balls of the same color into a bin, but also they should be in order in each bin. In some other problems, the puzzle asks us to arrange the balls so that the sums of the numbers in every non-empty bins are equal. (3) It contains transparent balls. That is, these transparent balls are just used for arranging balls, and their final positions do not matter in the final state. Those extensions seems to be reasonable future work.
References
- [1] Michael R. Garey and David S. Johnson. Computers and Intractability — A Guide to the Theory of NP-Completeness. Freeman, 1979.
- [2] William H. Gates and Christos H. Papadimitriou. Bounds for sorting by prefix reversal. Discret. Math., 27(1):47–57, 1979. doi:10.1016/0012-365X(79)90068-2.
- [3] R. A. Hearn and E. D. Demaine. Games, Puzzles, and Computation. A K Peters Ltd., 2009.
- [4] Hiro Ito, Junichi Teruyama, and Yuichi Yoshida. An almost optimal algorithm for Winkler’s sorting pairs in bins. Progress in Informatics, 9:3–7, 2012.
- [5] Takehiro Ito, Erik D. Demaine, Nicholas J.A. Harvey, Christos H. Papadimitriou, Martha Sideri, Ryuhei Uehara, and Yushi Uno. On the complexity of reconfiguration problems. Theoretical Computer Science, 412:1054–1065, 2011.
- [6] Donald E. Knuth. The Art of Computer Programming: Sorting and Searching, volume 3. Addison-Wesley Publishing Company, 2nd edition, 1998.
- [7] Peter Winkler. Mathematical puzzles: A Connoisseur’s collection, volume 143, pages 149–151. A K Peters, 2004.
- [8] Katsuhisa Yamanaka, Shin-ichi Nakano, Yasuko Matsui, Ryuhei Uehara, and Kento Nakada. Efficient Enumeration of All Ladder Lotteries and Its Application. Theoretical Computer Science, 411:1714–1722, 2010.