SlowGuess/NeurIPS_2023_OCR huggingface dataset:
Fully Dynamic  $k$ -Clustering in  $\tilde{O}(k)$  Update Time
Sayan Bhattacharya
University of Warwick s.bhattacharya@warwick.ac.uk
Martín Costa
University of Warwick martin.costa@warwick.ac.uk
Silvio Lattanzi
Google Research silviol@google.com
Nikos Parotsidis
Google Research nikosp@google.com
We present a  $O(1)$ -approximate fully dynamic algorithm for the  $k$ -median and  $k$ -means problems on metric spaces with amortized update time  $\tilde{O}(k)$  and worst-case query time  $\tilde{O}(k^2)$ . We complement our theoretical analysis with the first in-depth experimental study for the dynamic  $k$ -median problem on general metrics, focusing on comparing our dynamic algorithm to the current state-of-the-art by Henzinger and Kale [20]. Finally, we also provide a lower bound for dynamic  $k$ -median which shows that any  $O(1)$ -approximate algorithm with  $\tilde{O}(\mathrm{poly}(k))$  query time must have  $\tilde{\Omega}(k)$  amortized update time, even in the incremental setting.
Clustering is a fundamental problem in unsupervised learning with several practical applications. In clustering, one is interested in partitioning elements into different groups (i.e. clusters), so that elements in the same group are more similar to each other than to elements in other groups. One of the most studied formulations of clustering is the metric clustering formulation. In this setting, elements are represented by points in a metric space, and the distances between points represent how similar the corresponding elements are (the closer elements are, the more similar they are). More formally, the input to our problem consists of a set of points  $U$  in a metric space with distance function  $d: U \times U \to \mathbb{R}_{\geq 0}$ , a real number  $p \geq 1$ , and an integer  $k \geq 1$ . The goal is to compute a subset  $S \subseteq U$  of size  $|S| \leq k$ , so as to minimize  $\mathrm{cost}(S) := \sum_{x \in U} d(x, S)^p$ , where  $d(x, S) := \min_{y \in S} d(x, y)$ . We refer to the points in  $S$  as centers. We note that this captures well-known problems such as  $k$ -median clustering (when  $p = 1$ ) or  $k$ -means clustering (when  $p = 2$ ).
Due to this simple and elegant formulation, metric clustering has been extensively studied throughout the years, across a range of computational models [14, 22, 1, 12, 15, 2, 31, 11, 7]. In this paper, we focus on the dynamic setting, where the goal is to efficiently maintain a good clustering when the input data keeps changing over time. Because of its immediate real-world applications, the dynamic clustering problem has received a lot of attention from the machine learning community in recent years [16, 6, 10, 24, 18]. Below, we formally describe the model [13, 20, 3] considered in this paper.
At preprocessing, the algorithm receives the initial input  $U$ . Subsequently, the input keeps changing by means of a sequence of update operations, where each update inserts/deletes a point in  $U$ . Throughout this sequence of updates, the algorithm needs to implicitly maintain a solution  $S^{*} \subseteq U$  to the current input  $(U,d)$ . The algorithm has an approximation ratio of  $\rho \geq 1$  if and only if we always have  $\mathrm{cost}(S^{*}) \leq \rho \cdot \mathrm{opt}(U)$ , where  $\mathrm{opt}(U) := \min_{S \subseteq U, |S| \leq k} \mathrm{cost}(S)$  denotes the optimal objective. Finally, whenever one queries the algorithm, it has to return the list of centers in  $S^{*}$ . The performance of a dynamic algorithm is captured by its approximation ratio, its update time, and its query time. Let
$U_{\mathrm{INIT}}$  be the state of the input  $U$  at preprocessing. A dynamic algorithm has (amortized) update time  $O(\lambda)$  if for any sequence of  $t \geq 0$  updates, the total time it spends on preprocessing and handling the updates is  $O((t + |U_{\mathrm{INIT}}|) \cdot \lambda)$ . The time it takes to answer a query is called its query time.
Our Contributions. Our primary contribution is to design an algorithm for this problem with a near-optimal update time of  $\tilde{O}(k)$ , without significantly compromising on the approximation ratio and query time. $^1$  Interestingly, our algorithm can be easily extended to dynamic  $k$ -clustering for any constant  $p$  (see Appendix A). In the theorem below, we summarize our main result for dynamic  $k$ -median and  $k$ -means.
Theorem 1.1. There exists a dynamic algorithm that, with high probability, maintains a  $O(1)$ -approximate solution to the  $k$ -median and  $k$ -means problems for general metric spaces under point insertions and deletions with  $\tilde{O}(k)$  amortized update time and  $\tilde{O}(k^2)$  worst-case query time.
It is important to note that in practice an algorithm often receives the updates in batches of variable sizes [25, 29], so it is common for there to be a significant number of updates between cluster requests. As a consequence, optimizing the update time is of primary importance in practical applications. For example, if the size of each batch of updates is  $\Omega(k)$ , then our amortized query time becomes  $\tilde{O}(k^2 / k) = \tilde{O}(k)$ , because the algorithm has to answer a query after processing at least one batch. Thus, our dynamic algorithm has near-optimal update and query times when the updates arrive in moderately large batches.
In addition, it is possible to observe that any dynamic  $k$ -clustering algorithm must have  $\Omega(k)$  query time, since the solution it needs to return while answering a query can consist of  $k$  centers. We also show a similar lower bound on the update time of any constant approximate dynamic algorithm for this problem. This lower bound holds even in an incremental setting, where we only allow for point-insertions in  $U$ . We defer the proof of Theorem 1.2 to Appendix D.
Theorem 1.2. Any  $O(1)$ -approximate incremental algorithm for the  $k$ -median problem with  $\tilde{O}(\mathrm{poly}(k))$  query time must have  $\tilde{\Omega}(k)$  amortized update time.
Theorem 1.2 implies that the ultimate goal in this line of research is to get a  $O(1)$ -approximate dynamic  $k$ -clustering algorithm with  $\tilde{O}(k)$  update time and  $\tilde{O}(k)$  query time. Prior to this work, however, the state-of-the-art result for dynamic  $k$ -median (and  $k$ -means) was due to Henzinger and Kale [20], who obtained  $O(1)$ -approximation ratio,  $\tilde{O}(k^2)$  update time and  $\tilde{O}(k^2)$  query time. In this context, our result is a meaningful step toward obtaining an asymptotically optimal algorithm for the problem.
We supplement the above theorem with experiments that compare our algorithm with that of [20]. To the best of our knowledge, this is the first work in the dynamic clustering literature with a detailed experimental evaluation of dynamic  $k$ -median algorithms for general metrics. Interestingly, we observe that experimentally our algorithm is significantly more efficient than previous work without impacting the quality of the solution.
Our Techniques. We first summarize the approach used in the previous state-of-the-art result. In [20], the authors used a static algorithm for computing a coreset of size  $\tilde{O}(k)$  as a black box to get a fully dynamic algorithm for maintaining a coreset of size  $\tilde{O}(k)$  for general metric spaces in worst-case update time  $\tilde{O}(k^2)$ . Their algorithm works by maintaining a balanced binary tree of depth  $O(\log n)$ , where each leaf in the tree corresponds to a point in the metric space [4]. Each internal node of the tree then takes the union of the (weighted) sets of points maintained by its two children and computes its coreset, which is then fed to its parent. They maintain this tree dynamically by taking all the leaves affected by an update and recomputing all the coresets at nodes contained in their leaf-to-root paths. Using state-of-the-art static coreset constructions, this update procedure takes time  $\tilde{O}(k^2)$ . Unfortunately, if one wants to ensure that the final output is a valid corset of the metric space after each update, then there is no natural way to bypass having to recompute the leaf-to-root path after the deletion of a point that is contained in the final coreset. Hence, it is not at all clear how to modify this algorithm in order to reduce the update time to  $\tilde{O}(k)$ .
1 Throughout this paper, we use  $\tilde{O}(.)$  and  $\tilde{\Omega}(.)$  notations to suppress polylog $(n)$  factors, where  $n = |U|$ .
We circumvent this bottleneck by taking a completely different approach compared to [20]. Our algorithm instead follows from the dynamization of a  $\tilde{O}(nk)$  time static algorithm for  $k$ -median by Mettu and Plaxton [28], where  $n = |U|$ . We refer to this static algorithm as the MP algorithm. Informally, the MP algorithm works by constructing a set of  $O(\log(n/k))$  layers by iteratively sampling random points at each layer, defining a clustering of the points in the layer that are 'close' to these samples, and removing these clusters from the layer. The algorithm then obtains a solution to the  $k$ -clustering problem by running a static algorithm for weighted  $k$ -median (defined in Section 2), on an instance of size  $O(k\log(n/k))$  defined by the clusterings at each layer. In order to Dynamize this algorithm, we design a data structure that maintains  $O(\log(n/k))$  layers that are analogous to the layers in the MP algorithm. By allowing for some 'slack' in the way that these layers are defined, we ensure that they can be periodically reconstructed in a way that leads to good amortized update time, while only incurring a small loss in the approximation ratio. The clustering at each layer is obtained by random sampling every time it is reconstructed and is maintained by arbitrarily reassigning a point in a cluster as the new center when the current center is deleted. We obtain a solution to the  $k$ -clustering problem from this data structure in the same way as the (static) MP algorithm—by running a static algorithm for weighted  $k$ -median on an instance defined by the clusterings maintained at each layer.
In our computational model, the algorithm has access to the distance  $d(x,y)$  for any  $x,y\in U$  in  $O(1)$  time². Given any two sets  $X,S\subseteq U$ , define the cost of  $X$  w.r.t.  $S$  as  $\mathrm{cost}(S,X)\coloneqq \sum_{x\in X}\min_{s\in S}d(x,s)$ . In addition, let  $\mathrm{cost}(S) = \mathrm{cost}(S,U)$ . Next, define an assignment to be a function  $\pi :U\to U$ , and say that  $\pi$  assigns  $x\in U$  to  $\pi (x)$ . We refer to an assignment  $\pi$  with  $|\pi (U)|\leq m$  as an m-assignment. The cost of  $X\subseteq U$  w.r.t. assignment  $\pi$  is  $\mathrm{cost}(\pi ,X)\coloneqq \sum_{x\in X}d(x,\pi (x))$ . We denote  $\mathrm{cost}(\pi ,U)$  by  $\mathrm{cost}(\pi)$ . For any  $\rho \geq 1$ , say that  $\pi$  is  $\rho$ -approximate if  $\mathrm{cost}(\pi)\leq \rho \cdot \mathrm{opt}(U)$ , where  $\mathrm{opt}(U)$  is the cost of the optimal solution.
Given any subset  $U' \subseteq U$  and  $x \in U'$ , we denote by  $B_{U'}(x, r)$  the set  $\{y \in U' \mid d(x, y) \leq r\}$ , i.e. the closed ball in  $U'$  of radius  $r \geq 0$  centered at  $x$ . When it is clear from the context that  $U' = U$ , we omit the subscript  $U'$  and simply write  $B(x, r)$ . For  $X \subseteq U' \subseteq U$ , we define  $B_{U'}(X, r)$  to be the union of the balls  $B_{U'}(x, r)$  for  $x \in X$ .
Definition 2.1. Given  $0 < \rho < 1$  and subsets  $X \subseteq U' \subseteq U$ , we define the following real numbers:
$$
\nu_ {\rho} (X, U ^ {\prime}) := \min  \{r \geq 0 | | B _ {U ^ {\prime}} (X, r) | \geq \rho \cdot | U ^ {\prime} | \}, \mu_ {\rho} (U ^ {\prime}) := \min  \{\nu_ {\rho} (Y, U ^ {\prime}) | Y \subseteq U ^ {\prime}, | Y | = k \}.
$$
In words, the real number  $\nu_{\rho}(X,U^{\prime})$  is the smallest radius  $r$  such that the closed ball of radius  $r$  around  $X$  captures at least a  $\rho$ -proportion of the points in  $U^{\prime}$ . The real number  $\mu_{\rho}(U^{\prime})$  is then defined to be the smallest such radius  $\nu_{\rho}(X,U^{\prime})$  over all subsets  $X\subseteq U^{\prime}$  of size  $k$ . Note that  $\nu_{\rho}(X,U^{\prime})$  and  $\mu_{\rho}(U^{\prime})$  are both increasing as functions of  $\rho$ .
Finally, we will sometimes refer to the weighted  $k$ -median problem. An instance of this problem is given by a triple  $(U,d,w)$ , where  $w: U \to \mathbb{R}_{\geq 0}$  assigns a nonnegative weight to every point  $x \in U$ , in a metric space with distance function  $d$ . Here, the goal is to compute a subset of at most  $k$  centers  $S \subseteq U$ , so as to minimize  $\mathrm{cost}_w(S) := \sum_{x \in U} w(u) \cdot d(x,S)$ . We let  $\mathrm{opt}_w(U) := \min_{S \subseteq U: |S| \leq k} \mathrm{cost}_w(S)$  denote the optimal objective value of this weighted  $k$ -median instance. We will analogously use the symbol  $\mathrm{cost}_w(S,X) := \sum_{x \in X} w(x) \cdot d(x,S)$  to denote the cost of a set of points  $X$  w.r.t.  $S$  and the weight function  $w$ .
3 Our Algorithm
Throughout this section, we fix the following parameters:  $\alpha$ ,  $\beta$ ,  $\epsilon$ ,  $\tau$  and  $k'$ ; where  $\alpha \geq 1$  is a sufficiently large constant,  $\beta$  is any constant in the interval  $(0,1)$ ,  $\epsilon > 0$  is a sufficiently small constant,  $\tau \coloneqq \epsilon \beta$ , and  $k' \coloneqq \max\{k, \log(|U|)\}$ .
2This is a standard and common assumption in clustering settings.
3.1 The Static Algorithm of [28]
Our starting point is the static algorithm of [28] for computing a  $\tilde{O}(k)$ -assignment  $\sigma: U \to U$ , with  $S = \sigma(U)$ , such that  $\mathrm{cost}(\sigma) \leq O(1) \cdot \mathrm{opt}(U)$ . The relevant pseudocode appears in Algorithm 1 and Algorithm 2.
Algorithm 1 StaticAlgo(U)
1:  $i\gets 1$  and  $U_{1}\leftarrow U$    
2: while  $|U_i| > \alpha k'$  do   
3:  $(S_{i},C_{i},\sigma_{i},\nu_{i})\gets \mathrm{AlmostCover}(U_{i})$    
4:  $U_{i + 1}\gets U_i\setminus C_i$    
5:  $i\gets i + 1$    
6: end while   
7:  $t\gets i$    
8:  $S_{t}\gets U_{t},C_{t}\gets S_{t}$  and  $\nu_{t}\gets 0$    
9: Assign each  $x\in C_t$  to itself (i.e.,  $\sigma_t(x)\coloneqq x\in S_t$    
10:  $S\gets \cup_{j\in [t]}S_j$    
11: Let  $\sigma :U\to S$  be the assignment such that for all  $j\in [t]$  and  $x\in C_j$  , we have  $\sigma (x) = \sigma_{j}(x)$    
12: return  $(S,\sigma ,t)$
Algorithm 2 AlmostCover  $(U^{\prime})$
1: Construct a set  $S'$ , by sampling  $\alpha k'$  points from  $U'$  independently and u.a.r. with replacement  
2:  $\nu' \gets \nu_{\beta}(S', U')$  and  $C' \gets B(S', \nu')$   
3: Assign each  $x \in C'$  to some  $\sigma'(x) \in S'$  such that  $d(x, \sigma'(x)) \leq \nu'$   
4: return  $(S', C', \sigma', \nu')$
The algorithm runs for  $t$  iterations. Let  $U_{i} \subseteq U$  denote the set of unassigned points at the start of iteration  $i \in [t - 1]$  (initially, we have  $U_{1} = U$ ). During iteration  $i$ , the algorithm samples a set of  $\alpha k'$  points  $S_{i}$  as centers, uniformly at random from  $U_{i}$ . It then identifies the smallest radius  $\nu_{i}$  such that the balls of radius  $\nu_{i}$  around  $S_{i}$  cover at least  $\beta$ -fraction of the points in  $U_{i}$ . Let  $C_{i} \subseteq U_{i}$  denote the set of points captured by these balls (note that  $C_{i} \supseteq S_{i}$  and  $|C_i| \geq \beta \cdot |U_i|$ ). The algorithm assigns each  $x \in C_{i}$  to some center  $\sigma_{i}(x) \in S_{i}$  within a distance of  $\nu_{i}$ . It then sets  $U_{i + 1} \gets U_{i} \setminus C_{i}$  and proceeds to the next iteration. In the very last iteration  $t$ , we have  $|U_t| \leq \alpha k'$ , and the algorithm sets  $S_{t} := U_{t}, C_{t} := U_{t}$ , and  $\sigma_t(x) := x$  for each  $x \in U_t$ .
The algorithm returns the assignment  $\sigma$  with set of centers  $\sigma(U) = S$ , where  $\sigma$  is simply the union of  $\sigma_{i}$  for all  $i \in [t]$ .
Fix any  $i \in [t - 1]$ . Since  $|C_i| \geq \beta \cdot |U_i|$ , we have  $|U_{i + 1}| \leq (1 - \beta) \cdot |U_i|$ . As  $\beta$  is a constant, it follows that the algorithm runs for  $t = \tilde{O}(1)$  iterations. Since each iteration picks  $O(k)$  new centers, we get:  $|S| = \tilde{O}(k)$ , and hence  $\sigma$  is a  $\tilde{O}(k)$ -assignment. Furthermore, [28] showed that  $\mathrm{cost}(\sigma) = O(1) \cdot \mathrm{opt}(U)$ .
3.2 Our Dynamic Algorithm
The main idea behind our dynamic algorithm is that it maintains the output  $S$  of the static algorithm from Section 3.1 in a lazy manner, by allowing for some small slack at appropriate places. Thus, it maintains  $t$  layers, where each layer  $i \in [t]$  corresponds to iteration  $i$  of the static algorithm. In the dynamic setting, the value of  $t$  changes over time. Specifically, layer  $i \in [t]$  consists of the tuple  $(U_i, S_i, C_i, \sigma_i, \nu_i)$ , as in Algorithm 1. Whenever a large fraction of points gets inserted or deleted from some layer  $j \in [t]$ , the dynamic algorithm rebuilds all the layers  $i \in [j, t]$  from scratch.
The dynamic algorithm maintains the sets  $U_{i}, S_{i}$  and  $C_i$  explicitly, and the assignment  $\sigma_{i}$  implicitly, in a manner which ensures that for all  $x \in C_i$  it can return  $\sigma_{i}(x) \in S_{i}$  in  $O(1)$  time. Furthermore, for each layer  $i \in [t]$ , it explicitly maintains the value  $n_i$  (resp.  $n_i^*$ ), which denotes the size of the set  $U_{i}$  at (resp. the number of updates to  $U_{i}$  since) the time it was last rebuilt. We explain this in more detail below. The value  $\nu_{i}$  is needed only for the sake of analysis.
1:  $U_{1} \gets U$
2: ConstructFromLayer(1)
Algorithm 3 Preprocess  $(U)$
Algorithm 4 ConstructFromLayer(i)
1:  $j\gets i$    
2: while  $|U_j| > \alpha k'$  do   
3:  $n_j\gets |U_j|$  and  $n_j^*\gets 0$    
4:  $(S_{j},C_{j},\sigma_{j},\nu_{j})\gets \mathrm{AlmostCover}(U_{j})$    
5:  $U_{j + 1}\leftarrow U_j\setminus C_j$    
6:  $j\gets j + 1$    
7: end while   
8:  $t\gets j$    
9:  $S_{t}\gets U_{t},C_{t}\gets S_{t}$  and  $\nu_{t}\gets 0$    
10: Assign each  $x\in C_t$  to itself (i.e.,  $\sigma_t(x)\coloneqq x\in S_t$    
11:  $S\gets \cup_{j\in [t]}S_i$    
12: Let  $\sigma :U\to S$  be the assignment such that for all  $j\in [t]$  and  $x\in C_j$  , we have  $\sigma (x) = \sigma_{j}(x)$
Preprocessing: At preprocessing, we essentially run the static algorithm from Section 3.1 to set the value of  $t$  and construct the layers  $i \in [t]$ . See Algorithm 3 and Algorithm 4. Note that at this stage  $n_j^* = 0$  for all layers  $j \in [t]$ .
Algorithm 5 Insert(x)
1: for  $i = 1,\dots,t$  do  
2: Add  $x$  to  $U_{i}$   
3:  $n_i^* \gets n_i^* + 1$   
4: end for  
5: Add  $x$  to  $C_t$  and  $S_t$ , and set  $\sigma_t(x) \gets x$   
6: Rebuild
Handling the insertion of a point  $x$  in  $U$ : We simply add the point  $x$  to  $U_i$ , for each layer  $i \in [t]$ . Next, in the last layer  $t$ , we create a new center at point  $x$  and assign the point  $x$  to itself. Finally, we call the Rebuild subroutine (to be described below). See Algorithm 5.
Handling the deletion of a point  $x$  from  $U$ : Let  $j \in [t]$  be the last layer (with the largest index) containing the point  $x$ . We remove  $x$  from each layer  $i \in [j]$ . Next, if  $x$  was a center at layer  $j$ , then we pick any arbitrary point  $x' \in \sigma_j^{-1}(x) \setminus \{x\}$  (if such a point exists), make  $x'$  a center, and assign every point  $y \in \sigma_j^{-1}(x) \setminus \{x\}$  to the newly created center  $x'$ . Finally, we call the Rebuild subroutine (to be described below). See Algorithm 6.
The rebuild subroutine: We say that a layer  $i \in [t]$  is rebuilt whenever our dynamic algorithm calls ConstructFromLayer(j) for some  $j \leq i$ , and that there is an update in layer  $i$  whenever we add/remove a point in  $U_i$ . It is easy to see that  $n_i^*$  denotes the number of updates in layer  $i \in [t]$  since the last time it was rebuilt (see Line 3 in Algorithm 4, Algorithm 5 and Algorithm 6). Next, we observe that Line 6 in Algorithm 5 and Line 16 in Algorithm 6, along with the pseudocode of Algorithm 7, imply the following invariant.
Invariant 3.1.  $n_i^* \leq \tau n_i$  for all  $i \in [t]$ . Here,  $n_i$  is the size of  $U_i$  just after the last rebuild of layer  $i$ , and  $n_i^*$  is the number of updates in layer  $i$  since that last rebuild.
Intuitively, the above invariant ensures that the layers maintained by our dynamic algorithm remain close to the layers of the static algorithm in Section 3.1. This is crucially exploited in the proofs of Lemma 3.2 (which helps us bound the update and query times) and Lemma 3.3 (which leads to the desired bound on the approximation ratio). We defer the proofs of these two lemmas to Appendix B.
Lemma 3.2. We always have  $t = \tilde{O}(1)$ , where  $t$  denotes the number of layers maintained by our dynamic algorithm.
Algorithm 6 Delete  $(x)$
1: for  $i = 1,\dots,t$  do  
2: if  $x\in U_i$  then  
3: Remove  $x$  from  $U_{i}$  , and set  $n_i^*\gets n_i^* +1$   
4: if  $x\in C_i$  then  
5: Remove  $x$  from  $C_i$   
6: if  $x\in S_i$  then  
7: Remove  $x$  from  $S_{i}$   
8: if  $\exists y\in \sigma_i^{-1}(x)\setminus \{x\}$  then  
9: Pick any such  $y$  and place it into  $S_{i}$   
10: Set  $\sigma_i(z)$  to  $y$  for each  $z\in \sigma_i^{-1}(x)$   
11: end if  
12: end if  
13: end if  
14: end if  
15: end for  
16: Rebuild
Algorithm 7 Rebuild
1:  $i\gets 1$    
2: while  $i\leq t$  and  $n_i^* < \tau n_i$  do   
3:  $i\gets i + 1$    
4: end while   
5: if  $i\leq t$  then   
6: ConstructFromLayer(i)   
7: end if
Lemma 3.3. The assignment  $\sigma$  maintained by our dynamic algorithm always satisfies  $\mathrm{cost}(\sigma) = O(1)\cdot \mathrm{opt}(U)$ .
Answering a query: Upon receiving a query, we consider a weighted  $k$ -median instance  $(S,d,w)$  where each point  $x\in S$  receives a weight  $w(x)\coloneqq |\sigma^{-1}(x)|$ . Next, we compute a  $O(1)$ -approximate solution  $S^{*}\subseteq S$ , with  $|S^{*}|\leq k$ , to this weighted instance, so that  $\mathrm{cost}_w(S^*,S)\leq O(1)\cdot \mathrm{opt}_w(S)$ . We then return the centers in  $S^{*}$ .
Corollary 3.4.  $\mathrm{cost}(S^{*}) = O(1)\cdot \mathrm{opt}(U)$
Corollary 3.4 implies that our dynamic algorithm has  $O(1)$  approximation ratio. It holds because of Lemma 3.3, and its proof immediately follows from [19]. We delegate this proof to Appendix C.
Corollary 3.5. Our algorithm has  $\tilde{O}(k^2)$  query time.
Proof (Sketch). By Lemma 3.2, we have  $t = \tilde{O}(1)$ . Since the dynamic algorithm maintains at most  $\tilde{O}(k)$  centers  $S_i$  in each layer  $i$  (follows from Invariant 3.1), we have  $|S| = \sum_{i \in [t]} |S_i| = \tilde{O}(k)$ . Using appropriate data structures (see the proof of Claim 3.7), in  $O(1)$  time we can find the number of points assigned to any center  $x \in S$  under  $\sigma$  (given by  $|\sigma^{-1}(x)| = w(x)$ ). Thus, the weighted  $k$ -median instance  $(S, d, w)$  is of size  $\tilde{O}(k)$ , and upon receiving a query we can construct the instance in  $\tilde{O}(k)$  time. We now run a static  $O(1)$ -approximation algorithm [27] on  $(S, d, w)$ , which returns the set  $S^*$  in  $\tilde{O}(k^2)$  time.
Lemma 3.6. Our algorithm has  $\tilde{O}(k)$  update time.
Corollaries 3.4, 3.5 and Lemma 3.6 imply Theorem 1.1. It now remains to prove Lemma 3.6.
3.3 Proof of Lemma 3.6
We first bound the time taken to handle an update, excluding the time spent on rebuilding the layers.
Claim 3.7. Excluding the calls to Rebuild, Algorithm 5 and Algorithm 6 both run in  $\tilde{O}(1)$  time.