Abstract 1 Introduction 2 NP-Hardness 3 Algorithms 4 Conclusion and Future Work References

Hexasort – the Complexity of Stacking Colors on Graphs

Linus Klocker ORCID TU Wien, Austria Simon Dominik Fink ORCID Department of Algorithms and Complexity, TU Wien, Austria
Abstract

Many popular puzzle and matching games have been analyzed through the lens of computational complexity. Prominent examples include Sudoku [13], Candy Crush [7], and Flood-It [4]. A common theme among these widely played games is that their generalized decision versions are NP-hard, which is often thought of as a source of their inherent difficulty and addictive appeal to human players. In this paper, we study a popular single-player stacking game commonly known as Hexasort. The game can be modelled as placing colored stacks onto the vertices of a graph, where adjacent stacks of the same color merge and vanish according to deterministic rules. We prove that Hexasort is NP-hard, even when restricted to single-color stacks and progressively more constrained classes of graphs, culminating in strong NP-hardness on trees of either bounded height or degree. Towards fixed-parameter tractable algorithms, we identify settings in which the problem becomes polynomial-time solvable and present dynamic programming algorithms.

Keywords and phrases:
Hexasort, offline color stacking on graphs, NP-complete, polynomial-time solvable, dynamic programming
Copyright and License:
[Uncaptioned image] © Linus Klocker and Simon Dominik Fink; licensed under Creative Commons License CC-BY 4.0
2012 ACM Subject Classification:
Theory of computation Problems, reductions and completeness
Related Version:
Full Version: https://arxiv.org/abs/2603.01244 [10]
Funding:
Vienna Science and Technology Fund (WWTF) grant [10.47379/ICT22029] and Austrian Science Fund (FWF) grant [10.55776/Y1329]
Editor:
John Iacono

1 Introduction

We study the computational complexity of the single-player stacking game Hexasort. There exist many different browser-based or mobile phone app implementations111See e.g. hexasort.io, games-by-sam.com/bee-sort-by-sam, or spielaffe.de/Spiel/Hexa-Sort-3D., often with various variations to the mechanics. Common to all is that they are played on a hexagonal game board, where a given sequence of stacks of colored hexagons need to be placed onto the empty cells. Whenever the topmost hexagons of adjacent stacks have the same color, all hexagons of said color move from one stack onto the other; see Figure 1. If this results in sufficiently many same-colored hexagons on top of each other, all of them vanish. The goal of the game is to place the full input sequence without running out of empty cells.

Refer to caption
(a) Original game screenshot.
(b) Graph abstraction.
(c) Result after placing .
Figure 1: Example of a Fitting Hexasort game with its graph representation (not showing stack heights). After placing , the green stacks merge, reach t and vanish.

In our model of the game, we generalize the game board to an arbitrary graph, where the colored stacks are placed onto its vertices. We restrict our consideration to monochromatic stacks and define that whenever a stack is placed adjacent to stacks of the same color, the old stacks completely move onto the new one. We will not consider any further complications to the mechanics such as initially non-empty game boards and blockers that can only be removed after having performed a certain amount of merges, although we will later show that similar mechanics can be achieved through gadgets. An instance (G,S,t) of Hexasort thus consists of a graph G=(V,E), a threshold t+, and a sequence S of stacks having colors C, where each stack s=(c,h)S has a color cC and a height h+.

A configuration of the game assigns to each vertex either a stack or nothing, that is, it is a function δ:V(G)C×+ where δ(v)= if vertex v is currently empty, and otherwise δ(v)=(c,h) if v hosts a stack of color c and height h. Initially, we have δ(v)= for all vV. To place the next stack si=(c,h) from S, the player chooses an empty vertex x. Let Nc[x] denote the non-empty neighbors of x that have color c and let h be the sum of their heights. We obtain a new configuration δ from δ by setting δ(u)= for all uNc[x] and updating δ(x) depending on the accumulated height as follows. If h+ht, we set δ(x)=, otherwise we set δ(x)=(c,h+h).

The central algorithmic question of the problem Empty Hexasort is whether all stacks in S can be placed on the graph in their given order such that all vertices are empty again after S is exhausted. The problem variant Fitting Hexasort relaxes this to only placing all stacks in S in order, without the requirement for all vertices to be empty afterwards. Note that any instance of Empty Hexasort can be turned into an equivalent one of the Fitting variant by appending |V| stacks to S, all having new and distinct colors as well as a height smaller than t. Conversely, any yes-instance of Empty Hexasort is trivially also a yes-instance for the Fitting variant.

This paper explores the boundary between tractable and intractable instances of Fitting and Empty Hexasort. In Section 2, we show that Empty Hexasort is NP-hard, even under severe structural restrictions. In particular, the problem is weakly NP-hard with one color on graphs consisting of two independent edges, and strongly NP-hard on trees of either bounded height or degree. Towards fixed-parameter tractable algorithms, in Section 3 we identify settings in which both variants become polynomial-time solvable and present a dynamic programming approach with running time exponential in the size of the input graph.

1.1 Related Work

Several games with mechanics modeling partial aspects of Hexasort, such as placing objects on graphs, monochromatic components, as well as merging and elimination thresholds, have been investigated. To the best of our knowledge, the combination of these mechanics and especially the computational complexity of Hexasort has not been studied previously. As we will see, the combination of non-monotone development of game states, threshold-based elimination that discards any excess, together with having to eliminate all stacks in Empty Hexasort poses an especially interesting challenge.

A recent example of a game involving the placement of objects on a graph is Matching Match Puzzle, analyzed by Iburi et al. [8]. In this puzzle, the player is given a colored graph and a set of matchsticks, each having a color assigned to both of its endpoints. The task is to place the matchsticks onto the edges of the graph such that, at every vertex, all incident matchstick endpoints agree on a single color consistent with the given graph coloring. The problem is NP-hard even on spiders and, interestingly, this result is established via a reduction from 3-Partition, similar to the reduction used in Section 2.2.

One well-studied example of a game relying on monochromatic components is the game Flood-It. A survey by Fellows et al. [4] summarizes complexity results for two variants known as Flood-It and Free-Flood-It. Both are played on graphs with colored vertices. In Flood-It, a fixed pivot vertex is repeatedly recolored, causing its whole monochromatic connected component to change color as well. In Free Flood-It, the vertex whose monochromatic component shall be recolored can be freely chosen. In both cases, the objective is to make the entire board monochromatic using as few moves as possible. Flood-It is already NP-hard on boards of size 3×n with four colors.

A game that combines merging and vanishing mechanics is Clickomania. In Clickomania, the player is presented a grid of colored blocks. A move consists of selecting a monochromatic connected component of at least two blocks, which removes them from the board. Blocks above the removed blocks fall down, and empty columns vanish. The objective is to clear the board completely or minimize the number of remaining blocks. Clickomania remains NP-hard even when restricted to only two colors and two columns [1].

Outside the algorithmic study of games, the merging and threshold-based elimination of stacks in Hexasort exhibits parallels with the behavior of physical water droplets on digital microfluidics platforms like OpenDrop [2]. Here, a grid of electrodes is used to manipulate discrete fluid droplets. By activating specific electrodes, droplets can be moved, mixed, and merged on the surface. Due to surface tension and physical constraints, a single electrode tile can only support a specific volume of liquid; exceeding this volume makes the droplet unstable. In this analogy, stack height corresponds to droplet volume, and merging occurs based on proximity. In addition to medical and chemical applications, the device has also been gamified using colored droplets [12].

1.2 Preliminaries

We distinguish between sets (unordered collections, denoted by {}) and sequences (ordered lists, denoted by ). For two sequences A=a1,,an and B=b1,,bm, we define their concatenation, denoted as A+B, as the new sequence formed by appending the elements of B to the end of A, so A+B=a1,,an,b1,,bm.

We use the term three-merge to denote a move that places the current stack such that it has exactly two adjacent non-empty vertices of the same color. For a color c, we denote sum(c,S) as the sum of heights of color c currently remaining in S.

We will assume that ht for all (c,h)S, as any stacks exceeding t can equivalently be capped to have height t. Observe that no two stacks that are adjacent on G can have the same color in a configuration δ. Finally, in Empty Hexasort, we assume that for any cC, we have sum(c,S)t, as the instance is trivially negative otherwise.

2 NP-Hardness

Our first proof that Empty Hexasort is NP-hard is via a reduction from the weakly NP-complete problem Partition to Empty Hexasort with a single color on graphs consisting of only two independent edges in Section 2.1. We afterwards extend this hardness result to connected graphs with nine vertices. We further show strong NP-hardness via a reduction from 3-Partition to Empty Hexasort on trees of bounded height or degree in Section 2.2. Thereby, we show hardness even when either the graph size and number of colors are small constants, or when all numeric inputs are polynomially bounded in the input size.

The figures in this section employ a visual encoding for colored stacks to ensure accessibility: , , , and depict red, blue, green, and black stacks, respectively. Note that the exact stack heights are usually not relevant for the figures and thus not shown.

2.1 Reduction from Partition

In the Partition problem, we are given a multiset P of positive integers and must decide whether P can be partitioned into two disjoint subsets P1 and P2=PP1 such that P1=P2. The Partition problem is a well-known special case of the Subset Sum problem and is (weakly) NP-hard [6]. We restrict our attention to Partition instances where (1) the sum of elements P is even, (2) no element pP satisfies pP/2, and (3) the desired partition (if it exists) is not formed by the sum of a prefix of P. Note that instances that violate one of these criteria can be trivially decided in polynomial time.

Partition can be reduced to Empty Hexasort as follows. Let P={p1,p2,,pn} be an arbitrary instance of Partition. We construct a corresponding instance IH=(G,S,t) of Empty Hexasort with threshold t=P/2 where the graph G=(V,E) has four vertices V={v1,v2,v3,v4} and two edges E={v1v2,v3v4}. The input sequence of stacks S is defined as S=(c,p1),(c,p2),,(c,pn), where c is the only color.

Theorem 1.

Empty Hexasort on two independent edges and a single color is weakly NP-hard.

Proof.

In our above construction, it is easy to see that any solution to P can be translated to a solution of the derived IH by alternatingly placing the stacks of the first partition on v1 and v2 while placing those of the second on v3 and v4. As each partition sums up to exactly P/2=t, the stacks vanish exactly once the last element of the partition is placed.

For the converse direction, we first show that placing all stacks of IH exclusively on the endpoints of one of the two edges cannot empty the graph. Recall that we excluded the trivial case of a prefix of P forming the desired partition. Thus, placing all stacks on one edge will at some point form a stack that exceeds t by a value of at least 1 and consequently vanishes. Afterwards, the remaining stacks sum up to height at most t1 and thereby cannot vanish. Thus, the stacks of IH need to be distributed onto the two independent edges. Assuming one edge receives stacks exceeding t leaves less than t for the other edge.

Note that above approach could easily be extended to connected graphs if we allowed a given initial configuration with some already-placed differently-colored stack keeping both edges separate until the end. Instead, we construct a gadget to adequately restrict the placement of stacks on a more involved but connected graph structure. The goal is to argue that a given sequence of stacks can only be placed in such a way that two specific, non-adjacent paths of length two remain available for processing the Partition input sequence. We adapt our above construction to obtain an instance IH=(G,S,t) where G is a connected spider graph, that is a tree with only one vertex of degree greater than two. Our specific spider graph G has nine vertices: one central vertex x with degree five, two short arms consisting of the vertices s1,s2, respectively, and three long arms of length two; see Figure 2. For long arm i{1,2,3}, let ai be the neighbor of x while bi is the leaf.

We will now use the four colors: cblack (for the Partition input in S), and cred, cblue, cgreen (for the gadget stacks). Define the sequence of the initial gadget stacks as Sinitial=(cred,t1),(cred,t1),(cblue,t1),(cblue,t1). Define the reservation gadget stacks as Sreservation=(cgreen,t2+1),(cgreen,t2+1),(cgreen,t21),(cgreen,t21). Define the sequence of the connector gadget stacks Sconnector which frees the gadget of the initial stacks as Sconnector=(cred,t1),(cblue,t1). We construct the full input sequence as S=Sinitial+Sreservation+S+Sconnector. The idea behind our construction is that the gadget stacks (non-cblack) must vanish to empty the graph, but block most of the graph while the cblack stacks of S are processed, such that they need to be placed on the four endpoints of exactly two independent edges. To formally show this, we need the following lemmas, which will also be useful for later settings.

Lemma 2 (Forced three-merge).

Let c be a color of an Empty Hexasort instance I=(G,S,t) such that S contains exactly three stacks (c,h1),(c,h2),(c,h3) of color c in the given order and where hi<t for i1,2,3 while h1+h2t. In any solution to I, the first and second stack need to be placed such that they are non-adjacent but have a common neighbor that must be empty when the third stack is placed.

Proof.

If the first two stacks are placed on adjacent vertices, they merge immediately and vanish, leaving the third stack unremoved at the end. If instead they are placed more than one vertex apart, they cannot later form a three-merge. Hence, exactly one vertex must separate them, onto which the third stack is placed to remove all of them in one go.

Thus, two colors to which Lemma 2 applies cannot alternate on a path as shown in Figure 2(a).

(a) After placing Sinitial,Sreservation and S the next red stack in Sconnector cannot three-merge the red stacks, violating Lemma 2.
(b) The admissible configuration of Lemma 4.
Figure 2: Labeled graph G with red and blue stacks placed. The configuration depicted in (a) cannot lead to an empty graph, while (b) can.
Lemma 3.

Let c be a color of an Empty Hexasort instance I=(G,S,t) such that S contains exactly four stacks (c,h),(c,h),(c,hs),(c,hs) of color c in the given order and where hs<t2<h such that hs+ht. In any solution to I, these stacks can vanish either (a) via two independent merges of an h-stack with an hs-stack, or (b) by merging all four stacks in one move. In case (a), the stacks are placed on the endpoints of two independent edges, in case (b) on a star of degree 3.

Proof.

Every color-c stack is smaller than t, so no stack can vanish on its own. Any merge involving exactly three color-c stacks would cause precisely those three stacks to vanish, leaving a single stack that can never vanish. Merging the first two stacks (of height 2h>t) would cause them to vanish, leaving the latter two stacks of total height 2hs<t that cannot vanish. In contrast, two stacks of different height sum to at least t and thus vanish. Thus, only the latter two hs stacks could be merged without vanishing, although this leaves no chance of merging them with the former h ones to make them vanish. Altogether, this shows the color-c stacks need to vanish in either (a) two pairs of two, or (b) all four in one go. In case (a), we first need to place the first two stacks without them merging, and then the third stack so that it only merges with one of them, necessitating the first edge. Finally placing the last stack adjacent to the leftover h stack requires the second, independent edge. For case (b), the only way of merging all four stacks in one move is placing them on a star of degree 3, with the last stack placed on the center.

Together, this allows us to show the following lemma; see also Figure 2(b).

Lemma 4.

After placing Sinitial+Sreservation in IH=(G,S,t), any solution needs to have the cred stacks on x and b1, the cblue stacks on s1,s2 and all other vertices empty.

Proof.

First, suppose that vertex x remains empty after placing Sinitial, i.e., the first two cred and two cblue stacks. Lemma 2 now implies that four neighbors of x must be occupied by the cred and cblue stacks, while all leaf vertices are empty. However, occupying four neighbors of x leaves no space for the cgreen stacks, inevitably violating Lemma 3. Hence, the central vertex x must host a stack of color either cred or cblue, which means that we only need to consider case (a) of Lemma 3.

After placing Sinitial vertex x holds the stack (ca,t1) where ca{cred,cblue}. From Lemma 2 it follows that the second ca stack of Sinitial must be placed at bi for some i{1,2,3}, say w.l.o.g. b1. We now show that the two other stacks of Sinitial, call their color cb, must be placed on s1 and s2. First, if a cb stack is placed on any vertex of 2 or 3, there only remains one path of length two, which violates Lemma 3. Second, if a cb stack is placed on a1, between the two existing ca stacks, this violates Lemma 2, as a1 must remain empty for the ca three-merge. Thus, only s1 and s2 remain.

Finally, we show that ca=cred and cb=cblue. Recall that Lemma 2 especially means that the common neighbor of the first two stacks needs to be empty when placing the third on it. As the third cred stack appears before the third cblue stack in Sconnector, the common neighbor of the two previous cred stacks needs to be empty at that point before placing Sconnector. This is only the case when x and b1 are cred while s1 and s2 are cblue. Removing the cred stacks then frees up x as common neighbor of the cblue stacks.

(a) Step 1: Green stacks are placed, merged and vanish.
(b) Step 2: Two empty independent paths remain.
(c) Step 3: Partition stacks are placed and merged.
(d) Step 4: Red stacks vanish.
(e) Step 5: Blue stacks vanish.
(f) Step 6: G is empty.
Figure 3: Spider graph stack placement progression leading to an empty graph.

Figure 3 visualizes how the two edges that are empty after Lemma 4 can be used to solve the partition instance encoded in S and then clear the graph via Sconnector. In the full version [10], we show that any solution must make these steps, thereby obtaining the following theorem.

Theorem 5.

Empty Hexasort on spider graphs with nine vertices and four colors is weakly NP-hard.

2.2 Reduction from 3-Partition

Note that by reducing from Partition in the last section, we rely on large numbers and therefore can only show weak NP-hardness. In this section, we now want to show strong NP-hardness, that is hardness even when t (and thereby also all stack heights) is bounded by a polynomial in the input size [5]. For this, we use a reduction from the strongly NP-hard problem 3-Partition, where we are given a multiset A of 3m positive integers and a bound BZ+. The question is whether A can be partitioned into m triplets A1,A2,,Am such that each triplet sums up to exactly B. As noted by Garey et al. [6, p. 224] the problem is NP-complete in the strong sense, and we can assume that every input value aA is restricted such that B/4<a<B/2 and aAa=mB.

For a reduction from 3-Partition to Empty Hexasort, we will adapt our prior gadget to now leave exactly three distinct, isolated vertices available for the placement of the three elements of a single triplet. Using m disjoint copies of this gadget, we can thereby model the m triplets in a 3-Partition solution. More specifically, our reduction works as follows. A gadget consists of one central vertex x of degree five, four arms of length two with vertices ai,lbi for i{1,2,3,4}, and one arm of length one, s1; see Figure 4.

(a) The admissible configuration of Lemma 6.
(b) Removing the gadget stacks , and finally .
(c) Pulling the stacks to merge and vanish on x.
Figure 4: The spider gadget used in our 3-Partition reduction.

Given a 3-Partition instance (A,B) we obtain an Empty Hexasort instance IH=(G,S,t) where graph G has m=|A|3 disjoint copies of the gadget graph and the threshold is t=B+4. For S, we use 3m+1 colors: cblack for the triplet elements, and three gadget colors credj, cbluej, cgreenj for j{1,,m}. The input sequence is constructed as follows.

  • The initial gadget stack sequence Sinitialj=(credj,t1),(credj,t1),(cbluej,t1),
    (cbluej,t1),(cgreenj,t1),(cgreenj,t1) forces the isolation of three vertices.

  • The sequence SA=(cblack,a1),,(cblack,a3m) represents A={a1,,a3m}.

  • The connector gadget stack sequence Sconnectorj=(credj,t1),(cbluej,t1),(cgreenj,t1) frees each gadget of the (initial) gadget stacks.

  • The triplet connector sequence Sfinalj=(cblack,1),(cblack,1),(cblack,1),(cblack,1) draws the placed triplet, causing all its stacks to merge and vanish.

We construct the full input sequence as S=Sinitial+SA+Sconnector+Sfinal, where Sinitial, Sconnector and Sfinal are obtained by concatenating all Sinitialj, Sconnectorj, and Sfinalj, respectively, for all j{1,,m}. Observe that Lemma 2 still applies to all cbluej, credj, and cgreenj. Analogously to Lemma 4, we again show that Sinitial forces a certain configuration; see Figure 4(a) and the full version [10].

Lemma 6.

After placing Sinitial in IH, any solution has, for each gadget, the vertices ai and all bji empty for some i{1,2,3,4}. Furthermore, the two neighbors of ai have the same color c, and each gadget consists of exactly three colors, where c appears first in Sfinal.

Proof sketch..

First observe that no color can be split onto multiple disconnected gadgets due to Lemma 2. Using a case analysis together with the pigeonhole principle, one can then show that every gadget contains exactly six stacks, which then need to use exactly three colors. The positions of empty vertices then follow similar to Lemma 4.

We will again assume without loss of generality that a1 as well as b2,lb3,lb4 are always the empty vertices. As each gadget received exactly three colors, where each color needs to be put onto a single gadget, the exact position of a color within Sinitial and Sfinal is irrelevant, as long as c appears first in Sfinal. We assume w.l.o.g. that the j-th gadget received colors credj, cbluej, cgreenj such that its x is credj, while s1 and a2 are cgreenj; see Figure 4(a). We can now show the following; see [10] for the full proof.

Theorem 7.

Empty Hexasort on the disjoint union of spider graphs with ten vertices each is strongly NP-hard.

Proof sketch..

We focus on showing that any solution to IH implies a solution to (A,B), continuing where Lemma 6 left off. Here, as a1 cannot be used by SA due to Lemma 2, exactly the three vertices b2,lb3, and b4 remain available for placing SA. It is easy to see that, again due to pigeonhole principle, exactly three stacks from SA must be placed on them. Next, the stacks of Sconnector are placed. Since for each gadget j now only a1 is empty, the corresponding credj connector stack is forced onto this vertex, merging the red triplet. The cbluej and cgreenj connector stacks are then forced to three-merge on x, see Figure 4(b). This leaves the gadget empty except for b2,lb3, and b4 containing stacks from SA. Emptying the gadget then requires at least four cblack height-1 stacks from Sfinal to pull the triplet stacks onto x; see Figure 4(c). By the pigeon hole principle, each gadget can receive at most four such stacks, contributing a value of 4 to the required height of t=B+4. Thus, each gadget can only be emptied if its three stacks from SA sum up to at least (or rather exactly) B, implying a 3-Partition solution to (A,B). Our reduction can be performed in polynomial time and maintains the numerical values and especially their bounds from (A,B), thereby showing strong NP-hardness.

We again want to extend our result to connected graphs. For this, we will connect the s1 vertices of all gadgets in a tree-like manner: Let T be a tree on m vertices t1,,tm. For the Empty Hexasort instance IH=(G,S,t), the connected graph G is obtained by combining the m gadgets of G from above with T: for all j{1,,m}, identify the s1 vertex of the j-th gadget in G with the tree node tj in T. Observe that, if T has bounded height or degree, so does G. It remains to show that Lemma 6 still applies, which in this case hinges on stacks of the same color not being distributed over multiple gadgets.

Lemma 8.

After placing Sinitial in IH, any solution needs to have all stacks of exactly three colors on each gadget.

Proof sketch..

Note that due to Lemma 2, the only vertices that can host a (non-black) color that is split over multiple gadgets are the x and s1 ones. The same lemma means that placing a stack on bi forces a same-colored stack on x, which is only possible once, and that ai remains empty. Thus, no gadget can have 7 or more stacks, which via the pigeon hole principle implies that all gadgets have exactly 6 stacks. From this, we can conclude that, if either of x and s1 hosts a color that only appears once on the respective gadget, so does the other with a different color that only appears once on the gadget.

We will now take an arbitrary such pair, remove it from the graph, and show that the smaller remainder graph must always contain another such pair, which contradicts G being a finite acyclic graph. Consider a x vertex of gadget j that has a color c1 that only appears once on the gadget. Due to Lemma 2, the second stack of c1 needs to be on the s1 vertex of gadget j such that tj and tj are adjacent in T; see Figure 5. As noted above, vertex x of gadget j needs to have a different color c1 that only appears once on that gadget. Especially, the s1 vertex of gadget j cannot have color c1 as otherwise it would alternate with c1, violating Lemma 2. We remove gadget j, and continue by considering the pair of color c1 on the smaller, connected graph that is the connected component of the remaining gadget j. This yields a contradiction as this argument can continue indefinitely while our graph is finite.

Figure 5: A connected graph G with stacks placed across multiple gadgets excluded by Lemma 8.

The remainder of Lemma 6 and thereby the following theorem now follows; see the full version [10] for an explicit proof.

Theorem 9.

Empty Hexasort on trees of bounded height or degree is strongly NP-hard.

3 Algorithms

In this section we investigate algorithmic approaches towards solving Hexasort. Note that a trivial brute-force approach to decide an instance of Fitting or Empty Hexasort runs in O(|V||S|) time: We try for each stack in the sequence S each vertex on which it could be placed and then check whether the final configuration is admissible. We develop a dynamic programming approach to Hexasort by instead systematically exploring the space of reachable board configurations, yielding running time exponential in the graph size.

Theorem 10.

Fitting and Empty Hexasort can be decided in time 𝒪(|S|(|C|t)|V|).

Proof.

The core of our approach is a Boolean table, whose entry DP[i,δ] is true if and only if the board configuration δ is attainable after placing the first i stacks of S. We initialize the dynamic program by setting only DP[0,δ0]=true for the unique empty configuration δ0 where δ0(v)= for all vV. The recurrence relation processes the sequence step-by-step: for each index i from 1 to m and every configuration δ such that DP[i1,δ]=true, we consider every possible valid move. A move consists of placing the current stack si=(c,h) on any empty vertex and sets DP[i,δ]=true for the resulting configuration δ. An Empty Hexasort instance is positive if and only if DP[m,δ0]=true upon exhaustion of the sequence S. A Fitting Hexasort instance is positive if DP[m,δ0]=true holds for any δ.

Regarding the running time, observe that there are 𝒪((|C|t)|V|) configurations as each of the |V| vertices has |C|(t1)+1 possible states. Since the algorithm iterates through the sequence and considers up to |V| placement options for each reachable state, the total time complexity is 𝒪(|S|(|C|(t1)+1)|V||V|)=𝒪(|S|(|C|t)|V|).

While this algorithm becomes slower the larger the graph becomes, we will now show that once the graph becomes large enough to provide dedicated space for each color, the problem becomes trivially positive. For this, we need separate approaches for both problem variants.

Lemma 11.

Any Fitting Hexasort instance where G contains a matching of size at least |C| is positive.

Proof.

For each color cC, we reserve an edge from the matching. When a stack (c,h) arrives, it is placed alternatingly on one of the two endpoint vertices. Thereby, for each color at any point in time, at least one free vertex is available. Thus, all stacks can be placed.

Lemma 12.

Any Fitting Hexasort instance where G contains a vertex of degree |C|t is positive.

Proof.

If G contains a vertex x of degree at least |C|t, we can place the stacks from |S| on any of its neighbors, only placing a stack directly on x whenever its color has already t stacks adjacent, thereby eliminating all stacks of this color. As no color ever has more than t stacks on the graph and any stack placed on x immediately vanishes (the merge of its t neighbors surely exceeds size t), all stacks can be placed and the instance is thus always positive.

Observation 13.

Any Empty Hexasort instance where the stacks of some color c sum up to less than t when ignoring all height-t stacks is negative if the last color-c stack has height less than t.

Lemma 14.

Any Empty Hexasort instance, that is not trivially negative by Observation 13, and where G contains |C| non-overlapping spiders, each with two length-two legs and one length-one leg, is positive.

Proof.

For each color cC, we reserve a degree-3 spider with center x adjacent to the first vertices of the three legs s, a1b1, a2b2. Let z=(c,hz) be the last color-c stack in S. If hz=t or sum(c,S)hz<t, place all color-c stacks alternatingly on any two adjacent vertices of the spider. Otherwise, let X be an arbitrary minimal set of color-c stacks, excluding any height-t stack, such that ΣXt. Note that by assuming that Observation 13 does not make the instance trivially negative, such set must exist. Now let y=(c,hy) be the first stack of X in S, which we place on s. Place all other stacks of X, except for z, alternatingly on a1 and b1 such that the last stack of X is placed on a1. Place all other stacks of color c, except for z, alternatingly on a2 and b2 such that the last of them is on a2. Place the last stack z on the center x. By minimality of X, whether or not zX, at this point s contains y and a1 a stack that reaches height at least t when merged with y (or with z and y). When z is placed on x, it merges a1 and s with any remaining stack on a2, which makes color c vanish. Consequently, all stacks in S can be placed while emptying the graph in the end.

For Fitting Hexasort, combining these results now allows us to show fixed-parameter tractability when parametrizing by the number of colors and the threshold value.

Theorem 15.

Fitting Hexasort can be decided in time 𝒪(f(|C|,t)|S|2) for some function f.

Proof.

First note that if G is small enough such that |V| is bounded by |C|, the instance can be solved in 𝒪((|C|t)|C||S|) by Theorem 10. If G is instead large and contains a matching of size at least |C|, the instance is always positive by Lemma 11. The existence of such matching can be checked in polynomial time; see [3] for a survey of algorithms. Also, by Lemma 12, all instances with a vertex of degree at least |C|t are always positive. It remains to treat the case where G is large but has bounded degree and does not contain a large enough matching. Equivalently, the latter means that G has a vertex cover X of a size bounded by 2|C| [11, 9].

Let V0 denote the set of isolated vertices. It is easy to see that the instance is always positive if |V0||S| – place every stack on a single vertex. Thus G has a bounded vertex cover X, no high-degree vertex, and a bounded number of isolated vertices V0. This implies that, disregarding the isolated vertices V0, graph G has a size bounded by |C|2t. This is because each of the at most 2|C| vertices of X can have at most |C|t neighbors outside X, and any non-isolated vertex needs to either be in X or have a neighbor in X. The running time of the dynamic program from Theorem 10 can be factored as 𝒪(|S|(|C|t)|VV0|(|C|t)|V0|). As isolated vertices are structurally identical and cannot affect each other, we do not need to track their specific configurations. Instead, we simply count the number of non-empty isolated vertices and thus simplify the exponential factor ((|C|t)|V0|) with the linear factor |V0|. This yields a running time of 𝒪(|S|(|C|t)|VV0||V0|). Since |VV0| is bounded by |C|2t and |V0| is bounded by |S| the complexity simplifies to 𝒪((|C|t)|C|2t|S|2) satisfying our bound in this final case.

4 Conclusion and Future Work

In this work, we studied the computational complexity of the popular game Hexasort in the Empty and Fitting variants. In Section 2 we presented multiple NP-hardness reductions, starting with a reduction from the Partition problem to Empty Hexasort with one color on two independent paths of size two. Reducing Empty to Fitting Hexasort by appending four stacks with new colors to such instance, this result also applies to the latter variant when allowing five colors. We further used 3-Partition to show that Empty Hexasort is strongly NP-hard, even when restricted to trees of bounded height or degree. This demonstrates that the problem remains intractable even under severe structural restrictions on the graph, together with either a constant number of colors or numbers that are polynomially-bounded by the input size.

At the same time in Section 3, we identified several classes of instances that admit efficient algorithms. We introduced a dynamic programming algorithm that solves both variants on arbitrary graphs in time 𝒪(|S|(|C|t)|V|). Note that this immediately implies tractability on constant-size graphs when the threshold t is polynomially-bounded by the input size. We also showed that instances with sufficiently large graphs, which allow each color to be solved independently on its own exclusive part of the graph, are always tractable. Based on this, we were able to show that Fitting Hexasort is fixed-parameter tractable when parametrizing by the number of colors and the threshold value.

For Fitting Hexasort we thus obtain results for bounding any two of the graph size |V|, the number of colors |C| and the threshold t. While the problem is (weakly) NP-hard for constant |V| and |C|, it becomes tractable for either constant |V| and polynomially-bounded t or for constant |C| and t. We leave the complexity of the case where t is bounded by a constant as open question. Furthermore, it would be nice to extend our results to show also strong NP-hardness for constant |C|. Similarly, our results for Empty Hexasort are tight in the sense that, when bounding both the graph size by a constant as well as the elimination threshold by a polynomial in the input size, we obtain a polynomial-time algorithm. In contrast, only applying either of the bounds yields NP-hardness. We leave as an open question whether Empty Hexasort becomes tractable when bounding both the threshold and the number of colors, e.g. by using Lemma 14 as we did for the Fitting case. The main challenge here is arguing that any graph containing only a few copies of this more involved structure needs to have a simple structure itself.

Regarding the Fitting Hexasort instances found in playable versions of the game, their hexagonal grid is usually large enough w.r.t. the number of colors to allow for a sufficiently large matching. What instead complicates these instances is that stacks here are also allowed to contain hexagons of multiple different colors, which is a further direction in which our results could be extended.

References

  • [1] Aviv Adler, Erik D. Demaine, Adam Hesterberg, Quanquan Liu, and Mikhail Rudoy. Clickomania is hard even with two colors and columns. In The Mathematics of Various Entertaining Subjects (MOVES 2015), volume 2, pages 325–363. Princeton University Press, 2017. doi:10.23943/princeton/9780691171920.003.0018.
  • [2] Mirela Alistar and Urs Gaudenz. Opendrop: An integrated do-it-yourself platform for personal use of biochips. Bioengineering, 4(2), 2017. doi:10.3390/bioengineering4020045.
  • [3] Ran Duan and Seth Pettie. Linear-time approximation for maximum weight matching. J. ACM, 61(1), January 2014. doi:10.1145/2529989.
  • [4] Michael R. Fellows, Frances A. Rosamond, Maise Dantas da Silva, and Uéverton S. Souza. A Survey on the Complexity of Flood-Filling Games, pages 357–376. Springer International Publishing, Cham, 2018. doi:10.1007/978-3-319-98355-4_20.
  • [5] Michael R. Garey and David S. Johnson. “Strong” NP-completeness results: Motivation, examples, and implications. Journal of the ACM (JACM), 25(3):499–508, 1978. doi:10.1145/322077.322090.
  • [6] Michael R. Garey and David S. Johnson. Computers and Intractability: A Guide to the Theory of NP-Completeness. W. H. Freeman and Company, San Francisco, 1979. See Appendix A3, problem SP12 (PARTITION), p. 223.
  • [7] Luciano Gualà, Stefano Leucci, and Emanuele Natale. Bejeweled, candy crush and other match-three games are (NP-)hard. In Proceedings of the 2014 IEEE Conference on Computational Intelligence and Games (CIG 2014), pages 1–8. IEEE, 2014. doi:10.1109/CIG.2014.6932866.
  • [8] Yuki Iburi and Ryuhei Uehara. Computational complexity of matching match puzzle. In Andrei Z. Broder and Tami Tamir, editors, Proceedings of the 12th International Conference on Fun with Algorithms (FUN 2024), volume 291 of LIPIcs, pages 17:1–17:10. Schloss Dagstuhl – Leibniz-Zentrum für Informatik, 2024. doi:10.4230/LIPIcs.FUN.2024.17.
  • [9] Hugo Jacob, William Lochet, and Christophe Paul. On a tree-based variant of bandwidth and forbidding simple topological minors, 2025. doi:10.48550/arXiv.2502.11674.
  • [10] Linus Klocker and Simon D. Fink. Hexasort – the complexity of stacking colors on graphs, 2026. arXiv:2603.01244.
  • [11] Robert Sasak. Comparing 17 graph parameters. Master’s thesis, The University of Bergen, 2010.
  • [12] Steve Mould. Opendrop - electrowetting device demonstration [video]. https://www.youtube.com/watch?v=rf-efIZI_Dg, 2015.
  • [13] Takayuki Yato and Takahiro Seta. Complexity and Completeness of Finding Another Solution and Its Application to Puzzles. IEICE Trans. Fundam. Electron. Commun. Comput. Sci., E86-A(5):1052–1060, 2003. URL: https://globals.ieice.org/en_transactions/fundamentals/10.1587/e86-a_5_1052/_p.