Building a Nest by an Automaton

A robot modeled as a deterministic finite automaton has to build a structure from material available to it. The robot navigates in the infinite oriented grid \documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$${\mathbb {Z}} \times {\mathbb {Z}}$$\end{document}Z×Z. Some cells of the grid are full (contain a brick) and others are empty. The subgraph of the grid induced by full cells, called the shape, is initially connected. The (Manhattan) distance between the furthest cells of the shape is called its span. The robot starts at a full cell. It can carry at most one brick at a time. At each step it can pick a brick from a full cell, move to an adjacent cell and drop a brick at an empty cell. The aim of the robot is to construct the most compact possible structure composed of all bricks, i.e., a nest. That is, the robot has to move all bricks in such a way that the span of the resulting shape be the smallest. Our main result is the design of a deterministic finite automaton that accomplishes this task and subsequently stops, for every initially connected shape, in time \documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$O(sn)$$\end{document}O(sn), where s is the span of the initial shape and \documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$n$$\end{document}n is the number of bricks. We show that this complexity is optimal.


Introduction
The problem.A mobile agent (robot) modeled as a deterministic finite automaton has to build a structure from material available to it.The robot navigates in the infinite oriented grid Z × Z represented as the set of unit square cells in the two-dimensional plane, with all cell sides vertical or horizontal.The robot has a compass enabling it to move from a currently occupied cell to one of the four cells (to the North, East, South, West) adjacent to it.Some cells of the grid contain a brick, i.e., are full, other cells are empty.The subgraph of the grid induced by full cells, called the field, is initially connected.The (Manhattan) distance between the farthest cells of the field is called its span.Notice that the span of any current field may be much smaller than its diameter as a subgraph of the grid.In fact, this diameter may be sometimes undefined, if the field becomes disconnected.The robot starts at a full cell.It can carry at most one brick at a time.At each step, the robot can pick a brick from the currently occupied full cell (if it does not carry any brick at this time), moves to an adjacent cell, and can drop a brick at the currently occupied empty cell (if it carries a brick).The robot has no a priori knowledge of the initial field, of its span or of the number of bricks.
The aim of the robot is to construct the most compact possible structure composed of all bricks, i.e., a nest.That is, the robot has to move all bricks in such a way that the span of the resulting field be the smallest.The above task has many real applications.In the natural world, animals use material scattered in a territory (pieces of wood, small branches, leaves) to build a nest, and minimizing the span is important to better protect it.A mobile robot may be used to clean a territory littered by hazardous material, in which case minimizing the span of the resulting placement of contaminated pieces facilitates subsequent decontamination.A more mundane example is the everyday task of sweeping the floor, whose aim is to gather all trash in a small space and then get rid of it.Our results.Our main result is the design of a deterministic finite automaton that accomplishes the task of building a nest and subsequently stops, for every initially connected field, in time O(sz), where s is the span of the initial field and z is the number of bricks.The time is defined as the number of moves of the robot.We show that this complexity is optimal.
The essence of our nest building algorithm is to instruct the robot to make a series of trips to get consecutive bricks, one at a time, and carry them to some designated compact area.This approach ensures the optimal complexity.(In order to show where the problem is, we also sketch a much simpler algorithm that uses another approach but has significantly larger complexity).There are two major difficulties to carry out this plan.The first is that the span of the initial field may be much larger than the memory of the robot, and hence the robot that already put several bricks in a compact area and goes for the next brick cannot remember the way back to the area where it started building.Thus we need to prepare the way, so that the robot can recognize the backtrack path locally at each decision point.The second problem is that, while we temporarily disconnect the field during the execution of the algorithm, special care has to be taken so that the connected components of intermediary fields be close to each other, to prevent the robot from getting lost in large empty spaces.
To the best of our knowledge, the task of constructing structures from available material using an automaton, has never been studied before in the algorithmic setting.It is interesting to compare this task to that of exploration of mazes by automata, that is a classic topic with over 50 years of history (see the section "Related work").It follows from the result of Budach [9] (translated to our terminology) that if an automaton can only navigate in the field without moving bricks then it cannot explore all connected fields, even without the stop requirement, i.e., it cannot even see all bricks.By contrast, it follows from our result that the ability of moving bricks enables the automaton not only to see all bricks but to build a potentially useful structure using all of them and stop, and to accomplish all of that with optimal complexity.The model.We consider the infinite oriented grid Z × Z represented as the set of unit square cells tiling the two-dimensional plane, with all cell sides vertical or horizontal.Each cell has 4 adjacent cells, North, East, South and West of it.Some cells of the grid contain a brick, i.e., are full, other cells are empty.The subgraph of the grid induced by full cells is initially connected.At each step of the algorithm this subgraph can change, due to the actions of the robot, described below.At each step, the subgraph induced by the full cells is called the current field.Any maximal connected subgraph of the current field is called a component.Throughout the paper, the distance between two cells (x, y) and (x , y ) of the grid is the Manhattan distance between them, i.e., |x − x | + |y − y |.The number of cells of a field is called its size, and the distance between two farthest cells of a field is called its span.A nest of size z is a field that has the minimum span among all fields of size z.
We are given a mobile entity (robot) starting in some cell of the initial field and traveling in the grid.The robot has a priori no knowledge of the field, of its size or of its span.The robot is formalized as a finite deterministic Mealy automaton R = (X, Y, §, δ, λ, S 0 , S f ).X = {e, f } × {l, h} is the input alphabet, Y = {N, E, S, W } × {e, f } × {l, h} is the output alphabet.§ is a finite set of states with two special states: S 0 called initial and S f called final.δ : § × X → § is the state transition function, and λ : § × X → Y is the output function.
The meaning of the input and output symbols is the following.At each step of its functioning, the robot is at some cell of the grid and has some weight: it is either light, denoted by l (does not carry a brick) or heavy, denoted by h (carries a brick).Moreover, the current cell is either empty, denoted by e or full, denoted by f .The input x ∈ {e, f } × {l, h} gives the automaton information about these facts.The robot is in some state S. Given this state and the input x, the robot outputs the symbol λ(x, S) ∈ {N, E, S, W } × {e, f } × {l, h} with the following meaning.The first term indicates the adjacent cell to which the robot moves: North, East, South or West of the current cell.The second term determines whether the robot leaves the current cell empty or full, and the third term indicates whether the robot transits as heavy or as light to the adjacent cell.Since the robot can only either leave the current cell intact and not change its own weight, or pick a brick from a full cell leaving it empty (in case when the robot was previously light), or drop a brick on an empty cell leaving it full (in case when the robot was previously heavy), we have the following restrictions on the possible values of the output function λ: λ(S, e, l) must be (•, e, l) λ(S, e, h) must be either (•, e, h) or (•, f, l) λ(S, f, l) must be either (•, f, l) or (•, e, h) λ(S, f, h) must be (•, f, h) Seeing the input symbol x and being in a current state S, the robot makes the changes indicated by the output function (it goes to the indicated adjacent cell, possibly changes the filling of the current cell as indicated and possibly changes its own weight as indicated), and transits to state δ(x, S).The robot starts light in a full cell in the initial state S 0 (hence its initial input symbol is (f, l)) and terminates its action in the final state S f .Related work.Problems concerning exploration and navigation performed by mobile agents or robots in an unknown environment have been studied for many years (cf.[21,26]).The relevant literature can be divided into two parts, according to the environment where the robots operate: it can be either a geometric terrain, possibly with obstacles, or a network modeled as a graph in which the robot moves along edges.
In the geometric context, a closely related problem is that of pattern formation [12,15,29].Robots, modeled as points freely moving in the plane have to arrange themselves to form a pattern given as input.This task has been mostly studied in the context of asynchronous oblivious robots having full visibility of other robots positions.
The graph setting can be further specified in two different ways.In [1,3,4,13,20] the robot explores strongly connected directed graphs and it can move only in the tail-to-head direction of an edge, not vice-versa.In [2,7,9,14,16,17,25,27] the explored graph is undirected and the robot can traverse edges in both directions.Graph exploration scenarios can be also classified in another important way.It is either assumed that nodes of the graph have unique labels which the robot can recognize (as in, e.g., [13,17,25]), or it is assumed that nodes are anonymous (as in, e.g., [3,4,9,10,27]).In our case, we work with the infinite anonymous grid, hence it is an undirected anonymous graph scenario.The efficiency measure adopted in papers dealing with graph exploration is either the completion time of this task, measured by the number of edge traversals, (cf., e.g., [25]), or the memory size of the robot, measured either in bits or by the number of states of the finite automaton modeling the robot (cf., e.g., [14,19,20]).We are not concerned with minimizing the memory size but we assume that this memory is finite.However we want to minimize the time of our construction task.
The capability of a robot to explore anonymous undirected graphs has been studied in, e.g., [5,9,14,19,23,27].In particular, it was shown in [27] that no finite automaton can explore all cubic planar graphs (in fact no finite set of finite automata can cooperatively perform this task).Budach [9] proved that a single automaton cannot explore all mazes (that we call connected fields in this paper).Hoffmann [22] proved that one pebble does not help to do it.By contrast, Blum and Kozen [5] showed that this task can be accomplished by two cooperating automata or by a single automaton with two pebbles.The size of port-labeled graphs which cannot be explored by a given robot was investigated in [19].
Recently a lot of attention has been devoted to the problem of searching for a target hidden in the infinite anonymous oriented grid by cooperating agents modeled as either deterministic or probabilistic automata.Such agents are sometimes called ants.It was shown in [18] that 3 randomized or 4 deterministic automata can accomplish this task.Then matching lower bounds were proved: the authors of [11] showed that 2 randomized automata are not enough for target searching in the grid, and the authors of [8] proved that 3 deterministic automata are not enough for this task.Searching for a target in the infinite grid with obstacles was considered in [24].
Our present paper adopts the same model of environment as the above papers, i.e., the infinite anonymous oriented grid.However the task we study is different: instead of searching for a target, the robot has to build some structure from the available material.To the best of our knowledge, such construction tasks performed by automata have never been studied previously in the algorithmic setting.

Terminology and preliminaries
In the description and analysis of our algorithm we will categorize full cells.A full cell is said to be a border cell if it is adjacent to an empty cell.A full cell that has only one full adjacent cell is called a leaf.A full cell is called special, if it is either a leaf, or has at least two full cells adjacent to it, sharing a corner.
A finite deterministic automaton may remember a constant number of bits by encoding them in its states.We will use this fact to define several simple procedures and simplifications that we use in the sequel.The first simplification is as follows.We formulate the actions of the robot based on the configuration of bricks in its neighborhood.More precisely, at any step, the robot knows whether each cell at distance at most r = 8 from its current cell is full or empty.This can be achieved by performing a bounded local exploration with return, after each move of the robot.
We will use the notion of current orientation of the robot.At the beginning of its navigation, the robot goes in one of the four cardinal directions.Then its orientation is determined in one of the two ways: either by its last step (North, East, South or West) or by a turn: we say that the robot turns left (respectively right) meaning that it changes its orientation in the appropriate way while remaining at the same cell.Clearly the robot can remember its orientation, using its states.We refer to cardinal directions with respect to this current orientation.Thus, e.g., if the robot is oriented East then we say that its adjacent North (resp.East, South or West) cell is left (resp.in front, right, back ) of it.
Whenever we say that the robot located at a cell c and not carrying a brick brings a brick from a full cell c to c we mean that the robot moves from c to c , picks the brick from c , moves back to c and restores its original orientation.Whenever we say that the robot located at a cell c and carrying a brick places it at an empty cell c we mean that the robot moves from c to c , drops the brick at c , moves back to c and restores its original orientation.
Whenever the robot selects a cell according to some condition that is fulfilled by more than one cell, the robot selects the cell that is minimal with respect to the following total order ≺ on the set of all cells.For cells c = (x, y) and c = (x , y ), c ≺ c holds if and only if either y < y , or y = y and x ≤ x .We denote by |S| the number of cells in a sequence or a set S of cells.
We define a disc of radius r ≥ 0 with center c to be the set of all cells at distance at most r from c, see Fig. 1.A disc of radius r contains z r = 2(1+3+5+• • •+(2r−1))+(2r+1) = 2r 2 +2r+1 cells and has span 2r.A rough disc of size z, where z r ≤ z < z r+1 is defined as follows.If z = z r , then the rough disc is the disc of radius r.Otherwise, let F be the set of cells not belonging to the disc D of radius r but adjacent to some cell of it.Add to D exactly z − z r cells belonging to F , starting from the North neighbor of the East-most cell of D and going counterclockwise.If z r < z ≤ z r + 2r + 2 then the rough disc of size z has span 2r + 1 and if z r + 2r + 2 < z < z r+1 then the rough disc of size z has span 2r + 2, the same as the disc of radius r + 1 that has size z r+1 .This implies that C s has the following property: each border cell of C s is adjacent to two or three empty cells.Define an extremity to be an East-most, West-most, North-most or South-most cell of a field.Thus, either there exists a single East-most (respectively West-most, North-most or South-most) extremity, or there are two adjacent such extremities.Moreover, each border cell of C s that is not an extremity does not have an adjacent border cell.(Intuitively, the border is composed of "staircases").Denote by F s the class of fields with the above two properties.Note that C s which has maximum size among fields of span s belongs to F s .
We will compute the number of cells in any field C in the class F s .Partition the cells of C into rows R 1 , . . ., R t counted from North to South.We have proved that |R 1 | ∈ {1, 2} and |R l | ∈ {1, 2} because these sets consist of extremities only.We first consider the case when there is a total of four extremities in C. Thus, in particular, |R 1 | = |R t | = 1.(Note that, in the considered case, s must be even).Let j be the maximal index such that |R j | < |R j+1 | and let j be the minimal index such that It follows that for a field of maximum size in F s that has a total of four extremities, the index j must be as large as possible, i.e., j = s/2.Hence such a field is a disc of span s, i.e., of radius r = s/2.Now consider all other fields in the class F s , i.e., those in which some of the extremities are pairs of adjacent cells.Every such field F can be obtained from a field F in F s−1 ∪ F s−2 with exactly four extremities, by adding bricks in some cells adjacent to the border of F .The number of cells in each such field F equals |F | + c 1 x + c 2 y for some nonnegative constants c 1 and c 2 , where x is the span of F and y is the index j in the preceding paragraph, for the field F .Consider a field F of maximum size among all fields in F s .Hence the corresponding field F has maximum size among all fields in F s−1 or in F s−2 that have exactly four extremities.As proved above, such a field F must be a disc.Denote by h(s) the maximum size of a field in F s .In particular we have that h(s) = max{z s/2 , z (s−2)/2 + 2s − 1} = z s/2 for even s, and h(s) = z (s−1)/2 + s for odd s.
Let z be such that z r ≤ z < z r+1 and consider a nest N of size z.Denote its span by s z .We have three cases depending on the possible values of z.
First suppose that z = z r .We have s z ≥ 2r because otherwise, for a maximum-size field The nests built by our automaton will be discs or rough discs, depending on the number of available bricks.The following proposition shows that the complexity of our nest-building algorithm is optimal, regardless of the relation between the size of the initial field and its span (recall that, by definition, the span must be smaller than the size z and it must be in Ω( √ z)).
Our lower bound on complexity follows from geometric properties of the grid, and hence it holds regardless of the machine that builds the nest, i.e., even if the robot is a Turing machine knowing a priori the initial field.
Proposition 2.2.Let s < z be positive integers such that s ∈ Ω( √ z).There exists an initial field of size z and span s ∈ Θ(s ), such that any algorithm that builds a nest starting from this field must use time Ω(sz).
Proof.The proposition is proved by considering an appropriate initial field that requires large time to be transformed to a nest.We define a rough rectangle of size z and sides a ≤ b, such that z − ab < b, to be the following set of cells: there is a grid a × b of cells (with a rows and b columns), and the remaining z − ab cells are attached to the North-most cells of the z − ab West-most columns.Case 1. s ≥ 10 √ z.
Take the rough rectangle R with sides b = s , a = z/b as the initial field.The span of R is s ∈ Θ(s ).There exist subsets A and B of R of size at least z/3 at distance at least s /4: A is composed of the z/(3a) West-most columns of the grid and B is composed of the z/(3a) East-most columns of the grid.Since the span of a nest of size z is smaller than 2 √ z ≤ s /5, in order to transform R into a nest, at least z/3 bricks have to be moved at distance at least s /40.Hence the time of building a nest from the initial field R is at least s z/120 ∈ Ω(sz).Case 2. s < 10 √ z.
In this case we have s ∈ Θ( √ z).Take the rough rectangle R with sides b = 10 √ z , a = z/b as the initial field.The span of R is s ∈ Θ( √ z) = Θ(s ) and s ≥ 10 √ z.There exist subsets A and B of R of size at least z/3 at distance at least s/4.Since the span of a nest of size z is smaller than 2 √ z ≤ s/5, in order to transform R into a nest, at least z/3 bricks have to be moved at distance at least s/40.Hence the time of building a nest from the initial field R is at least sz/120 ∈ Ω(sz).

The algorithm
The robot will move bricks from the original field and build two special components.One of them will be a rough disc that will be gradually extended.The second one will be a one-cell component whose only cell is called the marker.The robot will periodically get at large distances from the rough disc being built, and the role of the marker will be to indicate to the robot that it got back in the vicinity of the rough disc.Any component that is different from the rough disc and from the marker will be called a free component.
During the execution of the entire algorithm, the robot will not ensure that the full cells outside of the rough disc and of the marker form one component -they may form several components -but after adding a new brick to the rough disc these components will be always at a bounded distance, i.e., at distance O(1), from the rough disc that the robot is constructing.
We are now ready to sketch the high-level idea of the algorithm, whose pseudo-code is presented at the end of this section as algorithm Nest.First the robot performs some preliminary actions by establishing the marker and the initial rough disc and by calling procedure Sweep, which together result in constructing the first rough disc D (of size one), placing the marker next to it and ensuring that no full cells other than the marker are at distance at most 7 from D. Then each iteration of the main loop of algorithm Nest performs three actions.First, it executes procedure FindNextBrick that allows the robot to find a brick in a free component C.This brick must be carefully chosen.For example, greedily picking the closest available brick would soon result in creating large empty spaces between components of the field, in which the robot could get lost.This brick will be later used to extend the rough disc.However, this procedure may lead the robot far from the rough disc and may also disconnect C into many components.Disconnecting C is one of the main tools in our construction.It is done by the robot on purpose to allow it to find its way back to the marker and so that it is possible to recover the connectivity of C on the way back.Such a walk back to the marker is the second action performed in the main loop and described as procedure ReturnToMarker.The third action is done once the robot is back at the marker, and it is given as procedure ExtendRoughDisc.This procedure extends the rough disc, ensuring the property that there are no full cells at a prescribed bounded distance from the rough disc, except the marker.While restoring this property, the robot may again disconnect some components but all of them are at a bounded distance from the rough disc and thus the robot will be able to find them easily.Additionally, it may happen that the cell brought to the rough disc was the last cell of C. In such a case, as the last part of procedure ExtendRoughDisc, the robot places the marker near another component close to the rough disc, if one exists.This will be the new free component C in which the robot will find the next brick in the next iteration of the main loop.If no such C exists, then the robot adds the brick from the marker to the rough disc, thus completing the construction of the nest.
Many of the difficulties described above come from our desire to keep the complexity of the algorithm optimal, i.e., O(sz).If complexity were not an issue, the following much simpler algorithm would be sufficient.The robot first builds a horizontal line at the level of a South-most cell of the initial field, by gradually squeezing down the field, keeping it connected at all times.Then it transforms the line into a nest.The squeezing down can be performed by iteratively repeating the following steps.First, the robot makes sure that it is not on the lowest level (if the line is not yet constructed, this can be done by finding a full cell with a full South neighbor).Then the robot goes to some North-West extremity of the field.Then it picks the brick from this cell and drops it one level down, ensuring the connectivity.When the field is squeezed down to a line, the robot will recognize this and easily transform it into a nest.
This idea potentially requires time Θ(z) to lower a brick by one level.Since there are z bricks possibly on Θ(s) levels, the entire time would be Θ(sz 2 ) in the worst case, which is suboptimal.Thus we proceed with the detailed description of the optimal algorithm Nest whose high-level idea was described before.

Moving bricks out of the way
One of the challenges in constructing the rough disc is to have enough room so that, while expanding, it does not merge with the remainder of the field.This is one of the goals of procedure Sweep.Its high-level idea is the following.It ensures an invariant that has to hold whenever the agent goes to retrieve the next brick in order to extend the rough disc.This invariant is that there are no full cells, other than the cell M that is the marker, within a given bounded distance from the current rough disc D. This procedure is called when the robot is at the marker, in two situations.The first one is right before the main loop: in this case the marker, the rough disc (of size one) and its corresponding neighborhood occupy a constant number of cells and hence in this case the robot is able to decide whether a given cell is in D ∪ {M }.The second situation occurs after each extension of the rough disc.In this case, the size of D may be unbounded but a walk around its border (which can be done with stop, using the marker) allows to determine if there is a full cell c within a bounded distance from D, that does not belong to the rough disc itself.Whenever such a full cell c is found, the robot picks the brick from c and searches for an empty cell at distance at least 7 from the rough disc.This searching walk is done in such a way as to ensure the return to the rough disc.At the end of the procedure, the robot places the marker next to some free component.Below is the pseudo-code of procedure Sweep.In this pseudo-code, we use the notion of the robot going in direction away from the rough disc D. This is the direction which strictly increases the distance between the robot and the rough disc.In the case where there are two such directions, we use the priority North, East, South, West.
Procedure Sweep: sweeping away bricks that are close to the rough disc

Finding the next brick
The high-level idea of procedure FindNextBrick is the following.In may happen that the cells that belong to a free component C and are close to the marker cannot be rearranged in such a way that the robot be able to obtain a brick that it can then use to extend the rough disc and at the same time keep the connectivity of C and ensure that C remains close to the marker.Thus, the robot has to retrieve the needed brick by following a potentially long walk; we will call it a search walk and formally define it in Section 3.2.1.The search walk needs to be carefully chosen to ensure that it ends at a location where it is possible to find the desired brick and so that the robot be able to return to the marker.Moreover, this walk has to be sufficiently short to guarantee the time O(sz) of the algorithm, i.e., the length of each walk must be O(s).We ensure the latter as follows: each search walk W leads alternately in two non-opposite directions, e.g., North and West.The return is guaranteed by repeatedly performing an action called switch while walking along W, which we formally describe in Section 3.2.2.Intuitively speaking, the switch eliminates the cells adjacent to W at which the robot may incorrectly turn on its way back to the marker.The switch potentially disconnects the component but the robot is able to recover the connectivity while backtracking along W. Finally, in Section 3.2.3,we describe how the desired brick can be found at the end of W.

Search walks
Suppose that the robot is currently at a full cell and there is a full cell in front of the robot.A left-free (respectively right-free) segment consists of all full cells that will be visited by the robot that moves without changing its direction until one of the following conditions holds: (S1) the robot arrives at a full cell that has an empty cell in front of it and has an empty cell to the left (respectively right) of it; such a segment is called terminal, (S2) the robot arrives at the first special cell such that the cell to the left (respectively right) of the robot is full.
Whenever the orientation is not important or clear from the context we will refer to a left-free or right-free segment by saying segment.Note that not every special cell terminates the above sequence of moves.
We now define a search walk W of the robot in an arbitrary component C (cf. the example in Fig. 2).A search walk depends on the initial position of the robot in C and on its orientation.We make two assumptions in the definition: the robot is initially located at a full cell of C and, if |C| > 1, then there is a full cell in front of the robot.The search walk W is a concatenation of segments.The first segment is both left-free and right-free.If the cell c at the end of this segment is a leaf, then the construction of W is completed.Otherwise, note that there is a full cell to the left of the robot located at c or to the right of it.In the former case, the search walk is called left-oriented and in the latter it is right-oriented.Intuitively, a left-oriented search walk prescribes going straight until it is possible to go left, then going straight until it is possible to go right, and so on, alternating directions, until a stop condition is satisfied.A similar intuition concerns right-oriented search walks.
More formally, if |C| > 1, then the search walk W consists of a single cell.Otherwise, in a right-oriented (respectively left-oriented) search walk, the segments are sequentially added to W, cyclically alternating the following construction steps.
(W1) The robot traverses a right-free (respectively left-free) segment, adding it to W. This segment becomes the last segment in W if it is a terminal.If the segment is not the last one, then the robot turns right (respectively left).
(W2) The robot traverses a left-free (respectively right-free) segment, adding it to W. This segment becomes the last segment in W if it is a terminal.If the segment is not the last one, then the robot turns left (respectively right).

Ensuring the return from a search walk
We start with a high-level idea of the mechanism that will ensure the return from a search walk.Whenever the robot follows a search walk W, it may a priori not be able to return to the origin of W. This is due to the fact that, e.g., if W is left-oriented, then any segment that is right-free may have an unbounded number of special cells such that each of them is adjacent to a cell that does not belong to W. Thus, the returning robot is not able to remember, using its bounded memory, which of such cells does not belong to the search walk and should be skipped.To overcome this difficulty, the robot will make small changes in the field close to the search walk while traversing it for the first time.These changes may disconnect the component in which the robot is walking, and this may result in creating many new components.While doing so, we will ensure two properties.First, thanks to the modifications in the field performed while traversing W, the robot is able to return to the first cell of this search walk.Second, while backtracking on W, the robot is able to undo earlier changes and recover the connectivity of the component.
Each cell c at which the robot stops to perform the above-mentioned modification will be called a break point and is defined as follows.First, we require that c be an internal cell of a segment S, i.e., neither the first nor the last cell of S. Second, if S is left-free (respectively right-free), then when the robot traversing S is at c, then there is a full cell f to the right (respectively left) of it.Clearly, the cell e to the left (respectively right) of the robot is empty.The following couple of actions performed by the robot located at such a cell c are called a switch: if the robot is not carrying a brick, then the robot brings the brick from f and then places it at e, and if the robot is carrying a brick, then the robot places it at e and then brings the brick from f .Note that the switch may disconnect the component in which the robot is located thus creating two new components.One new component is the one in which the robot is located and this is the component that contains the search walk.The second new component is the one that contains the cell f adjacent to f and at distance two from c, if f is full.If the cell f is full and belongs to a separate component, then this second component containing f will be called a switch-component.Whenever the robot traversing a segment performs the switch at each internal special cell of the segment, we say that this is a switch-traversal (cf. the example in Fig. 3).

R
Figure 3: The field from Figure 2 after switch-traversal of the search walk from Figure 2. The cells w 6 , w 7 , w 9 , w 13 , w 15 and w 16 are the break points at which the robot moves a brick from a cell f i to e i for i ∈ {1, . . ., 6}.Note that a brick is moved from f 2 to e 2 while traversing the second segment and then the same brick is moved to e 3 while traversing the third segment.

Obtaining a brick at the end of a search walk
Informally, the purpose of traversing the entire search walk by a robot is to arrive at a location in the current component C of the robot, where the robot can start a procedure aimed at obtaining a brick whose removal will not disconnect C. We will say that such a brick is free.In our algorithm, we check the condition (S1) to learn if the last segment is terminal.According to the condition, the terminal segment may end with a leaf, and in such a case the robot is at a cell with a free brick.If the terminal segment does not end with a leaf, then there need not exist a free brick located in a close neighborhood of the robot.However, we will prove that it is possible to perform a series of changes to the field that results in creating a configuration of bricks that does contain a free brick.
We now define the behavior of the robot that ended the switch-traversal of the last segment S of a search walk W and arrived at a cell that is not a leaf.The following series of moves is called shifting (cf.Fig. 4).Suppose that the cell to the right (respectively left) of the robot is full.Note that this implies that S is left-free (respectively right-free).First the robot changes its direction so that a cell of S is in front of it (i.e., the robot turns back).The following three actions are performed until the stop condition specified in the third action occurs.First, the robot picks the brick from the currently occupied cell.Second, the robot moves one step forward -thus backtracking along S. Third, when the robot is at a special cell, then the shifting is completed, and otherwise the robot places the brick at the cell to the left (respectively right) of it.Note that when the robot arrives at a special cell, it is carrying a brick and this is the desired free brick.4: The field from Fig. 3 at the end of shifting.The shifting ends with a right-free segment, at the cell w 16 because it has a full neighbor, the cell e 6 .There is one fewer brick than in Fig. 3 and this is the free brick obtained and carried by the robot.
We now give a pseudo-code of procedure FindNextBrick that obtains this brick.
Procedure FindNextBrick: finding a free brick 1 W := the search walk that starts at the cell where the robot is located 2 go to the nearest cell belonging to a free component 3 perform a switch-traversal of W 4 if the robot is at a leaf then

Back to the marker
Before presenting the high-level idea of procedure ReturnToMarker that takes the robot carrying a brick back to the marker, we need the following definitions.If S is a segment, then the reversal of S, denoted by ϕ(S), is the segment composed of the same cells as S but in the reversed order.For a search walk W that is a concatenation of segments S 1 , . . ., S l , define the reversal of W, denoted by ϕ(W), to be the walk that is the concatenation of segments ϕ(S l ), ϕ(S l−1 ), . . ., ϕ(S 1 ), in this order.Thus, following ϕ(W) means backtracking along W, and in this section we give a procedure performing it, that reconnects the previous free component on the way.We also define the orientation of ϕ(W) as follows.If the last segment of W is left-free, then ϕ(W) is leftoriented, and otherwise ϕ(W) is right-oriented.Thus, if W ended with a left-free (respectively right-free) segment, then ϕ(W) also starts with a left-free (respectively right-free) segment.
At a high level, the robot will perform a switch-traversal along ϕ(W), stopping at each break point to reconnect the corresponding switch-component with the component in which the robot is walking.However, we need to ensure that, at the end, the robot stops at the right point, i.e., at the marker.In order to ensure this, we define the following condition: (S1') the robot arrives at a cell at distance at most 4 from the marker.
We define a return switch-traversal of ϕ(W) to be a switch-traversal of ϕ(W) in which each verification of condition (S1) is replaced by the verification of condition (S1').Recall that the condition (S1) is checked in the definition of a switch-traversal to determine the termination of a segment and consequently the termination of the entire search walk.Intuitively, by replacing condition (S1) with (S1') we change the behavior of the robot so that it is looking for the marker while backtracking along W, i.e., going along ϕ(W).
A high-level sketch of procedure ReturnToMarker is the following.As indicated earlier, the robot essentially follows ϕ(W) and, as the return switch-traversal dictates, reconnects the switch components.However, there is one special case in which the robot should not perform a switch while being at a cell c of ϕ(W), although c satisfies the definition of a break point.This case occurs if the shifting moved all internal cells of the last segment of W. In this case, it is enough for the robot to move to the next cell after c and start the return switch-traversal of ϕ(W) from there.This is feasible because the cell c and its neighbors are at a bounded distance from the robot when the shifting is completed.Below is the pseudo-code of procedure ReturnToMarker.
Procedure ReturnToMarker: going back to the marker

Extending the rough disc
The aim of procedure ExtendRoughDisc is triple: it adds a new brick to the current rough disc in a specific place, and it calls procedure Sweep to extend, if necessary, the empty space around the rough disc and to ensure that the marker is close to some free component.Whenever procedure ExtendRoughDisc is called, the following conditions will be satisfied: the robot is at the marker and it is carrying a brick.Below is the pseudo-code of procedure ExtendRoughDisc.
Procedure ExtendRoughDisc: adding one brick to the rough disc D 1 place the brick at the unique cell e such that D ∪ {e} is a rough disc 2 call procedure Sweep Now the pseudo-code of the entire algorithm can be succinctly formulated as follows.
Algorithm Nest: building a nest from any connected field 1 if the span of the field is at most 2 then 2 exit the field is already a nest 3 the cell occupied by the robot becomes the marker 4 a full cell at distance 2 from the marker becomes the initial rough disc 5 call procedure Sweep 6 while there exists a free component C do The following is the main result of this paper.
Theorem 3.1.Algorithm Nest builds a nest starting from any connected field of size z and span s in time O(sz).This time is worst-case optimal.

Analysis of the algorithm
This section is devoted to the proof of Theorem 3.1.

Search walks
The first three observations provide simple properties of search walks that will be used in the sequel.
Observation 4.1.Suppose that W is the search walk from procedure FindNextBrick.After performing the switch-traversal in line 3 of the procedure, the robot is at the end of W.
Observation 4.2.A left-free (respectively right-free) segment becomes right-free (respectively left-free) as a result of a switch-traversal.
Observation 4.3.Let W be a search walk whose last segment S is left-free (respectively rightfree).Upon completion of a switch-traversal of W, if the robot is not at a leaf, then the cell to the right (respectively left) of the robot is full and the cells in front of it and to the left (respectively right) of it are empty.
We will need some additional notation.Let W be a search walk in a component C and b be the last break point of W. All cells of W starting at b are called the prefix of ϕ(W).Note that, at each point of the execution of procedure FindNextBrick, some parts of C become the switch-components.The other component, namely the one that overlaps with W will be called the W-component.(Note that we specify that it overlaps with W and not contains W since, after the shifting, some cells of W that are in the prefix of ϕ(W) are empty and thus are not a part of the W-component.)Upon completion of shifting, each component that contains a brick placed during shifting will be called a shift-component.

(i)
The shift-component is unique.
(ii) Let c be the cell adjacent to the cell occupied by the robot and also adjacent to the cell on which the last brick has been placed during shifting.If c is full, then it belongs to the shift-component and it belongs to W. If c is empty, then it is adjacent to a cell of the shift-component.
(iii) The robot is located at the penultimate special cell of W and is carrying a brick.
Proof.Consider a shifting that occurs after a switch-traversal of W. Let S be the last segment of W. The segment S is either left-free or right-free, by the definition of a search walk.We consider the former case (cf. the example in Fig. 5); the latter one is analogous and will be skipped.and (f) shows the field at the end of shifting.In this case c = c l = c 6 and this cell belongs to the penultimate segment of the search walk.During the traversal of the last segment no switch occurred.As we will prove, the two latter properties are always satisfied together.By Observation 4.1 and the formulation of procedure FindNextBrick, when the shifting starts the robot is at the last cell of S. Denote this cell by c 1 .Let c 2 , . . ., c l be the cells of S such that c i is adjacent to c i−1 for each i ∈ {2, . . ., l}, c l is a special cell in W and c 2 , . . ., c l−1 are not special cells in W. Note that the latter sequence may be empty if two last cells of S are special cells.Denote by c i the cell that is to the left of the robot when it is at c i during shifting.
According to the definition of shifting, the following occurs: for each i ∈ {1, . . ., l − 1}, the brick from c i is moved to the cell c i+1 .We argue that shifting is feasible, i.e., that the cell c i , i ∈ {1, . . ., l − 1}, is full when the robot picks the brick from it and that the cell c i+1 , i ∈ {1, . . ., l − 2}, is empty when the robot then drops the brick at it, for i ∈ {1, . . ., l − 1}.The former is due to the fact that c i belongs to a segment which by definition is a sequence of full cells.The latter holds because c 2 , . . ., c l−1 are not special cells, i.e., their neighbors outside of S, in particular the cells c 2 , . . ., c l−1 , are empty.
We now analyze what types of components are created during shifting from the component C in which it started.Consider the components C 1 , . . ., C t obtained from C by making the cells c 1 , . . ., c l−1 empty.These are all cells from which the robot picked the bricks during shifting.First observe that there are exactly two components as above, i.e., t = 2.The first one, say C 1 , contains the cell c 1 because, according to the 'if' statement in line 4 of procedure FindNextBrick, c 1 is not a leaf and hence, due to Observation 4.3, the cell c 1 is full.The second component, let it be C 2 , contains c l , which is full because it belongs to W and has not been emptied during shifting.To finish the argument that t = 2, note that by definition of shifting, the cells c 2 , . . ., c l−1 are not special cells and thus have no full neighbors outside of S, and by Observation 4.3, the cell c 1 has no other full neighbors in C except c 2 and c 1 .Observe that making the cells c 2 , . . ., c l−1 full during shifting glues these cells to C 1 , resulting in a single component.This is the unique shift-component.Thus property (i) is proved.
The cell that is to the right of the robot upon completion of shifting is c l .Note that c = c l and this cell is adjacent to c l−1 .Hence, if c l is empty, then (ii) holds.Suppose that c l is full.The shift-component C 1 and the W-component C 2 are the same: c l is adjacent to the full cell c l−1 of C 1 and is also adjacent to the full cell c l that belongs to C 2 .Since S is left-free, by the definition of switch-traversal we obtain that the brick located at c l has been dropped at c l as a result of a switch.Moreover, it cannot be a neighbor of an internal cell of S. Since this is not the last cell of S because l > 1, we have that c l is the first cell of S. Hence c l is the penultimate segment of W. This proves property (ii).
Finally, the brick picked from c l−1 is carried by the robot at the end of shifting.At the end of shifting, after picking the brick, the robot moves to c l which is the penultimate special cell of W because the cells c 2 , . . ., c l−1 are not special cells by definition.This proves property (iii) and completes the proof of the lemma.
We say that the rough disc has a gap of width k if each free component is at distance k + 1 from the rough disc.We say that a field is structured if it satisfies the following conditions: (F1) the marker is at distance 3 from the rough disc and at distance 4 from some free component, (F2) the rough disc has a gap of width 7.
The next lemma characterizes the situation upon completion of procedure FindNextBrick.Lemma 4.2.Suppose that the field is structured at the beginning of an execution of procedure FindNextBrick.Let W be the search walk traversed by the robot in a free component C during this execution.At the end of this execution we have the following properties: the robot is located in the prefix ϕ(W) and carries a brick.
(ii) each brick that belongs to C at the beginning of the execution is either in a switchcomponent, in the shift-component or in the W-component, (iii) if W starts with a cell at distance 7 from the rough disc, then the rough disc has a gap of width at most 7 and at least 5.
Proof.Consider the robot at the end of the execution of procedure FindNextBrick.We consider two cases depending on the execution of the 'if' instruction in line 4 of procedure FindNextBrick.In the first case, suppose that the instruction in line 5 is executed.The robot is at the last cell c of W and carries a brick that it picked from c.This proves (i) in this case.Since c was a leaf upon arrival of the robot at c, picking c did not disconnect the W-component, which implies that the shift-component equals the W-component and consequently proves (ii) of the lemma.In the second case, suppose that instruction in line 7 of procedure FindNextBrick is executed, i.e., the robot performed shifting.Then, statement (i) follows from Lemma 4.1(iii), since, by definition, the prefix ϕ(W) contains all cells between the last special cell and the penultimate special cell of W, inclusively.Again by Lemma 4.1(i), the shift-component is unique which implies statement (ii) in this case.
We now prove (iii).Since the field is structured at the beginning of the execution of procedure FindNextBrick, it has a gap of width 7. The first cell of W does not change its position and since its distance from the rough disc is 7, we have that the gap is not larger than 7 at the end of the execution.Now we analyze how much the gap can decrease.During the execution of procedure FindNextBrick, the robot performs two types of changes in the field.The first one is a switch, which places each brick next to a full cell of the component.Thus, this can decrease the gap by at most one.The second one is the shifting, which moves each brick to a cell at distance 2 from its origin, which may decrease the gap by at most 2. The bricks moved during a switch do not belong to W and the bricks moved during shifting belong to it.This proves (iii) and concludes the proof of the lemma.

Reversals of search walks
If a component is at distance larger than 7 from the rough disc, then it is called a lost component.Recall the definition of a switch, in which the robot located at a cell c brings a brick from a full cell f and places it at an empty cell e.The cell e will be called a switch-cell.If, during a return switch-traversal, the robot is backtracking on a segment S, which was added to the walk using condition (W1) or (W2), and it checks, upon arrival at a special cell c of S, whether the cell to the left (respectively right) of c is full, then we say that the robot is looking for left (respectively right) turn during this traversal of S. Suppose that the robot is located at a cell c of a segment S during backtracking.We say that the robot is oriented on ϕ(S) if it is directed towards the beginning of S. When the robot backtracks on S, then all cells that belong to S and are located between c (inclusively) and the beginning of S (exclusively) are called the leftover of S. In other words, these cells are the ones to be yet visited by the robot backtracking on S.
In the three following lemmas we prove that procedure ReturnToMarker can be correctly executed by the robot.
Proof.Note that lines 1 and 2 result in no actions of the robot and only introduce notation for the description of the remaining steps of the procedure.The condition in the 'if' statement in line 3 can be checked using Lemma 4.1(ii): the robot tests whether the cell c in the lemma is full or not and it is full if and only if the robot is at the first cell of S l .Now we analyze the instructions within the 'if' statement.In line 4, the penultimate cell of S l−1 is the above cell c and hence the robot can perform the correct turn.The value of |S l−1 |, may be large and thus unknown to the robot.However, for calculating min{2, |S l−1 | − 1} it is enough to know whether This can be remembered by the robot while performing the traversal of a search walk in procedure FindNextBrick (although the robot does not know whether the current segment is penultimate, it may always remember the information about the two recently traversed segments).To execute the instruction in line 6, the robot can remember whether l > 2, and it turns in the opposite direction to the turn just made in line 4, which follows from the definition of a search walk.Lemma 4.4.Suppose that the robot is oriented on ϕ(S), where S is a left-free (respectively right-free) segment of some search walk.If: the robot previously performed a switch-traversal along S, (ii) the robot is looking for a left (respectively right) turn while traversing ϕ(S), then, during the return switch-traversal, the robot arrives at the end of ϕ(S) and turns left (respectively right).
Proof.Assume that the robot is looking for a left turn.The other case is analogous and we omit it.Thus, S is left-free prior to the switch-traversal performed in (i).By Observation 4.2, ϕ(S) is also left-free after the switch-traversal.Let S be the maximal sequence of cells that the robot traversed during the return switch-traversal while looking for a left turn, before it turned left.Since ϕ(S) is left-free, this implies that the robot reaches the end of ϕ(S), i.e., S contains the endpoint of ϕ(S).Let c be the cell at which the robot finishes the traversal of S .Let c be the first cell of S. We need to show that c = c , i.e., that the robot does make a turn when located at c.By the definition of a search walk, the first cell of S is a special cell.By the definition of a switch-traversal, the robot does not perform a switch at the end nor at the beginning of a segment.Thus, in particular, no switch occurred when the robot was located at c during the traversal of S while performing the search walk W. This means that a neighbor of c that is not in S is full during the traversal of S in W if and only if this neighbor is full during the traversal of S .Thus, c is a special cell when the robot arrives at c while traversing S .Moreover, this special cell ends the segment p(S) that precedes S in W. Since p(S) is not a terminal segment, we have that it ended by satisfying condition (S2).Since S is left-free, the segment p(S) is right-free by the definition of a search walk.This implies that when the robot arrived at c during the search walk W, it had a full cell to its right.This full cell belongs to S and therefore, when the robot arrives at c while traversing S , it has a full cell to its left.Since the robot is looking for a left turn, c becomes the last cell of S proving that c = c , and moreover, the robot turns left at c, as required by the lemma.
The following lemma shows that, while executing procedure ReturnToMarker, the robot does not loose its way during the backtrack on a previously traversed search walk.Lemma 4.5.Let W be a search walk.Suppose that the robot traversed W by executing procedure FindNextBrick and then executed procedure ReturnToMarker.These two executions ensure that the robot traverses ϕ(W) after the traversal of W.
Proof.Denote by S 1 , . . ., S l the segments whose concatenation, in this order, gives the search walk W. By definition, a return switch-traversal is a walk that traverses a sequence of segments denoted by S 1 , . . ., S l .We argue by induction on i ∈ {1, . . ., l } that: (a) S i = ϕ(S l−i+1 ), and (b) upon arrival at the end of S i , the robot becomes oriented on S l−i .
Before giving the inductive proof we note that property (a) implies that the return switch traversal is a concatenation of segments ϕ(S l ), . . ., ϕ(S 1 ) and thus it is the walk ϕ(W) as required by the lemma.We note that property (b) is used for technical reasons to conduct the inductive argument.
For the base case of i = 1 we need to argue that the robot traverses the segment ϕ(S l ) and makes the turn after which the traversal of ϕ(S l−1 ) will start.By Lemma 4.2(i), the robot is at a cell c in the segment ϕ(S l ) at the end of execution of procedure FindNextBrick.Thus, the robot is in ϕ(S l ) when procedure ReturnToMarker starts.The segment S l has been appended to W in construction step (W1) or (W2) as a left-free or a right-free segment.We assume that S l is left-free.The proof for a right-free segment S l is analogous and will be omitted.If shifting did not occur during the execution of procedure FindNextBrick then, according to line 5, the robot is at the end of the search walk W, i.e., at the end of S l .If shifting did occur in line 7 of procedure FindNextBrick, then c is not the last cell of S l .
If c is the first cell of S l (which is the last cell of ϕ(S l )), then by Lemma 4.3, the robot can correctly verify this in line 3 of procedure ReturnToMarker.Moreover, this implies that (a) holds for i = 1.Then, the robot performs the turn in line 4 which proves (b).Now suppose that c is not the first cell of S l .The assumption (i) of Lemma 4.4 is satisfied, where we take S = S l in the lemma.By the definition of return switch-traversal, the fact that W ended with a left-free segment (recall that S l is left-free) implies that, when the robot starts its walk in line 7 of procedure ReturnToMarker, it follows a left-oriented walk.The traversal of S 1 is thus determined by the first construction step in (W1) for this left-oriented walk.Thus, the robot traverses a left-free segment as the first segment S 1 , i.e., it is looking for a left turn.This implies that the assumption (ii) of Lemma 4.4 is satisfied.Then, (a) and (b) follow from Lemma 4.4.This completes the proof of the base case.
For the proof of the inductive step suppose that assertions (a) and (b) hold for some i ∈ {1, . . ., l − 1} and we prove them for i + 1.By the inductive assumption, the robot is at the end of ϕ(S l−i+1 ) when procedure ReturnToMarker starts.We consider three cases.
Case 1. i+1 = 2, S l−1 is of length 2, and the robot is at the beginning of S l upon completion of shifting performed in line 7 of procedure FindNextBrick.
By Lemma 4.3, the condition in the 'if' statement in line 3 of procedure ReturnToMarker is correctly verified by the robot, and by inductive assumption, it is oriented on ϕ(S l−1 ).Since the length of S l−1 is 2, the robot makes one step forward (see line 5) and makes a turn (see line 6).This ensures claims (a) and (b) in this case.
Case 2. i + 1 = 2, S l−1 is of length larger than 2, and the robot is at the beginning of S l upon completion of shifting performed in line 7 of procedure FindNextBrick.
By the inductive assumption, the robot is oriented on S l−1 .By Lemma 4.3, the robot makes two steps along ϕ(S l−1 ) by executing instruction in line 5 of procedure ReturnToMarker and thus it is at the third cell of ϕ(S l−1 ) when the return switch-traversal in line 7 starts.Thus, since the robot is at a cell of S l−1 , condition (i) of Lemma 4.4 is satisfied.By arguments analogous to the ones in the base case of induction, condition (ii) holds as well.Thus by Lemma 4.4, claims (a) and (b) hold for i + 1.
Case 3. i + 1 > 2 or (i + 1 = 2 and the robot is not at the beginning of S l upon completion of shifting performed in line 7 of procedure FindNextBrick).
The robot is at the beginning of S l−1 when the return switch-traversal starts.As before, conditions (i) and (ii) of Lemma 4.4 are satisfied.Thus by Lemma 4.4, claims (a) and (b) hold for i + 1.
We say that the field is strongly structured if the following conditions are satisfied: (T1) the field is structured, (T2) there are no lost components, and (T3) the robot is at the marker.
We will show that the field is strongly structured at the beginning of any iteration of the 'while' loop in Algorithm Nest.
The following lemma shows that while backtracking on a search walk, the robot reconnects all switch components that were disconnected during procedure FindNextBrick.Moreover, this backtracking stops when the robot is at the marker.Lemma 4.6.Suppose that at the beginning of an iteration of the "while" loop in algorithm Nest the field is strongly structured.Then there are no lost components in the field and the robot is at the marker after the execution of procedures FindNextBrick and ReturnToMarker.

Analysis of the rough disc construction
The following lemma shows that procedure Sweep does not create lost components and makes sufficient room around the rough disc.Moreover, the cost of each call of the procedure is O(s).Lemma 4.7.Suppose that the robot is at the marker and that there are no lost components.An execution of procedure Sweep takes time O(s) and results in a strongly structured field.
Proof.We first prove that the field is structured after executing the procedure.The robot can correctly go to the nearest cell of the rough disc D in line 2. Indeed, in a call to procedure Sweep in line 5 of algorithm Nest this is because |D| = 1 and D is at distance 2 from the robot.In each call made in procedure ExtendRoughDisc this is because the robot is at the marker and, by Lemma 4.7(iii), the rough disc and the marker form two separate components.While traversing the border of the rough disc in line 3, the robot iterates over each cell c of the gap in all executions of the 'for' loop in line 4. Since the distance between the current location c ∈ D and c is at most 7, the robot can correctly execute instructions in line 6.In line 7, the robot finds the first empty cell in the direction d away from D. Then, in line 8, the robot drops the brick at this cell and returns to c .We argue that the latter is feasible.This is done by first finding the closest empty cell e in the direction opposite to d.This cell is at distance at most 7 from the rough disc, due to the condition used in line 7. Thus, the robot can remember the relative position of e with respect to c because the distance between them is bounded.The latter is due to the selected direction d.Indeed, the fact that direction d is away from D implies that the prefix of the walk in line 7 consisting of cells at distance at most 7 from c is of bounded length.
The above proves that each cell at distance 7 from the rough disc, except the marker, is empty upon completion of the traversal in line 3. Since the 'for' loop in line 4 does not select cells at distance larger than 7 from the rough disc, the rough disc has a gap of width 7, i.e., the field satisfies condition (F2).Moreover, this implies that no lost component is created during the traversal in line 3 because each brick dropped in line 8 is placed next to a full cell (see line 7).
Checking if there exists a free component C in line 9 can be done by traversing the border of the rough disc because, by assumption, there are no lost components.By the same argument, the robot can relocate the marker in line 10 of procedure Sweep.This also ensures that the field satisfies condition (F1).Thus, the field is structured, i.e., it satisfies condition (T1).
By assumption, there are no lost components, and each brick dropped in line 8 is placed next to a full cell.Thus, no lost component is created during procedure Sweep, which implies that the field satisfies condition (T2).According to line 11 of procedure Sweep, upon its completion the robot is at the marker, which ensures condition (T3).Thus, the field is strongly structured as required.
Now we analyze the time needed to perform an execution of procedure Sweep.For each cell c selected in the 'for' loop in line 4, all the following steps take time O(1), possibly except for the walk performed in line 7 and the return from it in line 8.The cells traversed in line 7 are called a sweep-walk.Thus, to estimate the execution time of procedure Sweep, we estimate the total length of all such walks that occur during the execution.
The call in line 5 in algorithm Nest takes time O(s) because, for the initial rough disc of size 1, O(1) cells need to be moved to ensure property (F2) of the field, and for each such cell, the sweep-walk is of length O(s).
We now consider one execution of procedure Sweep called in procedure ExtendRoughDisc.In order to analyze the time of executing this call, we divide sweep-walks depending on their length.Let W 2 be all sweep-walks of length at most 2 and let W * be all the remaining sweepwalks.A sweep-walk X is traversed by the robot because condition (F2) is violated, i.e., there is a full cell c at distance less than 7 from the rough disc.We consider two cases.is carrying a brick at the beginning of the return switch-traversal implies that the robot is carrying a brick at the end of it.Thus in particular, the robot is carrying a brick when procedure ExtendRoughDisc is called in line 9 of algorithm Nest.This implies that the assumptions of Lemma 4.8 are satisfied.By this lemma, the execution of procedure ExtendRoughDisc extends the rough disc by one brick.The call to procedure Sweep in line 2 of procedure ExtendRoughDisc results in a field that is strongly structured, in view of Lemma 4.7.This completes the inductive argument for the (i + 1)-th iteration.Thus we showed that, at the beginning of each iteration of the 'while' loop in algorithm Nest the field is strongly structured.
The above implies that there will be z − 2 iterations of the 'while' loop in algorithm Nest and, upon the exit from the loop, there are only two components: the rough disc of size z − 1 and the marker.By Proposition 2.1, adding the brick from the marker in line 10 to the rough disc results in a nest.
By the definition of a search walk, the length of any search walk is O(s).The time needed to execute the two procedures that traverse the search walk and backtrack on it is linear in the length of the walk.Using Lemmas 4.7 and 4.8, the overall time of algorithm Nest is O(sz).By Proposition 2.2, this time is worst-case optimal.

Conclusion
We designed a finite deterministic automaton that builds the most compact structure starting from any connected field of bricks, and does it in optimal time.An interesting problem yielded by our research is to characterize the classes of target structures that can be built by a single automaton, starting from any connected field of bricks in the grid.Another problem is that of how the building task parallelizes, i.e., how much time many automata use to build some structure.

Figure 1 :
Figure 1: (a) disc of size z r for r = 3; (b) a rough disc of size z r + 7 and span 2r + 1, r = 3; (c) a rough disc of size z r + 11 and span 2r + 2, r = 3 we would have |M | ≥ |N |, and |M | = h(2r − 1) = z r−1 + 2r = 2r 2 + 1 < z r , which would imply |N | < z, contradicting the definition of N .Thus, the disc of size z = z r has span at most s z and thus it is a nest.Next suppose that z r < z ≤ z r + 2r + 2. We have s z ≥ 2r + 1 because otherwise, for a maximum-size field M in F 2r , we would have |M | ≥ |N |, and |M | = h(2r) = z r < z, which would imply |N | < z, contradicting the definition of N .Hence, the rough disc of size z has span at most s z and thus it is a nest.Finally suppose that z r + 2r + 2 < z < z r+1 .We have s z ≥ 2r + 2 because otherwise, for a maximum-size field M in F 2r+1 , we would have |M | ≥ |N |, and |M | ≤ h(2r+1) = z r +2r+1 < z, which would imply |N | < z, contradicting the definition of N .This proves that the rough disc of size z has span at most s z and thus it is a nest.

1 M
:= the marker 2 go to the closest cell of the rough disc D 3 perform a full counterclockwise traversal of the border of D, executing the following actions after each step: 4 for each full cell c / ∈ D ∪ {M } at distance at most 7 from the robot do 5 c := the cell currently occupied by the robot 6 go to c and pick the brick 7 move in direction away from D and stop at the first empty cell at distance at least 7 from D 8 drop the brick and return to c 9 if there exists a free component C then 10 pick the brick from marker and place it at distance 3 from the rough disc and at distance 4 from C, creating a new marker 11 go to the marker

1 W 1 5 2 7
:= the search walk traversed in the last call to FindNextBrick 2 let S 1 , . . ., S l be the segments in W 3 if the robot is at the first cell of S l then 4 turn towards the penultimate cell of S l−move min{2, |S l−1 | − 1} cells forward 6 if |S l−1 | = 2 and l > 2, then make a turn towards the penultimate cell of S l−starting at the current location, perform a return switch-traversal of ϕ(W) 8 go to the marker moves the marker if necessary 10 pick the brick and place it at the unique cell e of the rough disc D such that D ∪ {e} is a rough disc

Lemma 4 . 1 .
Upon completion of shifting performed in procedure FindNextBrick, we have the following properties.

Figure 5 :
Figure 5: (a)-(c) illustrates the case when the cell c in Lemma 4.1(ii) is empty: (a) shows the robot traversing the penultimate segment after which, in (b), a switch-traversal of the last segment (s 1 , . . ., s 8 ) is completed, and (c) depicts the field at the end of shifting.Note that in this case c = c l = c 5 .(d)-(f) illustrates the case when c is full: (d) and (e) again depict the switch-traversal of the last two segments, and (f) shows the field at the end of shifting.In this case c = c l = c 6 and this cell belongs to the penultimate segment of the search walk.During the traversal of the last segment no switch occurred.As we will prove, the two latter properties are always satisfied together.
We write d(a, b) to denote the distance between two cells a and b.For a given span s, consider a field C s that has span s and a maximum number of cells among fields of this span.Consider three consecutive full cells f 1 , f 2 and f 3 of C s in a row or in a column.Let c 1 , c 2 and c 3 be three consecutive cells such that c i is adjacent to f i , i ∈ {1, 2, 3}.We prove that c 2 is full.We proceed by contradiction, i.e., suppose that c 2 is empty.Consider the field C obtained from C s by adding a brick to the cell c 2 .By definition of C s , the span of C is larger than s.Thus, there exists a shortest path W = (w 1 , . . ., w s+2 ) in the grid, that connects two full cells w 1 and w s+2 of C .If c 2 is different from w 1 and w s+2 then C s has span larger than s, which is a contradiction.Thus, without loss of generality, we can assume c 2 = w s+2 .We have d(w 1 , f 2 ) ≥ s because the length of W is s + 1 and f 2 and c 2 are neighbors.Thus, since f 2 is a neighbor of both f 1 and f 3 , we have either d(f 1 , w s+2 ) ≥ s + 1 or d(f 3 , w s+2 ) ≥ s + 1.Since f 1 , f 3 and w s+2 are in C s , this gives a contradiction with the span of C s being at most s.Hence c 2 is full.