Compacting Squares

05/17/2021 ∙ by Irina Kostitsyna, et al. ∙ TU Eindhoven 0

Edge-connected configurations of squares are a common model for modular robots in two dimensions. A well-established way to reconfigure such modular robots are so-called sliding moves. Dumitrescu and Pach [Graphs and Combinatorics, 2006] proved that it is always possible to reconfigure one edge-connected configuration of n squares into any other using at most O(n^2) sliding moves, while keeping the configuration connected at all times. For certain configurations Ω(n^2) sliding moves are necessary. However, significantly fewer moves may be sufficient. In this paper we present a novel input-sensitive in-place algorithm which requires only O(P̅ n) sliding moves to transform one configuration into the other, where P̅ is the maximum perimeter of the respective bounding boxes. Our Gather Compact algorithm is built on the basic principle that well-connected components of modular robots can be transformed efficiently. Hence we iteratively increase the connectivity within a configuration, to finally arrive at a single solid xy-monotone component. We implemented Gather Compact and compared it experimentally to the in-place modification by Moreno and Sacristán [EuroCG 2020] of the Dumitrescu and Pach algorithm (MSDP). Our experiments show that Gather Compact consistently outperforms MSDP by a significant margin, on all types of square configurations.

READ FULL TEXT VIEW PDF
POST COMMENT

Comments

There are no comments yet.

Authors

page 3

page 6

page 7

page 8

page 9

page 10

page 11

page 14

Code Repositories

compacting-squares

Implementation of the Gather&Compact algorithm for modular robot reconfiguration


view repo
This week in AI

Get the week's most popular data science and artificial intelligence research sent straight to your inbox every Saturday.

1 Introduction

Modular self-reconfigurable robots [survey2007] consist of many identical modules that can move with respect to each other. In this way, modular robots can change shape and potentially adapt to different environments and purposes. One of the best-studied paradigms of modular robots is the sliding cube model [heterogeneous-03]. In this model, a robot configuration is a face-connected set of cubic modules on the cubic grid. The cubes can perform two types of moves, illustrated in two dimensions in Figure 1. First, a module can slide along two face-adjacent cubes to reach a face-adjacent empty grid cell.

Figure 1: Moves admitted by the sliding cube model: (a) slide, (b) convex transition.

Second, a module can make a convex transition around a module to end in a vertex-adjacent empty grid cell. For this second move to be feasible, also the grid cell (not occupied by ) face-adjacent to both the starting and the ending positions must be empty. There are several prototypes of modular robots that realize the sliding cube model in 2D [EMCube, Chiang01, Vertical98]. Units of multiple other prototypes, including expandable and contractible units [Crystal, Telecube] as well as large classes of modular robots [metamodule1, metamodule2], can be arranged into cubic meta-modules consisting of several units such that the meta-module can perform slide and convex transition moves.

There are several other well-studied models for modular robots, such as the pivoting cube model [heuristics-square, M-blocks]. This model strengthens the free-space requirements for each move and has also been realized by some existing prototypes. Akitaya et al. [pivoting-socg21] showed very recently that in the pivoting cube model in two dimensions it is NP-hard to decide if it is possible at all to reconfigure one configuration into the other. However, if one allows six squares in addition to the input configuration, then there is a worst-case optimal reconfiguration algorithm [musketeers]. Other models for squares relax the face-connectivity condition [nadia], restrict or enlarge the set of allowed moves [PSPACE-sliding-corners], or relax the free-space requirements [squeezing11]. In this paper we study the reconfiguration problem for the sliding cube model in two dimensions. Given two configuration of  squares, we want to compute a short sequence of moves that transforms one configuration into the other, while preserving edge-connectivity at all times. This implies that only those squares can be moved whose removal preserves the connectivity of the configuration. Dumitrescu and Pach [dumitrescu-pushing-squares-2006] described an reconfiguration algorithm which transforms any two configurations of  squares into each other using moves. This bound is worst-case optimal, since there are configurations of squares which require moves during the transformation. However, significantly fewer moves may be sufficient. In this paper we present the first universal input-sensitive algorithm for the reconfiguration problem. Our algorithm requires moves to transform one configuration into the other, where is the maximum perimeter of the respective bounding boxes. Furthermore, our algorithm is in-place: one square at a time is allowed to move through cells edge-adjacent to these bounding boxes, otherwise all squares stay inside.

Definitions and notations. Let be an edge-adjacent configuration of squares on the square grid and let be the edge-adjacency graph of . With slight abuse of notation we identify the squares and the nodes in the graph. A square is called a cut square if is disconnected. Otherwise, is called a stable square. A chunk is any inclusion-maximal set of squares in  bounded by (and including) a simple cycle in  of length at least (its boundary cycle), including any edge-adjacent squares with degree-1 in  (its loose squares). A link is a connected component of squares which are not in any chunk. A connector is a chunk square edge-adjacent to a square in a link or in another chunk. By definition a connector is always a cut square. The size of a chunk  is the number of squares contained in  (including its boundary cycle and any loose squares).

Figure 2a shows an example configuration with its chunks, links, connectors, and cut / stable squares marked. Note that a square can be part of two chunks simultaneously, in which case it must be a connector (for example, see the leftmost connector in Figure 2a). A chunk can contain both cut and stable stables.

Figure 2: (a) A configuration . (b) The component tree .

The component tree of has a vertex for each chunk or link and an edge iff the chunks / links represented by and  have edge-adjacent squares or share a square (in the case of two adjacent chunks). The component tree is rooted at the component that contains the leftmost square in the bottom row, the root square, of  (see Figure 2b). If a chunk is a leaf of then we call it a leaf chunk.

A hole in  is a finite maximal vertex-connected set of empty grid cells. The infinite vertex-connected set of empty grid cells is the outside. If a chunk  encloses a hole in , we say that is fragile. Otherwise, we say that is solid. The boundary of the configuration  is the set of squares which are vertex-connected to any grid cell on the outside. The boundary of a hole  is the set of squares vertex-connected to any grid cell in . Note that the boundary of a hole is edge-connected. We can construct the component tree in time by walking along the boundary of .

Consider now the bounding box of on the square grid. We refer to the bottom-most left-most grid cell inside as the origin. We say that is the perimeter of , and hence any square in can be connected to the origin by an -monotone path of at most squares.

When we use grid coordinates, we assume the usual directions (the -axis increases towards e and the -axis increases towards n). Let  be a grid cell. We use compass directions (n, ne, e, etc.) to indicate neighbors of  (so the n-neighbor of  is , etc.) Similarly, we indicate slide moves using compass directions (‘a w-move’) and convex transitions using a sequence of two compass directions (‘a ws-move’).

Algorithmic outline. Our algorithm is built on the basic principle that well-connected components of configurations can be transformed efficiently. Hence we iteratively increase the connectivity until we reach our desired configuration: an -monotone configuration inside bounding box . Specifically, we gather squares from the leaves of the component tree  until each leaf is a chunk of size at least . In Section 2 we explain how to grow chunks using at most moves per square which is added to a chunk. During this process, the final position of each square is chosen inside bounding box , but squares can move through the layer of grid cells adjacent to .

After gathering, all leafs are heavy chunks of size at least . Our goal is now to make each leaf chunk contain the origin, while ensuring that all squares remain part of their chunk. A heavy chunk contains sufficient squares to form a chunk that contains both its connector and the origin: we can connect the connector of with the origin by an -monotone path of at most squares; two such paths, which are disjoint, form a new boundary cycle for . We do not explicitly construct these two paths, but instead we compact the configuration by filling holes and using lexicographically monotone movement towards the origin for squares in heavy leaf chunks. In Section 3 we explain the details of the compaction algorithm and prove that it leaves us with a solid -monotone component.

During compaction each square in a leaf chunk makes only lexicographic monotone moves towards the origin while staying inside : s- and w-moves (slides), as well as sw-, ws-, nw-, and wn-moves (convex transitions). In some cases, a square in the leftmost column or bottom row can exit , and move along the bounding box to enter the same column/row again closer to the origin. This is the only time when a non-lexicographic monotone move is used, and every square can perform it at most times. Hence the total number of moves during compacting is .

When compacting, every (heavy) leaf chunk will eventually contain a square at the origin. This means that the whole configuration becomes a single chunk, as all leaves of the component tree have merged into a single component. Therefore, once compacting has finished we arrive at an -monotone configuration that fits inside . If at any point during this process the configuration becomes -monotone, then we simply stop. In particular, if the configuration is -monotone at the start, for example squares in only a single row or column, then we do not have to gather or compact, even though there are no heavy chunks. See the top row of Figure 3 for a visual impression of our algorithm.

In the special case that the input configuration contains less than squares, we first ensure that contains the origin and then execute the gathering and compaction steps as before. The number of moves is trivially bounded by , details can be found in Section 4. Furthermore, in Section 5 we explain how to convert any configuration into a different -monotone configuration with at most moves, where is the maximum perimeter of the bounding boxes of start and end configuration. Since all moves are reversible we can transform the start into the end configuration via this transformation.

Let and be two configurations of squares each, let and denote the perimeters of their respective bounding boxes, and let . We can transform into using at most moves while maintaining edge-connectivity at all times.

Proof.

For any two configurations and of squares each, we can apply gathering and compacting to find -monotone configurations in and in moves, for  and respectively. If we want to transform into , we first gather and compact into , transform into in moves, and proceed by reversing the sequence of steps for to get configuration . In Sections 2 and 3 and Appendix 5 we show that gathering, compacting and transforming -monotone configurations require the appropriate number of moves, such that the total number of moves is . ∎

Figure 3: A spiral configuration in a bounding box of size . (a) Gather&Compact: gathering done after moves; total moves. (b) MSDP [dumitrescu-pushing-squares-2006, flooding]: total moves.

Comparison with Dumitrescu and Pach. The algorithm by Dumitrescu and Pach [dumitrescu-pushing-squares-2006] constructs a canonical shape from both input configurations. In the original paper this canonical shape is a strip that grows to the right of a right-most square and hence, necessarily, their algorithm always requires move. Moreno and Sacristán [BachelorMoreno, flooding] modify Dumitrescu and Pach to be in-place; their canonical shape is a rectangle within the bounding box of the input. For either type of canonical shape the algorithm roughly proceeds as follows. If there is a square which is a leaf in , then the algorithm attempts to move this square along the boundary towards the canonical configuration. If this leaf square “gets stuck” on the way, and hence increases its connectivity, or if there is no leaf in the first place, then the algorithm identifies a 2-connected square on the outside of the configuration which it can move towards the canonical configuration. Hence, if configurations are tree-like (such as the spiral illustrated in Figure 3 left), then each square moves along all remaining squares, for a total of moves (see Figure 3 bottom row). However, the width and the height of this spiral configuration is . Our algorithm gathers squares from the end of the spiral and then compacts in a total of moves.

The in-place modification by Moreno and Sacristán of Dumitrescu and Pach (henceforth MSDP) has the potential to use few than moves in practice. In Section 6 we compare our Gather&Compact to MSDP experimentally; Gather&Compact consistently outperforms MSDP by a significant margin, on all types of square configurations. Our online material111https://alga.win.tue.nl/software/compacting-squares/ contains two movies which show the two algorithm side-by-side on the configurations depicted in Figures 3 and 13.

2 Gathering

In this section we show how to gather squares from the leaves of the component tree until we create a chunk of size at least that is a leaf of . In the following, let be a connector or a cut square in a link. By definition, lies on the boundary of . Since is a cut square, removing from results in at least two connected components. One of these components contains the root of . We say that the other (up to three) components are descendants of . Let  be the set of squares in the descendant components of . We say that the capacity of  is , and that light if its capacity is lower than  and heavy otherwise.

A light square  can be made a part of a chunk with a sequence of moves made by squares in . The final position  is within bounding box .

Proof.

First, observe that there are at most two empty cells (and ) neighboring , such that placing squares in them results in being part of a chunk component. Additionally, (and ) can be chosen in such a way that they lie inside bounding box : these cells must exist since always at least three neighboring cells are inside , unless is a single row/column and the configuration was already -monotone. If such cells are already occupied by squares then is already in a chunk. We argue that we can move squares from the descendant components of into these empty cells inside the bounding box with at most moves. Once this is accomplished, we repeat the process in the descendant components, for the next light square of maximal capacity, until no light squares remain in the component tree below the chunk containing .

Let be a subset of boundary squares in the descendants of of the subconfiguration . Select an arbitrary stable square . Such a square exists because of the following: if there is a link component in that is a leaf in the component tree, then its degree-1 node is stable; and if there is a chunk component in that is a leaf in the component tree, then an extremal square of the chunk in one of the ne, nw, se, sw directions is stable (only one of them can be a connector square).

Consider moving along the boundary of towards . Let be the set of cells that needs to cross to reach . If is empty, then we simply move to (see Figure 4a), and repeat the procedure for (if it exists). In this case, takes moves to get to .

Now consider the case where is not empty. Let be the first square in on the way from to ; let be the square in that is just before and edge-adjacent to . As is not part of the boundary along which the path from to is considered, it must be vertex-adjacent to a square that is on that part of the boundary.

Figure 4: Illustration to Lemma 2. Light square is shown in green; filling the cells and makes part of a chunk; a stable square (dark grey) moves towards along the boundary of the configuration. (a) reaches . (b) square (brown) belongs to a component outside of , moving to creates a chunk containing . (c) square  belongs to a component in , moving to creates a hole whose inner boundary will not be traversed again.

There can be two cases: either or . In the first case, moving to merges a component in with some component outside of (see Figure 4b). Thus, a chunk is created that contains , resulting in no longer being a light square.

In the second case, when , moving to creates a chunk within (see Figure 4c). In this case we select another arbitrary stable square in the new subconfiguration , and repeat the procedure. Observe that the empty squares traversed by are now part of a hole in the new chunk. Thus the path from to does not overlap with the path taken by . Let be the sequence of such stable squares chosen by our algorithm as candidates to be moved to . For any square , its path along the boundary to does not overlap with any of the cells traversed by all with . Thus, there is some such that either reaches , or it merges the components within and outside of into a chunk containing . The total number of the empty cells traversed by all squares () is .

We repeat the above procedure to fill . Note that the path taken by  (and ) may not always be inside bounding box . When this path exits , it will always stay adjacent to cells in , and hence it will use only the single row/column of cells adjacent to . ∎

Using the procedure described in the lemma above, we can iteratively reduce the number of light squares and obtain a component tree where all leaves are chunks of size at least .

There is a sequence of moves which ensures that all leaves in the component tree are chunks of size at least .

Proof.

By Lemma 2, we can make part of a chunk in moves, by moving cubes from the descendants of . Note that we create only new light squares, if removal of a square  breaks a cycle in . Thus, if new light squares are created, they are confined to .

We repeat the procedure iteratively selecting a light square of maximal capacity at every step. Overall, a square can be light at most once in the process. Thus, after moves no light squares remain, and all the leaves in the component tree are chunks of size at least . Note that, while the root square always has capacity , an adjacent square can have capacity , and the resulting chunk will be the root component, either because the root square is on the boundary cycle, or is a loose square. In case the adjacent square is too light, namely , then the root component may stay a link. ∎

3 Compacting

After the gathering phase, each leaf of the component tree  is a heavy chunk, that is, each leaf is a chunk of size at least . In this section, we describe how we compact the configuration in order to turn it into a left-aligned histogram containing the origin. Our procedure uses three types of moves: LM-moves, corner moves, and chain moves, which we discuss below. We iteratively apply any of these moves, as long as they are valid.

LM-moves. We say that a move is a lexicographically monotone move (LM-move for short) if it is either an s- or w-move (slides), or an sw-, ws-, nw-, or wn-move (convex transitions). Note that an LM-move will never move a square to the east. Squares can move to the north, but only when they also move to the west. Hence, if a square starts at coordinate , and we perform a series of LM-moves, it stays in the region .

Let  be a square in a heavy leaf chunk  of , and consider an LM-move made by . We say that this move is valid if stays inside bounding box , and all squares are still in a single chunk after the move. While compacting, we allow only valid LM-moves, that is, we allow each chunk to grow, but a chunk can never lose any squares.

Corner moves. LM-moves on their own are not necessarily sufficient to compact a chunk into a suitable left-aligned shape. For example, consider the configuration in Figure 5a, which does not admit any valid LM-moves. However, it has a concave corner that we can fill with two moves (see Figure 5b), to expand the chunk in that direction. Repeating such corner moves allows us to make the chunk in the example left-aligned.

Figure 5: (a) A configuration that does not admit LM-moves. For example, a nw-move (in red) of the top-right square is not valid! (b) Two slide moves expand the concave corner in sw direction.

We define corners of a chunk  with boundary cycle  as follows. A top corner (Figure 6a–d) is an empty cell with squares as n-, ne-, and e-neighbors. Similarly, a bottom corner (Figure 6e–h) is an empty cell with squares as s-, se-, and e-neighbors. Note that a corner can be either inside a hole in  (internal corner), or on the outside of  (external corner), and we treat both of these in the same way.

Figure 6: Empty squares shown in red are corners. The boundary cycle of the chunk is shown in orange. (a)–(d) Top corners; (e)–(h) bottom corners. Corners shown in (a) and (e) can be both external and internal. Corners shown in (b)–(d) and (f)–(h) can only be internal.

Let  be a top corner in  with neighbors as above. In the case where are consecutive squares in (Figure 6a), we can fill  by two slide moves: first move either or  into , and then move  into the cell left empty by the first move. We call this a top corner move. We can fill a bottom corner with consecutive (Figure 6e) in the same way, just mirrored vertically (a bottom corner move). Just like for LM-moves, we say that a corner move is valid if all squares are still in a single chunk after the corner move. Note that all corners where are not consecutive in (Figure 6b–d and 6f–h) do not allow valid corner moves, as and/or becomes a connector.

Chain moves. Besides LM- and corner moves, we need a special move to prevent getting stuck when no LM-moves are valid, because they have to leave bounding box . For example, some squares on the bottom row or leftmost column of  would be able to perform LM-moves, if they were situated in any other row/column of , as shown in Figure 7.

Figure 7: Examples of a chain moves: a square moves outside to the first empty cell closer to the origin. This may require a loose square to move as well.

A chain move is a series of moves that is started by such an LM-move that violates validity only by leaving bounding box . A chain move for a square in the bottom row of  requires an empty cell closer to the origin, and works as follows. Square must be able to perform an LM-move, more precisely an sw-move that is invalid only because it leaves . We want to place in this empty cell , unless it creates a link component, which happens only if the square on position is a loose square. We slide such a loose square upwards with a n-move, and identify the emptied cell as . Note that is again the closest empty cell in the bottom row, closer to the origin. We can then move to by performing an sw-move, a series of w-moves and finally a wn-move into . For a square in the leftmost column, the direction of all moves is mirrored in .

[] Let  be a leaf chunk that does not admit valid LM-moves, corner moves, or chain moves. Then boundary cycle of  outlines a left-aligned histogram, and is solid.

Proof.

We first show that cannot have holes. Suppose for the sake of contradiction that has a hole. Consider the topmost and the bottommost empty squares and of the rightmost column of empty squares of any hole in ( may be equal to ). By assumption, and  are not valid corners. First, if has a non-boundary n- or e-neighbor, then there is an LM-move. Similarly, if has a non-boundary e-neighbor, then there is an LM-move, but can have a non-boundary s-neighbor. Thus, assume that n- and e-neighbors of  and e-neighbors of  belong to .

As is a leaf chunk, it has at most one connector square. Consider the four cases for  (see Figure 6a–d), in which we identify the possible location of the connector square.

  1. Case in Figure 6a: If does not admit a valid top corner move, then its ne-neighbor must be a connector.

  2. Case in Figure 6b: Let be the ne-neighbor of , and be the e-neighbor of . As is part of the rightmost empty squares of any hole in , any part of extending further right must be solid. Hence the boundary can only traverse some of the squares in the same row and to the right of , make one step down, and then traverse the squares in the same row and to the right of (see Figure 8a). If at any point would go upwards from the row containing , then there would be a valid bottom corner move of type (a), since this part of is solid (see Figure 8b). Similarly, if would protrude downwards from the row containing , then there would be a valid top corner move of type (a) (see Figure 8c). This segment of between and does not allow a valid LM-move only if it contains the connector square.

    Figure 8: (a) Corner of type (b), with and as nw- and w neighbors of . Protruding upwards (b) or downwards (c) from the rows of and respectively leads to valid corner moves.
  3. Case in Figure 6c: Let have coordinates , then the empty square at is an external bottom corner, which contradicts our assumption.

  4. Case in Figure 6d: Similarly to case (b), the segment of between the ne- and the e-neighbors of must contain a connector square if there is no valid LM-move in it.

In all considered cases that did not contradict yet, if is a top corner inside , then the connector square must appear on the segment of between the ne- and e-neighbors of .

If we assume that the s-neighbor of is part of , then by repeating the same arguments as above for the bottom corner , and recalling that there can be only one connector square in a leaf chunk, we arrive at a contradiction. What remains is to show the same when the s-neighbor of is not part of boundary cycle .

First, assume there are no empty cells enclosed by in rows below . Therefore, in the rows below, LM-moves and chain moves will move squares in s-, sw- and w-direction, unless these moves remove squares from , or there is a connector, in which case we have again found a contradiction. Hence will occupy the column of in the rows below, and no longer occupy the column adjacent in e-direction, since the cubes in that column can perform valid LM-moves or chain moves. As a result, the s-neighbor of must be in , and hence we have found a contradiction.

Now assume that there are empty cells enclosed by , in rows below . Observe that these cells will be located in the same column as , since they cannot be located in e-direction by assumption, and they cannot be more w either, as then we could do LM-moves in w-direction to move the empty cells in e-direction. If does not enter the column of between and the lower empty cells, then there are LM-moves, moving the squares below in s-direction. However, if passes twice between the empty cells, then one of three following cases applies.

  1. A connector is located on this part of , and as before this will result in a contradiction.

  2. LM-moves and chain moves will move squares below , either until the s-neighbor of is part of , which contradicts our assumption;

  3. or until the column of fills up completely between and the empty squares enclosed by . These squares can be moved in s-direction by valid LM-moves, and hence we also find a contradiction.

Thus there can be no holes in , since all cases are lead to a contradiction.

To show that outlines a left-aligned histogram, we observe that any external corner admits a valid corner move. Indeed, none of the three neighbors of the empty square can be a loose square, as those would admit LM-moves. Furthermore, we proved that does not have a hole and hence a boundary square cannot be a cut square for a component on the inside of . Finally, since is a leaf square, the only cut vertex in for a component on the outside is its connector. Since only one out of and can be this connector, we can perform a valid corner move starting with the other (non-connector) square. Therefore, by our assumption that there are no corner moves in , there cannot be external corners, and thus outlines the shape of a left-aligned histogram (see Figure 10). ∎

Figure 9: Cycle of the chunk outlines a left-aligned histogram.
Figure 10: Result of the compaction phase. The chunk forms a double- shape, with one or two loose squares (orange).

[] Let the component tree have more than one leaf, and let  be a leaf chunk that does not contain the origin and does not admit valid LM-moves, corner moves, or chain moves. Then the boundary cycle of  outlines a double- (see Figure 10).

Proof.

Let be the set of loose squares in and denote as . By Lemma 7, is a left-aligned histogram. Note that cannot contain a loose square in its topmost row. Otherwise, such a loose square would have a valid w-, ws-move, or a vertical chain move if it has an s-neighbor in , or it would have an s-move if it has a w- or e-neighbor in .

We repeatedly apply the following argument to show that certain configurations lead to a contradiction. Consider rows of from top to bottom for some and . Since is a left-aligned histogram, all rows must be left aligned. Consider the first row in the sequence such that and , where can be an empty row if is the bottom row of . The rightmost square in is either a connector, it has a valid sw-move, or it has a horizontal chain move if is the bottom row of . Both latter cases contradict our assumption that there are no valid LM- or chain moves.

Consider the top two rows and of , and consider the position of the connector square of . It lies on , and thus in . First, assume that is in row of , with . Due to two-connectivity of , the top row of has at least two squares. If has strictly more than two squares, then consider the rows . By applying the argument from above, either there is a valid sw-move in row ( has fewer squares than ), or all rows have at least as many squares as . In the latter case, the leftmost square in can perform a valid ws-move or a valid chain move. If has exactly two squares, its leftmost square always has either a valid ws-move or a vertical chain move.

Next, assume that is in or , but it is not the rightmost square in those rows. We apply the above argument to find a row that extends further to the right than the row below it, and thus to find a valid LM- or chain move, unless all rows have width two. These rows cannot have less than two squares in them, since that would contradict the definition of . In case all rows have width two, the left square on the bottom row has a valid wn-move.

Finally, consider the case when is the rightmost square of or . If all the remaining rows have length two, then the lemma holds. Thus, let , for , be the lowest row with . By the same argument as before, this row has a valid LM- or chain move. ∎

Configuration stays in its original bounding box and is -monotone, once compaction is completed and no valid LM-, corner, and chain moves are possible.

Proof.

In the compaction phase we iteratively apply LM-, corner, and chain moves on the configuration in which each leaf chunk contains at least squares. After compaction, a leaf chunk can either contain the origin, or not. Consider such a chunk that does not contain the origin. By Lemma 10, the cycle of will outline a double-, and since is a leaf, it has at least squares. During gathering and compacting, always the initial bounding box  of the configuration, if they ever move outside it, and hence the connector of will also be inside . Since is the perimeter of , any double- of squares, completely inside , will reach the bottom left corner of . Thus, must contain the origin, as one of the top two rows connects to the connector inside . As a result, every leaf chunk contains the origin at some point during compaction.

Once every leaf chunk contains the origin, the whole configuration is one single chunk: all leafs of the component tree form a single component now. Continuing the compacting hence results in a left-aligned histogram, by Lemma 7. Finally consider the topmost row of this histogram that is longer than the row below it. During compaction, the rightmost cube of  can perform a valid LM-move, namely a s- or sw-move to the row below it. Note that these moves cannot put cubes outside of the original bounding box  of . Thus, once the compacting phase is completed, the configuration is -monotone inside . ∎

There is a sequence of moves which reconfigures a configuration in which all leaves are chunks of size at least to an -monotone configuration.

Proof.

Using Lemmata 710, and 3, we can transform a configuration , in which all leaves are chunks of size at least , to an -monotone configuration. Let be a square in . We assign to  the score , and let . Each LM-, and bottom corner move performed in  decreases  by at least , while every top corner move decreases  by two. Initially, , so the total number of LM- and corner moves is also at most . Every square  is involved in at most chain moves, since each chain move places closer to the origin in the bottom row/leftmost column. Furthermore, every chain move adds at most one additional move for a loose square, which increases the above score by at most two, hence the total number of moves as a result of chain moves is also at most . ∎

4 Light configurations

We say that a configuration  is light, if it consists of fewer than squares, where is the perimeter of the bounding box of . Our algorithm, as explained above, cannot directly handle such configurations if does not contain the origin: there are too few squares to guarantee that compacting will always result in a chunk that contains the origin. However, we can use a simple preprocessing step to ensure that will contain the origin.

For a light configuration  which does not contain the origin, we select a stable square as in the gathering phase: a stable square in a link, or an extremal stable square in a chunk. We iteratively move this stable square along the boundary of  to the empty cell  that is the w-neighbor of the root square. We iteratively continue to do so until contains the origin. Note that must necessarily be empty.

At this point, we can simply gather and compact and arrive at an -monotone configuration, for the following reason. The gathering phase works as in the main text, since we can iteratively apply Lemma 2 on the light square closest to the root (which can be the root itself), to get a single chunk. As the root square is located at the origin, and we never move the root during gathering, we get a chunk containing the origin. Similarly, in the compaction phase, this chunk will become a solid left-aligned histogram by Lemma 7. Since it already contains the origin, and we do only monotone moves towards the origin, this chunk still contains the origin. Finally the topmost row of this histogram, that is longer than the row below it, still has valid LM-moves. These moves will happen during compacting, hence the result is an -monotone configuration.

There are at most empty cells to the left of the root. We fill each of these cells by by walking along the boundary of . Since configuration  consists of less than squares, this requires at most moves. Both gathering and compacting take moves, as proven in the main text, so including the preprocessing, we still arrive at a bound of for the number of moves.

5 Transforming compact -monotone configurations

After performing the operations described in the previous sections we obtain a configuration that is an -monotone staircase. However, this staircase might not be regular and it might contain several stairs of height greater than one as well as a long tread on the second stair (due to the presence of a loose square in the bottom row). Examples are shown in Fig. 11 (left and right). In this section we show how to transform between two such -monotone staircases. To precisely define this transformation we introduce a potential function. Given a square or an empty grid cell with non-negative coordinates , its potential is the sum of the two coordinates. We assume that in the configuration every square has non-negative coordinates. In every step of the transformation we move a misplaced cube with maximum potential to an empty grid cell with minimum potential that should become occupied.

Figure 11: Transforming between two -monotone configurations. The dashed lines go through squares and cells with the same potential.

We can reconfigure -monotone configurations and of squares, with perimeters and of their respective bounding boxes in moves, with .

Proof.

Let be the current configuration, let be the bottommost square in with maximum potential, and let be the topmost empty grid cell with minimum potential that is occupied in . The idea is to iteratively move to cell until . We first show that such a step maintains -monotonicity. Removing cannot break this property: by definition of potential and since is -monotone, cannot have neighbors in in the e, ne, and n directions. Moreover, if it has a nw-neighbor then, by -monotonicity of , it has a w-neighbor too. Analogously, adding a square in also maintains -monotonicity. By the definition of potential and since is -monotone, the cells neighboring in the w, sw, and s directions must be occupied if they are inside the bounding box of . Moreover, -monotonicity of  guarantees that the cells neighboring in the n, ne, and e directions are empty.

In every step we move a square from a position occupied in to a position occupied in . In every step the sum of the width and height of the bounding box of the configuration is . Since moving along the boundary of this -monotone configuration takes at most moves and no cube is moved more than once, in total it takes moves to reconfigure. ∎

6 Experiments

We experimentally compared our Gather&Compact algorithm to the JavaScript implementation222https://dccg.upc.edu/people/vera/TFM-TFG/Flooding/ of the in-place modification by Moreno and Sacristán [BachelorMoreno, flooding] of the Dumitrescu and Pach [dumitrescu-pushing-squares-2006] algorithm, which we refer to as MSDP in the remainder of this section. The original algorithm by Dumitrescu and Pach always requires moves, since it builds a horizontal line to the right of a rightmost square as canonical shape. The in-place modification of Moreno and Sacristán has the potential to be more efficient in practice, since it builds a rectangle within the bounding box of the input.

We captured the output (sequence of moves) of MSDP and reran the reconfiguration sequences in our tool, to be able to verify movement sequences, count moves, and generate figures. Doing so, we discovered that MDSP was occasionally executing illegal moves, see Appendix A for details and for our corresponding adaptations. Some of these issues could be traced to the same origin: MSDP is breaking convex transitions into two separate moves and sometimes acts on the illegal intermediate state. The number of moves we report in Table 6 counts one move both for convex transitions and for slides; hence the numbers can be lower than the numbers Moreno and Sacristán report. However, our adaptations do replace illegal moves with the corresponding (and generally longer) legal movement sequences, and hence the number of moves can also be higher than those they report.

Figure 12: Example input instances on a grid: density (a) ; (b) ; (c) .

We use the same square grids of sizes , , , , for our experiments. The data sets for MSDP were created by hand and are not available any more.333V. Sacristán, personal communication, April 2021. Based on the figures in their publications, we attempted to create data sets of the same nature by starting with a fully filled square grid and then removing varying percentages of squares while keeping the configuration connected. We arrived at three densities, namely (, , ), which arguably capture the different types of inputs well (see Figure 12). Note that here we always have . For both algorithms we count moves until they reach their respective canonical configurations: a rectangle with a partial top row on the bottom of the bounding box for MSDP and a solid -monotone configuration within the bounding box for our Gather&Compact. The compaction step of Gather&Compact does not rely on any particular order of the available valid moves. In our implementation we prioritize downwards LM-moves and top corner moves, by decreasing distance to the origin. Our code for Gather&Compact, the generation of the input instances, as well as the adapted version of MSDP, are available online444https://github.com/tue-alga/compacting-squares/.

X[0.33,c] X[r]X[r]X[0.9,r] X[1.16,r]X[1,r]X[r] & Gather&Compact & MSDP
[c] & & & & & &
& 237 & 156 & 95 & 502 & 427 & 233
& 5.395 & 4.188 & 2.529 & 28.759 & 18.447 & 10.027
& 25.916 & 20.024 & 12.124 & 193.390 & 116.431 & 61.617
& 77.745 & 60.516 & 36.395 & 638.847 & 344.529 & 235.413
& 150.666 & 118.232 & 69.488 & 1.318.232 & 743.133 & 513.113

Table 1: The number of moves for Gather&Compact and MSDP on various grid sizes (, such that ) and densities (in % of

). Averages and standard deviations (in % of average) over 10 randomly generated instances are shown.

Table 6 summarizes our results and Figure 13 shows snapshots for both algorithms on one of the instances. We can observe that Gather&Compact always uses significantly fewer moves than MSDP, even on high density instances where most squares are already in place. This can directly be attributed to the fact that MSDP walks squares along the boundary of the configuration, while Gather&Compact shifts squares locally into better position. Figure 13(b) gives a hint to this behavior at move 600 where one can observe a square on its way along the bottom boundary; in the online materialFootnote 1 we also included a movie of the execution of both algorithms which shows this more clearly.

Figure 13: Execution of the two algorithms on one of the input instances for grid size , density . (a) Gather&Compact; (b) MSDP.

7 Conclusion

We introduced the first universal in-place input-sensitive algorithm to solve the reconfiguration problem for the sliding cube model in two dimensions. Our Gather&Compact algorithm is input-sensitive with respect to the size of the bounding box of the start and end configurations. We experimentally establish that Gather&Compact not only improves the existing theoretical bounds, but that it also leads to significantly fewer moves in practice.

However, there may still be room to improve on the bound in this paper. Specifically, it may be possible to improve the hidden constants, by gathering to leaf chucks of size instead of . These chunks still have enough squares to reach the origin, but they have to give up 2-connectivity to do so, and hence the algorithm becomes more complex. Once all leaves create an -monotone path to the origin, the configuration again consists of a single chunk, and thus the remaining parts of our algorithm still apply.

Finally, extending our algorithm to three dimensions is currently work in progress. While well-connected components can also be transformed more efficiently in 3D, the algorithm may require higher degrees of connectivity than 2-connectivity.

References

Appendix A Illegal moves in MSDP

The checks implemented in our tool detected illegal moves executed by the implementation of MSDP available online.Footnote 2 These illegal moves come in different flavours (see Figure 14):

  1. illegal convex transitions around an empty grid position,

  2. truncated convex transitions (can be also thought of as illegal slides) in which the final position is the intermediate cell of a convex transition,

  3. illegal slides in which one of the two cells along which the square slides is empty, and

  4. illegal move sequences in which a convex transition is broken into two different moves and a new move starts on the illegal intermediate state. This can be also thought of as an illegal slide.

The illustrations in Figure 14 are screenshots from the online tool. They can be reproduced by drawing the configuration on the left in the online tool and trying to reconfigure it into any other connected configuration with the same number of squares.

To remove illegal moves, we adapted the movement sequences of MSDP as follows. First of all, we removed all positions which correspond to the intermediate cell occupied in a (valid) convex transition. We then try to find valid movement sub-sequences connecting any two consecutive positions. However, some positions of the movement sequence given by the MSDP implementation are simply not reachable by a valid sequence of moves, such as the yellow cell in Figure 14 (a). In those cases, we delete these unreachable positions. All the remaining positions of the movement sequence can be connected using valid sub-sequences; we find those by computing the shortest sequence of moves between subsequent positions. Note that in all cases we encountered, the end position of a move sequence was always reachable from the start position.

(a) Illegal convex transition around an empty cell.
(b) Illegal slide/truncated convex transition. The red cell is an intermediate position in a valid convex transition, but the move sequence cannot end here.
(c) Illegal slide: the green square cannot directly slide to the adjacent yellow cell.
(d) Illegal move sequence triggered by convex transition executed as two moves.
Figure 14: Examples of illegal move sequences found in the implementation of MSDP.Footnote 2 In each figure, the starting configuration is shown on the left. On the right, the square marked in green is moved to the position marked in red by moving through the positions marked in yellow. The illustrations are screenshots from the online tool.

7 Conclusion

We introduced the first universal in-place input-sensitive algorithm to solve the reconfiguration problem for the sliding cube model in two dimensions. Our Gather&Compact algorithm is input-sensitive with respect to the size of the bounding box of the start and end configurations. We experimentally establish that Gather&Compact not only improves the existing theoretical bounds, but that it also leads to significantly fewer moves in practice.

However, there may still be room to improve on the bound in this paper. Specifically, it may be possible to improve the hidden constants, by gathering to leaf chucks of size instead of . These chunks still have enough squares to reach the origin, but they have to give up 2-connectivity to do so, and hence the algorithm becomes more complex. Once all leaves create an -monotone path to the origin, the configuration again consists of a single chunk, and thus the remaining parts of our algorithm still apply.

Finally, extending our algorithm to three dimensions is currently work in progress. While well-connected components can also be transformed more efficiently in 3D, the algorithm may require higher degrees of connectivity than 2-connectivity.

References

Appendix A Illegal moves in MSDP

The checks implemented in our tool detected illegal moves executed by the implementation of MSDP available online.Footnote 2 These illegal moves come in different flavours (see Figure 14):

  1. illegal convex transitions around an empty grid position,

  2. truncated convex transitions (can be also thought of as illegal slides) in which the final position is the intermediate cell of a convex transition,

  3. illegal slides in which one of the two cells along which the square slides is empty, and

  4. illegal move sequences in which a convex transition is broken into two different moves and a new move starts on the illegal intermediate state. This can be also thought of as an illegal slide.

The illustrations in Figure 14 are screenshots from the online tool. They can be reproduced by drawing the configuration on the left in the online tool and trying to reconfigure it into any other connected configuration with the same number of squares.

To remove illegal moves, we adapted the movement sequences of MSDP as follows. First of all, we removed all positions which correspond to the intermediate cell occupied in a (valid) convex transition. We then try to find valid movement sub-sequences connecting any two consecutive positions. However, some positions of the movement sequence given by the MSDP implementation are simply not reachable by a valid sequence of moves, such as the yellow cell in Figure 14 (a). In those cases, we delete these unreachable positions. All the remaining positions of the movement sequence can be connected using valid sub-sequences; we find those by computing the shortest sequence of moves between subsequent positions. Note that in all cases we encountered, the end position of a move sequence was always reachable from the start position.

(a) Illegal convex transition around an empty cell.
(b) Illegal slide/truncated convex transition. The red cell is an intermediate position in a valid convex transition, but the move sequence cannot end here.
(c) Illegal slide: the green square cannot directly slide to the adjacent yellow cell.
(d) Illegal move sequence triggered by convex transition executed as two moves.
Figure 14: Examples of illegal move sequences found in the implementation of MSDP.Footnote 2 In each figure, the starting configuration is shown on the left. On the right, the square marked in green is moved to the position marked in red by moving through the positions marked in yellow. The illustrations are screenshots from the online tool.

References

Appendix A Illegal moves in MSDP

The checks implemented in our tool detected illegal moves executed by the implementation of MSDP available online.Footnote 2 These illegal moves come in different flavours (see Figure 14):

  1. illegal convex transitions around an empty grid position,

  2. truncated convex transitions (can be also thought of as illegal slides) in which the final position is the intermediate cell of a convex transition,

  3. illegal slides in which one of the two cells along which the square slides is empty, and

  4. illegal move sequences in which a convex transition is broken into two different moves and a new move starts on the illegal intermediate state. This can be also thought of as an illegal slide.

The illustrations in Figure 14 are screenshots from the online tool. They can be reproduced by drawing the configuration on the left in the online tool and trying to reconfigure it into any other connected configuration with the same number of squares.

To remove illegal moves, we adapted the movement sequences of MSDP as follows. First of all, we removed all positions which correspond to the intermediate cell occupied in a (valid) convex transition. We then try to find valid movement sub-sequences connecting any two consecutive positions. However, some positions of the movement sequence given by the MSDP implementation are simply not reachable by a valid sequence of moves, such as the yellow cell in Figure 14 (a). In those cases, we delete these unreachable positions. All the remaining positions of the movement sequence can be connected using valid sub-sequences; we find those by computing the shortest sequence of moves between subsequent positions. Note that in all cases we encountered, the end position of a move sequence was always reachable from the start position.

(a) Illegal convex transition around an empty cell.
(b) Illegal slide/truncated convex transition. The red cell is an intermediate position in a valid convex transition, but the move sequence cannot end here.
(c) Illegal slide: the green square cannot directly slide to the adjacent yellow cell.
(d) Illegal move sequence triggered by convex transition executed as two moves.
Figure 14: Examples of illegal move sequences found in the implementation of MSDP.Footnote 2 In each figure, the starting configuration is shown on the left. On the right, the square marked in green is moved to the position marked in red by moving through the positions marked in yellow. The illustrations are screenshots from the online tool.