On Counting the Population Size
OOn Counting the Population Size
Petra Berenbrink Dominik Kaaser Tomasz Radzik Universität HamburgHamburg, Germany {petra.berenbrink, dominik.kaaser}@uni-hamburg.de King’s College LondonLondon, UK [email protected]
Abstract
We consider the problem of counting the population size in the population model . In thismodel, we are given a distributed system of n identical agents which interact in pairs with thegoal to solve a common task. In each time step, the two interacting agents are selected uniformlyat random. In this paper, we consider so-called uniform protocols, where the actions of twoagents upon an interaction may not depend on the population size n . We present two populationprotocols to count the size of the population: protocol Approximate , which computes withhigh probability either b log n c or d log n e , and protocol CountExact , which computes the exactpopulation size in optimal O( n log n ) interactions, using ˜O( n ) states. Both protocols can also beconverted to stable protocols that give a correct result with probability 1 by using an additionalmultiplicative factor of O(log n ) states.
1. Introduction
In this paper we consider the problem of counting the population size in the probabilistic populationmodel . The model was introduced in [5] to model distributed systems of resource-limited mobileagents, which interact with each other in order to solve a common task. The computation of aprobabilistic population protocol can be viewed as a sequence of pairwise interactions of randomlychosen agents. In each interaction, the two participating agents observe each others’ states andupdate their own state according to a transition function common to all agents.In this setting, we are interested in so-called uniform population protocols, where the transitionfunction does not depend on the size of the population, so it can be applied to any populationsize. In the original definition of population protocols in [5], each agent is a copy of the samefinite state machine. Such a protocol is by definition uniform, as its state space has constant size(independent of the size of the population). However, more recent results have shown that manyproblems can be solved much faster if agents are equipped with a number of states that growswith the population size. For example, a simple protocol with a constant number of states solvesthe majority problem in expected O (cid:0) n (cid:1) interactions [17, 20]. On the other hand, a number ofprotocols have been proposed which solve the majority problem in O( n polylog n ) interactions usingO(polylog n ) states [1, 2, 12, 9]. These protocols are all non-uniform since their transition functionsrefer to values which have to be Θ(log n ). We note that Ω( n log n ) interactions are required to reach1 a r X i v : . [ c s . D C ] M a y positive constant probability that each agent participates in at least one interaction, so Ω( n log n )is a lower bound on the number of interactions of a protocol which solves any nontrivial problem.Turning to the problem of counting the population size, either exactly or approximately, wenaturally have to consider population protocol models with growing memory, where lower boundson the required number of states depend on the desired counting accuracy. The exact countingof the population size requires Ω( n ) states, while estimating the population size up to a constantfactor requires Ω(log n ) states.There is a simple and uniform protocol for exact population counting, which completes in expectedΘ( n ) interactions and uses Θ( n ) states: the agents start with one token each and keep combiningthe tokens into bags, propagating at the same time the maximum size of a bag and using thatmaximum as their current output. The transition function of this protocol does not refer in anyway to (any estimate of) the size of the population.In this paper we are interested in uniform population counting protocols which run in O( n polylog n )interactions and use a small number of states (relatively to the lower bounds). We present newprotocols for exact and approximate population counting, which use a significantly smaller numberof states than the previously best protocols shown in [13, 14]. Our protocols have also furtherdesirable properties. Our exact protocol is the first protocol for this problem which completes thecomputation within asymptotically optimal O( n log n ) interactions. A variant of our approximateprotocol is the first O(polylog n )-state, O( n polylog n )-interaction protocol always converging toa value which is within a constant factor from the exact size of the population. To give formalstatements of the previous results and our new results, we have to introduce first some details ofthe model and the way the efficiency of population protocols is measures. The computation model is a population of n agents which are capable of performing local compu-tations. A population protocol is specified by a state space Q , an output domain O , a transitionfunction δ : Q × Q → Q × Q , and an output function ω : Q → O . Each agent has a state q ∈ Q ,which is updated during interactions. The current output of an agent in state q is ω ( q ). Thecurrent configuration of the system is the vector from Q n with the current states of the agents. Thecomputation of a population protocol is a sequence of pairwise interactions of agents. In everytime step, a probabilistic scheduler selects independently and uniformly at random a pair of agentsfor interaction, with the first agent called the initiator and the second the responder . During theinteraction the agents update their states by applying the transition function δ . Such an update isdenoted by ( x, y ) → ( x , y ), where ( x, y ) refers to the states of the agents before the interactionand ( x , y ) = δ ( x, y ) to the states after the interaction.A given problem which we want to solve by population protocols specifies the set of initial(input) configurations, the output domain O , and the desired (output) configurations for giveninput configurations. For the exact population counting problem, all agents are initially in thesame state q and the output domain is the set of positive integers. A desired configuration iswhen all agents output correctly the (exact) size of the population. In the leader election problem,which arises as a sub-problem in many population protocols, including in the protocols for thepopulation counting problem, all agents start with the same initial state and the output domain is O = { leader , follower } . A desired configuration is when exactly one agent outputs that it is theleader.The following two definitions are commonly used to capture important aspects of the timeefficiency of population protocols. The convergence time T C of an execution of a protocol is thenumber of interactions until the system enters a desired configuration and never leaves the set of2esired configurations again. The stabilization time T S of an execution of a protocol is the numberof interactions until the system enters a desired stable configuration, meaning that starting from thisconfiguration no sequence of pairwise interactions can take the system outside of the set of desiredconfigurations. We always have T C ≤ T S for any execution of a protocol, but the stabilization timemay be strictly greater than the convergence time. A population protocol is always correct (orstable), if for each initial configuration the computation reaches a correct stable configuration withprobability 1, or w.h.p. correct , if there is a small (of the order of n − Ω(1) ), but positive, probabilitythat the system settles with an incorrect output or does not settle at all. We say that a protocolconverges (resp. stabilizes) in T ( n ) time w.h.p. (resp. in expectation), if T C (resp. T S ) is at most T ( n ) w.h.p. (resp. in expectation).The second measure of efficiency of population protocols is the required number of states . This isa straightforward notion for simple protocols with constant number of states. For more complexprotocols, transition functions are usually described by pseudo-codes, which refer to variables . Thestate space of a protocol which is defined in this way is the Cartesian product of the ranges of thevariables. If we consider all possible executions of a uniform protocol, then the ranges of somevariables may be very large in terms of n , potentially infinite. We are, however, interested in boundson the ranges of the variables (and thus bounds on the whole state space) that hold w.h.p.A population protocol is called uniform if the same transition function is used for all populationsizes [7]. Uniformity of a protocol is thus a desired property since such protocols can be appliedwithout knowing the size of the population in advance. The original model from [4, 5] is uniformsince the number of states of an agent is constant. It is shown in [4, 5] that with a constant number of states all semilinear predicates (which include,e.g., the majority predicate), can be computed in expected O (cid:0) n log n (cid:1) stabilization time. RecentlyKosowski and Uznanski [19] have shown constant-state protocols for computing the semilinearpredicates and for electing a leader, which w.h.p. have O( n polylog n ) convergence time. Achievingfast O( n polylog n ) stabilization time for the majority and leader election problems requires agrowing state space, as O (cid:0) n − ε (cid:1) stabilization time is not possible with a constant number of states[16, 1].There are a number of protocols for the majority problem and the leader election problemwith O( n polylog n ) stabilization time and O(polylog n ) states [3, 1, 12, 2, 9], but they all arenon-uniform. The transition functions of these protocols depend on n as they use values Ω(log n ) orΩ(log log n ). For example, each agent may be counting its interactions (which are uniform updates,not dependent on n ) and progress to the next phase of the computation when its counter reacheslog n (a non-uniform update).While the computational power of (uniform) constant-space population protocols is well understoodby now, less is known about the power of uniform protocols which allow the state space to grow withthe size of population. Doty et al. [14] show a protocol for the exact population counting problemwhich has O( n log n log log n ) stabilization time and uses O (cid:0) n (cid:1) states, w.h.p. and in expectation.They also formalize the notion of uniform population protocols, modeling the agents as copies ofthe same Turing machine, but describe their protocols using pseudo-codes and take the size of thestate space as the product of the ranges of the variables.Doty and Eftekhari [13] consider the problem of estimating the size of population within a constantfactor, which they view as the problem of computing log n ± O(1). They show a protocol which w.h.p. The expression with high probability (w.h.p.) refers to a probability of at least 1 − n − Ω(1) . (cid:16) n log n (cid:17) convergence time and uses O (cid:16) log n log log n (cid:17) states. Their protocol is not alwayscorrect and they left open the question to design a protocol which uses expected O( n polylog n )interactions and O(polylog n ) states and computes log n ± O(1) with probability 1. An earlier workby Alistarh et al. [1] includes a uniform protocol which uses O( n log n ) interactions and O(log n )states in expectation to compute an integer which w.h.p. is between c log n and c log n , for someconstants 0 < c < < c . This can be viewed as approximating the population size within apolynomial factor. In this paper we present and analyze two uniform protocols, protocol
Approximate for approxi-mating the population size within constant factors, and protocol
CountExact for computing theexact number of agents in the population. Unless stated otherwise we assume that all agents have tooutput the population size or its approximation. The theorems below summarize the performanceof our protocols.
Theorem 1.
Protocol
Approximate is uniform and outputs w.h.p. either b log n c or d log n e .1) It converges in at most O( n log n ) interactions using O(log n · log log n ) states, w.h.p.2) A variant of the protocol stabilizes in O( n log n ) interactions using at most O(log n · log log n ) states, w.h.p.3) Stabilization can also be achieved w.h.p. in O( n log n ) interactions using O(log n · log log n ) states, if not all but only n − log n agents need to output the result. Theorem 2.
The protocol
CountExact is uniform and outputs the exact population size n . Itstabilizes in O( n log n ) interactions and uses ˜O( n ) states, w.h.p. Our protocols improve considerably the time and space bounds of the previous work [14] and [13].Moreover, our approximate protocol is stable, answering the open question posed in [13], and itcalculates a tighter approximation converging to b log n c or d log n e instead of log n ± . n factor away from the lower bound n . We note that in our protocol forexact counting the output value of an agent is a function of the state of this agent, and n is notkept explicitly in the agent’s state. This is consistent with the approach in [14].Our algorithms are based on leader election followed by load balancing phases. A load balancingphase starts with the total load of M tokens in the system and the goal is to relate M to n (the sizeof the population). If this load balancing phase completes with some agents having zero load, thenw.h.p. M ≤ cn , for a fixed constant c >
1. On the other hand, if all agents end up with positiveload, then we must have M ≥ n . To achieve good time and space bounds, we have to carefullycontrol the load balancing phases and integrate them with the process of leader election. To achievestability both of our protocols use an error detection routine and the leader initializes a processtesting if the calculated answer is correct or not.In Section 2 we discuss the auxiliary protocols which we use in our main protocols. We outlineour approximate and exact protocols in Sections 3 and 4, respectively. We define ˜O( f ( n )) = O( f ( n ) · log O(1) ( f ( n ))). . Auxiliary Protocols In this section we define auxiliary protocols that we will apply in our protocols: one-way epidemics,the junta process, leader election, and phase clocks.
One-Way Epidemics.
The goal of one-way epidemics is to spread an information to all membersof the population. The state space of the protocol is { , x } for some x >
0. Initially at least oneagent has the value x , which is then spread to all other agents. The transitions are formally definedas δ ( u, v ) = (max { u, v } , v ). We will refer to one-way epidemics also as broadcast . A naturalextension is maximum broadcast , where agents do not only spread one possible value x , but insteadeach agent starts with its own value from the integer interval [0 , x ]. For maximum broadcast, anagent always adopts the maximum, which is covered by the transition rule above as well. The resulton one-way epidemics carries over immediately to maximum broadcast. The following result iswell-known, see for example [6]. Lemma 3.
Let T bc be the number of interactions required to complete (maximum) broadcast. W.h.p., T bc = O( n log n ) . Junta Process.
The goal of the junta process [18, 8] is to mark Θ( n ε ) agents – the junta . Thestate of each agent v in this process is formed by a triplet ( level v , active v , junta v ) ∈ N × { , } initially set to (0 , , v interacts withan active agent on the same level it increases its level, otherwise it sets active v to 0. Whenever v interacts with an agent on a higher level it sets junta v to 0. Inactive agents adopt the level of theircommunication partner if that is higher. The protocol stabilizes when all agents are inactive. The junta is formed by all agents v that reached the maximum level and have their junta v bit set to 1.The following lemma is shown in [8], here it is adapted to our setting. Lemma 4. ([8]) Let level ∗ = max { level v } be the maximal level reached by the junta process. Allagents become inactive within O( n log n ) interactions, log log n − ≤ level ∗ ≤ log log n + 8 , and thenumber of agents on the maximal level is O( √ n · log n ) , w.h.p. Phase Clocks.
The existence of a non-empty junta of size in [1 , n ε ] allows the agents to synchronizethemselves via so-called phase clocks [6, 18]. The phase clocks allow all agents to divide the time inphases of Θ( n log n ) interactions each. All agents v have a state clock v in { , . . . , m − } where m is a constant. Intuitively, these clock states can be seen as the hours on a clock face. The basic ideain every interaction is that the agents always adopt the larger clock state w.r.t. the circular ordermodulo m . Additionally, in order to keep the clock running, the members of the junta proceed oneadditional step when they interact with another agent on the same clock state. An agent v enters anew phase in interaction t if clock v crosses the boundary between m − ticks .For easy access to the phase clocks, we equip each agent v with a variable phase v of constant sizethat counts the current phase of an agent modulo some constant. Additionally, each agent v has aflag firstTick v . This flag is set to 1 whenever the phase v counter is incremented, and it is set to 0otherwise.Let D i = [ D start i , D end i ] be the interval of interactions in Phase i such that the last agent entersPhase i in interaction D start i and the first agent leaves Phase i in interaction D end i + 1. Note that for all flags we use false (resp. true ) and 0 (resp. 1) interchangeably. emma 5 ([18]) . For any constant c ≥ we can construct a phase clock using m = m ( c ) = O(1) states such that w.h.p. for all phases ≤ i ≤ poly( n ) we have cn log n ≤ D end i − D start i ≤ cn log n +Θ( n log n ) . Leader Election.
In [18] the authors present a stable and uniform protocol to perform leaderelection called leader_elect . It runs in O(log n ) phases of O( n log n ) interactions each.The process starts with junta election in order to start two nested phase clocks. Agents performan interaction of the outer phase clock once per phase of the inner phase clock. In the beginning,every agent runs the protocols for the phase clocks, junta election, and leader election in parallel.Whenever an agent encounters another agent on a higher (junta) level, it resets the clocks and theleader election protocol. In that way, all agents eventually run the phase clocks and the leaderelection process based on the junta on the highest level. The actual leader election process is quitesimple: the set of leaders is halved from phase to phase, and the time is measured by the innerphase clock.For stable leader election the authors of [18] combine their protocol with a slow protocol which isalways correct in the following way: the outer phase clock runs only if at least one leader remains inthe system. If at some point it counts to m , w.h.p. a total of Θ (cid:16) n log n (cid:17) interactions have occurred.At this time, all agents override their current leader state from the slow protocol with that of thefast protocol. When the outer phase clock ticks (i.e., it reaches m ), at least one leader exists. Weequip each agent v with an additional flag leaderDone v , initially set to false . It is set to true assoon as the outer phase clock ticks. Lemma 6 ([18]) . The uniform protocol leader_elect elects a unique leader. It stabilizesin O (cid:16) n log n (cid:17) interactions, using O(log log n ) many states, w.h.p. Furthermore, after at most O (cid:16) n log n (cid:17) interactions all agents v have leaderDone v set to true w.h.p., and at that time there isexactly one leader w.h.p. Fast Leader Election.
In [8] the authors describe a stable leader election protocol that admits atrade-off between the running time and the number of states. When using ˜O( n ) states, their protocolstabilizes w.h.p. in O( n log n ) interactions. In the following, we call this protocol FastLeader-Election . The main idea of the protocol is to use O(log n ) random bits to reduce the number ofactive leaders much faster than in the original protocol from [18]. Lemma 7 ([8]) . The uniform protocol
FastLeaderElection elects a unique leader. It stabilizesin O( n log n ) interactions, using ˜O( n ) many states, w.h.p. Furthermore, after at most O (cid:16) n log n (cid:17) interactions all agents have leaderDone u set to true w.h.p.
3. Approximate Counting
In this section we show Theorem 1. In Section 3.1 we assume that a unique leader exists andwe present a protocol which calculates b log n c or d log n e using a load balancing algorithm. InSection 3.2 we analyze the protocol. In Section 3.3 we show how to combine the protocol fromSection 3.1 with a leader election protocol. This shows the first part of Theorem 1. Finally, inSection 3.4 we show how to build a stable protocol by showing the correctness of our error detectionmechanism. This shows the second and the third part of Theorem 1.6 .1. Approximating n with a Leader In this section we assume that a unique leader is given and that all agents are synchronized via thephase clocks. The main idea of our algorithm is to inject an increasing amount of tokens into thesystem and to use a load balancing routine to estimate the number of agents in the system. Theprocess is finished as soon as roughly n tokens are injected into the system. To save on the numberof states, the agents do not store the exact number of tokens, they hold but the logarithm of thatnumber.Every agent v stores two variables ( k v , searchDone v ) ∈ { − , , , . . . } × { , } , initially set to( − , k v stores the logarithm of the load of agent v , where the special value − searchDone v indicates that the search isfinished. The leader u now orchestrates a linear search over k u ∈ { , , . . . } and finds k ∗ u for whichlog n − < k ∗ u < log n + 1. The protocol runs in rounds consisting of multiple phases each. At thebeginning of round r the leader injects 2 r many tokens into the system. The load is then balancedusing a powers-of-two load balancing process which restricts the load of any agent to a power of two.The search stops once an agent v has load larger than 1 ( k v > u sets searchDone u to true and broadcasts the value to all agents using one-way epidemics.In the classical load balancing process (cf. [10]), it is assumed that m indistinguishable tokensare distributed arbitrarily among n agents. At interaction t , the load vector L ( t ) is definedas L ( t ) = ( ‘ ( t ) , . . . , ‘ n ( t )) ∈ N n , where ‘ i ( t ) is the number of tokens (load) present at agent i at that interaction. Assume agents u and v balance their loads in interaction t ≥
1, then( ‘ u ( t + 1) , ‘ v ( t + 1)) = ( b ( ‘ u ( t ) + ‘ v ( t )) / c , d ( ‘ u ( t ) + ‘ v ) / e ). For the powers-of-two load balancingprocess we define the logarithmic load vector K = ( k , . . . , k n ) ∈ { − , , , . . . } n such that for anyagent v we have ‘ v ( t ) = 2 k v if k v ≥ ‘ v ( t ) = 0 if k v = −
1. Assume now that agent u interactswith agent v . A balancing action is only permitted if either u or v is empty (i.e., k u = − k v = −
1) and both of them are not the leader. Let k u and k v be the resulting logarithmic loadvalues. Then (cid:0) k u , k v (cid:1) = ( k u − , k u −
1) if k u > k v = − k v − , k v −
1) if k u = − k v > k u , k v ) otherwise. (1)The Search
Protocol is defined in Algorithm 1. The protocol runs in rounds of 5 phases. Everyagent v uses the variable phase v to count the number of phases modulo 5 and the flag firstTick v which is set to true when v initiates the first interaction of a phase (see Section 2). In Phase 1 andPhase 4 the leader is active, in the remaining phases the non-leaders are active.• Phase 0 and Phase 1 are used for the initialization. Every agent v which is not the leaderresets k v to − u, v ) in Phase 1 the leader u transfers2 k u tokens to v (Line 3).• In Phase 2, the non-leader agents perform powers-of-two load balancing, as described above.(Line 14). Phase 3 consists of one-way epidemics where the agents communicate their highestload value (Line 16).• In Phase 4 (Line 5), the leader u decides if the search is finished or not. If the maximumlogarithmic load is less than 1 it concludes that the injected load was smaller than or equal tolog n −
1. The leader u therefore proceeds to the next round and injects twice as many tokens.If the observed maximum logarithmic load is larger than 1, the leader concludes the protocolby setting searchDone u to true . 7 Interaction ( u, v ) in the
Search
Protocol : if leader u = true and searchDone u = false then . Leader: if phase u = 1 and firstTick u = true then . Phase 1: load infusion k v ← k u if phase u = 4 and firstTick u = true then . Phase 4: decision if k v ≤ then k u ← k u + 1 else searchDone u ← true if leader u = false and leader v = false then . Followers: if phase u = 0 then . Phase 0: initialize k u ← − if phase u = 2 then . Phase 2: load balancing if min { k u , k v } = − and max { k u , k v } > then k u , k v ← max { k u , k v } − if phase u = 3 then . Phase 3: one-way epidemics k u , k v ← max { k u , k v } Algorithm 1:
The
Search
Protocol, centerpiece of protocol
Approximate
The number of agents is now estimated as 2 k u . In the following, the state of an agent v in thisalgorithm is called s ( v ). The state space S is the Cartesian product of the domains of the individualvariables ( phase v , phase v , firstTick v , leader v , k v , searchDone v ). For an overview over the states ofnode v in the Search
Protocol, see Figure 1. s ( v ) = (cid:16) phase v , firstTick v , | {z } Phase Clocks leader v , | {z } Leader Election k v , searchDone v | {z } Search
Protocol (cid:17) S = { , . . . , } × { , } × { , } × { − , , , . . . } × { , } Figure 1:
State space of the
Search
Protocol (Algorithm 1)
Search
Protocol
Based on the definition in Equation (1) we first show that the powers-of-two load balancing processon n agents balances the load such that the maximum load is at most 1 (i.e., k v ≤ v )as long as at most 3 / n tokens are injected by the leader. Lemma 8.
Assume there exists an agent u with k u (0) = κ and k v (0) = − for all v = u . If κ ≤ / · n , then w.h.p. after t = 16 n log n interactions max v { k v ( t ) } = 0 . The proof of Lemma 8 uses the same ideas and shows a similar statement as in Lemmas 2 and 3from [10] for the classical load balancing process.In the following, we assume that precisely one agent u is the leader and all agents synchronizethemselves via the phase clocks. Based on this assumption we can show the following lemma for the Search
Protocol.
Lemma 9.
After at most
O(log n ) rounds the leader u sets searchDone u to true , w.h.p. At thattime we have w.h.p. that / · n < k u ≤ d log n e . Interaction ( u, v ) of protocol
Approximate : if level v > level u then re-initialize states of u for PhaseClocks , LeaderElection , SearchProtocol JuntaProcess [( level u , junta u ) , ( level v , junta v )] . update auxiliary protocols PhaseClocks [( phase u , firstTick u ) , ( phase v , firstTick v )] if not leaderDone u then . Stage 1: Leader Election [18] LeaderElection [( leader u , leaderDone u ) , ( leader v , leaderDone v )] if leaderDone u and not searchDone u then . Stage 2: Search Stage SearchProtocol [( k u , searchDone u ) , ( k v , searchDone v )] if leaderDone u and searchDone u then . Stage 3: Broadcasting Stage ( searchDone v , k v ) ← ( true , k u ) Algorithm 2:
Protocol
Approximate state of node v : (cid:16) level v , active v , junta v , | {z } Junta Process clock v , phase v , firstTick v , | {z } Phase Clocks leader v , leaderDone v , | {z } Leader Election s ( v ) |{z} Search
Protocol (cid:17) state space: N × { , } × { , } × [ m ] × { , . . . , } × { , } × { , } × { , } × S Figure 2:
State space of protocol
Approximate (Algorithm 2)
Proof.
The
Search
Protocol runs in multiple phases. In the following, we combine every fiveconsecutive phases to a round such that round r ≥ r to 5 r + 4. For a fixedround r let k v ( r ) be the value of variable k of agent v at the beginning of round r . Assuming thatall agents are properly synchronized by the phase clocks (see Lemma 5), we can w.h.p. assumethat the number of interactions in any round is sufficient for spreading information with one-wayepidemics (see Lemma 3) and for the powers-of-two load balancing (see Lemma 8).Observe that the leader sets searchDone u to true in Line 8 of Phase 4 of the Search
Protocol.Let v be an agent with maximal load at the beginning of Phase 3. In Phase 3 the agents usedone-way epidemics to disseminate the value k v to all agents. Hence, searchDone u is set to true inthe first round r where an agent v has k v ( r ) > v has aload of at least two.To show the lemma we consider 3 cases, depending on the value k u ( r ) which corresponds to theload injected by the leader at the beginning of round r ≥ Case k u ( r ) ≤ / · n : In Phase 1 the leader injects 2 r many tokens. According to Lemma 8, w.h.p.at the end of Phase 2 of Round r no agent will have a load larger that one, meaning for all agents v we have k v ≤
0. The maximum value k v which is communicated via one-way epidemics is at mostzero and the leader does not set searchDone u to true in Phase 4 of Round r . Hence, it injects 2 r +1 tokens at the beginning of the next round. Case / · n < k u ( r ) < n : Again, in Phase 1 the leader injects 2 r many tokens. In this case eitherthere exists an agent v with a load larger than one ( k v >
0) at the end of Phase 2, or for all agents v we have k v ≤
0. In the latter case we are back to the previous case: the leader does not set searchDone u to true in Phase 4 of Round r . Hence, it injects 2 r +1 tokens at the beginning of thenext round. If there exists an agent v with k v > searchDone u to true .9 ase n ≤ k u ( r ) : In Phase 1 the leader injects 2 r ≥ n many tokens. The agents balance at least n tokens on n − r there exists an agent with a load of at least two, meaning k v ≥
1. Theleader sets searchDone u to true in Phase 4 of Round r .From the above cases it follows that after at most d log n e rounds searchDone u is set to true andthen 3 / · n < k u ≤ d log n e . Search
Protocol with Leader Election
In this section we combine the
Search
Protocol with the
LeaderElection protocol from [18].The combined protocol works as follows. All agents run the
JuntaProcess and the
PhaseClocks protocols in parallel to the
LeaderElection protocol and, later, the
Search
Protocol. In thejunta process, agents cannot decide whether they have already reached the maximal junta level.Therefore, some agents already perform some interactions of the
LeaderElection protocol orthe
Search
Protocol at a lower junta level. These agents might be badly synchronized, since thephase clock ticks correctly w.h.p. only on the maximal junta level. We therefore define the followingprocedure that has already been used in [18]: Whenever an agent u interacts with an agent v in ahigher level ( level u < level v ) all variables for PhaseClocks , LeaderElection and the
Search
Protocol are re-initialized. In that way, all agents start the
LeaderElection protocol and the
Search
Protocol at the maximal junta level from a clean state.The protocol uses two flags leaderDone v and searchDone v for each agent v , which are initially setto false . The first flag, leaderDone v , is set once an agent has concluded the leader election protocol(see Section 2). The second flag, searchDone v , is set by the Search
Protocol (see Section 3.1).The two flags leaderDone v and searchDone v allow agent v to distinguish between three stages of theexecution of the protocol, the Leader Election Stage (1), the
Search Stage (2), and the
BroadcastingStage (3).In the Leader Election Stage all agents use the protocol from [18] to elect a leader. Recall thatthe flag leaderDone u is set to true once the external phase clock ticks (see Section 2). Note thatat this time there exists exactly one leader w.h.p. [18]. In the Search Stage, we use the Search
Protocol defined in Section 3.1. In the Broadcasting Stage the leader uses one-way epidemics toinform all other agents of its value k u .We now give the proof of the first part of Theorem 1, where we show that protocol Approximate computes either b log n c or d log n e w.h.p. Proof of Statement 1) of Theorem 1.
From Lemma 4 it follows that after O( n log n ) interactions thejunta is elected and all agents are inactive. From that point on no agent in the protocol is everre-initialized again. Let u be the single leader that according to Lemma 6 w.h.p. concludes theleader election protocol. According to Lemma 9, the leader u sets searchDone u to true after atmost d log n e phases of O( n log n ) interactions each, and at that time k u is either b log n c or d log n e w.h.p. According to Lemma 3, within O( n log n ) further interactions all agents know the value k u from the leader u . Together, this gives a total number of O (cid:16) n log n (cid:17) interactions, after which allagents know the value k u from the leader and thus output either b log n c or d log n e w.h.p.Regarding the required number of states we observe that level v from the junta process and k v from the Search
Protocol are the only variables of an agent v that are not of constant size and thusmay grow with the population size n . For an overview over the states of agent v , see Figure 2. Notethat level v is bounded w.h.p. by O(log log n ) Lemma 4, and k v is bounded according to Lemma 9w.h.p. by O(log n ). Together this gives that w.h.p. the state space has size O(log log n · log n ). Thisconcludes the proof of Statement 1) of Theorem 1.10 .4. The Stable Protocol In this section we sketch our stable protocol and proofs of Statements 2) and 3) of Theorem 1. Thedetails can be found in the appendix. Our stable protocol is a hybrid protocol which combines theprotocol
Approximate with a slow protocol which always finds the correct solution.The slow protocol works as follows. Every agent starts with exactly one token. Whenever twoagents interact and both have the same amount of tokens, one of them hands its tokens over tothe other. The agents do not store the exact number of tokens they hold but the logarithm ofthat number. Note that, due to the definition of the process, the load of every agent is a powerof two. After O (cid:0) n log n (cid:1) many interactions one agent will store b log n c many tokens. For each0 ≤ i < b log n c there might be one agent storing 2 i tokens (depending on the value of n ). Thenumber of states required by this process is O(log n ) if it is sufficient for all but log n agents to knowthe approximation for n . Otherwise, the value b log n c has to be sent to every agent via one-wayepidemics. In this case there are up to log n many agents that need O (cid:16) log n (cid:17) many states (exactlythose agents which store a value 2 i ).The hybrid protocol now works as follows. It first runs Approximate , then the leader u checksif the approximated value k u is correct by injecting 2 k tokens and by balancing them. If the valueof k u is not correct it switches the output over to the slow protocol. The hybrid protocol stabilizesw.h.p. within O (cid:16) n log n (cid:17) many interactions using O (cid:16) log log n · log n (cid:17) many states if all agentshave to know the approximation of n , otherwise O(log log n · log n ) states are sufficient.
4. Counting the Exact Population Size
In this section we consider the problem of counting the exact population size and present ourprotocol
CountExact . The main idea is as follows. The protocol first selects a junta using themodified junta process from [8] (see Section 2). It then creates phase clocks and selects a leaderusing the
FastLeaderElection protocol (see Section 2). Again, every agent v has two flags leader v and leaderDone v which indicate whether v is a leader and v has completed the leader electionprotocol, respectively. Recall that the junta uses a variable level v that stores the maximum levelwhich is reached during the junta election. In [8] it has been shown that in the modified juntaprocess the level reaches w.h.p. a value of log log n ± c for some constant c . Hence, we can use2 level v as a first approximation for n . This approximation will be refined in two stages, called ApproximationStage and
RefinementStage , which are described in Section 4.1 and Section 4.2,respectively. The protocol
CountExact can be found in Algorithm 3. The output of every agentin the protocol
CountExact is defined by the
RefinementStage . In this section we assume that a unique leader has been elected and that all agents are synchronizedvia the phase clocks. Additionally, we assume that all agents are on the same maximal junta level level ∗ w.r.t. the modified junta process from [8].The goal of protocol ApproximationStage is to compute log n up to an additive error of ± level u − = n η tokens into the system for some 0 < η ≤
1. Italternates between increasing the number of injected tokens by a factor of n η and load balancinguntil the total number of tokens M is at least n/
2. Unfortunately it is possible that M is very closeto n η , resulting in a multiplicative error. Hence, the protocol outputs k u = log M − b log l u c . InLemma 10 we will show that log n − ≤ k u ≤ log n + 3.11 Interaction ( u, v ) of protocol
CountExact : if level v > level u then re-initialize states of u for PhaseClocks , FastLeaderElection , ApproximationStage , Refine-mentStage JuntaProcess [ level u , level v ] . update auxiliary protocols PhaseClocks [( phase u , firstTick u ) , ( phase v , firstTick v )] if not leaderDone u then . Stage 1: Fast Leader Election FastLeaderElection [( leader u , leaderDone u ) , ( leader v , leaderDone v )] if leaderDone u and not ApxDone u then . Stage 2: Approximation Stage ApproximationStage [( k u , l u , ApxDone u ) , ( k v , l v , ApxDone v )] if leaderDone u and ApxDone u then . Stage 3: Refinement Stage RefinementStage [( k u , l u ) , ( k v , l v )] Algorithm 3:
Protocol
CountExact state of node u : (cid:16) level u , | {z } Junta Process phase u , firstTick u , | {z } Phase Clock leader u , leaderDone u , | {z } Leader Election i u , k u , l u , ApxDone u | {z } Approximation and Refinement Stages (cid:17) state space: N × N × { , } × { , } × { , } × N × { − , , , . . . } × N × { , } Figure 3:
State space of protocol
CountExact (Algorithm 3) Interaction [( k u , l u , ApxDone u ) , ( k v , l v , ApxDone v )] in the ApproximationStage protocol: if firstTick u = true then if leader u = true and i u = 0 then . initialize first phase l u ← if leader u and l u ≥ then . found an approximation ApxDone u ← true k u ← i u · level u − − b log l u c ( i u , l u ) ← (cid:16) i u + 1 , l u · level u − (cid:17) . start new phase: load explosion ( l u , l v ) ← ( b ( l u + l v ) / c , d ( l u + l v ) / e ) . classical load balancing ApxDone u ← max { ApxDone u , ApxDone v } . broadcast ApxDone
Algorithm 4:
CountExact
Approximation Stage Interaction [( k u , l u ) , ( k v , l v )] in the RefinementStage protocol: if phase u = 0 then . initialize agents and broadcast k u ( k u , k v , l u , l v ) ← (max { k u , k v } , max { k u , k v } , , if firstTick u = true then if phase u = 1 and leader u = true then . the leader starts with load · k u l u ← · k u if phase u = 2 then . multiply load with k u l u ← l u · k u ( l u , l v ) ← ( b ( l u + l v ) / c , d ( l u + l v ) / e ) Algorithm 5:
CountExact
Refinement Stage n log n ) interactions each (the phases are determined by the phase clock). Every agent v usesthe variables i v (initialized to 0) as a phase counter and l v for load balancing. The leader u hasan additional variable k u in which it eventually stores the approximation. In the first phase, everyagent v initializes its load l v to 0 (non-leaders) or to 1 (leader). In the first interaction of everyphase, every agent v increases the phase counter i v and multiplies its load with n η (see Line 7).During the remainder of the phase, all agents us the classical load balancing process from [10] tobalance their tokens. The leader u additionally checks before the multiplication whether it has aload of at least 4 (in which case the total load is at least 2 n w.h.p.). If this is the case, the leadercalculates the approximation k u as i u · η − b log l u c and raises the flag ApxDone u , indicating thatthe approximation stage has concluded (see Line 5). The flag is then sent to all other agents viaone-way epidemics (see Line 9), and the raised flag terminates the process. Lemma 10.
Let u be the leader. After at most O( n log n ) interactions of the Approximation Stageall agents v set ApxDone v to true w.h.p. At that time, k u = log n ± w.h.p. The protocol usesw.h.p. at most ˜O( n ) states.Proof. First we bound the number of phases until all agents v have ApxDone v = true . When theleader u sets ApxDone u to true , all other agents follow via one-way epidemics in the same phase,w.h.p. Therefore it suffices to bound the number of phases until the leader u sets ApxDone u to true .Let ˆ ι be the first phase in which the leader has a load of at least 4. From load balancing [10] itfollows that the total load M at that time is at least 2 n . At the beginning of Phase 1 the leader u has n η = 2 level v − tokens and all other agents are empty. In the first interaction of Phase i > v multiply their load with n η (see Line 7). Hence, in Phase i ≤ ˆ ι the total load is M = n iη ,w.h.p. and in Phase ˆ ι we have 2 n ≤ n ˆ ιη ≤ · n η (see [10]). From Lemma 4 we obtain that w.h.p.log log n − ≤ level v and therefore η ≥ / w.h.p. Since n ˆ ιη ≤ · n η we get ˆ ι ≤ /η + 1 + o(1).Hence the number of phases ˆ ι until u sets ApxDone u to true is bounded by a constant ˆ ι = O(1).We now bound the quality of the approximation k u . In Phase ˆ ι , the leader sets its variable k u to k u = ˆ ι · level u − − b log l u c . From load balancing [10] we get l u ≤ ˆ ι · level u − /n + 1 . k u ≥ ˆ ι · level u − − log (cid:16) ˆ ι · level u − /n + 1 . (cid:17) ≥ ˆ ι · level u − − log (cid:16) ˆ ι · level u − /n · (1 + 1 . / (cid:17) (2)= ˆ ι · level u − − log (cid:16) ˆ ι · level u − (cid:17) + log n − log 1 . ≥ log n − , where (2) holds since 2 ˆ ι · level u − ≥ n . Analogously, k u ≤ ˆ ι · level u − − log l u + 1 ≤ ˆ ι · level u − − log (cid:16) ˆ ι · level u − /n · (1 − . / (cid:17) + 1 ≤ log n + 3 . It remains to bound the required number of states. For each agent v , the variables i v and k v require w.h.p. at most O(log n ) many states, and the variable l v stores up to O( n η ) ≤ n manytokens. 13 .2. Refining the Approximation As before, we assume that a unique leader has been elected and that all agents are synchronized viathe phase clocks. We also assume that the leader holds an approximation of log n ± v holds a variables k v for the approximation of log n and avariable l v that is used for load balancing. The protocol consists of 3 phases. The first phase isused for initialization. The value k u (the approximation calculated by the leader in the previousstage) is spread among all agents via one-way epidemics (Line 2). In the beginning of the secondphase the leader injects 2 · k u many tokens into the system which are balanced, as before. In thebeginning of third phase every agent multiplies its load with 2 k u , and then the load is balancedagain. In Lemma 11 we show that total number of tokens M is bounded by 2 · n ≤ M ≤ · n .At the end of the phase every agent can compute n as its output function ω ( v ) = b · k v /l v e . Lemma 11.
After at most O( n log n ) interactions of the Refinement Stage, all agents v output ω ( v ) = n w.h.p. The protocol uses w.h.p. at most ˜O( n ) states.Proof. At the end of Phase 0, w.h.p. all agents know k u from the leader according to one-wayepidemics. At the end of Phase 1, w.h.p. C · k u tokens have been balanced such that l v = Θ(1) forany agent v . At the end of Phase 2, a total of at least M ≥ · n tokens have been balanced andevery agent v has load l v = b C · k u /n e ± v can output n using its output function ω ( v ) = b C k v /l v e ,provided that v knows the value k v = k u = log n ± M is atleast M = C · k u ≥ n tokens. The proof is based on the proof of Lemma 3.8 from [15].Observe that after the load balancing, w.h.p. l v = C · k u /n + r where r ∈ [ − . , .
5] (the errorterm r covers the remaining discrepancy and the rounding error). Let ˆ ω ( v ) be the output of agent v before the rounding is applied. We derive, analogously to [15],ˆ ω ( v ) = Ml v = C · k u C · k u /n + r = C · k u · nC · k u + rn = C · k u · nC · k u (cid:16) rnC · ku (cid:17) = n rnC · ku which gives us, since r ∈ [ − . , . ω ( v ) ≥ n . nC · ku ( M ≥ n ) ≥ n . n n = n n = n − (cid:18) − n + 1) (cid:19) > n − ω ( v ) ≤ n − . nC · ku ( M ≥ n ) ≤ n − . n n and = n − n = n + 13 + 13(3 n − < n + 12 . Therefore, ω ( v ) = b ˆ ω ( v ) e = n . CountExact
We now show Theorem 2. Due to space limitations, we only show that
CountExact calculates thevalue of n w.h.p. In the appendix we describe how to combine the protocol with a slow always-correctprotocol, which shows that protocol CountExact stabilizes.14 roof.
From Lemma 4 it follows that after O( n log n ) interactions the junta is elected and all agentsare inactive. From that point on no agent in the protocol is ever re-initialized again. Let u be thesingle leader that w.h.p. concludes the FastLeaderElection protocol according to Lemma 7after at most O( n log n ) interactions. According to Lemma 10, the leader u computes log n ± ApxDone u to true after at most O( n log n ) further interactions w.h.p. Finally, according toLemma 11, all agents output n after O( n log n ) further interactions. The total number of interactionstherefore is O( n log n ). Observe that all protocols require at most ˜O( n ) states. This concludes theproof.In order to argue that CountExact stabilizes, we use the same idea as in Section 3.4, wherewe combined the protocol for approximate counting with a slow protocol which is always correct.Together with an error detection mechanism, this gives a protocol that stabilizes w.h.p. in O( n log n )interactions such that every agent outputs the exact value n , using ˜O( n ) states. Further details canbe found in the appendix. References [1] Dan Alistarh, James Aspnes, David Eisenstat, Rati Gelashvili, and Ronald L. Rivest. Time-spacetrade-offs in population protocols. In Philip N. Klein, editor,
Proceedings of the Twenty-EighthAnnual ACM-SIAM Symposium on Discrete Algorithms, SODA 2017, Barcelona, Spain, HotelPorta Fira, January 16-19 , pages 2560–2579. SIAM, 2017.[2] Dan Alistarh, James Aspnes, and Rati Gelashvili. Space-optimal majority in populationprotocols. In Artur Czumaj, editor,
Proceedings of the Twenty-Ninth Annual ACM-SIAMSymposium on Discrete Algorithms, SODA 2018, New Orleans, LA, USA, January 7-10, 2018 ,pages 2221–2239. SIAM, 2018.[3] Dan Alistarh, Rati Gelashvili, and Milan Vojnovic. Fast and exact majority in populationprotocols. In Chryssis Georgiou and Paul G. Spirakis, editors,
Proceedings of the 2015 ACMSymposium on Principles of Distributed Computing, PODC 2015, Donostia-San Sebastián,Spain, July 21 - 23, 2015 , pages 47–56. ACM, 2015.[4] Dana Angluin, James Aspnes, Zoë Diamadi, Michael J. Fischer, and René Peralta. Computationin networks of passively mobile finite-state sensors. In Soma Chaudhuri and Shay Kutten,editors,
Proceedings of the Twenty-Third Annual ACM Symposium on Principles of DistributedComputing, PODC 2004, St. John’s, Newfoundland, Canada, July 25-28, 2004 , pages 290–299.ACM, 2004.[5] Dana Angluin, James Aspnes, Zoë Diamadi, Michael J. Fischer, and René Peralta. Computationin networks of passively mobile finite-state sensors.
Distributed Computing , 18(4):235–253,2006.[6] Dana Angluin, James Aspnes, and David Eisenstat. Fast computation by population protocolswith a leader.
Distributed Computing , 21(3):183–199, 2008.[7] Dana Angluin, James Aspnes, David Eisenstat, and Eric Ruppert. The computational power ofpopulation protocols.
Distributed Computing , 20(4):279–304, 2007.[8] Petra Berenbrink, Robert Elsässer, Tom Friedetzky, Dominik Kaaser, Peter Kling, and TomaszRadzik. Majority & stabilization in population protocols.
CoRR , abs/1805.04586, 2018.159] Petra Berenbrink, Robert Elsässer, Tom Friedetzky, Dominik Kaaser, Peter Kling, and TomaszRadzik. A population protocol for exact majority with o(log / n ) stabilization time and θ (log n ) states. In Ulrich Schmid and Josef Widder, editors, , volume 121of LIPIcs , pages 10:1–10:18. Schloss Dagstuhl - Leibniz-Zentrum fuer Informatik, 2018.[10] Petra Berenbrink, Tom Friedetzky, Dominik Kaaser, and Peter Kling. Tight & simple loadbalancing. In
Proceedings of the 33rd IEEE International Parallel and Distributed ProcessingSymposium (IPDPS), Rio de Janeiro, Brazil, 20–24 May 2019 , pages 718–726, 2019.[11] Petra Berenbrink, Dominik Kaaser, Peter Kling, and Lena Otterbach. Simple and efficientleader election. In , pages 9:1–9:11, 2018.[12] Andreas Bilke, Colin Cooper, Robert Elsässer, and Tomasz Radzik. Brief announcement:Population protocols for leader election and exact majority with O (log n ) states and O (log n )convergence time. In Elad Michael Schiller and Alexander A. Schwarzmann, editors, Proceedingsof the ACM Symposium on Principles of Distributed Computing, PODC 2017, Washington, DC,USA, July 25-27, 2017 , pages 451–453. ACM, 2017.[13] David Doty and Mahsa Eftekhari. Efficient size estimation and impossibility of termination inuniform dense population protocols.
CoRR , abs/1808.08913, 2018.[14] David Doty, Mahsa Eftekhari, Othon Michail, Paul G. Spirakis, and Michail Theofilatos. Briefannouncement: Exact size counting in uniform population protocols in nearly logarithmic time.In , pages 46:1–46:3, 2018.[15] David Doty, Mahsa Eftekhari, Othon Michail, Paul G. Spirakis, and Michail Theofilatos. Exactsize counting in uniform population protocols in nearly logarithmic time.
CoRR , abs/1805.04832,2018.[16] David Doty and David Soloveichik. Stable leader election in population protocols requireslinear time.
Distributed Computing , 31(4):257–271, 2018.[17] Moez Draief and Milan Vojnovic. Convergence speed of binary interval consensus.
SIAM J.Control and Optimization , 50(3):1087–1109, 2012.[18] Leszek Gasieniec and Grzegorz Stachowiak. Fast space optimal leader election in populationprotocols. In
Proceedings of the Twenty-Ninth Annual ACM-SIAM Symposium on DiscreteAlgorithms, SODA 2018, New Orleans, LA, USA, January 7–10, 2018 , pages 2653–2667. SIAM,2018.[19] Adrian Kosowski and Przemyslaw Uznanski. Brief announcement: Population protocols arefast. In
Proceedings of the 2018 ACM Symposium on Principles of Distributed Computing,PODC 2018, Egham, United Kingdom, July 23-27, 2018 , pages 475–477, 2018.[20] George B. Mertzios, Sotiris E. Nikoletseas, Christoforos L. Raptopoulos, and Paul G. Spi-rakis. Determining majority in networks with local interactions and very small local memory.
Distributed Computing , 30(1):1–16, 2017. 16 . Load Balancing
In this appendix we show Lemma 8. The proof follows along the lines of the proofs of Lemmas 2and 3 from [10]. We re-state the lemma as follows.
Lemma 8.
Assume there exists an agent u with k u (0) = κ and k v (0) = − for all v = u . If κ ≤ / · n , then w.h.p. after t = 16 n log n interactions max v { k v ( t ) } = 0 .Proof. In the following, we will argue about individual tokens. However, this is only used for thesake of the analysis. In the actual process, all agents store at all times only the logarithm of theircurrent numbers of tokens. We assume that each agent u holds a number of l u ( t ) = 2 k u ( t ) tokenswhich are stored in some order. Initially, the tokens are ordered arbitrarily. We define the height h ( b ) of a token b as the number of tokens below b w.r.t. that order. Whenever an agent u interactswith another agent v that does not have any load, every second token is moved from agent u toagent v and stored on v in the original order. See Figure 4 for an example. Therefore, all tokens u Agent u Agent v k u = 4 k v = 0 k u = 2 k v = 2 b b b b b b b b b b b b b b b b Figure 4:
Example: Agent u moves every second token to the previously empty agent v . participating in the interaction reduce their height by a factor of at least 2. According to theassumptions of Lemma 8 we have 2 κ ≤ / · n and thus there are at most 3 / · n tokens in thesystem. Even when spread out as much as possible, these tokens cannot take up more than 3 / · n agents. Therefore, the probability that a specific token b of height h ( b ) > /
4. (Note that theactual probability is bounded only by 1 / · (1 − /n ), since the leader does not participate in theload balancing process. For simplicity we work with the expression 1 /
4, and for n large enough ourfinal results holds nonetheless.) We now observe that1) the agent on which b is stored is selected with probability 2 /n , and2) the other agent selected does not have any load with probability 1 / t with probability 1 / (2 n ) the token b reduces its height by at least a constant factor of 2 or has already reached height 0. We denotethis event as a good interaction t for token b . We now consider 16 n log n interactions and use X b,i to denote an indicator random variable for good interactions of token b . Since for one specifictoken the interactions occur independently from each other, we may apply Chernoff bounds to17 b = P n log ni =1 X b,i and obtainPr[ X b < log n − < exp (cid:18) − n (cid:19) < n − . From the union bound over all tokens we obtain that after at most 16 n log n interactions all tokenshave reached height 0 with probability 1 − n − . B. The Stable Protocol
In this appendix we show that we can build a stable protocol using only an additional multiplicativefactor of O(log n ) states. We first describe our error detection mechanisms and then show State-ment 2) of Theorem 1. Finally, we show how to reduce the required number of states by accepting asmall number of agents that do not output the correct result.Our approach is closely related to the idea of building a hybrid protocol in [18] (see also Section 2).We need to handle two possible sources of errors. The first type of error comes from leader election,where it may happen that no agent ever completes the fast leader election protocol, or multipleleaders do. The second type of error occurs when the Search
Protocol fails.If no agent completes leader election, then no agent will ever set leaderDone . In that case, we usethe following solution: From the very beginning, all agents run a slow backup protocol in parallel.(We give a description of the backup protocol in Appendix C.) Only if an agent v completes theleader election protocol and sets leaderDone v to true , it stops the execution of the slow backupprotocol and proceeds with the Search
Protocol, which is governed by the leader.If multiple agents conclude the leader election stage as a leader, then this error can be detectedwhenever two leaders interact with each other. The agent v which detected the error raises an errorflag error v . This flag is adopted by all other agents using one-way epidemics. When an agent setsits error flag, it ignores all of its previous computations and executes a new instance of the backupprotocol. Note that stopping the backup protocol while running the Search
Protocol allows usto save states by re-using the states from the backup protocol (at costs of having to re-start thebackup protocol in case of a later error).The second type of error occurs when some agents fail to properly execute the
Search
Protocol.E.g., either load balancing or maximum broadcast could fail, resulting in an incorrect value k u atthe leader u . In the remainder of this section, we describe how such an error can be detected afterthe execution of the Search
Protocol. As before, whenever an agent detects an error, it raises theerror flag and switches to a new instance of the backup protocol.To detect an error during the execution of the
Search
Protocol, all agents use after the
Search
Protocol (i.e., when
ApxDone v = true for agent v ) an additional error detection protocol ErrorDe-tection defined in Algorithm 7. This protocol is enabled by replacing the Broadcasting Stage fromthe
Approximate (Algorithm 2) with the Error Detection Stage by calling the
ErrorDetection protocol (Algorithm 7) as follows: Updated lines of protocol
Approximate : if leaderDone u and ApxDone u then . Stage 3: Error Detection Stage ErrorDetection [( k u , ApxDone u ) , ( k v , ApxDone v )] Algorithm 6:
Additional Lines for Protocol
Approximate to enable error detection v in addition to the states from Algorithm 1a flag error v which is initially set to false and an additional variable l v ∈ { , , . . . , } used forload balancing. Intuitively, the leader u balances 2 k u tokens among the agents, and the resultingload at each agent allows the leader to validate its result. The phases of the ErrorDetection protocol have the following purposes.1) In the first phase ( phase u = 0), the leader u initializes another agent in its first interactionwith a load of 2 k u − tokens stored in powers of two (Line 4). Note that the − − v except the leader perform the powers-of-two load balancingprocedure on their k v values.3) In the third phase, every agent v initializes its additional token counter l v either to 0 if k v = − k v = 0 (the agent had one token in the powers-of-two load balancing process). Here, the factorof 32 over-compensates for the smaller number of tokens initially used in the first phase.4) In the fourth phase, all agents v perform the classical load balancing process from [10] ontheir l v -values.5) In the last phase, all agents perform the following. Every agent v checks if its remaining load l v is at least 3. If this is not the case, an error has occurred, the agent raises the error flag error v and moves over to the backup protocol. Furthermore, every agent v checks whether theremaining discrepancy is at most 2. In addition, once an agent v has reached phase v = 4, itstops its phase clock. In that way, it runs through the error detection protocol exactly onceand then remains in Phase 4 forever (unless an error is detected).The output of every agent v is the value k u from the leader u , which is set in Line 19. It is thensent to all agents in Phase 4 via one-way epidemics.19 Interaction ( u, v ) in protocol
ErrorDetection : if ApxDone v = false then . a new agent enters error detection ( k v , l v , ApxDone v ) ← ( − , , true ) if phase u = 0 and firstTick u = true and leader u = true then k v ← k u − . load infusion if phase u = 1 and leader u = leader v = false then . powers-of-two load balancing if min { k u , k v } = − and max { k u , k v } > then k u , k v ← max { k u , k v } − if phase u = 2 and firstTick u = true then . initialize load balancing with con-stantly many tokens per agent if k u = − or leader u = true then l u ← else if k u = 0 then l u ← else error u ← true if phase u = 3 then . classical load balancing ( l u , l v ) ← ( b ( l u + l v ) / c , d ( l u + l v ) / e ) if phase u = 4 then if leader u = true and firstTick u = true then k u ← b k u + 3 − log l u e . compute approximation of log n if l u < or | l u − l v | > then error u ← true . balancing error detected k u ← max { k u , k v } . broadcast result from the leader stop the phase clock. Algorithm 7:
Error Detection protocol in protocol
Approximate
We now put everything together and show the stability of Algorithm 2. Recall that in Section 3.2we have shown that the protocol works as claimed w.h.p. It remains to show that the protocolstabilizes.
Proof of Statement 2) and Statement 3) of Theorem 1.
Recall that in any error case, all agents v raise their error v flag. The error v flag is always adopted by all other agents using one-way epidemics.Whenever an agent v sets its error v flag, it ignores all of its previous computations and starts toexecute a new instance of the backup protocol. The backup protocol stabilizes after O (cid:16) n log n (cid:17) interactions w.h.p. (see Lemma 12) in Appendix C.We first show that the protocol stabilizes unless an error is detected.W.h.p., the phase clocks work correctly such that the phase length is sufficiently long enoughfor load balancing to complete (see Lemma 5). The powers-of-two load balancing process balancesthen the load in Phase 1 such that all agents v (except the leader u ) have a value k v ∈ { − , } (seeLemma 8). The resulting number of tokens is then multiplied with 32 and balanced in Phase 3using the classical load balancing process from [10].From the result on load balancing we obtain the following.• From Lemma 9 we obtain that k u ≥ b log n c w.h.p. Therefore, at least 2 k u − · > · n tokensare balanced over n agents. From [10, Theorem 1] we obtain that after balancing every agenthas load at least 3 w.h.p. Otherwise, the error flag is raised by every agent which has loadless than 3 (see Line 21). 20 As before, the error flag is raised by every agent which observes a balancing error. However,note that after balancing the discrepancy is at most 2 w.h.p. [10]. Therefore, a load balancingerror does not occur w.h.p. and thus in Line 21 of Algorithm 7 the error flag remains unsetw.h.p.Once the load balancing has successfully completed such that l v ≥ v , for all pairs ofagents ( u, v ) we have | l u − l v | ≤
2, all agents are in Phase 4, and all agents have adopted the maximalvalue k u from the leader u , the protocol does not provide any transitions due to which an agent couldchange its output. Together this implies that the protocol stabilizes w.h.p. if k u ∈ { b log n c , d log n e } after the Search
Protocol.Observe that the leader u computes its approximation in the ErrorDetection protocol as k u = b k u + 3 − log l u e (see Line 19). We use l u = b · k u e ± k u = log n ± δ forsome δ <
1. Therefore, k u ∈ { b log n c , d log n e } provided the load balancing has concluded with amaximal remaining discrepancy of 2.To now show Statement 2) of Theorem 1 we handle the various sources of errors one after theother.• Leader Election Errors:If no leader concludes the leader election protocol from [18], then no agent ever sets leaderDone to true . In that case all agents output the result from the backup protocol indefinitely.If multiple agents conclude the leader election protocol, then this is detected when two of theminteract with each other. In that case, both agents raise the error flag, causing the system tostabilize due to the backup protocol.• Synchronization Errors:Observe that the ApxDone flag is transmitted to all agents via one-way epidemics (Line 2 ofAlgorithm 7). Once the flag is set, the
ErrorDetection protocol runs in 5 phases countedin the phase variable. It is crucial that this number of phases is a constant, and that thephase clock gets stuck in Phase 4.Recall that we define that the phase clocks are updated at the beginning of an interaction.If two agents interact thereafter that have different phase values, the execution has becomeasynchronous and the participating agents raise the error flag. Note that this also implies thatevery agent initiates at least one interaction of every phase (thereby executing the specialinstructions conditioned on firstTick = true ), since otherwise a phase difference of at least 1is detected in a later interaction.• Errors in the Search
Protocol:During the
Search
Protocol, no explicit error detection mechanisms are used. However,observe that as soon as a leader is present the
Search
Protocol, the total load in the systemstarts to grow over time. This follows from the fact that the phase clock performs an infinitesequence of ticks with probability 1. Therefore the value k u from the leader is incrementedrepeatedly, and therefore the total load in the system grows such that the leader will eventuallyconclude the Search
Protocol.At the time when the leader u concludes the Search
Protocol, its estimate k u of log n mightbe too small. However, this error is detected in the ErrorDetection protocol and the errorflag is raised by some agent. As soon as the error flag is raised, all agents adopt the error flagvia one-way epidemics, and the protocol then stabilizes via the backup protocol.21t remains to show that the protocol stabilizes w.h.p. within O (cid:16) n log n (cid:17) interactions. Recallthat without the ErrorDetection protocol the protocol converges w.h.p. within O (cid:16) n log n (cid:17) interactions (see Statement 1) of Theorem 1). The ErrorDetection protocol runs in 5 additionalphases. Together this gives us that w.h.p. the protocol stabilizes in O (cid:16) n log n (cid:17) interactions.Regarding the required number of states, we observe that the ErrorDetection protocol operateson the same states as the
Search
Protocol, and the only non-constant size variable of any agent u is k u , which uses w.h.p. up to log n states – as in the Search
Protocol. Except for the BackupProtocol, the stable protocol therefore does not require more states then the
Search
Protocol. TheBackup Protocol requires O (cid:16) log n (cid:17) states (see Appendix C. Since it has to be run in parallel untilan agent u sets leaderDone u to true , our protocol requires in total a number of O (cid:16) log n log log n (cid:17) states w.h.p.It remains to show that we can further reduce the required number of states if we slightly relaxthe output definition such that only all but log n agents output the correct result. This is achievedby using a slightly modified backup protocol which requires only O(log n ) states, but up to log n agents do not output the correct result. C. Backup Protocols
C.1. Backup Protocol for Approximate Counting
In this appendix we define our backup protocol to compute b log n c , using at most log n states.In the slow but stable backup protocol, each agent starts with one token. Agents then keepcombining tokens in groups of sizes which are powers of two. Each agent v keeps a pair ( k v , k max v ),where k v is the number of tokens in this agent and k max v is the largest k w that agent v is aware of.Let k v = − , u, v ), thereare now two possible cases. If both agents have the same number of tokens, than the first agenttakes all of them. Otherwise, if the two agents have different numbers of tokens, no exchange oftokens is possible. In both cases, the agents always update the maximum number of tokens, whichis stored in the k max -values. Formally, the protocol has the transitions δ h(cid:16) k u , k max u (cid:17) , (cid:16) k v , k max v (cid:17)i = ((cid:2)(cid:0) k u + 1 , K (cid:1) , ( − , K ) (cid:3) if k u = k v ≥ (cid:2)(cid:0) k u , K (cid:1) , (cid:0) k v , K (cid:1)(cid:3) otherwise, (3) K = max { k max u , k max v } . whereWe now show the following result for the backup protocol. Lemma 12.
Let K i = { u : k u = i } be the set of agents which have k u = i and let n i be the i -th bitof the binary representation of n . Then the backup protocol converges to a configuration where• | K i | = n i for all i ≥ and• max u { k u } = b log n c and k max v = b log n c for all agents v .It uses at most (log n + 1) states and stabilizes w.h.p. within O (cid:16) n log n (cid:17) interactions. The proof follows straightforwardly from the coupon collector’s problem. For completeness, weformally show the claim as follows. 22 roof.
We show by induction on j in { , , . . . , b log n c } that after j · O( n log n ) interactions for all i < j it holds that | K i | = n i w.h.p.The base case trivially holds. For the induction step, fix a j ∈ { , , . . . , b log n c } . From theinduction hypothesis we obtain that for any level i < j there are n i token stuck on that level. Notethat n i ≤
1: this means that no further tokens from any agent in K i with i < j can be merged, andtherefore | K j | cannot increase w.h.p. We now apply a standard coupon collecting argument: AfterO (cid:0) n log n (cid:1) interactions, as many tokens as possible on level j have been merged w.h.p. Thus, | K j | is either 0 or 1 after j · O( n log n ) interactions w.h.p. It remains to argue that | K j | = n j .From the binary representation of n it follows that the total number of tokens that can ever reachlevel j is ˆ k j = (cid:16) n − P j − i =0 n j · i (cid:17) / j = (cid:4) n/ j (cid:5) . If n j = 0, ˆ k j is even. All ˆ k j tokens can thereforebe merged to level j such that eventually | K j | = 0. If n j = 1, a single token remains on level j indefinitely. This shows that | K j | = n j after j · O (cid:0) n log n (cid:1) interactions.From the binary representation of n it furthermore follows that eventually | K b log n c | = 1. Let u ∈ K b log n c at some time t and observe that k u = b log n c > k v for any v = u . According to Lemma 3on one-way epidemics it follows that k max u = k u is transmitted to all other agents after additionalO( n log n ) interactions after time t w.h.p.Finally, the protocol requires at each agent at most b log n c + 1 states for both, the k u and the k max u values. This concludes the proof. C.2. Backup Protocol for Exact Counting
In the backup protocol for exact counting, every agent u has variables ( c u , n u ) initially set to( false , c u , n u ) , ( c v , n v )] = ( [( false , n u + n v ) , ( true , n u + n v )] if c u = c v = false [( c u , max { n u , n v } , ( c v , max { n u , n v } )] otherwise. (4)Intuitively, in this protocol every agent starts with one token and a bit c u indicating whether itstoken has been counted. Agents combine tokens until eventually a single agent holds all n tokens.Those agents which have already been counted broadcast the maximum value they have observed sofar. The following lemma follows directly from coupon collecting. Lemma 13.
The backup protocol defined via Equation (4) stabilizes w.h.p. within O (cid:0) n log n (cid:1) interactions. When it stabilizes, every agent outputs the exact value n . D. Fast Leader Election using n ε States
The following protocol performs leader election in O( n log n ) interactions using O( n ε ) many statesfor some small constant ε >
0. It is a special case of the protocol presented in [8]. It has theproperty that there is always at least one leader, and when the first agent sets leaderDone u to true ,there is at most one leader w.h.p.To simplify the description of the protocol, we assume that agents can toss a random coin andgenerate one random bit uniformly and independently in each interaction. While this is not coveredby the actual population model, the randomness from the scheduler can be exploited. This techniquehas been called synthetic coins by Alistarh et al. [1], and a simple analysis has been presented in [11].Intuitively, the idea is that every agent keeps track of the parity of its current interaction numberby flipping a bit in every interaction. This bit then functions as a source of randomness for theinteraction partner. 23he FastLeaderElection protocol operates for each agent u on the variables leader u , l u and j u .The variable leader u indicates whether u is still a leader contender. When the protocol has reached phase u = 16, it concludes by setting leaderDone u to true . To get an estimate of log n , it uses the level u variable from the Junta process.The protocol then runs in multiple phases. In even phases, all leader contender agents sampleΘ(log n ) random bits. In odd phases, the maximum of the numbers represented by these randombits is sent to all agents via one-way epidemics. If a leader contender u observes a larger valuein the broadcast, it becomes a follower by setting leader u to false . The protocol is specified inAlgorithm 8 Interaction [( leader u , leaderDone u ) , ( leader v , leaderDone v )] in the FastLeaderElection protocol: if phase u = phase v then if phase u is even and firstTick u = true then . initialize a new round l u , j u ← if phase u is even and leader u = true and j u < level u − then . generate random number l u [ j u ] ← (cid:26) /
21 with probability 1 / . set j u -th bit of l u to random value j u ← j u + 1 if phase u is odd and l u < l v then . u observed another leader leader u ← false l u ← l v . adopt the maximum value if phase u = 2 then leaderDone u ← true Algorithm 8:
Protocol
FastLeaderElection used in Algorithm 3
We now show Lemma 7 from Section 2.
Lemma 7 ([8]) . The uniform protocol
FastLeaderElection elects a unique leader. It stabilizesin O( n log n ) interactions, using ˜O( n ) many states, w.h.p. Furthermore, after at most O (cid:16) n log n (cid:17) interactions all agents have leaderDone u set to true w.h.p.Proof. First, observe that the protocol runs in constantly many phases. Therefore, every agent setsits flag leaderDone to true after at most O( n log n ) interactions w.h.p.Secondly, observe that there is always at least one leader contender. For every phase t , let L ( t )be the set of agents u which have sampled the maximum value l u . By definition, L ( t ) is non-empty.Any contender can only become a follower in Line 8, and due to that rule it is impossible thatany agent in the set L ( t ) becomes a follower. Assume towards a contradiction that an agent w in L ( t ) becomes a follower. This can only occur if l w < l v in some interaction with another agent v .However, this is a contradiction to w ∈ L ( t ), the set of agents u with largest value l u .Now consider an arbitrary but fixed pair of agents ( u, v ). If u and v do not sample precisely thesame bits in every even phase, at least one of them will become a follower in the next odd phase.The probability p that both agents sample the same sequence of bits during the entire course of theprotocol is p = 1 / · level u − . For the maximal junta level level ∗ in the modified junta process it holds that level ∗ ≥ log log n − p ≤ / · log log n − = 1 /n .
24e now use the above observation that there is always at least one leader contender. Let u bea leader contender that concludes the FastLeaderElection protocol by setting leaderDone u to true . We take the union bound over all other agents v = u and obtain that with probably at most1 /n there exists a second leader contender. E. Omitted Proofs from Section 4
Proof of Lemma 11.
At the end of Phase 0, w.h.p. all agents know k u from the leader accordingto one-way epidemics. At the end of Phase 1, w.h.p. C · k u tokens have been balanced such that l v = Θ(1) for any agent v . At the end of Phase 2, a total of at least M ≥ · n tokens have beenbalanced and every agent v has load l v = j C · k u /n m ± v can output n using its output function ω ( v ) = j C k v /l v m ,provided that v knows the value k v = k u = log n ± M is atleast M = C · k u ≥ n tokens. The proof is based on the proof of Lemma 3.8 from [15].Observe that after the load balancing, w.h.p. l v = C · k u /n + r where r ∈ [ − . , .
5] (the errorterm r covers the remaining discrepancy plus 0 . ω ( v ) be the output ofagent v before the rounding is applied. We derive, analogously to [15],ˆ ω ( v ) = Ml v = C · k u C · k u /n + r = C · k u · nC · k u + rn = C · k u · nC · k u (cid:16) rnC · ku (cid:17) = n rnC · ku which gives us, since r ∈ [ − . , . ω ( v ) ≥ n . nC · ku ( M ≥ n ) ≥ n . n n = n n = n − (cid:18) − n + 1) (cid:19) > n − ω ( v ) ≤ n − . nC · ku ( M ≥ n ) ≤ n − . n n = n − n = n + 13 + 13(3 n − < n + 12 . andTherefore, ω ( v ) = b ˆ ω ( v ) e = n . F. Stable Exact Counting
In this appendix we describe how to build a stable protocol to count the exact population size.We use a similar approach as in Section 3. We check for errors during the course of the protocol,and if we detect an error, the interacting agents raise an error flag. This error flag is sent to allother agents via one-way epidemics. Every agent which has set the error flag performs a simplebackup protocol described in Appendix C that outputs the correct result with probability 1 afterO (cid:0) n log n (cid:1) interactions w.h.p.We first describe which error detection mechanisms are additionally required for Algorithm 3 andthen we show that the combined protocol stabilizes.For the FastLeaderElection protocol (see Appendix D) we know that there is always at leastone leader. If, however, more than two leaders conclude the leader election protocol, this error canbe detected whenever they directly interact with each other. In this case they both raise the errorflag.The second type of error we detect during the course of the protocol are synchronization problemswith the phase clock. We assume that all agents count the exact number of phases (until the last25hase of the
RefinementStage protocol). When now two agents interact that have a differentphase counter, both agents raise the error flag.Finally, in the
RefinementStage protocol all agents v verify in Line 6 that they have a load ofat least l v ≥ before multiplying their load with 2 k v . At the same time, all agents start to comparetheir k v values. If any agent does not have sufficiently many tokens, or if two agents interact withdifferent k v values, the agents raise the error flag.We now give the full proof for Theorem 2, which is re-stated for completeness as follows. Theorem 2.
The protocol
CountExact is uniform and outputs the exact population size n . Itstabilizes in O( n log n ) interactions and uses ˜O( n ) states, w.h.p.Proof. There is always at least one leader. If, however, multiple leaders conclude the
FastLeader-Election protocol, this error is detected and the protocol stabilizes due to the backup protocol.We therefore consider in the following only the case when precisely one leader concludes the
FastLeaderElection protocol.Note that In the way we elect the junta there is always at least one junta agent. Therefore thephase clocks perform an infinite sequence of ticks with probability 1. However, we cannot guaranteethat all agents are always in exactly the same phase w.r.t. their ticks, and the length of the phasesmight diverge drastically from the expectation (albeit only with low probability). Now since allagents count the exact number of phases, whenever two agents interact that have a different phasecounter, both agents raise the error flag and the protocol stabilizes due to the backup protocol.The presence of a unique leader guarantees that in the
ApproximationStage protocol the totalload is initially at least 1. Recall that the phase clock ticks with probability 1, even if the agents arenot properly synchronized. Therefore, the load grows in the
ApproximationStage protocol overtime such that eventually a leader u concludes the protocol in Line 5 by setting ApxDone u to true .Therefore, with probability 1 the leader starts the RefinementStage protocol by setting its load l u to C · k u . In the RefinementStage protocol all agents v verify that they have a load of atleast l v ≥ − . k v . If this is the case for every agent, thevalue k u from the leader u has been at least log n −
3, and the total load is at least 4 n as requiredin the proof of Lemma 11. Otherwise, the under-loaded agents raise the error flag since the totalload is insufficient to exactly compute n . Again, the protocol stabilizes due to the backup protocol.If all agents have passed this last error check in Phase 2 of the RefinementStage protocol (andall agents v hold the same value k v ), then all agents will eventually output n with probability 1.Otherwise, at least one agent v is in the error state (or will enter the error state when comparing its k v value) and thus with probability 1 all agents will eventually output n via the backup protocol.It remains to argue the required number of states and the time until the protocol stabilizes.For the required number of states, observe that for each agent u w.h.p. level u = O(log log n )(see Lemma 4), i u = O(1) and k u ≤ log n + 3 (see Lemma 10), and thus l u holds at most O( n )tokens in the RefinementStage protocol. The total number of states is therefore w.h.p. at mostO(log log n · log n · n ).The protocol stabilizes once all agents have concluded the second phase of the RefinementStage protocol. The
FastLeaderElection protocol requires w.h.p. at most O( n log n ) interactions (seeLemma 7), the ApproximationStage protocol also requires w.h.p. at most O( n log n ) interactions(see Lemma 10), and the RefinementStage protocol requires 3 phases, i.e., O( n log n ) interactions,as well. Therefore the protocol CountExact stabilizes within O( n log nn