1 Introduction

1.1 The model and the problem

Leader election is one of the fundamental problems in distributed computing, first stated in [38]. Each entity in some set has a Boolean variable initialized to 0 and, after the election, exactly one of these entities, called the leader, should change this value to 1. All other entities should know which one becomes the leader. In this paper we consider the problem of leader election among mobile agents that operate in a network. Electing a leader enables the agents to accomplish many tasks in networks more efficiently. For example, the leader may subsequently navigate in the network by itself to periodically check the functionality of network components, thus relieving other agents to accomplish other tasks. Alternatively, the leader may split some computational task among agents that will complete different parts of it in parallel, thus decreasing the total time of computation.

We assume that neither nodes of the network nor agents have labels that can be used for leader election. This assumption is motivated by scenarios where nodes and/or agents may refrain from revealing their identities, e.g., for privacy reasons. Agents may want to cooperate and may well be trustworthy, but they may be reluctant to tell who they are. Hence it is desirable to have leader election algorithms that do not rely on identities but exploit asymmetries in the initial configuration of agents due to its topology and to port labelings. From the methodological point of view, leader election in anonymous networks performed by anonymous agents is interesting because it isolates the structural properties of the network and of the initial configuration of agents as the only features that can be exploited to break symmetry. With unlabeled nodes and agents, leader election is impossible for symmetric initial configurations, e.g., in a ring in which ports at each node are 0, 1, in the clockwise direction and agents are initially situated at every node. Our goal is to answer the following question:

For which initial configurations of agents is leader election possible and how to do it when it is possible?

A network is modeled as an undirected connected graph with unlabeled nodes. It is important to note that the agents have to be able to locally distinguish ports at a node: otherwise, the adversary could prevent an agent from choosing a particular edge, thus making navigation in the network impossible even in the simple case of trees. This justifies a common assumption made in the literature: ports at a node of degree d have arbitrary fixed labelings \(0,\ldots , d-1\). Throughout the paper, we will use the term “graph” to mean a graph with the above properties. We do not assume any coherence between port labelings at various nodes. Agents can read the port numbers when entering and leaving nodes.

At the beginning, identical agents are situated in some nodes of the graph, at most one agent at each node. The graph with bicolored nodes (black if the node is occupied, white if it is not) is called an initial configuration. Agents do not have labels and have unlimited memory: they are modeled as identical Turing machines. They execute the same deterministic algorithm.

Agents navigate in the graph in an asynchronous way which is formalized by an adversarial model used in [6, 15, 18, 19, 28] and described below. Two important notions used to specify movements of agents are the route of the agent and its walk. Intuitively, the agent chooses the route where it moves and the adversary describes the walk on this route, deciding how the agent moves. More precisely, these notions are defined as follows. The adversary initially places an agent at some node of the graph. The route is chosen by the agent and is defined as follows. The agent chooses one of the available ports at the current node. After getting to the other end of the corresponding edge, the agent learns the port number by which it enters and the degree of the entered node. Then it chooses one of the available ports at this node or decides to stay at this node. The resulting route of the agent is the corresponding sequence of edges \((\{v_0,v_1\},\{v_1,v_2\},\ldots )\), which is a (not necessarily simple) path in the graph.

We now describe the walk \(w\) of an agent on its route. Let \(R=(e_1,e_2,\ldots )\) be the route of an agent. Let \(e_i=\{v_{i-1},v_i\}\). Let \((t_0,t_1,t_2,\ldots )\), where \(t_0=0\), be an increasing sequence of reals, chosen by the adversary, that represent points in time. Let \(w_i:[t_i,t_{i+1}]\rightarrow [v_i,v_{i+1}]\) be any continuous function, chosen by the adversary, such that \(w_i(t_i)=v_i\) and \(w_i(t_{i+1})=v_{i+1}\). For any \(t\in [t_i,t_{i+1}]\), we define \(w(t)=w_i(t)\). The interpretation of the walk w is as follows: at time t the agent is at the point \(w(t)\) of its route. This general definition of the walk and the fact that (as opposed to the route) it is designed by the adversary, are a way to formalize the asynchronous characteristics of the process. The movement of the agent can be at arbitrary speed, the adversary may sometimes stop the agent or move it back and forth, as long as the walk in each edge of the route is continuous and covers all of it.

This definition makes the adversary very powerful, and consequently agents have little control on how they move. This, for example, makes meetings between agents hard to achieve. Note that agents can meet either at nodes or inside edges of the graph.

Agents with routes \(R_1\) and \(R_2\) and with walks \(w_1\) and \(w_2\) meet at time t, if points \(w_1(t)\) and \(w_2(t)\) are identical. A meeting is guaranteed for routes \(R_1\) and \(R_2\), if the agents using these routes meet at some time t, regardless of the walks chosen by the adversary.

When agents meet, they notice this fact and can exchange all previously acquired information. However, if the meeting is inside an edge, they continue the walk prescribed by the adversary until reaching the other end of the current edge. New knowledge acquired at the meeting can then influence the choice of the subsequent part of the routes constructed by each of the agents.

Since agents do not know a priori the topology of the graph and have identical memories at the beginning, the only way to elect a leader among agents is by learning the asymmetries in their initial positions in the graph. Hence agents must gain this knowledge by navigating in the network and share it with other agents to accomplish leader election. Sharing the knowledge can be done only as a result of meetings of agents, which is difficult because of the asynchronous way in which they move.

It is not hard to see (cf. Proposition 2) that in the absence of a known upper bound on the size of the graph, leader election is impossible even for asymmetric configurations. Hence we assume that all agents know a priori a common upper bound n on the size of the graph. This is the only information about the environment available to the agents when they start the task of leader election.

Having described our model, we can now make the initial problem more precise. Call an initial configuration eligible if, starting from this configuration, leader election can be accomplished regardless of the actions of the adversary. Thus in order that a configuration be eligible, it is enough to have some leader election algorithm starting from it, even one dedicated to this specific configuration. Now our problem can be reformulated as follows.

Which initial configurations are eligible? Find a universal leader election algorithm that elects a leader regardless of the actions of the adversary, for all eligible configurations in graphs of size at most n, where n is known to the agents.

1.2 Our results

Assuming an upper bound n on the size of the graph, known a priori to all agents, we characterize all eligible initial configurations and construct an algorithm that accomplishes leader election for all of them. More precisely, we formulate a combinatorial condition on the initial configuration, which has the following properties. On the one hand, if this condition does not hold, then the adversary can prevent leader election starting from the given initial configuration. On the other hand, we construct an algorithm that elects a leader, regardless of the adversary, for all initial configurations satisfying the condition, in graphs of size at most equal to the given bound n.

Intuitively, leader election is possible when the initial configuration is asymmetric and when agents can learn this, regardless of the actions of the adversary. Both these requirements are contained in the necessary and sufficient condition on eligibility, which we formulate in Sect. 3. In fact, the process of learning the asymmetries by the agents is the main conceptual and technical challenge in the design and analysis of our algorithm. Agents acquire and share this knowledge as a result of meetings. The difficulty is to design the algorithm in such a way that all asymmetries be finally learned by all agents and that all agents be aware of this fact and thus capable to correctly elect the leader.

1.3 Related work

Leader election in networks was mostly studied assuming that all nodes have distinct labels and election has to be performed among nodes. This task was first studied for rings. A synchronous algorithm, based on comparisons of labels, and using \(O(n \log n)\) messages was given in [30]. It was proved in [26] that this complexity is optimal for comparison-based algorithms. On the other hand, the authors showed an algorithm using a linear number of messages but requiring very large running time. An asynchronous algorithm using \(O(n \log n)\) messages was given, e.g., in [42] and the optimality of this message complexity was shown in [9]. Deterministic leader election in radio networks has been studied, e.g., in [31, 32, 39] and randomized leader election, e.g., in [45]. In [29] the leader election problem is approached in a model based on mobile agents for networks with labeled nodes.

Many authors [35, 8, 23, 35, 36, 43, 46, 48] studied various computing problems in anonymous networks. In particular, [7, 48] characterize message passing networks in which leader election can be achieved when nodes are anonymous. In [47] the authors study the problem of leader election in general networks, under the assumption that labels are not unique. They characterize networks in which this can be done and give an algorithm which performs election when it is feasible. They assume that the number of nodes of the network is known to all nodes. In [25] the authors study feasibility and message complexity of sorting and leader election in rings with nonunique labels, while in [24] the authors provide algorithms for the generalized leader election problem in rings with arbitrary labels, unknown (and arbitrary) size of the ring and for both synchronous and asynchronous communication. Characterizations of feasible instances for leader election and naming problems have been provided in [10, 12, 13]. Memory needed for leader election in unlabeled networks has been studied in [27].

Problems involving mobile agents navigating in various environments have been extensively studied in the literature. The environment can be either the plane [14] or a network modeled by a graph. In the latter case the tasks accomplished by agents include exploration of the network [2, 11] and rendezvous, in which two or more agents have to gather in the same place. Scenarios in which rendezvous was considered can be broadly divided into randomized, which are the subject of the book [1], and deterministic, which are surveyed in [41]. Deterministic rendezvous in networks was studied either assuming the possibility of marking nodes by agents [16, 37], or forbidding it [17, 18, 20, 44]. Another important dichotomy is synchronous versus asynchronous navigation of the agents. In the first case [17, 20, 21, 44], agents traverse edges in lock-step. The asynchronous scenario was in turn studied in two variations. In the first one [33, 34], agents could observe positions of other agents, but were oblivious, i.e., could not remember previously seen configurations. The second variation, which is the model used in this paper, has been previously used in [6, 15, 18, 19, 28] in the context of rendezvous between two agents. In [6, 15, 18, 19] agents had different labels and in [28] agents were anonymous, as in our present scenario.

In the scenario of mobile agents that get aware of other agents only by meeting them, rendezvous of a pair of agents and the problem of leader election are tightly connected. Indeed, in order to elect a leader agents must meet other agents. Hence, not surprisingly, we use rendezvous of two agents, described in [18, 28] as a building block for our leader election algorithm. However, it should be pointed out that leader election among many agents is a much harder task than meeting of two agents. In the latter case, the task is accomplished after meeting and the agents stop. No information transfer is necessary. By contrast, in order to perform leader election, agents must meet some agents, get information from them, separate, meet other agents, try to recognize if these other agents are those seen previously or different ones (recall that agents are anonymous, and can share only their memory content—see Sect. 2), and combine all this knowledge to finally elect a leader.

1.4 Roadmap

In Sect. 2 we formalize the description of how agents decide. This concerns both navigation decisions (on what basis the agents construct their routes) and the final decision who is the leader. We define memory states of the agents that are the basis of all these decisions. In Sect. 3 we formulate the combinatorial condition EC concerning initial configurations that is then proved to be equivalent to eligibility, and we formulate our main result. In Sect. 4 we prove two negative results concerning leader election: one saying that condition EC is necessary for eligibility and the other saying that the assumption concerning knowledge of the upper bound cannot be removed. In Sect. 5 we give our main contribution: we construct a universal algorithm electing a leader for all configurations satisfying condition EC, if agents know an upper bound on the size of the graph. Section 6 contains conclusions.

2 Memory states and decisions of agents

In this section we describe formally on what basis the agents make decisions concerning navigation in the graph (i.e., how they construct their routes) and on what basis they make the decision concerning leader election. All these decisions depend on the memory states of the agents. At every time t the memory state of an agent is a finite sequence of symbols defined as follows. Before an agent is woken up by the adversary, its memory state is blank: it is the empty sequence. When an agent is woken up, it perceives the degree d of its initial position, i.e., its memory state becomes the sequence (d). Further on, the memory state of an agent changes when it visits a node. It is caused by the following three types of events: entering a node by the agent, meeting other agents, and leaving a node by the agent. A change of a memory state of an agent is done by appending to its current memory state a sequence of symbols defined as follows. The change due to entering a node of degree d by port number p, consists of appending the sequence \((p,d)\) to the current memory state of the agent. The change due to leaving a node by port q consists of appending q to the current memory state of the agent. The change due to meeting other agents is defined as follows. When entering a node v the agent considers all meetings with other agents that occurred since leaving the previous node. Suppose that the current memory states of the agents met in this time interval by agent \(\lambda \) were \(\sigma _1,\ldots , \sigma _k\), in lexicographic order, regardless of the order of meetings in this time interval and disregarding repeated meetings corresponding to the same memory state (and thus to the same agent). Agent \(\lambda \) appends the sequence of symbols \(([\sigma _1]\ldots [\sigma _k])\) to its current memory state. When two or more of these events occur simultaneously, for example an agent meets another agent when it enters a node, or an agent meets simultaneously several agents, then the appropriate sequences are appended to its current memory one after another, in lexicographic order. When in the previous memory state the agent made a decision to stay idle at the current node, then its memory state can change only if and when some other agent enters this node. This completes the description of how the memory states of agents evolve. Notice that after traversing an edge the action of agent \(\lambda \) consisting of appending a sequence of symbols \([\sigma ]\) due to a meeting with an agent with current memory state \(\sigma \) since leaving the previous node, is performed by \(\lambda \) at most once. Since the number of agents is finite, this implies that, by any given moment in time, the memory state of an agent has changed only a finite number of times, and each time a finite sequence of symbols has been appended. Hence memory states are indeed finite sequences of symbols.

The decisions of agents are made always when an agent is at a node and they are of three possible types: an agent can decide to stay idle, it can decide to exit the current node by some port, or it can elect a leader and stop forever. All these decisions are based on the memory state of the agent after entering the current node and are prescribed by the algorithm. (Recall that agents execute the same deterministic algorithm.) If an agent decides to stay at a given node, then it remains idle at it until another agent enters this node. At this time the memory state of the idle agent changes, and in the new memory state the agent makes a new decision. If an agent decides to leave the current node by a given port, it walks in the edge in the way prescribed by the adversary and makes a new decision after arriving at the other end of the edge. Finally, if an agent decides to elect a leader, it either elects itself, or it decides that it is not a leader, in which case it has to give a sequence of port numbers leading from its own initial position to the initial position of the leader: this is the meaning of the requirement that every non-leader has to know which agent is the leader.

3 Feasibility of leader election

In this section we express the necessary and sufficient condition on eligibility of an initial configuration and we formulate the main result of this paper. We first introduce some basic terminology.

We will use the following notion from [48]. Let G be a graph and v a node of G. We first define, for any \(l \ge 0\), the truncated view \(\mathcal{V }^l(v)\) at depth l, by induction on l. \(\mathcal{V }^0(v)\) is a tree consisting of a single node \(x_0\). If \(\mathcal{V }^l(u)\) is defined for any node u in the graph, then \(\mathcal{V }^{l+1}(v)\) is the port-labeled tree rooted at \(x_0\) and defined as follows. For every node \(v_i, i=1,\ldots ,k\), adjacent to v, there is a child \(x_i\) of \(x_0\) in \(\mathcal{V }^{l+1}(v)\) such that the port number at v corresponding to edge \(\{v,v_i\} \) is the same as the port number at \(x_0\) corresponding to edge \(\{x_0,x_i\}\), and the port number at \(v_i\) corresponding to edge \(\{v,v_i\} \) is the same as the port number at \(x_i\) corresponding to edge \(\{x_0,x_i\}\). Now node \(x_i\), for \(i=1,\ldots ,k\) becomes the root of the truncated view \(\mathcal{V }^l(v_i)\).

The view from v is the infinite rooted tree \(\mathcal{V }(v)\) with labeled ports, such that \(\mathcal{V }^l(v)\) is its truncation to level l, for each \(l\ge 0\). For an initial configuration in which node v is the initial position of an agent, the view \(\mathcal{V }(v)\) is called the view of this agent.

We will also use a notion similar to that of the view but reflecting the positions of agents in an initial configuration. Consider a graph G and an initial configuration of agents in this graph. Let v be a node occupied by an agent. A function \(f\) that assigns either 0 or 1 to each node of \(\mathcal{V }(v)\) is called a binary mapping for \(\mathcal{V }(v)\). A pair \((\mathcal{V }(v),g)\), where f is a binary mapping for \(\mathcal{V }(v)\), such that \(f(x)=1\) if and only if x corresponds to an initial position of an agent, is called the enhanced view from v. Thus, the enhanced view of an agent additionally marks in its view the nodes corresponding to initial positions of other agents in the initial configuration.

For any route \(R=(e_1,e_2,\ldots , e_k)\) such that \(e_i=\{v_{i-1},v_i\}\), we denote \(b(R)=v_0\) and \(d(R)=v_k\), and we say that R leads from \(v_0\) to \(v_k\) in G. Since nodes of G are unlabeled, agents traveling on a route are aware only of the port numbers of the edges they traverse. Hence, it will be usually more convenient to refer to these sequences of port numbers rather than to the edges of the route. Any finite sequence of non-negative integers will be called a trail.

We define an operator \(\mathcal{T }\), that provides the trail corresponding to a given route. More formally, if \(R=(\{v_1,v_2\},\{v_2,v_3\},\ldots ,\{v_{j-1},v_j\})\) is a route in \(G\), then define \(\mathcal{T }(R)=(p_1,\ldots ,p_{2j-2})\) to be the trail such that \(p_{2i-1}\) and \(p_{2i}\) are the port numbers of \(\{v_i,v_{i+1}\}\) at \(v_i\) and \(v_{i+1}\), respectively, for \(i=1,\ldots ,j-1\). We say that a trail \(T\) is feasible from v in \(G\), if there exists a route R in G such that \(b(R)=v\) and \(\mathcal{T }(R)=T\), and in such a case the route R is denoted by \(\mathcal{R }(v,T)\).

For a sequence \(A=(a_1,\ldots ,a_k)\) we denote by \(\overline{A}\) the sequence \((a_k,a_{k-1},\ldots ,a_1)\). For two sequences \(A=(a_1,\ldots ,a_k)\) and \(B=(b_1,\ldots ,b_r)\) we write \((A,B)\) to refer to the sequence \((a_1,\ldots ,a_k,b_1,\ldots ,b_r)\).

For any agent \(\lambda \), let \(h(\lambda )\) denote its initial position. Consider two agents \(\lambda \) and \(\lambda ^{\prime }\). Consider any route \(R\) leading from \(h(\lambda )\) to \(h(\lambda ^{\prime })\) and let \(T=\mathcal{T }(R)\). If \(T=\overline{T}\), then we say that the route \(R\) is a palindrome. For a given initial configuration, a palindrome \(R\) is called uniform, if for any route \(R^{\prime }\) such that \(\mathcal{T }(R^{\prime })=\mathcal{T }(R)\), whenever \(b(R^{\prime })\) is occupied by an agent, then \(d(R^{\prime })\) is also occupied by an agent.

We are now ready to formulate our condition on an initial configuration, that will be called EC (for eligibility condition) in the sequel:

Enhanced views of all agents are different and (There exist agents with different views or There exists a non-uniform palindrome)

We now formulate our main result whose proof is the objective of the rest of the paper.

Theorem 1

Assume that all agents are provided with an upper bound \(n\) on the size of the graph. Then an initial configuration is eligible if and only if condition EC holds for this configuration. Moreover, there exists an algorithm electing a leader for all eligible configurations, regardless of the actions of the adversary.

4 The negative results

In this section we prove two negative results concerning the feasibility of leader election. Impossibility results concerning breaking symmetry by anonymous agents navigating in anonymous networks are based on general ideas going back to Angluin [3]. In our scenario we use a perfectly synchronous adversary that forces agents to move in lock-step, and we prove that for each agent there is another agent that has the same memory state at each round, and consequently leader election is impossible because at least two leaders would have to be elected. A similar argument was used in [28] in the context of rendezvous, but in the present situation we need to deal with an additional problem that was absent in [28]: showing that the two agents have the same memory state after each of them meets other agents. This will have to be argued in the proof of Lemma 2.

The first result shows that condition EC is necessary to carry out leader election, even if the graph (and hence its size) is known to the agents.

Proposition 1

Suppose that the condition EC does not hold for the initial configuration. Then there exists an adversary, such that leader election cannot be accomplished for this configuration, even if the graph is known to the agents.

The proof of Proposition 1 is split into two lemmas. Condition EC can be abbreviated as \(\alpha \wedge (\beta \vee \gamma )\), where \(\alpha \) is “Enhanced views of all agents are different”, \(\beta \) is “There exist agents with different views”, and \(\gamma \) is “There exists a non-uniform palindrome”.

Lemma 1

Condition \(\alpha \) is necessary for leader election.

Proof

Fix an initial configuration. Suppose that \(\alpha \) does not hold. This means that there exist agents \(\lambda \) and \(\lambda ^{\prime }\) with the same enhanced view. This in turn implies that for every agent \(\mu \) there exists an agent \(\mu ^{\prime }\) that has the same enhanced view as \(\mu \). Indeed, if \(T\) is the trail corresponding to a route that leads from \(h(\lambda )\) to \(h(\mu )\), agent \(\mu ^{\prime }\) is the agent whose initial position is at the end of the route corresponding to \(T\) and starting at \(h(\lambda ^{\prime })\). For every agent \(\mu \) we will call the agent \(\mu ^{\prime }\) its twin. Consider a hypothetical leader election algorithm and the “perfectly synchronous” adversary that starts the execution of the algorithm simultaneously for all agents and moves all of them with the same constant speed. Such an adversary induces rounds which are units of time in which all agents traverse an edge. The beginning of a round coincides with the end of the previous round. Hence at the beginning and at the end of each round every agent is at a node. If agents meet inside an edge, they must meet exactly in the middle of a round in which they traverse an edge in opposite directions. We will show that the memory state of twins is identical at the end of each round. This implies that leader election is impossible, as an agent elects a leader when it is at a node, and consequently if some agent elects itself as a leader, its twin would elect itself as well, violating the uniqueness of the leader.

The invariant that the memory state of twins is identical at the end of each round is proved by induction on the round number. It holds at the beginning, due to the same degree of initial positions of twins. Suppose that after some round \(i\) the memory states of twins are identical. Consider twins \(\mu \) and \(\mu ^{\prime }\). In round \(i+1\) they exit by the same port number and enter the next node by the same port number. If in round \(i+1\) they don’t meet any agent in the middle of the edge, at the end of the round \(\mu \) must meet agents with the same memory states as those met by \(\mu ^{\prime }\) (if any), and hence memory states of \(\mu \) and \(\mu ^{\prime }\) at the end of round \(i+1\) are identical. If in round \(i+1\) agent \(\mu \) meets some agents in the middle of the edge, then agent \(\mu ^{\prime }\) must meet exactly the twins of these agents in the middle of the edge. By the inductive hypothesis, these twins have the same memory states as agents met by \(\mu \) and hence again, at the end of the round the memory states of \(\mu \) and \(\mu ^{\prime }\) are identical. This concludes the proof of the lemma. Notice that the argument holds even when agents know the graph in which they operate. \(\square \)

Lemma 2

Condition \(\beta \vee \gamma \) is necessary for leader election.

Proof

Suppose that \(\beta \vee \gamma \) is false. This means that views of all agents are identical and every palindrome for the initial configuration (if any) is uniform. For any trail \(\pi \) that yields a uniform palindrome, this gives a partition of all agents into pairs \((\mu _{\pi }, \mu ^{\prime }_{\pi })\) of agents at the ends of routes that correspond to this trail.

Again we consider the “perfectly synchronous” adversary described in the proof of Lemma 1. There are two subcases. If there is no palindrome in the initial configuration, then we prove the following invariant, holding at the beginning of each round, by induction on the round number: the memory state of all agents is the same and there is no palindrome between agents. The invariant holds at the beginning by assumption. Suppose it holds after round i. In round \(i+1\) all agents choose the same port number and enter the next node by the same port number. There are no meetings in round \(i+1\). Indeed, the only meeting could be in the middle of an edge but this would mean that agents were joined by a one-edge palindrome at the beginning of round \(i+1\). If a pair of agents were joined by a palindrome after round \(i+1\), they would have to be joined by a palindrome longer or shorter by two edges at the beginning of the round, contradicting the inductive assumption. Hence the invariant holds by induction.

The second subcase is when there is a palindrome in the initial configuration (and hence all such palindromes are uniform). Now we prove the following invariant holding in the beginning of each round: the memory state of all agents is the same and every agent is at the end of a palindrome corresponding to the same trail. The invariant holds at the beginning by the assumption. Suppose the invariant holds after round i. In round \(i+1\) all agents choose the same port number and enter the next node by the same port number. If after round i no pair of agents were at the ends of an edge with both ports equal, or they were but agents did not choose this port in round \(i+1\), then no meeting occurred and the invariant carries on after round \(i+1\). If after round i every pair of agents were at the ends of an edge with both ports p and the agents chose this port in round \(i+1\), then meetings of agents with identical memory states occurred in pairs in the middle of each joining edge. Since meeting agents had identical memory state during the meeting, this holds also after round \(i+1\) and agents are again in pairs at the ends of edges with both ports p. Thus the invariant holds at the end of round \(i+1\).

Hence at the beginning of each round the memory state of all agents is the same. This implies that with the “perfectly synchronous” adversary leader election is impossible. Notice that the argument holds even when agents know the graph in which they operate. \(\square \)

Now Proposition 1 follows directly from Lemmas 1 and 2.

Our second negative result shows that the assumption about the knowledge of an upper bound on the size of the graph cannot be removed from Theorem 1.

Fig. 1
figure 1

a Configuration \(C_1\) in a ring of size \(m=5\); b configuration \(C_2\) in the corresponding ring of size \(4tm\). Special nodes are encircled

Proposition 2

There is no algorithm that accomplishes leader election regardless of the adversary for all initial configurations satisfying condition EC.

Proof

Suppose for a contradiction that such a universal algorithm \(\mathtt{A}\) exists. Consider an “almost” oriented ring of size m: ports 0,1 are in the clockwise direction at each node except one, where they are counterclockwise. This node is called special. The initial configuration on this ring consists of two agents: one at the special node, and one at the neighbor clockwise from it. Call this configuration \(C_1\) (cf. Fig. 1a). This configuration satisfies condition EC: agents have different views. Hence algorithm \(\mathtt{A}\) must elect a leader for this configuration, regardless of the adversary. Consider a “perfectly synchronous” adversary that starts the execution of the algorithm simultaneously for all agents and moves all of them with the same constant speed. It induces rounds corresponding to edge traversals by all agents. Suppose that a leader is elected for this adversary after t rounds.

Now consider a ring of size \(4tm\) in which there are \(4t\) special nodes at distances \(m\): at these nodes ports 0, 1 are in the counterclockwise direction, and in all other nodes they are in the clockwise direction. The initial configuration consists of \(8t+1\) agents. There is an agent at every special node and at every clockwise neighbor of a special node. Additionally there is an agent at the counterclockwise neighbor of one special node. Call this configuration \(C_2\) (cf. Fig. 1b). This configuration satisfies condition EC. Indeed, due to the single group of three consecutive agents, all agents have distinct enhanced views. On the other hand, agents at special nodes have a different view from agents at clockwise neighbors of special nodes. Hence algorithm \(\mathtt{A}\) must elect a leader for this configuration as well, regardless of the adversary. Consider the same “perfectly synchronous” adversary as before.

Consider the agent \(\lambda \) in the configuration \(C_2\) that is initially situated at the special node \(v\) antipodal to the special node with both neighbors hosting agents. Consider the agent \(\lambda ^{\prime }\) initially situated at the special node \(v^{\prime }\) that is clockwise from \(v\) and closest to \(v\). In the first \(t\) rounds of the execution of \(\mathtt{A}\) starting from configuration \(C_2\), memory states of agents \(\lambda \) and \(\lambda ^{\prime }\) are identical to memory states of the agent \(\mu \) initially situated at the special node of configuration \(C_1\). This is due to the large size of the ring in configuration \(C_2\). Hence if in configuration \(C_1\) agent \(\mu \) elects itself, then in configuration \(C_2\) agents \(\lambda \) and \(\lambda ^{\prime }\) elect each of them itself as the leader after \(t\) rounds. If in configuration \(C_1\) agent \(\mu \) elects its neighbor, then in configuration \(C_2\) agents \(\lambda \) and \(\lambda ^{\prime }\) elect each of them their neighbor as the leader after \(t\) rounds. In both cases two different agents are elected, which is a contradiction. \(\square \)

5 The algorithm and its correctness

In this section we present an algorithm that elects a leader for all initial configurations satisfying condition EC, assuming that an upper bound on the size of the graph is known to all agents. In view of Proposition 2, this assumption cannot be removed. This upper bound, denoted by n, is an input of our algorithm.

The section is divided into three subsections. In the first subsection we provide additional terminology and notation, as well as some auxiliary results used in the algorithm and in its analysis. In the second subsection we give the intuitive overview of the algorithm and its formal description, and we provide some illustrative examples of its functioning. Finally, the third subsection is devoted to the proof that the algorithm is correct.

5.1 Additional notions and auxiliary results

Let G be a graph and let \(v\) be any node of \(G\). For any integer \(l>0\), we define the code of \(\mathcal{V }^l(v)\) as a sequence of integers denoted by \(\xi (\mathcal{V }^l(v))\) and obtained as follows. Perform the complete depth first search traversal of \(\mathcal{V }^l(v)\), starting at its root, in such a way that at each node an edge with a smaller port number is traversed prior to an edge with a larger port number. Let the \(i\)th traversed edge be from a node \(x_i\) to a node \(y_i, i=1,\ldots ,j\), where \(j\) is the number of edges traversed, and let \(p_i\) and \(p_i^{\prime }\) be the port numbers of the edge \(\{x_i,y_i\}\) at \(x_i\) and \(y_i\), respectively. Then, the length of \(\xi (\mathcal{V }^l(v))\) is \(2j\), and the \((2i-1)\)th and \((2i)\)th elements of \(\xi (\mathcal{V }^l(v))\) are \(p_i\) and \(p_i^{\prime }\), respectively, \(i=1,\ldots ,j\).

The following is a direct consequence of this definition.

Proposition 3

Let \(u\) and \(v\) be any nodes of \(G\) and let \(l>0\) be an integer. \(\mathcal{V }^l(u)\ne \mathcal{V }^l(v)\) if and only if \(\xi (\mathcal{V }^l(u))\ne \xi (\mathcal{V }^l(v))\). \(\square \)

Let \(l>0\) be an integer. We extend the notion of binary mappings to the truncated views. We say that \(f\) is a binary mapping for \(\mathcal{V }^l(v)\), if \(f\) assigns either 0 or 1 to each node of \(\mathcal{V }^l(v)\). If \(f\) is a binary mapping for \(\mathcal{V }(v)\) (or for \(\mathcal{V }^{l^{\prime }}(v)\) for some \(l^{\prime }>0\)), then \((\mathcal{V }^l(v),f)\) (where \(l\le l^{\prime }\), respectively) refers to \(f\) restricted to the nodes of \(\mathcal{V }^l(v)\). Given two binary mappings \(f_1,f_2\) for \(\mathcal{V }^l(v)\), we write \(f_1\trianglelefteq f_2\) if \(f_1(x)\le f_2(x)\) for each node \(x\) of \(\mathcal{V }^l(v)\). If \((\mathcal{V }(v),f)\) is the enhanced view from \(v\) and \(f^{\prime }\) is a binary mapping for \(\mathcal{V }^l(v)\) such that \(f^{\prime }\trianglelefteq f\), then the pair \((\mathcal{V }^l(v),f^{\prime })\) is called a partially enhanced view from \(v\). Intuitively, in a partially enhanced view only some nodes corresponding to initial positions of agents are marked.

Let \((\mathcal{V }(v),f)\) be the enhanced view from \(v\), where \(v\) is selected so that there exists an agent \(\lambda \) with \(h(\lambda )=v\). Then, \((\xi (\mathcal{V }^{n-1}(v)),f)\) is called the complete identifier of \(\lambda \). The significance of the notion of a complete identifier is the following. An agent can never get the entire view or the entire enhanced view, as these are infinite objects. However, the following propositions from [40] show that to differentiate two views or two enhanced views, it is enough to consider their truncations to depth \(n-1\). Thus, as stated in Corollary 1, complete identifiers identify agents with different enhanced views.

Proposition 4

([40]) For a \(n\)-node graph \(G\) and for all nodes \(u\) and \(v\) of \(G, \mathcal{V }(u)=\mathcal{V }(v)\) if and only if \(\mathcal{V }^{n-1}(u)=\mathcal{V }^{n-1}(v)\). \(\square \)

Proposition 5

([40]) For a \(n\)-node graph \(G\), for all nodes \(u\) and \(v\) of \(G\), if \((\mathcal{V }(u),f)\) and \((\mathcal{V }(v),f^{\prime })\) are the enhanced views from \(u\) and \(v\), respectively, then \((\mathcal{V }(u),f)=(\mathcal{V }(v),f^{\prime })\) if and only if \((\mathcal{V }^{n-1}(u),f)=(\mathcal{V }^{n-1}(v),f^{\prime })\). \(\square \)

Corollary 1

For a \(n\)-node graph \(G\) and for any agents \(\lambda \) and \(\lambda ^{\prime }\), the enhanced views from \(h(\lambda )\) and \(h(\lambda ^{\prime })\) are equal if and only if the complete identifiers of \(\lambda \) and \(\lambda ^{\prime }\) are equal. \(\square \)

A sequence \(\alpha =(\mathcal{C },f_1,\ldots ,f_j)\) is called a label, if the following conditions hold:

  1. (i)

    \(j>0\) is an integer,

  2. (ii)

    there exists a graph \(G^{\prime }\) with at most \(n\) nodes and there exists a node \(v\) of \(G^{\prime }\) such that \(\mathcal{C }\) is the code of the truncated view to depth \(3(n-1)\) from \(v\) in \(G^{\prime }\),

  3. (iii)

    \(f_i\) is a binary mapping for \(\mathcal{V }^{3(n-1)}(v)\) for each \(i=1,\ldots ,j\),

  4. (iv)

    \(f_1\) is the binary mapping for \(\mathcal{V }^{3(n-1)}(v)\) that assigns 1 only to the root, and \(f_i\trianglelefteq f_{i+1}\) for every index \(i=1,\ldots ,j-1\).

Moreover, we say that \(j\) is the length of the label \(\alpha \), denoted by \(\ell (\alpha )\). Let \(\mathcal{L }_j\) be the set of all labels of length at most \(j\). Note that if \(j>1\), then we do not impose any restrictions other than \(f_{j-1}\trianglelefteq f_{j}\) on the binary mapping \(f_j\). Also notice that the definition of \(\alpha \) does not depend on \(G\), but it depends on \(n\).

5.2 The algorithm

In this section we give a high-level description of the algorithm and its pseudo-code formulation.

An important ingredient of the algorithm are meetings between agents during which information is exchanged. The method that guarantees that some meetings between pairs of agents will occur uses the idea of tunnels introduced in [18] and also used in [28] in the context of rendezvous of two anonymous agents. The routes \(R=(e_1,\ldots ,e_j)\) and \(R^{\prime }\) form a tunnel if \(R^{\prime }=(e_i,e_{i-1},\ldots ,e_1,e_1^{\prime },\ldots ,e_{j^{\prime }}^{\prime })\) for some \(i\in \{1,\ldots ,j\}\) and for some \(j^{\prime }\ge 0\). Moreover, we say that the route \((e_1,\ldots ,e_i)\) is the tunnel core with respect to \(R\). Note that if \(C\) is the tunnel core with respect to \(R\), then \(\overline{C}\) is the tunnel core with respect to \(R^{\prime }\).

Proposition 6

([18]) Let \(\lambda _i\) be an agent with route \(R_i, i=1,2\). If \(R_1\) and \(R_2\) form a tunnel with the tunnel core \(C=(e_1,\ldots ,e_c)\), then \(\lambda _1\) and \(\lambda _2\) are guaranteed to have a meeting such that \((e_1,e_2,\ldots ,e_i)\) and \((e_c,e_{c-1},\ldots ,e_i)\) are the routes of the agents traversed till the meeting, where \(i\in \{1,\ldots ,c\}\). \(\square \)

Informally speaking, if the routes of two agents form a tunnel, then they are guaranteed to have a meeting with the property that the routes traversed to date by the agents give (by taking one of the routes and the reversal of the other) the tunnel core.

Let \(\mathcal{S }_n\) be the set of all integer sequences with terms in \(\{0,\ldots ,n-2\}\), whose length is even and equals at most \(6(n-1)\). Then, we define

$$\begin{aligned} \mathcal{P }^n&= \big (\left( \alpha ,\alpha ^{\prime },T\right) :\alpha ,\alpha ^{\prime }\in \mathcal{L }_{3}\quad \text { and }\quad \ell (\alpha )=\ell (\alpha ^{\prime })\quad \text { and } \\&\quad T\in \mathcal{S }_n\quad \text { and}\quad (\alpha \ne \alpha ^{\prime }\vee (\alpha =\alpha ^{\prime }\wedge T=\overline{T}))\big ), \end{aligned}$$

and let \(\mathcal{P }_i^n\) be the \(i\)th triple in \(\mathcal{P }^n, i=1,\ldots ,|\mathcal{P }^n|\).

In our leader election algorithm we will proceed in phases, and in each phase the label of each agent is fixed. (Due to the fact that the model is asynchronous, the adversary may force the agents to be in different phases in a particular point of time.) The total number of phases for each agent is 3. After the first phase each agent computes its label used in phase 2. These labels are defined in such a way that there exist two agents \(\lambda \) and \(\lambda ^{\prime }\) with different labels. The aim of phase 2 is that agents \(\lambda \) and \(\lambda ^{\prime }\) correctly identify each other’s initial positions in their respective views. After phase 3 every agent can identify the initial positions of all agents in its view and hence is able to perform leader election.

The label of an agent \(\lambda \) used in phase \(p\) is denoted by \(\alpha _p(\lambda ), p=1,2,3\). Label \(\alpha _1(\lambda )\) is computed before the start of phase 1, and \(\alpha _{p+1}(\lambda )\) is computed at the end of phase \(p\), for \(p=1,2,3\). Label \(\alpha _4(\lambda )\) is used to elect the leader at the end of the algorithm. Each phase is divided into \(|\mathcal{P }^n|\) stages. By \(R_{p,s}(\lambda )\) we denote the route traversed by agent \(\lambda \) till the end of stage \(s\) in phase \(p, p=1,2,3, s=1,\ldots ,|\mathcal{P }^n|\). As we prove later, each agent \(\lambda \) starts and ends each stage at its initial position \(h(\lambda )\). Let \(R_{p,0}(\lambda )\) be the route of an agent \(\lambda \) traversed till the beginning of phase \(p\), and hence till the end of phase \(p-1\), whenever \(p>0\). Hence, \(R_{1,0}(\lambda )\) is the route traversed by \(\lambda \) prior to the beginning of the first phase.

Now we give an informal description of Algorithm \(\mathtt{Leader}\mathtt{\text {-}}\mathtt{Election}\). This algorithm is executed by each agent, and \(\lambda \) in the pseudo-code is used to refer to the executing agent. Note that the upper bound \(n\) on the number of nodes of \(G\) is given as an input. The pseudo-code of the algorithm and pseudo-codes of its subroutines are in frames. In the informal description we refer to lines of these pseudo-codes.

First, we discuss Procedure \(\mathtt{Initialization}\) that is called at the beginning of Algorithm \(\mathtt{Leader}\mathtt{\text {-}}\mathtt{Election}\). The agent starts by computing \(\mathcal{P }^n\). This can be done knowing \(n\), without any exploration of the graph because in order to construct \(\mathcal{P }^n\) the agent needs to compute \(\mathcal{S }_n\) and \(\mathcal{L }_3\), and both of them (by the definition) depend only on the integer \(n\). Then, the agent computes \(\mathcal{V }^{3(n-1)}(h(\lambda ))\) by performing a DFS traversal of \(G\) to the depth \(3(n-1)\) (line \(2\)). The function \(f^{\lambda }\) is set (line \(3\)) to be the binary mapping for \(\mathcal{V }^{3(n-1)}(h(\lambda ))\) that assigns \(0\) to all nodes of \(\mathcal{V }^{3(n-1)}(h(\lambda ))\) except for the root. Hence, \((\mathcal{V }^{3(n-1)}(h(\lambda )),f^{\lambda })\) is a partially enhanced view from \(h(\lambda )\). The value of \(\alpha _1(\lambda )\), that will be the label of \(\lambda \) in the first phase, is set to \((\xi (\mathcal{V }^{3(n-1)}(h(\lambda ))),f^{\lambda })\) (line \(4\)).

figure a

Now we informally describe the main part of Algorithm \(\mathtt{Leader}\mathtt{\text {-}}\mathtt{Election}\), refering to the lines of the pseudo-code given below. The \(p\)th iteration of the main ‘for’ loop in lines \(2\)\(13\) is responsible for the traversal performed by the agent in phase \(p, p\in \{1,2,3\}\). An internal ‘for’ loop in lines \(3\)\(10\) is executed and its \(s\)th iteration determines the behavior of \(\lambda \) in stage \(s\). The stage \(s\) of each phase ‘processes’ the \(s\)th element \((\alpha ^{\prime },\alpha ^{\prime \prime },T)\) of \(\mathcal{P }^n\). If \(\alpha _p(\lambda )\notin \{\alpha ^{\prime },\alpha ^{\prime \prime }\}\), then the agent \(\lambda \) does not move in this stage and proceeds to the next one. Otherwise let \(\alpha _p(\lambda )=\alpha ^{\prime }\) (we describe only this case as the other one is symmetric). The agent checks in line \(5\) whether a certain trail \((T,H)\) is feasible from \(h(\lambda )\), and if it is not, then the stage ends. As we prove later, the verification of the feasibility of \((T,H)\) can be done by inspecting \(\mathcal{V }^{3(n-1)}(h(\lambda ))\). If \((T,H)\) is feasible from \(h(\lambda )\), then \(\lambda \) follows the route \(\mathcal{R }(h(\lambda ),(T,H,\overline{T}))\) (line \(6\)), which guarantees that:

  • if the agent \(\lambda \) is located at \(h(\lambda )\) at the beginning of stage \(s\), then \(\lambda \) is located at \(h(\lambda )\) at the end of stage \(s\) (see Lemma 5), and

  • if the route \(\mathcal{R }(h(\lambda ),T)\) leads from \(h(\lambda )\) to the initial position of another agent \(\lambda ^{\prime }\) and \(\alpha _p(\lambda ^{\prime })=\alpha ^{\prime \prime }\), then the routes \(R_{p,s}(\lambda )\) and \(R_{p,s}(\lambda ^{\prime })\) form a tunnel.

The agent ends phase \(p\) by updating its label. This is done by calling Function \(\mathtt{Update}\mathtt{\text {-}}\mathtt{Label}\) in line \(11\), which produces a binary mapping \(f^{\lambda }\) that is used to update the label in line \(12\). After completing the tree phases (for \(p=1,2,3\)) the agent calls Procedure \(\mathtt{Choose}\mathtt{\text {-}}\mathtt{Leader}\) which completes the task of leader election.

figure b

In order to formally describe the trail \(H\) mentioned above we need the following notation. Let \(\alpha ^{\prime }\in \mathcal{L }_3\) be a label of length \(p\in \{1,2,3\}\), let \(s\in \{1,\ldots ,|\mathcal{P }^n|\}\) and let \(v\) be a node of \(G\). We define \(\mathcal{H }(\alpha ^{\prime },s)\) to be the trail that corresponds to the route performed till the end of stage \(s\) of phase \(p\) by an agent \(\lambda ^{\prime }\) whose label equals \(\alpha ^{\prime }\) in phase \(p, \alpha _p(\lambda ^{\prime })=\alpha ^{\prime }\), and whose initial position corresponds to the root of the truncated view \(\mathcal{V }^{3(n-1)}(h(\lambda ^{\prime }))\) in \(\alpha ^{\prime }\). We prove (see Lemma 4) that, for any \(\alpha \) and \(s\) the trail \(\mathcal{H }(\alpha ,s)\) can be computed on the basis of \(\alpha \) and \(s\). The trail \(H\) mentioned above is \(\overline{\mathcal{H }(\alpha ^{\prime \prime },s-1)}\).

Figure 2 illustrates the routes of a pair of agents \(\lambda \) and \(\lambda ^{\prime }\) that execute one iteration of the internal ‘for’ loop in lines \(3\)\(10\) of \(\mathtt{Leader}\mathtt{\text {-}}\mathtt{Election}\), for the same values of \(p\) and \(s\), such that \((\alpha _p(\lambda ),\alpha _p(\lambda ^{\prime }),T)=\mathcal{P }_s^n\). We assume in this example that \(\mathcal{R }(h(\lambda ),T)\) leads from \(h(\lambda )\) to \(h(\lambda ^{\prime })\) in \(G\). Figure 2 gives the prefixes of two routes \(R_{p,s}(\lambda )\) and \(R_{p,s}(\lambda ^{\prime })\) traversed by the two agents. The fact that the routes form a tunnel (as shown in Fig. 2) follows from Lemma 7 proven later. The routes in Fig. 2 are extended to \(R_{p,s}(\lambda )\) and \(R_{p,s}(\lambda ^{\prime })\) once \(\lambda \) goes from \(h(\lambda ^{\prime })\) to its initial position \(h(\lambda )\) by following the route \(\mathcal{R }(h(\lambda ^{\prime }),\overline{T})\) in \(G\), and \(\lambda ^{\prime }\) follows \(\mathcal{R }(h(\lambda ),T)\) in order to return to \(h(\lambda ^{\prime })\), respectively. Note that the routes \(R_{p,s-1}(\lambda ), R_{p,s-1}(\lambda ^{\prime })\) and \(\mathcal{R }(h(\lambda ),T)\) are not edge disjoint.

Fig. 2
figure 2

The routes \((R_{p,s-1}(\lambda ),\mathcal{R }(h(\lambda ),T),\overline{R_{p,s-1}(\lambda ^{\prime })})\) (dashed route) and \((R_{p,s-1}(\lambda ^{\prime }),\mathcal{R }(h(\lambda ^{\prime }),\overline{T}), \overline{R_{p,s-1}(\lambda )})\) (dotted route)

Although we use tunnels, we use them differently than in [18, 28]. First, we are able to construct much simpler routes that form tunnels. This is due to the fact that [18, 28] deals with the rendez-vous problem in finite graphs of unknown size and in infinite graphs. As argued before, for leader election we have to assume that an upper bound n on the size of the graph is known, and we take advantage of knowing n to construct ‘shorter’ tunnels which simplifies our analysis. Second and more importantly, it is not sufficient for our purposes to just generate a meeting for a particular pair of agents—the meetings are generated to perform the exchange of information. In particular, as a result of a meeting that occurs in a tunnel an agent should be able to determine the node (in its own view) corresponding to the initial position of the other agent. This leads us to the following concept of meetings with ‘confirmation’ of a trail.

Definition 1

Let \(T\in \mathcal{S }_n\). Suppose that agents \(\lambda \) and \(\lambda ^{\prime }\) meet. We say that \(\lambda \) confirms \(T\) as a result of this meeting if

  1. (i)

    \(\lambda \) is in stage \(s\) of phase \(p\) and \(\lambda ^{\prime }\) is in stage \(s^{\prime }\) of phase \(p^{\prime }\), where \(p^{\prime }<p\), or \(p^{\prime }=p\) and \(s^{\prime }\le s\),

  2. (ii)

    \(\mathcal{P }_s^n=(\alpha _p(\lambda ),\alpha ^{\prime },T^{\prime })\), where \(T^{\prime }=T\), or \(\mathcal{P }_s^n=(\alpha ^{\prime },\alpha _p(\lambda ),T^{\prime })\), where \(T^{\prime }=\overline{T}\),

  3. (iii)

    if \(T_{\lambda }\) and \(T_{\lambda ^{\prime }}\) are the trails traversed by \(\lambda \) and \(\lambda ^{\prime }\), respectively, till the meeting, then

    $$\begin{aligned} (\mathcal{T }(R_{p,s-1}(\lambda )),T^{\prime }, \overline{\mathcal{H }(\alpha ^{\prime },s-1)})=(T_{\lambda },\overline{T_{\lambda ^{\prime }}}). \end{aligned}$$

As we prove in Sect. 5.3, if an agent \(\lambda \) confirms \(T\) as a result of a meeting with \(\lambda ^{\prime }\), then \(d(\mathcal{R }(h(\lambda ),T^{\prime }))=h(\lambda ^{\prime })\).

Figure 3 depicts the equation in part (iii) of Definition 1. Figure 3a presents the trail

$$\begin{aligned} \left( \mathcal{T }(R_{p,s-1}(\lambda )),T^{\prime }, \overline{\mathcal{H }(\alpha ^{\prime },s-1)}\right) \end{aligned}$$

that is a prefix of the trail corresponding to the route \(R_{p,s}(\lambda )\) followed by the agent \(\lambda \) till the end of stage \(s\) of phase \(p\). Figure 3b presents the trails \(T_{\lambda }\) (dashed line) and \(T_{\lambda ^{\prime }}\) (dotted line) in the case of the meeting that occurs when \(\lambda \) is in stage \(s\) of phase \(p\) and \(\lambda ^{\prime }\) has not completed the traversal of its route till the end of stage \(s-1\) of phase \(p\).

Fig. 3
figure 3

a The trail \((\mathcal{T }(R_{p,s-1}(\lambda )),T^{\prime },\overline{\mathcal{H }(\alpha ^{\prime },s-1)})\); b a meeting of \(\lambda \) and \(\lambda ^{\prime }\) in case when condition (iii) in Definition 1 is satisfied

It remains to describe Function \(\mathtt{Update}\mathtt{\text {-}}\mathtt{Label}\) and Procedure \(\mathtt{Choose}\mathtt{\text {-}}\mathtt{Leader}\) that are called in the Algorithm \(\mathtt{Leader}\mathtt{\text {-}}\mathtt{Election}\).

figure c

We start by giving intuition of the first of them. This procedure is crucial for the entire algorithm, as it takes advantage of memory state exchanges between agents that meet and permits every agent to insert initial positions of all agents in its view. This in turn allows the agents to learn asymmetries in the initial configuration and thus correctly perform leader election. Function \(\mathtt{Update}\mathtt{\text {-}}\mathtt{Label}\) takes as an input the current memory state of an agent \(\lambda \) and returns a binary mapping \(f^{\lambda }\) for its view \(\mathcal{V }^{3(n-1)}(h(\lambda ))\), such that \((\mathcal{V }^{3(n-1)}(h(\lambda )),f^{\lambda })\) is a partially enhanced view for agent \(\lambda \). Agent \(\lambda \) considers memory states \(M_1,\ldots ,M_j\) of all previously met agents at the times of the meetings. The memory state \(M_i, i\in \{1,\ldots ,j\}\), of an agent \(\lambda ^{\prime }\) and the memory state of \(\lambda \) at the time of their meeting permit the agent \(\lambda \) to verify whether \(\lambda \) or \(\lambda ^{\prime }\) confirmed \(T\) as a result of their meeting. If one of the agents confirms \(T\), then \(\lambda \) takes the advantage of this fact to determine the nodes of its view corresponding to initial positions of agents. In particular, \(\lambda \) is able to locate a node in its own view that corresponds to the initial position of \(\lambda ^{\prime }\), because there exists a route corresponding to \(T\) and connecting the initial positions of the two agents. Afterwards, if this route is of length at most \(n-1\), then \(\lambda \) recursively calls Function \(\mathtt{Update}\mathtt{\text {-}}\mathtt{Label}\) for the memory state \(M_i\) (which is shorter than the current memory state of \(\lambda \) and thus recursion is correct). Hence, \(\lambda \) can compute the binary mappings corresponding to memory states of all previously met agents at the times of the meetings. Using trails between initial positions of these agents and \(h(\lambda )\), as well as the obtained binary mappings, agent \(\lambda \) can correctly position all partially enhanced views of these agents in its own view. A call to Function \(\mathtt{Update}\mathtt{\text {-}}\mathtt{Label}\) at the end of phase \(p\) executed by agent \(\lambda \) permits to compute \(\alpha _{p+1}(\lambda )\).

In the formulation of Function \(\mathtt{Update}\mathtt{\text {-}}\mathtt{Label}\) we use the following notions. Let \(u\) and \(v\) be two nodes of \(G\). We say that a function \(\varphi \) assigning to each node of \(\mathcal{V }^{2(n-1)}(u)\) a node of \(\mathcal{V }^{3(n-1)}(v)\) is a transition from \(\mathcal{V }^{2(n-1)}(u)\) to \(\mathcal{V }^{3(n-1)}(v)\), if \(\varphi (x)\) and \(x\) correspond to the same node of \(G\) for each node \(x\) of \(\mathcal{V }^{2(n-1)}(u)\). For any trail \(T\) and any node \(v\), we say that a node \(x\) at depth \(i\) in \(\mathcal{V }(v)\) is at the end of \(T\) from the root, if the length of \(T\) is \(2i\) and the sequence of ports corresponding to the simple path from the root of \(\mathcal{V }(v)\) to \(x\) is \(T\).

In the following example we illustrate one iteration of the ‘for’ loop in lines \(3\)\(14\) of Function \(\mathtt{Update}\mathtt{\text {-}}\mathtt{Label}\). The graph \(G\) is given in Fig. 4a, and let \(n=4\) be an upper bound that was initially provided to each agent. The black nodes of \(G\) are the initial positions of some agents. Denote by \(\lambda _a,\lambda _b\) and \(\lambda _c\) the agents whose initial positions are \(a,b\) and \(c\), respectively. Note that the views from any two nodes of \(G\) are identical in this case. However, the enhanced view from each node of \(G\) is unique. We focus on the instance of Function \(\mathtt{Update}\mathtt{\text {-}}\mathtt{Label}\) executed by \(\lambda _c\) during its meeting with \(\lambda _b\). For simplicity, we show only some subtrees of \(\mathcal{V }^{3(n-1)}(c)\) and \(\mathcal{V }^{3(n-1)}(b)\) in Fig. 4b, c, respectively. Note that the nodes of \(G\), and therefore the nodes of any view, are unlabeled and we provide the labels only for the illustrative purpose. In this example the trail \(T^{\prime }\), computed in line \(4\) of Function \(\mathtt{Update}\mathtt{\text {-}}\mathtt{Label}\) equals \((0,0,1,1)\), which determines the node of the truncated view \(\mathcal{V }^{3(n-1)}(c)\) that corresponds to the root of \(\mathcal{V }^{3(n-1)}(b)\). The black nodes of both views correspond to the initial positions of agents that \(\lambda _c\) and \(\lambda _b\) determined prior to this meeting. The dotted arrows give the part of the transition \(\varphi \) that maps the nodes of \(\mathcal{V }^{2(n-1)}(c)\) to the black nodes of \(\mathcal{V }^{3(n-1)}(b)\). It follows from the definition of the view that, in general, more than one node of \(\mathcal{V }^{n-1}(h(\lambda ))\) can be mapped by \(\varphi \) to a node of \(\mathcal{V }^{3(n-1)}(v)\).

Fig. 4
figure 4

a A graph \(G\); b \(\mathcal{V }^{n-1}(c)\); c a subtree of \(\mathcal{V }^{3(n-1)}(b)\)

We finally present Procedure \(\mathtt{Choose}\mathtt{\text {-}}\mathtt{Leader}\) that is called by Algorithm \(\mathtt{Leader}\mathtt{\text {-}}\mathtt{Election}\) after the third phase. The leader is selected by an agent \(\lambda \) on the basis of the label \(\alpha _4(\lambda )=(\xi (\mathcal{V }^{3(n-1)}(h(\lambda ))), f_1^{\lambda },\ldots ,f_4^{\lambda })\). Let \(x\) be a node at depth at most \(n-1\) in \(\mathcal{V }^{3(n-1)}(h(\lambda ))\) and satisfying \(f_4^{\lambda }(x)=1\). Let \(S\) be a subtree of depth \(n-1\) of \(\mathcal{V }^{3(n-1)}(h(\lambda ))\) rooted at \(x\). We prove later that the pair \((S, f_4^{\lambda })\) is the complete identifier of some agent. Since the initial positions of all agents have been detected till the end of phase \(3\), the agent can determine all complete identifiers and hence elect the leader.

figure d

5.3 Correctness of the algorithm

This section is devoted to the proof that Algorithm \(\mathtt{Leader}\mathtt{\text {-}}\mathtt{Election}\) correctly elects a leader whenever an initial configuration satisfies condition \(EC \), regardless of the actions of the adversary. The proof is split into a series of lemmas. The role of the first lemma is to show that knowing \(\mathcal{V }^{2n-1}(v)\) is enough to check if a trail of any length is feasible from \(v\).

Lemma 3

Let \(v\) be any node of \(G\). Using \(\mathcal{V }^{2n-1}(v)\), the truncated view \(\mathcal{V }^l(v)\) can be computed for any positive integer \(l\).

Proof

If \(l\le 2n-1\), then \(\mathcal{V }^l(v)\) is a subtree of \(\mathcal{V }^{2n-1}(v)\), so we may assume that \(l>2n-1\). We extend the view \(\mathcal{V }^{i}(v)\) to \(\mathcal{V }^{i+1}(v)\) for each \(i=2n-1,\ldots ,l-1\). To this end we perform the following computation for each node \(x\) at depth \(i-(n-1)\) of \(\mathcal{V }^i(v)\). Let \(u\) be the node of \(G\) that corresponds to \(x\). Note that the subtree of \(\mathcal{V }^i(v)\) rooted at \(x\) and containing all descendants of \(x\) in \(\mathcal{V }^i(v)\) is equal to \(\mathcal{V }^{n-1}(u)\). Hence, there exists a node \(x^{\prime }\) in \(\mathcal{V }^{n-1}(v)\) such that \(x^{\prime }\) corresponds to \(u\), in view of the connectedness of \(G\). This implies that there exists a node \(y\) in \(\mathcal{V }^{n-1}(v)\) such that the subtree of \(\mathcal{V }^{2n-1}(v)\) consisting of \(y\) and all its descendants to depth \(n-1\) from \(y\) is equal to \(\mathcal{V }^{n-1}(u)\). Hence, one can find any such node \(y\) of \(\mathcal{V }^{n-1}(v)\). Let \(u^{\prime }\) be the node of \(G\) that corresponds to \(y\). Due to Proposition 4, \(\mathcal{V }^n(u^{\prime })\) is equal to \(\mathcal{V }^n(u)\). Since \(y\) belongs to \(\mathcal{V }^{n-1}(v)\) we obtain that \(\mathcal{V }^n(u^{\prime })\) is a subtree of \(\mathcal{V }^i(v)\) rooted at \(y\), and therefore we can extend the subtree \(\mathcal{V }^{n-1}(u)\) by replacing in \(\mathcal{V }^i(v)\) the subtree rooted at \(x\) with \(\mathcal{V }^n(u^{\prime })\). \(\square \)

Corollary 2

Let \(v\) be any node of \(G\) and let \(x\) be any node of \(\mathcal{V }^{2n-1}(v)\). Then, the subtree of \(\mathcal{V }(v)\) to depth \(l\) and rooted at \(x\) can be computed for any \(l>0\), using \(\mathcal{V }^{2n-1}(v)\). \(\square \)

Corollary 3

Let \(T\) be any trail and let \(v\) be any node of \(G\). Using \(\mathcal{V }^{2n-1}(v)\) it can be verified if \(T\) is feasible from \(v\) in \(G\). \(\square \)

The next lemma shows that given any label \(\alpha \) and the stage number \(s\), it is possible to compute the trail \(\mathcal{H }(\alpha ,s)\) which, informally speaking, is the history of the moves of the agent with label \(\alpha \) till this stage.

Lemma 4

Using a label \(\alpha \in \mathcal{L }_3\) and an integer \(s\in \{1,\ldots ,|\mathcal{P }^n|\}\), the trail \(\mathcal{H }(\alpha ,s)\) can be computed.

Proof

Suppose that \(\alpha \) is of length \(p, p\in \{1,2,3\}\). By definition, \(\alpha =(\xi (\mathcal{V }^{3(n-1)}(v)),f_1,f_2,\ldots ,f_p)\), where \(f_i\) is a binary mapping for \(\mathcal{V }^{3(n-1)}(v)\) and \(v\) is a node of \(G\). First note that \(\mathcal{V }^{3(n-1)}(v)\) can be reconstructed from its code. Suppose that \(\lambda \) is an agent whose initial position is \(v\) and whose label in phase \(p\) is \(\alpha , \alpha _p(\lambda )=\alpha \).

The trail \(\mathcal{H }(\alpha ,s)\) can be computed by simulating the execution of Algorithm \(\mathtt{Leader}\mathtt{\text {-}}\mathtt{Election}\) for the agent \(\lambda \). By its formulation, the agent \(\lambda \) executed Procedure \(\mathtt{Initialization}, p-1\) iterations of the main ‘for’ loop in lines \(2\)\(13\) of Algorithm \(\mathtt{Leader}\mathtt{\text {-}}\mathtt{Election}\), and exactly \(s\) iterations of the nested ‘for’ loop in lines \(3\)\(10\) of Algorithm \(\mathtt{Leader}\mathtt{\text {-}}\mathtt{Election}\) in the \(p\)th iteration of the main ‘for’ loop. Thus results in traversing the route \(\mathcal{R }(h(\lambda ),\mathcal{H }(\alpha ,s))\). Note that \(G\) is unknown to \(\lambda \), but we will reconstruct the route by simulating edge traversals in \(\mathcal{V }(v)\). (While \(\mathcal{V }(v)\) is infinite, it can be reconstructed from \(\mathcal{V }^{3(n-1)}(v)\) to any finite depth, using Corollary 3.)

We prove the lemma by induction on the total number of stages ‘processed’ by an agent. Note that if \(\alpha \in \mathcal{L }_1\) and \(s=0\), then the trail \(\mathcal{H }(\alpha ,s)\) corresponds to the route that is the DFS traversal of \(G\) to depth \(3(n-1)\) and starting at \(v\). This trail can be obtained by performing the DFS traversal of \(\mathcal{V }^{3(n-1)}(v)\) that starts and ends at the root.

Now assume that \(s>0\). In order to simulate the behavior of \(\lambda \) in any stage \(j, j\in \{1,\ldots ,|\mathcal{P }^n|\}\), of phase \(i, i\in \{1,\ldots ,p\}\), one needs to know \(\alpha _i(\lambda )\). By construction, \(\alpha _i(\lambda )=(\xi (\mathcal{V }^{3(n-1)}(v)),f_1,\ldots ,f_i)\). The induction hypothesis and Corollary 3 imply that the second part of the condition in line \(5\) of Algorithm \(\mathtt{Leader}\mathtt{\text {-}}\mathtt{Election}\) can be checked. If \(\alpha _i(\lambda )\notin \{\alpha ^{\prime },\alpha ^{\prime \prime }\}\), where \((\alpha ^{\prime },\alpha ^{\prime \prime },T)=\mathcal{P }_j^n\), then \(\lambda \) does not move in stage \(j\) of phase \(i\). Hence, assume without loss of generality that \(\alpha _i(\lambda )=\alpha ^{\prime }\). This implies that \(\lambda \) executes the instruction in line \(6\) of Algorithm \(\mathtt{Leader}\mathtt{\text {-}}\mathtt{Election}\). By the induction hypothesis, the trail \(\mathcal{H }(\alpha ^{\prime \prime },j-1)\) in lines \(5\) and \(6\) can be computed on the basis of \(\alpha ^{\prime \prime }\) and \(j\). \(\square \)

We say that a route \(R\) is closed if \(b(R)=d(R)\).

The following lemma implies that at the end of each stage each agent comes back to its initial position.

Lemma 5

Let \(\lambda \) be any agent. For every \(p\in \{1,2,3\}\) and for every \(s\in \{0,1,\ldots ,|\mathcal{P }^n|\}\) the route \(R_{p,s}(\lambda )\) is closed.

Proof

Denote by \(R_{p,s}^{\prime }(\lambda )\) the route that the agent \(\lambda \) follows in stage \(s, s\in \{1,\ldots ,|\mathcal{P }^n|\}\), of phase \(p, p\in \{1,2,3\}\). We prove the lemma by induction on the total number of stages processed in all phases by an agent.

First note that \(R_{1,0}(\lambda )\), i.e., the route of \(\lambda \) performed as a result of the execution of line \(2\) of Procedure Initialization is closed. Hence, it remains to prove that if \(R_{p,s}(\lambda ^{\prime })\) is closed for each agent \(\lambda ^{\prime }\), for some \(p\in \{1,2,3\}\) and for some \(s\in \{0,\ldots ,|\mathcal{P }^n|-1\}\), then \(R_{p,s+1}(\lambda )\) is closed as well. Note that \(R_{p,s+1}(\lambda )=(R_{p,s}(\lambda ),R_{p,s+1}^{\prime }(\lambda ))\). Hence, \(b(R_{p,s+1}^{\prime }(\lambda ))=d(R_{p,s}(\lambda ))=h(\lambda )\) and therefore it is enough to argue that \(R_{p,s+1}^{\prime }(\lambda )\) is closed.

Let \(\mathcal{P }_{s+1}^n=(\alpha ^{\prime },\alpha ^{\prime \prime },T)\). If \(\alpha _p(\lambda )\notin \{\alpha ^{\prime },\alpha ^{\prime \prime }\}\), then according to lines \(5\) and \(7\) of Algorithm \(\mathtt{Leader}\mathtt{\text {-}}\mathtt{Election}, R_{p,s+1}^{\prime }(\lambda ^{\prime })\) is empty, i.e., \(\lambda \) does not move in stage \(s+1\) of phase \(p\). In this case the proof is completed. Otherwise, we obtain that

$$\begin{aligned} R_{p,s+1}^{\prime }(\lambda )=\mathcal{R }\left( h(\lambda ), (T^{\prime }, \overline{\mathcal{H }(\alpha ,s)}, \overline{T^{\prime }})\right) , \end{aligned}$$

where \(T^{\prime }\in \{T,\overline{T}\}\) and \(\alpha \in \{\alpha ^{\prime },\alpha ^{\prime \prime }\}\) (see lines \(6\) and \(8\) of Algorithm \(\mathtt{Leader}\mathtt{\text {-}}\mathtt{Election}\)). Let \(u=d(\mathcal{R }(h(\lambda ),T^{\prime }))\). Hence, \(R_{p,s+1}^{\prime }(\lambda )\) is closed if and only if \(\mathcal{R }(u,\mathcal{H }(\alpha ,s))\) is closed. However, by definition, the latter route equals \(R_{p,s}(\lambda ^{\prime })\) for an agent \(\lambda ^{\prime }\) such that its initial position is \(u\) and \(\alpha _p(\lambda ^{\prime })=\alpha \) (if such an agent exists). It follows from the induction hypothesis that \(R_{p,s}(\lambda ^{\prime })\) is closed, which completes the proof of the lemma. \(\square \)

The next lemma shows the importance of confirmation of a trail. It implies that if an agent \(\lambda \) confirms \(T\) as a result of a meeting with \(\lambda ^{\prime }\), then it can correctly situate the initial position of \(\lambda ^{\prime }\) in its view.

Lemma 6

Let \(p\in \{1,2,3\}\), let \(s\in \{1,\ldots ,|\mathcal{P }^n|\}\) and let \(\mathcal{P }_s^n=(\alpha ^{\prime },\alpha ^{\prime \prime },T)\). Suppose that the agent \(\lambda \) meets an agent \(\lambda ^{\prime }\), when \(\lambda \) is in stage \(s\) of phase \(p\). If \(\alpha _p(\lambda )=\alpha ^{\prime }\) and \(\lambda \) confirms \(T^{\prime }=T\), or if \(\alpha _p(\lambda )=\alpha ^{\prime \prime }\) and \(\lambda \) confirms \(T^{\prime }=\overline{T}\) as a result of this meeting, then \(h(\lambda ^{\prime })=d(\mathcal{R }(h(\lambda )),T^{\prime })\).

Proof

Suppose without loss of generality that \(\alpha ^{\prime }=\alpha _p(\lambda )\). Condition (iii) in Definition 1 implies that the route \(R=(R_{p,s-1}(\lambda ),\mathcal{R }(h(\lambda ),(T^{\prime },\overline{\mathcal{H }(\alpha ^{\prime \prime },s-1)})))\) leads from \(h(\lambda )\) to \(h(\lambda ^{\prime })\) in \(G\). Let \(u=d(\mathcal{R }(h(\lambda ),T^{\prime }))\). By Lemma 5, both \(R_{p,s-1}(\lambda )\) and \(\mathcal{R }(u,\overline{\mathcal{H }(\alpha ^{\prime \prime },s-1)})\) are closed. This implies that \(h(\lambda ^{\prime })=d(R)=d(\mathcal{R }(h(\lambda ),T^{\prime }))\) as required. \(\square \)

The following lemma shows that processing an appropriate triple \((\alpha ,\alpha ^{\prime },T)\) by two agents guarantees their meeting confirming \(T\).

Lemma 7

Let \(p\in \{1,2,3\}\). Let \(\lambda \) and \(\lambda ^{\prime }\) be two agents such that \(\mathcal{P }_s^n=(\alpha _p(\lambda ),\alpha _p(\lambda ^{\prime }),T)\) for some \(T\in \mathcal{S }_n\) and \(s\in \{1,\ldots ,|\mathcal{P }^n|\}\). If \(d(\mathcal{R }(h(\lambda ),T))=h(\lambda ^{\prime })\), then prior to the first moment when one of the agents completes phase \(p\), the agents \(\lambda \) and \(\lambda ^{\prime }\) have a meeting as a result of which either \(\lambda \) confirms \(T\) or \(\lambda ^{\prime }\) confirms \(\overline{T}\).

Proof

Suppose without loss of generality that \(\lambda \) ends the traversal of \(R_{p,s-1}(\lambda )\) at the same time or earlier than \(\lambda ^{\prime }\) ends the traversal of \(R_{p,s-1}(\lambda ^{\prime })\). Let \(R_{p,s}^{\prime }(\lambda )\) be the route traversed by \(\lambda \) in stage \(s\) of phase \(p\). By Lemma 5, \(b(R_{p,s}^{\prime }(\lambda ))=d(R_{p,s-1}(\lambda ))=h(\lambda )\). The route \(R_{p,s}^{\prime }(\lambda )\) is constructed as a result of the execution of lines \(5\)\(6\) of Algorithm \(\mathtt{Leader}\mathtt{\text {-}}\mathtt{Election}\) by \(\lambda \). Since \(\alpha ^{\prime \prime }=\alpha _p(\lambda ^{\prime })\) in line \(6\) of Algorithm \(\mathtt{Leader}\mathtt{\text {-}}\mathtt{Election}\), we obtain that \(R_{p,s}^{\prime }(\lambda )\) and \(R_{p,s}(\lambda ^{\prime })\) form a tunnel with the tunnel core \(C=(\mathcal{R }(h(\lambda ),(T,\overline{\mathcal{H }(\alpha _p(\lambda ^{\prime }),s-1)})))\). By Proposition 6, \(\lambda \) and \(\lambda ^{\prime }\) will have a meeting while \(\lambda \) is in stage \(s\) of phase \(p\) and before \(\lambda ^{\prime }\) ends the traversal of \(R_{p,s}(\lambda ^{\prime })\). This implies that (i) of Definition 1 holds. Moreover, (ii) of Definition 1 for \(\alpha ^{\prime }=\alpha _p(\lambda ^{\prime })\) and \(T^{\prime }=T\) is satisfied by assumption. Let \(T_{\lambda }\) and \(T_{\lambda ^{\prime }}\) be the trails traversed by \(\lambda \) and \(\lambda ^{\prime }\), respectively, till the meeting. By Proposition 6, \((\mathcal{T }(R_{p,s-1}(\lambda )),\mathcal{T }(C))=(T_{\lambda },\overline{T_{\lambda ^{\prime }}})\), where \(C\) is the tunnel core. This proves that (iii) of Definition 1 holds. Hence, the agent \(\lambda \) confirms \(T\) as a result of the meeting. \(\square \)

The role of the next lemma is to show that an agent never marks falsely an initial position of another agent in its view.

Lemma 8

Let \(\alpha _p(\lambda )=(\xi (\mathcal{V }^{3(n-1)}(h(\lambda ))),f_1^{\lambda }, \ldots ,f_p^{\lambda })\) be the label of any agent \(\lambda \) in phase \(p\in \{1,2,3\}\). If \(z\) is any node of \(\mathcal{V }^{3(n-1)}(h(\lambda ))\) corresponding to a node of \(G\) that is not an initial position of an agent, then \(f_p^{\lambda }(z)=0\).

Proof

Suppose for a contradiction that an agent \(\lambda \) sets \(f^{\lambda }(z)\) to be \(1\), and \(z\) corresponds to a node of \(G\) that is not an initial position of an agent.

Suppose that the input memory state \(M\) of the agent \(\lambda \) is the shortest that satisfies this property. This assumption implies that if \(f^{\prime }\) is computed by agent \(\lambda \) in line \(8\) of Function \(\mathtt{Update}\mathtt{\text {-}}\mathtt{Label}\) for any memory state \(M_i, i=1,\ldots ,j\), then \((\mathcal{V }^{3(n-1)}(v),f^{\prime })\) is a partially enhanced view from \(v\), where \(v\) corresponds to the node \(x\) at the end of \(T^{\prime }\) from the root in \(\mathcal{V }^{3(n-1)}(h(\lambda ))\). Hence, if \(v\) is an initial position of an agent, then, due to the definition of transition, each node \(y\) from line \(11\) of Function \(\mathtt{Update}\mathtt{\text {-}}\mathtt{Label}\) corresponds to an initial position of an agent.

The latter implies that the node \(x\) in line \(6\) of Function \(\mathtt{Update}\mathtt{\text {-}}\mathtt{Label}\) does not correspond to the initial position of \(\lambda _i\). By construction, \(x\) is at the end of \(T^{\prime }\) from the root in \(\mathcal{V }^{3(n-1)}(h(\lambda ))\). Due to line \(5\) of Function \(\mathtt{Update}\mathtt{\text {-}}\mathtt{Label}\), either \(\lambda \) or \(\lambda _i\) confirms \(T^{\prime }\in \{T,\overline{T}\}\) as a result of their meeting. By Lemma 6, \(d(h(\lambda ),T^{\prime })=h(\lambda _i)\), where \(T^{\prime }\) is determined in line \(4\) of \(\mathtt{Update}\mathtt{\text {-}}\mathtt{Label}\). Thus, \(h(\lambda _i)\) corresponds to the node at the end of \(T^{\prime }\) from the root in \(\mathcal{V }^{3(n-1)}(h(\lambda ))\). The latter implies that \(h(\lambda _i)\) corresponds to \(x\), a contradiction. \(\square \)

The next lemma is a companion result to Lemma 8. It says that if an agent confirms a trail \(T\) as a result of a meeting with \(\lambda ^{\prime }\), then both of them correctly mark their respective initial positions in their views.

Lemma 9

Let \(p\in \{1,2,3\}\). Let \(\lambda \) and \(\lambda ^{\prime }\) be two agents with labels \(\alpha _{p+1}(\lambda )=(\alpha _p(\lambda ),f_{p+1}^{\lambda })\) and \(\alpha _{p+1}(\lambda ^{\prime })=(\alpha _p(\lambda ^{\prime }),f_{p+1}^{\lambda ^{\prime }})\). If agent \(\lambda \) confirms \(T\) as a result of a meeting with agent \(\lambda ^{\prime }\) in phase \(p^{\prime }, p^{\prime }\le p\), then

  1. (i)

    \(f_{p+1}^{\lambda }(x)=1\), where \(x\) is at the end of \(T\) from the root in \(\mathcal{V }^{3(n-1)}(h(\lambda ))\) and \(d(\mathcal{R }(h(\lambda ),T))=h(\lambda ^{\prime })\).

  2. (ii)

    \(f_{p+1}^{\lambda ^{\prime }}(x^{\prime })=1\), where \(x^{\prime }\) is at the end of \(\overline{T}\) from the root in \(\mathcal{V }^{3(n-1)}(h(\lambda ^{\prime }))\) and \(d(\mathcal{R }(h(\lambda ^{\prime }),\overline{T}))=h(\lambda )\).

Proof

Suppose that \(\lambda \) is in some stage of phase \(p^{\prime }, p^{\prime }\le p\), when a meeting with \(\lambda ^{\prime }\) occurs as a result of which \(\lambda \) confirms \(T\). Hence, one of the memory states in line \(2\) of Function \(\mathtt{Update}\mathtt{\text {-}}\mathtt{Label}\) called at the end of phase \(p\) is the memory state \(M_i, i\in \{1,\ldots ,j\}\), of \(\lambda ^{\prime }\) at the time of the meeting. We consider the \(i\)th iteration of the ‘for’ loop in lines \(3\)\(14\) of Function \(\mathtt{Update}\mathtt{\text {-}}\mathtt{Label}\), i.e., informally speaking, the iteration in which \(\lambda \) ‘analyzes’ the meeting with \(\lambda ^{\prime }\). The agent \(\lambda \) determines in line \(5\) of Function \(\mathtt{Update}\mathtt{\text {-}}\mathtt{Label}\) the fact that \(\lambda \) confirms \(T\) as a result of the meeting. Then, in line \(6\) of Function \(\mathtt{Update}\mathtt{\text {-}}\mathtt{Label}, f^{\lambda }(x)\) is set to \(1\), where \(x\) is at the end of \(T^{\prime }\) from the root of \(\mathcal{V }^{3(n-1)}(h(\lambda ))\). By Lemma 6 and by the choice of \(T^{\prime }\) in line \(4\) of Function \(\mathtt{Update}\mathtt{\text {-}}\mathtt{Label}, x\) corresponds to \(h(\lambda ^{\prime })\). Function \(\mathtt{Update}\mathtt{\text {-}}\mathtt{Label}\) returns \(f^{\lambda }\). Due to line \(12\) of Algorithm \(\mathtt{Leader}\mathtt{\text {-}}\mathtt{Election}, f_{p+1}^{\lambda }(x)=1\).

In order to prove the second part of the lemma notice that agent \(\lambda ^{\prime }\) has also access to the memory states of \(\lambda \) and \(\lambda ^{\prime }\) at the time of the meeting. Hence, upon completing phase \(p\) it performs analogous computations as \(\lambda \) during its execution of Function \(\mathtt{Update}\mathtt{\text {-}}\mathtt{Label}\) at the end of phase \(p\) and sets \(f_{p+1}^{\lambda ^{\prime }}(x^{\prime })=1\). \(\square \)

Let \(\lambda \) be an agent and let \(x\) be a node of the truncated view \(\mathcal{V }^{3(n-1)}(h(\lambda ))\) at the end of a trail \(T\) from the root of \(\mathcal{V }^{3(n-1)}(h(\lambda ))\). If the agent \(\lambda \) sets \(f^{\lambda }(x)=1\) during the execution of Function \(\mathtt{Update}\mathtt{\text {-}}\mathtt{Label}\) and \(x\) corresponds to the initial position of some agent \(\lambda ^{\prime }\), then we say that \(\lambda \) noted \(\lambda ^{\prime }\) on \(T\).

A label \(\alpha =(\xi (\mathcal{V }^{3(n-1)}(h(\lambda ))),f_1^{\lambda },\ldots ,f_p^{\lambda })\), where \(p\in \{1,2,3\}\), of an agent \(\lambda \) is complete with respect to \(\lambda ^{\prime }\) if \(f_p^{\lambda }(x)=1\) for each node \(x\) of \(\mathcal{V }^{3(n-1)}(h(\lambda ))\) that corresponds to an initial position of \(\lambda ^{\prime }\). We say that \(\alpha \) is semi-complete with respect to \(\lambda ^{\prime }\) if \(f_p^{\lambda }(x)=1\) for each node \(x\) that corresponds to an initial position of \(\lambda ^{\prime }\) and belongs to a level \(i\le 2(n-1)\) of \(\mathcal{V }^{3(n-1)}(\lambda )\).

The next lemma explains how agents confirm trails as a result of their meetings. Agents with different labels can confirm any trail in \(\mathcal{S }_n\) between their initial positions, while agents with equal labels are able to confirm only ‘palindromes’.

Lemma 10

Let \(p\in \{1,2,3\}\) and let \(\lambda \) and \(\lambda ^{\prime }\) be any two agents.

  1. (i)

    If \(\alpha _p(\lambda )\ne \alpha _p(\lambda ^{\prime })\), then \(\alpha _{p+1}(\lambda )\) is complete with respect to \(\lambda ^{\prime }\).

  2. (ii)

    If \(\alpha _p(\lambda )=\alpha _p(\lambda ^{\prime })\), then prior to the end of its phase \(p\) the agent \(\lambda \) has noted \(\lambda ^{\prime }\) on each trail \(T\) such that \(T\in \mathcal{S }_n,\,T=\overline{T}\) and \(h(\lambda ^{\prime })=d(\mathcal{R }(h(\lambda ),T))\).

Proof

To prove (i) let \(T\in \mathcal{S }_n\) be any trail such that \(\mathcal{R }(h(\lambda ),T)\) leads from \(h(\lambda )\) to \(h(\lambda ^{\prime })\) in \(G\). By construction of \(\mathcal{P }^n, \mathcal{P }_i^n=(\alpha _p(\lambda ),\alpha _p(\lambda ^{\prime }),T)\) for some index \(i\in \{1,\ldots ,|\mathcal{P }^n|\}\), because \(\alpha _p(\lambda )\ne \alpha _p(\lambda ^{\prime })\). By Lemma 7, \(\lambda \) confirms \(T\) or \(\lambda ^{\prime }\) confirms \(\overline{T}\) as a result of a meeting that occurs when the agent is in phase \(p\). This is done by checking the conditions (i), (ii) and (iii) in Definition 1, which can be accomplished by analyzing the memory states of \(\lambda \) and \(\lambda ^{\prime }\) at the time of the meeting. By Lemma 9, \(f^{\lambda }(x)=1\), where \(x\) is at the end of \(T\) in \(\mathcal{V }^{3(n-1)}(h(\lambda ))\), regardless of which agent confirmed a trail as a result of the meeting.

The proof of (ii) is analogous. \(\square \)

Lemma 11

There exist two agents \(\lambda \) and \(\lambda ^{\prime }\) such that \(\alpha _2(\lambda )\ne \alpha _2(\lambda ^{\prime })\).

Proof

If there exist two agents \(\lambda \) and \(\lambda ^{\prime }\) with different views, \(\mathcal{V }(h(\lambda ))\ne \mathcal{V }(h(\lambda ^{\prime }))\), then by Proposition 4, \(\mathcal{V }^{3(n-1)}(h(\lambda ))\ne \mathcal{V }^{3(n-1)}(h(\lambda ^{\prime }))\). Proposition 3 implies \(\alpha _1(\lambda )\ne \alpha _1(\lambda ^{\prime })\), and consequently \(\alpha _2(\lambda )\ne \alpha _2(\lambda ^{\prime })\). Thus, assume in the following that the views from the initial positions of all agents are equal. In view of condition \(EC \) there exists a non-uniform palindrome. Hence, there exist two agents \(\lambda ,\lambda ^{\prime }\) and a trail \(T\), such that \(T=\overline{T}, \mathcal{R }(h(\lambda ),T)\) leads from \(h(\lambda )\) to a node \(u\) that is the initial position of an agent \(\lambda ^{\prime \prime }\), and \(\mathcal{R }(h(\lambda ^{\prime }),T)\) leads from \(h(\lambda ^{\prime })\) to a node \(u^{\prime }\) that is not the initial position of any agent. Let \(x\) and \(x^{\prime }\) be the nodes of \(\mathcal{V }^{3(n-1)}(h(\lambda ))\) and \(\mathcal{V }^{3(n-1)}(h(\lambda ^{\prime }))\), respectively, at the end of \(T\) from the roots. Note that \(x\) corresponds to \(u\) and \(x^{\prime }\) corresponds to \(u^{\prime }\). By Lemma 10(ii), \(\lambda \) noted \(\lambda ^{\prime \prime }\) on \(T\) in phase \(1\) and, by Lemma 8, \(\lambda ^{\prime }\) does not note any agent on \(T\) during the entire execution of its algorithm. Hence, \(f^{\lambda }(x)=1\) and \(f^{\lambda ^{\prime }}(x^{\prime })=0\) after the execution of Procedure \(\mathtt{Update}\mathtt{\text {-}}\mathtt{Label}\) at the end of phase \(1\). Since the views \(\mathcal{V }^{3(n-1)}(h(\lambda ))\) and \(\mathcal{V }^{3(n-1)}(h(\lambda ^{\prime }))\) are equal, we have \(\alpha _2(\lambda )=(\xi (\mathcal{V }^{3(n-1)}(h(\lambda ))),f_1^{\lambda },f_2^{\lambda })\) and \(\alpha _2(\lambda ^{\prime })=(\xi (\mathcal{V }^{3(n-1)}(h(\lambda ^{\prime }))),f_1^{\lambda ^{\prime }},f_2^{\lambda ^{\prime }})\), where \(f_2^{\lambda }\ne f_2^{\lambda ^{\prime }}\). Hence, \(\alpha _2(\lambda )\ne \alpha _2(\lambda ^{\prime })\). \(\square \)

The role of the next lemma is to explain indirect learning of initial positions of other agents. If two agents \(\lambda \) and \(\lambda ^{\prime }\) have equal labels, then they mutually situate their initial positions using a third agent \(\lambda ^{\prime \prime }\) with a different label as an intermediary. Such an agent exists by Lemma 11. Agents \(\lambda \) and \(\lambda ^{\prime }\) can correctly fill their initial positions to depth \(2(n-1)\) in their views due to the fact that the intermediary \(\lambda ^{\prime \prime }\) has their initial positions to depth \(3(n-1)\).

Lemma 12

Let \(\lambda \) and \(\lambda ^{\prime }\) be two agents such that \(\alpha _p(\lambda )\) is complete with respect to \(\lambda ^{\prime }, p\in \{1,2,3\}\). If \(\lambda ^{\prime \prime }\) is any agent such that \(\alpha _p(\lambda ^{\prime \prime })\ne \alpha _p(\lambda )\), then \(\alpha _{p+1}(\lambda ^{\prime \prime })\) is semi-complete with respect to \(\lambda ^{\prime }\).

Proof

Since \(\alpha _p(\lambda ^{\prime \prime })\ne \alpha _p(\lambda )\) we obtain, by the definition of \(\mathcal{P }^n\), that \((\alpha _{p}(\lambda ^{\prime \prime }),\alpha _{p}(\lambda ),T)=\mathcal{P }_i^n\) for some \(i\in \{1,\ldots ,|\mathcal{P }^n|\}\), where \(T\) is such a trail that the route \(R^{\prime \prime }=\mathcal{R }(h(\lambda ^{\prime \prime }),T)\) contains at most \(n-1\) edges and \(d(R^{\prime \prime })=h(\lambda )\). We analyze the execution of Function \(\mathtt{Update}\mathtt{\text {-}}\mathtt{Label}\) by \(\lambda ^{\prime \prime }\) at the end of phase \(p\). By Lemma 7, \(\lambda ^{\prime \prime }\) confirms \(T\) or \(\lambda \) confirms \(\overline{T}\) as a result of their meeting prior to the completion of phase \(p\) by \(\lambda ^{\prime \prime }\), i.e., prior to the execution of Function \(\mathtt{Update}\mathtt{\text {-}}\mathtt{Label}\) we consider. The agent \(\lambda ^{\prime \prime }\) learns this fact in line \(5\) of Function \(\mathtt{Update}\mathtt{\text {-}}\mathtt{Label}\). Then, the condition in line \(7\) of Function \(\mathtt{Update}\mathtt{\text {-}}\mathtt{Label}\) is satisfied by assumption, and \(\lambda ^{\prime \prime }\) verifies this condition by checking whether the length of \(T\) does not exceed \(2(n-1)\). Consequently, \(\lambda ^{\prime \prime }\) finds in line \(9\) of Function \(\mathtt{Update}\mathtt{\text {-}}\mathtt{Label}\) a transition \(\varphi \) that maps the node at the end of \(T\) in \(\mathcal{V }^{3(n-1)}(h(\lambda ^{\prime \prime }))\) to the root of \(\mathcal{V }^{3(n-1)}(h(\lambda ))\), because \(d(R^{\prime \prime })=h(\lambda )\). Since \(R^{\prime \prime }\) is of length at most \(n-1, \lambda ^{\prime \prime }\) sets \(f^{\lambda ^{\prime \prime }}(y)=1\) (in the ‘for’ loop in lines \(10\)\(11\) of Function \(\mathtt{Update}\mathtt{\text {-}}\mathtt{Label}\)) for each node \(y\) at depth at most \(2(n-1)\) in \(\mathcal{V }^{3(n-1)}(h(\lambda ^{\prime \prime }))\) corresponding to \(h(\lambda ^{\prime })\). The latter is due to the fact that \(\alpha _p(\lambda )\) is complete with respect to \(\lambda ^{\prime }\). This proves that \(\alpha _{p+1}(\lambda ^{\prime \prime })\) is semi-complete with respect to \(\lambda ^{\prime }\).

See Fig. 5, where the root and some nodes that correspond to the initial position of \(\lambda ^{\prime }\) have been marked on \(\mathcal{V }^{3(n-1)}(h(\lambda ))\), and we show a transition that allows to determine all nodes in the view of \(\lambda ^{\prime \prime }\) to the depth \(2(n-1)\) corresponding to \(h(\lambda ^{\prime })\). \(\square \)

Fig. 5
figure 5

a \(\mathcal{V }^{3(n-1)}(h(\lambda ))\); b \(\mathcal{V }^{2(n-1)}(h(\lambda ^{\prime \prime }))\); \(\lambda ^{\prime \prime }\) learns all nodes corresponding to \(h(\lambda ^{\prime })\) to the depth \(2(n-1)\) in its view, on the basis of \(\mathcal{V }^{3(n-1)}(h(\lambda ))\)

In view of the next lemma, upon completion of phase 3 an agent has correctly situated all nodes corresponding to initial positions of all agents to depth \(2(n-1)\) in its view.

Lemma 13

Let \(\lambda \) be any agent. Then, \(\alpha _4(\lambda )\) is semi-complete with respect to each agent \(\lambda ^{\prime }\).

Proof

If \(\alpha _3(\lambda )\ne \alpha _3(\lambda ^{\prime })\), then by Lemma 10(i), \(\alpha _4(\lambda )\) is complete with respect to \(\lambda ^{\prime }\), and therefore it is semi-complete with respect to \(\lambda ^{\prime }\). Hence, suppose that \(\alpha _3(\lambda )=\alpha _3(\lambda ^{\prime })\), which implies \(\alpha _2(\lambda )=\alpha _2(\lambda ^{\prime })\). Hence, Lemma 11 implies that there exists an agent \(\lambda ^{\prime \prime }\) such that \(\alpha _2(\lambda ^{\prime \prime })\ne \alpha _2(\lambda )\). Due to Lemma 10(i), \(\alpha _3(\lambda ^{\prime \prime })\) is complete with respect to \(\lambda ^{\prime }\). Note that \(\alpha _2(\lambda )\ne \alpha _2(\lambda ^{\prime \prime })\) implies \(\alpha _3(\lambda )\ne \alpha _3(\lambda ^{\prime \prime })\). Lemma 12 completes the proof. \(\square \)

Our final lemma says that any agent can correctly reconstruct complete identifiers of all other agents. This is due to the fact that every agent has filled initial positions of all agents to depth \(2(n-1)\) in its view.

Lemma 14

Let \(\mathcal{A }=\{(I_1,T_1),\ldots ,(I_a,T_a)\}\) be the set computed in Procedure \(\mathtt{Choose}\mathtt{\text {-}}\mathtt{Leader}\) executed by an agent \(\lambda \). Then, for every agent \(\lambda ^{\prime }\), there exists an index \(i\in \{1,\ldots ,a\}\) such that \(I_i\) is the complete identifier of \(\lambda ^{\prime }\) and \(\mathcal{R }(h(\lambda ),T_i)\) leads from \(h(\lambda )\) to \(h(\lambda ^{\prime })\) in \(G\).

Proof

Let \(\alpha _4(\lambda )=(\xi (\mathcal{V }^{3(n-1)}(h(\lambda ))), f_1^{\lambda },\ldots ,f_4^{\lambda })\) be the label of \(\lambda \) obtained at the end of phase 3. Initially \(\mathcal{A }\) is set to be empty in line \(1\) of Procedure \(\mathtt{Choose}\mathtt{\text {-}}\mathtt{Leader}\). Consider the nodes \(v\) and \(x\) and the function \(f^{\prime }\) from line \(3\) of Procedure \(\mathtt{Choose}\mathtt{\text {-}}\mathtt{Leader}\). By Lemma 8, \(v\) is an initial position of an agent \(\lambda ^{\prime \prime }\), because \(f^{\prime }(x)=1\). Thus, in view of Lemma 13, \((\xi (\mathcal{V }^{n-1}(v)),f^{\prime })\) is the complete identifier of \(\lambda ^{\prime \prime }\). Due to lines \(4\)\(6\) of Procedure \(\mathtt{Choose}\mathtt{\text {-}}\mathtt{Leader}, ((\xi (\mathcal{V }^{n-1}(v)),f^{\prime }),T)\in \mathcal{A }\) for some trail \(T\) such that \(d(\mathcal{R }(h(\lambda ),T))=v=h(\lambda ^{\prime \prime })\). By Lemma 13, for any agent \(\lambda ^{\prime }\), there is a node in \(\mathcal{V }^{n-1}(h(\lambda ))\) corresponding to \(h(\lambda ^{\prime })\). This implies that, for any agent \(\lambda ^{\prime }\), the set \(\mathcal{A }\) contains (at the end of the ‘for’ loop in lines \(2\)\(7\) of Procedure \(\mathtt{Choose}\mathtt{\text {-}}\mathtt{Leader}\)) a pair \((I_i,T_i), i\in \{1,\ldots ,a\}\), such that \(I_i\) is the complete identifier of \(\lambda ^{\prime }\) and \(\mathcal{R }(h(\lambda ),T_i)\) leads from \(h(\lambda )\) to \(h(\lambda ^{\prime })\) in \(G\). \(\square \)

Theorem 2

If the condition \(EC \) is satisfied for an initial configuration, then Algorithm \(\mathtt{Leader}\mathtt{\text {-}}\mathtt{Election}\) correctly elects a leader regardless of the actions of the adversary.

Proof

Let \(\lambda ^*\) be the agent whose complete identifier \(I^*\) is lexicographically smallest among the complete identifiers of all agents. By condition \(EC \) (more precisely by its part saying that all enhanced views are different) and by Corollary 1, agent \(\lambda ^*\) is unique.

Each agent \(\lambda \) computes \(\alpha _4(\lambda )\) as a result of the execution of Algorithm \(\mathtt{Leader}\mathtt{\text {-}}\mathtt{Election}\). By Lemma 14, \(\lambda \) computes (in lines \(1\)\(7\) of Procedure \(\mathtt{Choose}\mathtt{\text {-}}\mathtt{Leader}\)), for each agent \(\lambda ^{\prime }\), the complete identifier of \(\lambda ^{\prime }\) and a trail \(T\) such that the route \(\mathcal{R }(h(\lambda ),T)\) leads from \(h(\lambda )\) to \(h(\lambda ^{\prime })\) in \(G\). By Corollary 1, the complete identifiers uniquely distinguish the agents. Using the lexicographic order \(\preceq \) on the set of all complete identifiers, the agent \(\lambda \) finds in line \(8\) of Procedure \(\mathtt{Choose}\mathtt{\text {-}}\mathtt{Leader}\) the complete identifier \(I=(\mathcal{V }^{n-1}(u),f)\) such that \((I,T)\in \mathcal{A }\) and \(I\preceq I^{\prime }\) for each \(I^{\prime }\) such that \((I^{\prime },T^{\prime })\in \mathcal{A }\) for some trail \(T^{\prime }\). By definition, \(I=I^*\). Then, \(\lambda \) decides in line \(9\) of Procedure \(\mathtt{Choose}\mathtt{\text {-}}\mathtt{Leader}\) that the agent with the initial position \(d(\mathcal{R }(h(\lambda ),T))\) is the leader. This is agent \(\lambda ^*\). \(\square \)

Theorem 2, together with Proposition 1 implies our main result which is Theorem 1 from Sect. 3.

6 Conclusion

We characterized all initial configurations of agents for which leader election is possible and we constructed a universal algorithm electing a leader for all such configurations, assuming that agents know an upper bound on the size of the graph. We observed that the latter assumption cannot be removed. In this paper we focused on the feasibility of leader election under a very harsh scenario in which the adversary controls the speed and the way in which agents move along their chosen routes. This adversarial scenario captures the totally asynchronous nature of mobile agents.

While we gave a complete solution to the problem of feasibility of leader election, we did not try to optimize the efficiency of the algorithm, e.g., in terms of its cost, i.e., of the total or of the maximum number of edge traversals performed by the mobile agents. In fact, any kind of such optimization appears to be quite challenging. It is clear that in order to elect a leader agents have to meet. Already the much simpler problem of optimizing the cost of meeting of two agents in our asynchronous model is open, both when agents have different labels [18] and when they are anonymous, as in our present scenario [28]. In particular, in the latter paper the authors asked if rendezvous of two agents can be accomplished (whenever it is feasible) at a cost polynomial in the size of the graph. Recently, an asynchronous rendezvous algorithm for labeled agents, working at a cost polynomial in the size of the graph and in the length of the shorter label, has been obtained in [22].

Leader election can be used to subsequently accomplish other tasks in networks, e.g., gathering all agents in one node. In our context this can be done as follows. As soon as an agent elects itself as leader, it goes back to its initial position. Every other agent, after electing a leader, goes to the initial position of the leader. (Recall that at the time of electing a leader each agent knows a path from its initial position to the initial position of the leader, and each agent remembers a path to its own initial position at all times.) In this way all agents will eventually gather at the initial position of the leader. Since the leader, at the time of electing itself, knows that it has seen all agents and that it distinguished all distinct agents, it may have counted them. The leader waits until all agents come to join it and declares that gathering is completed.