Polynomial Counting in Anonymous Dynamic Networks with Applications to Anonymous Dynamic Algebraic Computations

Starting with Michail, Chatzigiannakis, and Spirakis work, the problem of Counting the number of nodes in Anonymous Dynamic Networks has attracted a lot of attention. The problem is challenging because nodes are indistinguishable (they lack identifiers and execute the same program) and the topology may change arbitrarily from round to round of communication, as long as the network is connected in each round. The problem is central in distributed computing as the number of participants is frequently needed to make important decisions, such as termination, agreement, synchronization, and many others. A variety of algorithms built on top of mass-distribution techniques have been presented, analyzed, and also experimentally evaluated; some of them assumed additional knowledge of network characteristics, such as bounded degree or given upper bound on the network size. However, the question of whether Counting can be solved deterministically in sub-exponential time remained open. In this work, we answer this question positively by presenting Methodical Counting, which runs in polynomial time and requires no knowledge of network characteristics. Moreover, we also show how to extend Methodical Counting to compute the sum of input values and more complex functions without extra cost. Our analysis leverages previous work on random walks in evolving graphs, combined with carefully chosen alarms in the algorithm that control the process and its parameters. To the best of our knowledge, our Counting algorithm and its extensions to other algebraic and Boolean functions are the first that can be implemented in practice with worst-case guarantees.


Introduction
In this work, we address the standing question of whether the number of nodes of an Anonymous Dynamic Network (ADN) can be counted deterministically in polynomial time or not. We answer this question positively by presenting the Methodical Counting algorithm, and proving formally that after a polynomial number of rounds of communication all nodes know the size of the network and stop.
The problem has been thoroughly studied [15,7,8,9,6,16,4] because Counting is central for distributed computing. Indeed, more complex tasks need the network size to make various decisions on state agreement, synchronization, termination, and others. However, Anonymous Dynamic Networks pose a particularly challenging scenario. On one hand, nodes are indistinguishable from each other. For instance, they may lack identifiers or their number may be so massive that keeping record of them is not feasible. On the other hand, the topology of the network is highly dynamic. Indeed, the subsets of nodes that may communicate with each other may change all the time. All these features make ADN a valid model for anonymous ad hoc communication and computation.
In such a restrictive scenario, finding a way of providing theoretical guarantees of deterministic polynomial time has been elusive until now. Indeed, previous papers have either weaken the objective (e.g., computing only upper bound, only stochastic guarantees, etc.), assumed availability of network information (e.g., maximum number of neighbors, size upper bound, etc.), relied on a stronger model of communication, or provided only superpolynomial time guarantees.
Methodical Counting uses no information about the network. After completing its execution, all nodes obtain the exact size of the network and stop. Moreover, they stop all at the same time, allowing the algorithm to be concatenated with other computations.
Our algorithm is based on nodes continuously sharing some magnitude, which we call potential, 1 resembling mass-distribution and push-pull algorithms. Unlike previous algorithms, in Methodical Counting carefully and periodically (i.e. , "methodically") some potential is removed from the network, rather than greedily doing so continuously. This approach is combined with another methodological innovation testing whether the candidate value (for the network size) is within some polynomial range of the actual network size. This complex strategy yields an algorithm in which the progress in mass-distribution can be analyzed as a sequence of parametrized Markov chains (even though the algorithm itself is purely deterministic) enhanced by mass drift and alarms controlling the process and its parameters. Our analysis approach opens the path to study more complex tasks in Anonymous Dynamic Networks applying similar techniques.
Finally, we also present a variety of extensions of Methodical Counting to compute more complex functions. Most notably, we present an extension that, concurrently with finding the network size, computes the sum of input values held at each node without asymptotic time overhead. Having a method to compute the sum and network size, more complex computations are possible in polynomial time as well. Indeed, we also describe how to compute a variety of algebraic and Boolean functions. To the best of our knowledge, ours are the first algorithms for anonymous dynamic Counting and other algebraic computations that can be implemented in practice with worst-case guarantees.

Roadmap:
The rest of the paper is organized as follows. We specify the model and notation details in Section 2. Then, we overview previous work in Section 3 and present our results in Section 4. Section 5 includes the details of Methodical Counting, and we prove its correctness and running time in Section 6. Extensions to other functions are presented in Section 7.

Model, Problem, and Notation
The Counting Problem: The definition of the problem is simple. An algorithm solves the Counting Problem if, after completing its execution, all nodes have obtained the exact size of the network and stop.

Anonymous Dynamic Networks:
The following model is customary in the Anonymous Dynamic Networks literature. We consider a network composed by a set V of n > 1 network nodes with processing and communication capabilities. It was shown in [15] that Counting cannot be solved in Anonymous Networks without the availability of at least one distinguished node in the network. Thus, we assume the presence of such node called leader. Aside from the leader, we assume that all other nodes are indistinguishable from each other. That is, we do not assume the availability of labels or identifiers, and all non-leader nodes execute exactly the same program.
Each pair of nodes that are able to communicate define a communication link, and the set of links is called the topology of the network. The nodes in a communication link are called neighbors. The event of sending a message to neighbors is called a broadcast or transmission. Nodes and links are reliable, in the sense that no communication or node failures occur. Hence, a broadcasted message is received by all neighbors. Moreover, links are symmetric, that is, if node a is able to send a message to node b, then b is able to send a message to a.
Without loss of generality, we discretize time in rounds. In any given round, a node may broadcast a message, receive all messages from broadcasting neighbors, and carry out some computations, in that order. The time taken by the computations is assumed to be negligible.
The set of links among nodes may change from round to round, and nodes have no way of knowing which were the neighbors they had before. These topology changes are arbitrary, limited only to maintain the network connected in each round. That is, at any given round the topology is such that there is a path, i.e., a sequence of links, between each pair of nodes, but the set of links may change arbitrarily from round to round. This adversarial model of dynamics was called 1-interval connectivity in [14].
The following notation will be used. The maximum number of neighbors that any node may have at any given time is called the dynamic maximum degree and it is denoted as ∆. The maximum length of a path between any pair of nodes at any given time is called the dynamic diameter and it is denoted as D.
The maximum length of an opportunistic path between any pair of nodes over many time slots is called the chronopath [10] and it is denoted as D.

Previous Work
In this section we overview previous work directly related to this paper. A comprehensive overview of work related to Anonymous Dynamic Networks can be found in a survey by Casteigts et al. [3] and references in the papers cited here. The related work overviewed, in comparison with our results, is summarized in Table 1.
With respect to lower bounds, it was proved in [5] that at least Ω(log n) rounds are needed, even if D is constant. Also, a trivial observation is that Ω(D) is a lower bound as at least one node needs to hear about all other nodes to obtain the right count, and the chronopath D is the largest number of hops that a message from some node needs to take to reach other node in the network, possibly along multiple time slots.
Counting was already studied in [15], together with the problem of Naming, for dynamic and static networks. It was shown in this work that it is impossible to solve Counting without the presence of a distinguished node, even if nodes do not move. The Counting protocol presented for Anonymous Dynamic Networks requires knowledge of an upper bound on ∆, and the count obtained is only an upper bound on the network size, which may be as bad as exponential.
An exact count is obtained by the Conscious Counting algorithm presented in [7]. However, the computation relies on knowing initially an upper bound on the network size. The running time of this protocol is exponential only if the initial upper bound is tight.
In the same work and follow-up papers [8,9], the authors presented protocols under more challenging scenarios where ∆ is not known. However, either the protocol does not terminate [7], and hence the running time cannot be bounded, or the protocol is terminated heuristically [9]. In experiments [9], such heuristic was found to perform well on dense topologies, but for other topologies the error rate was high. That is, the results only apply to dense Anonymous Dynamic Networks. Another protocol in [8] is shown to terminate eventually, without running-time guarantees and under the assumption of having for each node an estimate of the number of neighbors in each round. In [15] it was conjectured that some knowledge of the network such as the latter would be necessary, but the conjecture was disproved later in [6]. On the other hand the protocol in [6] requires exponential space.
Recently, a protocol called Incremental Counting was presented in [16]. This algorithm reduced exponentially the running time guarantees with respect to previous works developed under the same model. Incremental Counting obtains the exact count, all nodes terminate simultaneously, the topology dynamics is only limited to 1-interval connectivity, it only requires polynomial space, and it only requires knowledge of the dynamic maximum degree ∆. The superpolynomial running time proved still does not provide enough guarantee for practical application, but reducing from doubly-exponential to exponential was an important step towards understanding the complexity of Counting.
In a follow-up paper [4], Incremental Counting was tested experimentally showing a promising polynomial behavior. The study was conducted on pessimistic inputs designed to slow the convergence, such as boundeddegree trees rooted at the leader uniformly chosen at random for each round, and a single path starting at the leader with non-leader nodes permuted uniformly at random for each round. The protocol was also tested on static versions of the inputs mentioned, classic random graphs, and networks where some disconnection is allowed. The results exposed important observations. Indeed, even for topologies that stretch the dynamic diameter, the running times obtained are below ∆n 3 . It was also observed that random graphs, as used in previous experimental studies [9], reduce the convergence time, and therefore are not a good choice to indicate worst-case behavior. These experiments showed good behavior even for networks that sometimes are disconnected, indicating that more relaxed models of dynamics, such as (α, β)-connectivity [10,11], are worth to study. All in all, the experiments in [4] showed that Incremental Counting behaves well in a variety of pessimistic inputs, but not having a proof of what a worst-case input looks like, and being the experiments restricted to a range of values of n far from the expected massive size of an Anonymous Dynamic Network, a theoretical proof of polynomial time remained an open problem even from a practical perspective.
In a recent manuscript [2] a polynomial Counting algorithm is presented relying on the availability of an algorithm to compute average with polynomial convergence time. Such average computation is modeled as a Markov chain with underlying doubly-stochastic matrix, which requires topology information within two hops (cf. [17]). In our model of Anonymous Dynamic Network, such information is not available, and gathering it may not be possible due to possible topology changes from round to round.

Our Contributions
We present and analyze a deterministic distributed algorithm to compute the number of nodes in an Anonymous Dynamic Network. We call such algorithm Methodical Counting. As opposed to previous works, our algorithm does not require any knowledge of network characteristics, such as dynamic maximum degree or an upper bound on the size. After O(n 5 ln 2 n) communication rounds of running Methodical Counting, all nodes obtain the network size and stop at the same round. To the best of our knowledge, this is the first polynomial deterministic Counting algorithm in the pure model of Anonymous Dynamic Network.
Our algorithm is based on distributing potential in a mass-distribution fashion, similarly as previous works for Counting. The main algorithmic novelty in our approach is that the leader participates in the process as any other node, removing potential only after it has accumulated enough. This approach allowed us to leverage previous work on random walks in evolving graphs. For this approach to work, we combine it with testing whether the candidate value for the network size is polynomially close to the actual value. Our approach also opens the path to study more complex computations in Anonymous Dynamic Networks using the same analysis.
Finally, we also present extensions of Methodical Counting to compute more complex functions. Most notably, we show how to modify Methodical Counting to compute the sum of input values held by nodes at the same time than counting. Having an algorithm to compute the network size and the sum of input values, we also show how to compute other algebraic and Boolean functions.

Methodical Counting
In this section we present Methodical Counting. First, we give the intuition of the algorithm, the details can be found in Figures 1 and 2. (References to algorithm lines are given as f igure# . line# .) Initially, the leader is assigned a potential of 0 and all the other nodes are assigned a potential of 1. Then, the algorithm is composed by epochs, each of which is divided into phases composed by rounds of communication. Epoch k corresponds to a size estimate k that is iteratively increased from epoch to epoch until the correct value n is found. Each epoch is divided into p phases. The purpose of each phase is for the leader to collect as much potential as possible from the other nodes in a mass-distribution fashion as follows.
Each phase is composed by r rounds of communication. In each round, each node 2 broadcasts its potential and receives the potential of all its neighbors. Each node keeps only a fraction 1/d of the potentials received. The parameters p, r, and d are functions of k. The specific functions needed to guarantee correctness and saught efficiency are defined in Theorem 2. This varying way of distributing potential is different from previous approaches using mass distribution. After communication, each node updates its own potential accordingly (cf. Lines 1.11 and 2.10). That is, it adds a fraction 1/d of the potentials received, and subtracts a fraction 1/d of the potential broadcasted times the number of potentials received. Then, a new round starts.
At the end of each phase the leader "consumes" its potential. That is, it increases an internal accumulator ρ with its current potential, which is zeroed for starting the next phase (cf. Lines 1.19 and 1.20). A node stops the update of potential described, raises its potential to 1, and broadcasts an alarm in each round until the end of the epoch if any of the following happens: 1) at the end of the first phase its potential is above some threshold τ as defined in Theorem 2 (cf. Lines 1.15 and 2.14), 2) at any round it receives more than d − 1 messages (cf. Lines 1.12 and 2.11), or 3) at any round it receives an alarm (cf. Lines 1.12 and 2.11). The alarm for case 1) allows the leader to detect that the estimate is wrong when k 1+ǫ < n for some ǫ > 0 (Lemmas 3 and 4), the alarm for case 2) allows the leader to detect that d is too small and hence the estimate is wrong, and the alarm for case 3) allows dissemination of all alarms. In the alarm status the potential is set to 1 to facilitate the analysis, but it is not strictly needed by the algorithm.
At the end of each epoch, the leader checks the value of ρ. If k − 1 − 1/k ≤ ρ ≤ k − 1 the current estimate is correct and the leader changes its status to "done" (cf. Line 1.21). Otherwise, all its variables are reset to start a new epoch with the next estimate (cf. Line 1.23). Before starting a new epoch the network is flooded with the status of the leader for k rounds (cf. Lines 1.28 and 2.17). If k = n, the leader initiates message "done" and the k rounds are enough for all the nodes to receive the "done" status and after completing the k rounds stop. Otherwise, nodes will not receive the "done" status and after completing the k rounds they start a new epoch.

Analysis
In this section we analyze Methodical Counting. References to algorithm lines are given as f igure# . line# .
We will use the standard notation for the L p norm of vector x = (x 1 , x 2 , . . . , x n ) 2 As opposed to previous work, in Methodical Counting the leader also follows this procedure. ρ ← 0 // accumulator of consumed potential 3: else // k is wrong 13: status ← alarm 14: Φ ← 1 /* r rounds completed */ 15: if phase = 1 and Φ > τ then // k is wrong 16: status ← alarm if status = normal then // prepare for next phase 19:   if status = normal and |N | ≤ d − 1 and ∀i ∈ N : status i = normal then 10: 1/p , for any p ≥ 1. Only for the analysis, nodes are labeled as 0, 1, 2, . . . , n − 1, where the leader has label 0. The potential of a node i at the beginning of round t is denoted as Φ t [i], and the potential of all nodes at the beginning of round t is denoted as a vector Φ t . The aggregated potential is then ||Φ t || 1 . The subindex t is used for rounds, phases, or dropped as needed. We will refer to the potential right after the last round of a phase as Φ r+1 . Such round does not exist in the algorithm, but we use this notation to distinguish between the potential right before the leader consumes its own potential (cf. Line 1.23) and the potential at the beginning of the first round of the next phase. First, we provide a broad description of our analysis of Methodical Counting. Consider the vector of potentials Φ i held by nodes at the beginning of any given phase i. The way that potentials are updated in each round (cf. Lines 1.11 and 2.10) is equivalent to the progression of a d-lazy random walk on the evolving graph underlying the network topology [1], where the initial vector of potentials is equivalent to an initial distribution p i on the overall potential ||Φ i || 1 and the probability of choosing a specific neighbor is 1/d. For instance, the initial vector of potentials Φ 0 = 0, 1, 1, . . . , corresponds to a distribution p 0 = 0, 1/(n − 1), 1/(n − 1), . . . on the initial ||Φ 0 || 1 = n − 1.
Note that our Methodical Counting is not a simple "derandomization" of the lazy random walk on evolving graphs. First, in the Anonymous Dynamic Network model neighbors cannot be distinguished, and even their number is unknown at transmission time (only at receiving time the node learns the number of its neighbors). Second, due to unknown network parameters, it may happen in an execution of Methodical Counting that the total potential received could be bigger than 1. Third, our algorithm does not know a priori when to terminate and provide result even with some reasonable accuracy, as the formulas on mixing and cover time of lazy random walks depend on (a priori unknown) number of nodes n. Nevertheless, we can still use some results obtained in the context of analogous lazy random walks in order to prove useful properties of parts of algorithm Methodical Counting, namely, some parts in which parameters are temporarily fixed and the number of received messages does not exceed parameter d.
It was shown in [1] that random walks on d-regular explorable evolving graphs have a uniform stationary distribution, and bounds on the mixing and cover time were proved as well. Moreover, it was observed that those properties hold even if the graph is not regular and d is only an upper bound on the degree. 3 Thus, for the cases where d is an upper bound on the number of neighboring nodes, we analyze the evolution of potentials within each phase leveraging previous work on random walks on evolving graphs. Specifically, we use the following result which is an extension of Corollary 14 in [1]. Theorem 1. (Corollary 14 in [1].) After t rounds of a d max -lazy random walk on an evolving graph with n nodes, dynamic diameter D, upper bound on maximum degree d max , and initial distribution p 0 , the following holds.
In between phases the leader "consumes" its potential, effectively changing the distribution at that point. Then, a new phase starts.
In Methodical Counting, given that d is a function of the estimate k, if the estimate is low there may be inputs for which d is not an upper bound on the number of neighbors. We show in our analysis that in those cases the leader detects the error and after some time all nodes increase the estimate.
First, we prove correctness when k = n as follows.
Lemma 1. If d ≥ k and k = n, after running the Methodical Counting protocol for p ≥ k 1−1/k ln(k(k − 1)) phases, each of r ≥ 4dk 2 ln k rounds, the potential ρ consumed by the leader is Proof. The second inequality is immediate because the initial total potential in the network is n − 1 and it does not increase during the execution. So, if k = n, the potential consumed by the leader cannot be more than k − 1.
For the first inequality, consider the vector of potentials Φ 1 at the beginning of round 1 of any phase i.
As explained above, we analyze the evolution of potentials within phase i as a random walk on the evolving graph underlying the network topology. Consider the initial distribution p i on the overall potential ||Φ 1 || 1 . Then, using Theorem 1, we know that after a phase i of r ≥ 4dk 2 ln k rounds the distribution is such that Notice that the latter is true for any initial distribution, as the distance to uniform in Equation 1 has been upper bounded by 1. Thus, applying recursively we have that after p ≥ k 1−1/k ln(k(k − 1)) phases it is Thus, the claim follows.
The previous lemma shows that if ρ > k − 1 or ρ < k − 1 − 1/k we know that the estimate k is wrong, but the complementary case, that is, k − 1 − 1/k ≤ ρ ≤ k − 1, may occur even if the estimate is k < n and hence the error has to be detected by other means. To prove correctness in that case, we show first that if k < n ≤ k 1+ǫ for some ǫ > 0 the leader must consume ρ > k − 1 potential if the protocol is run long enough. To ensure that d ≥ ∆ + 1, we restrict d ≥ k 1+ǫ . Lemma 2. If 1 < k < n ≤ k 1+ǫ ≤ d, ǫ > 0, after running the Methodical Counting protocol for p ≥ (2+ǫ)k 1+ǫ 1−1/k ln k phases, each of r ≥ (4 + 2ǫ)dk 2+2ǫ ln k rounds, the potential ρ consumed by the leader is ρ > k − 1.
It remains to show that even if n > k 1+ǫ Methodical Counting still detects that the estimate is low. First, we prove the following two claims that establish properties of the potential during the execution of Methodical Counting. (Recall that we use round r + 1 to refer to potentials at the end of the phase right before the leader consumes its potential in Line 1.23.) Claim 1. Given an Anonymous Dynamic Network of n nodes running Methodical Counting with parameter d, for any round t of the first phase, such that 1 ≤ t ≤ r + 1, if d was larger than the number of neighbors of each node x for every round t ′ < t, then ||Φ t || 1 = n − 1.
Proof. For the first round the claim holds as the initial potential of each node is 1 except the leader that gets 0. That is, ||Φ 1 || 1 = n − 1. For any given round 1 < t ≤ r + 1 in phase 1 and any given node x, if d is larger than the number of neighbors of x, the potential is updated only in Lines 1.11 and 2.10 as is the set of neighbors of node x in round t. Inductively, assume that the claim holds for some round 1 ≤ t ≤ r. We want to show that consequently it holds for t + 1. The potential for round t + 1 is In the Anonymous Dynamic Network model, communication is symmetric. That is, for every pair of nodes x, y ∈ V and round t, it is Consider the summations in Equation 2. Due to symmetric communication, we have that the potential Φ t [y ′ ] appears with positive sign when the indeces of the summations are x = x ′ and y = y ′ , and with negative sign when the indices are x = y ′ and y = x ′ . This observation applies to all pairs of nodes that communicate in any round t. Therefore, we can re-write Equation 2 as Thus, the claim follows.

Claim 2.
Given an Anonymous Dynamic Network of n nodes running Methodical Counting, for any round t of any phase and any node x, it is 0 Proof. If t = 1 the potential of the leader is Φ 1 [0] = 0 and the potential of any non-leader node x is Φ 1 [x] = 1. Thus, the claim follows. Inductively, for any round 2 < t ≤ r + 1, we consider two cases according to node status. If a node x is in alarm status at the beginning of round t, then it is Φ t [x] = 1 as, whenever the status of a node is updated to alarm, its potential is set to 1 and will not change until the next epoch (cf. Figures 1 and 2). On the other hand, if a node x is in normal status at the beginning of round t, it had its potential updated in all rounds t ′ < t only in Lines 1.11 and 2.10 as For all rounds t ′ < t, node x exchanged potential with less than d neighbors, because otherwise it would have been changed to alarm status in Lines 1.13 and 2.12. Therefore it is It remains to show that even if n > k 1+ǫ Methodical Counting still detects that the estimate is low. We focus on the first phase. We define a threshold τ such that, after the phase is completed, all nodes that have potential above τ can send an alarm to the leader, as such potential indicates that the estimate is low. We show that the alarm must be received after k 1+ǫ further rounds of communication.
Lemma 3. For ǫ > 0, after running the first phase of the Methodical Counting protocol, there are at most k 1+ǫ nodes that have potential at most τ = 1 − 1/k 1+ǫ .
Proof. We define the slack of node x at the beginning of round t as s t [x] = 1 − Φ t [x] and the vector of slacks at the beginning of round t as s t . In words, the slack of a node is the "room" for additional potential up to 1. Recall that the overall potential at the beginning of round 1 of phase 1 is ||Φ 1 || 1 = n − 1. Also notice that for any round and any node x the potential of x is non-negative as shown in Claim 2. Therefore, the overall slack with respect to the maximum potential that could be held by all the n nodes at the beginning of round 1 is ||s 1 || 1 = 1.
Consider a partition of the set of nodes {L, H}, where L is the set of nodes with potential at most τ = 1 − 1/k 1+ǫ at the end of the first phase, before the leader consumes its own potential in Line 1.23. That is, Φ r+1 [x] ≤ τ for all x ∈ L. Assume that the slack held by nodes in L at the end of the first phase is at most the overall slack at the beginning of the phase. That is, x∈L s r+1 [x] ≤ ||s 1 || 1 = 1. By definition of L, we have that for each node x ∈ L it is s r+1 Thus, |L| ≤ 1/(1 − τ ) = k 1+ǫ and the claim follows.
Then, to complete the proof, it remains to show that x∈L s r+1 [x] ≤ 1. Let the scenario where d is larger than the number of neighbors that each node has in each round of the first phase be called "case 1", and "case 2" otherwise. Claim 1 shows that in case 1 at the end of the first phase it is ||Φ r+1 || 1 = n − 1. Therefore, the slack held by all nodes is ||s r+1 || 1 = 1 and the slack held by nodes in L ⊆ V is x∈L s t [x] ≤ 1. We show now that indeed case 1 is a worst-case scenario. That is, in the complementary case 2 where some nodes have d neighbors or more in one or more rounds, the slack is even smaller. To compare both scenarios we denote the slack for each round t, each node x, and each case i as s Assume that some node x is the first one to have d ′ > d − 1 neighbors. Let 1 ≤ t ≤ r be the round of the first phase when this event happened. We claim that ||s (2) t+1 || 1 ≤ ||s (1) t+1 || 1 . The reason is the following. Given that more than d − 1 potentials are received, node x increases its potential to 1 for the rest of the epoch (cf. Lines 1.12 and 2.11). That is, the slack of x is s . Additionally, the potential shared by x with all neighbors during round t is Lines 1.11 and 2.10). That is, the potential shared by x with neighbors in case 2 is more than the potential that x would have shared in case 1. Then, combining both effects (the relative increase in potential of x and its neighbors') the overall slack is ||s (2) t+1 || 1 ≤ ||s (1) t+1 || 1 . The same argument applies to all other nodes with d or more neighbors in round t. Additionally, for any round t ′ of the first phase, such that t < t ′ ≤ r, we have to additionally consider the case of a node y that, although it does not receive more than d−1 potentials, it moves to alarm status because it has received an alarm in round t ′ . Then, notice that the potential of y is Φ t ′ +1 [y] = 1 ≥ Φ t ′ [y], and it will stay in 1 for the rest of the epoch (cf. Lines 1.13 and 2.12). Therefore, the slack of y is s Combining all the effects studied over all rounds, the slack at the end of the first phase is ||s In our last lemma, we show that if k 1+ǫ < n the leader detects the error.
And the latter is true for any k > 1. Consider a partition of the set of nodes {L, H}, where L is the set of nodes with potential at most τ = 1 − 1/k 1+ǫ at the end of the first phase. At the end of the first phase, the size of L is at most k 1+ǫ (cf. Lemma 3), and the size of H is at least 1 because n > k 1+ǫ . Thus, there is at least one node changing to alarm status in Line 2.15 in round 1 of phase 2, and due to 1-interval connectivity at least one new node moves from L to H in each of the following rounds. Thus, the claim follows.
Based on the above lemmata, we establish our main result in the following theorem.
Theorem 2. Given an Anonymous Dynamic Network with n nodes, after running Methodical Counting for each estimate k = 2, 3, . . . , n with parameters where ǫ > 0, all nodes stop after n k=2 (pr + k) rounds of communication and output n.
Proof. Notice that the above parameters fulfill the conditions of the previous lemmas. First we prove that Methodical Counting is correct. To do so, it is enough to show that for each estimate k < n the algorithm detects the error and moves to the next estimate, and that if otherwise k = n the algorithm stops and outputs k. We consider three cases: k = n, k < n ≤ k 1+ǫ , and k 1+ǫ < n, for a chosen value of ǫ > 0.
Assume first that k < n ≤ k 1+ǫ . Then, even if the leader does not receive an alarm during the execution, as shown in Lemma 2, at the end of the epoch in Line 1.21 the leader will detect that ρ is out of range and will not change its status to done. Therefore, no other node will receive a termination message (loop in Line 1.28), and all nodes will continue to the next epoch.
Assume now that k 1+ǫ < n. Lemma 4 shows that within the following k 1+ǫ rounds after the first phase the leader has received an alarm message, even if no node has more than d − 1 neighbors during the execution and alarms due to this are not triggered. For the given value of p and k ≥ 2, the epoch has more than one phase. Therefore, within k 1+ǫ rounds into the second phase the leader will change to alarm status in Line 1.13, will not change its status to done later in this epoch, and no other node will receive a termination message. Hence, all nodes will continue to the next epoch.
Finally, if k = n, Lemma 1 shows that the accumulated potential ρ will be k − 1 − 1/k ≤ ρ ≤ k − 1. Thus, in Line 1.21 the leader will change its status to done, and in the loop of Line 1.28 will inform all other nodes that the current estimate is correct. The number of iterations of such loop are enough due to 1-interval connectivity.
The claimed running time can be obtained by inspection of the algorithm, either for the leader or nonleader since they are synchronized. Refer for instance to the leader algorithm in Figure 1. The outer loop in Line 1.5 corresponds to each epoch with estimates k = 2, 3, . . . , n. For each epoch, Line 1.6 starts a loop of p phases followed by k rounds in Line 1.28. Each of the p phases has r rounds. Thus, the overal number of rounds is n k=2 (pr + k).

Extensions
We argue that Methodical Counting can be extended to compute the sum of values stored in the nodes, and thus also the average (as it computes the number of nodes n), and other functions. Assume that each node of the Anonymous Dynamic Network initially stores a value, represented as a sequence of bits. W.l.o.g. we could assume that the value stored at the leader is zero; otherwise, the nodes could compute the sum of other initial values (with the leader value set up to 0), and later the leader could propagate its actual initial value appended to the message "done" at the end of the execution to be added to the computed sum of other nodes. The modified Methodical Counting prepends the potential to the sequence. Instead of sending potential by the original Methodical Counting, each node transmits its current sequence (in which the potential stands in the first location). Changes at each position of the sequence are done independly by the same algorithm as used for the potential, cf. Figures 1 and 2. Re-setting the values, in the beginning of each epoch, means putting back the initial values of the sequence. It means that the modified algorithm maintains potential in exactly the same way as the original Methodical Counting, regardless of the initial values. At the end of some epoch, with number corresponding to the number of nodes n, all nodes terminate. When it happens, each node recalls the sequence stored in it at the end of the first phase of the epoch, multiplies the values stored at each position of the sequence by the epoch number n, and rounds each of the results to the closest integer; then it sums up the subsequent values multiplied by corresponding (consecutive) powers of 2. Note that such "recalling" could be easily implemented by storing and maintaining the sequence after the first phase of each epoch.
We argue that the computed value is the sum of the initial values. It is enough to analyze how the modified algorithm processes values at one position of the sequence, as positions are treated independently; therefore, w.l.o.g. we assume that each node has value 0 or 1 in the beginning. Consider the last epoch before the leader sends the final sequence (in our case, representing one value). In the beginning of the epoch, the values are re-set to the original one, and manipulated independently according to the rules in Figures 1 and 2. Therefore, let us focus on the first phase of this epoch. Since we already proved that the estimate of the last epoch is equal to the number of nodes, the value of d in this epoch (and thus also in its first phase) is an upper bound on the node degree. Thus, the mass distribution scaled down by the sum of the initial values behaves exactly the same as the probabilities of being at nodes in the corresponding round of the lazy random walk, with parameter d and starting from initial distribution equal to the initial values divided by the sum. Since the length of the phase is set up to guarantee that the distribution is close to the stationary uniform within error 1/n, and the sum of bits is not bigger than n, at the end of the phase the value stored by each node is close to the sum (i.e., scaling factor) divided by n by at most 1/n 4 (cf. Equation 1). Therefore, after multiplying it by n, each node gets value of sum within error of at most 1/n 3 , which after rounding will give the integer equal to the value of the sum.
Once having the number n and the sum, each node can compute the average. As argued in [12], the capacity of computing the sum of the input values makes possible the computation of more complex functions. Moreover, as opposed to [12] where the computation only converges, our approach outputs the exact sum. Therefore, the extension to database queries that can be approximated using linear synopses 4 is straightforward. Boolean functions f : {0, 1} n → {0, 1}, such as AND (sum = n), OR (sum > 0), and XOR (sum = 1), as well as their complementaries NAND (sum = n), NOR (sum = 0), and XNOR (sum = 1), can also be implemented having n and the sum. This applies also to other "symmetric" Boolean functions, i.e., which do not depend on the order of variables, as they could be computed based on computed sum of ones and n [13]. Maximum (L ∞ norm) and minimum can be computed subsequently by flooding. That is, each node broadcasts the maximum and minimum input values seen so far. Due to 1-interval connectivity within n rounds all nodes have the answers.
Note that all these computations, including the Methodical Counting, could be done using only polynomial estimates of values, that is, with messages of length O(log n), multiplied by the maximum number of coordinates of any of the initial values. This could be also traded for time: we could use only messages of length O(log n) with time increased by the maximum number of coordinates of any initial value (which is still polynomial in the size of the input, 5 which in this case is at least n plus the maximum number of coordinates).

Open Directions
Straightway questions emerging from our work include existence of polynomial (in n) lower bound and improvement of our upper bound. One of the potential ways could be through investigating bi-directional relationships between random processes and computing algebraic functions in Anonymous Dynamic Network. Extending the range of polynomially computable functions is another intriguing future direction. Finally, generalizing the model by not assuming connectivity in every round or dropping assumption on synchrony could introduce even more challenging aspects of communication and computation, including group communication and its impact on the common knowledge about the system parameters.