Faster DB-scan and HDB-scan in Low-Dimensional Euclidean Spaces

We present a new algorithm for the widely used density-based clustering method DBscan. Our algorithm computes the DBscan-clustering in $O(n\log n)$ time in $\mathbb{R}^2$, irrespective of the scale parameter $\varepsilon$ (and assuming the second parameter MinPts is set to a fixed constant, as is the case in practice). Experiments show that the new algorithm is not only fast in theory, but that a slightly simplified version is competitive in practice and much less sensitive to the choice of $\varepsilon$ than the original DBscan algorithm. We also present an $O(n\log n)$ randomized algorithm for HDBscan in the plane---HDBscan is a hierarchical version of DBscan introduced recently---and we show how to compute an approximate version of HDBscan in near-linear time in any fixed dimension.


Introduction
Clustering is one of the most fundamental tasks in data mining. Due to the wide variety of applications where clustering is important, the clustering problem comes in many variants. These variants differ for example in the dimensionality of the data set D and in the underlying metric, but also in the objective of the clustering. Thus a multitude of clustering algorithms has been developed [20], each with their own strengths and weaknesses. We are interested in density-based clustering, where clusters are defined by areas in which the density of the data points is high and clusters are separated from each other by areas of low density.
One of the most popular density-based clustering methods is dbscan; the paper by Ester et al. [11] on dbscan has been cited over 8,800 times, and in 2014 dbscan received the test-of-time award from KDD, a leading data-mining conference. dbscan has two parameters, ε and MinPts, that together determine when the density around a point p ∈ D is high enough for p to be part of a cluster (as apposed to being noise); see Section 2 for a precise definition of the dbscan clustering. Typically MinPts is a constant-in the original article [11] it is concluded that MinPts = 4 works well-but finding the right value for ε is more difficult. The worst-case running time of the original dbscan algorithm is Θ(n 2 ). It is often stated that the running time is O(n log n) for Euclidean spaces when a suitable indexing structure such as an R-tree is used to support the dbscan algorithm. While this may be true in certain practical cases, it is not true from a theoretical point of view.
Several variants of dbscan algorithm have been proposed, often with the goal to speed up the computation. Some (idbscan [5] and fdbscan [15]) do so at the expense of computing a slightly different, and not clearly defined, clustering. Others (gridbscan [16]) compute the same clustering as dbscan, but without speeding up the worst-case running time.
A fundamental bottleneck of the original dbscan algorithm is that it performs a query with each point p ∈ D to find N ε (p, D), the set of points within distance ε of p. Thus p∈D |N ε (p, D)| is a lower bound on the running time of the dbscan algorithm. In the worst case p∈D |N ε (p, D)| = Θ(n 2 ), so even with a fast indexing structure the worst-case running time of the original dbscan algorithm is Ω(n 2 ). (Apart from this, the worst-case query time of R-trees and other standard indexing structures is not logarithmic even if we disregard to time to report points.) In most practical instances the dbscan algorithm is much faster than quadratic. The reason is that ε is typically small so that the sets N ε (p, D) do not contain many points and the range queries can be answered quickly. However, the fact that the algorithm always explicitly reports the sets N ε (p, D) makes the running time sensitive to the choice of ε and the density of the point set D. For example, suppose we have a disk-shaped cluster with a Gaussian distribution around the disk center. Then a suitable value of ε will lead to large sets N ε (p, D) for points p near the center of the cluster.
Chen et al. [8] overcame the quadratic bottleneck of the standard approach, and designed an algorithm 1 with O(n 2− 2 d+2 polylog n) worst-case running time. Note that for d = 2 the running time of the exact algorithm is O(n 1.5 polylog n). They also present an approximate algorithm that is more practical. Chen et al. remark that their exact algorithm is mainly of theoretical interest. The natural question is then whether or not it is possible to to compute the dbscan clustering in subquadratic time in the worst case, irrespective of the value of ε, with a simple and practical algorithm?
Although dbscan is used extensively and performs well in many situations, it has its drawbacks. One is that it produces a flat (non-hierarchial) clustering which heavily depends on the choice of the scale parameter ε. Ankerst et al. [3] therefore introduced optics, which can be seen as a hierarchical version of dbscan. Recently Campello et al. [7] proposed an improved density-based hierarchical clustering method-similar to optics but cleaner-together with a cluster-stability measure that can be used to automatically extract relevant clusters. The new method, called hdbscan, only needs the parameter MinPts, which is much easier to choose than ε. (Campello et al. used MinPts=4 in all their experiments.) While hdbscan is very powerful, the algorithm to compute the hdbscan hierarchy runs in quadratic time; not only in the worst-case, but actually also in the best-case. There have been only few papers dealing with speeding up hdbscan or its predecessor optics. A notable recent exception is Poptics [19], a parallel algorithm that computes a similar (though not the same) hierarchy as optics. We do not know of any algorithm that computes the hdbscan or optics hierarchy in subquadratic time. Thus the second question we study is: is it possible to compute the hdbscan hierarchy in subquadratic time?
Our results. We present an O(n log n) algorithm to compute the dbscan clustering for a set D of n points in the plane, irrespective of the setting of the parameter ε used to define the dbscan clustering. (Here, and in our other results, we assume that the parameter MinPts is a fixed constant. As mentioned this is the case in practice, where one typically uses MinPts = 4.) We remark that our algorithm is not only fast in theory, but a slightly simplified version is also competitive in practice and much less sensitive to the choice of ε than the original dbscan algorithm. Some basic experimental results are provided in Section 6.
We also present a new algorithm for planar hdbscan: we show how to compute the hdbscan hierarchy in R 2 in O(n log n) expected time, thus obtaining the first subquadratic algorithm for the problem.
Finally, we provide a slightly improved version of the approximate dbscan clustering algorithm by Chen et al. [8] and by Gan and Tao [12] (their results are discussed in more detail below). Specifically we improve the dependency on the approximation parameter δ. We then extend the concept of an approximate dbscan clustering as defined to the hierarchical version. We thus obtain δ-approximate hdbscan, an approximate version of the hdbscan hierarchy of Campello et al. [7], where the parameter δ specifies the accuracy of the approximation. (Intuitively, a δ-approximate hdbscan hierarchy has the same clusters as the standard hdbscan hierarchy at any level ε, except that ε Figure 1: A neighborhood graph (with MinPts = 4 and ε as indicated). Solid disks are core points, open circles are border points, and crosses are noise. Edges between core points are solid, other edges are dotted. The solid disks and edges form the core graph.
clusters at distance (1 − δ) · ε from each other may be merged. See Section 5 for precise definition.) We show that a δ-approximate hdbscan hierarchy in R d can be computed in O((n/δ (d−1)/2 ) log n) time.
Further related work. This work should be viewed as the journal publication of the (so far unpublished) masters thesis of the second author [14], which contained the results on dbscan, extended with results on hdbscan. In the meantime, Gan and Tao [12] published a paper in which they extend the work from the masters thesis to R d , resulting in an algorithm for dbscan with a running time of O(n 2− 2 d/2 +1 +γ ); we briefly comment on how this is done at the end of Section 3. Gan and Tao also prove that computing the dbscan clustering in R d for d 3 is at least as hard as the so-called unit-spherical emptiness problem, which is believed to require Ω(n 4/3 ) time [10]. Finally, Gan and Tao show that a δ-approximate dbscan clustering can be computed in O(n/δ d−1 ) expected time, using a modified version of the exact algorithm. Their approximate clustering is the same as the approximate clustering defined by Chen et al. [8], who already showed how to compute it in O(n log n + n/δ d−1 ) time deterministically. (Gan and Tao were unaware of the paper by Chen et al..) As we show in Section 5 our algorithm can also be used to obtain a deterministic algorithm with O(n log n + n/δ d/3 ) running time.
2 Preliminaries on dbscan and dbscan * Let D be a set of points in R d . dbscan distinguishes three types of points: core points (points in the "interior" of a cluster), border points (points on the boundary of a cluster), and noise (points not in any cluster). The distinction is based on two global parameters, ε and MinPts. Define N ε (p, D) := {q ∈ D : |pq| ε} to be the neighborhood of a point p, where |pq| denotes the (Euclidean) distance between p and q; the point p itself is included in N ε (p, D). A point p ∈ D is a core point if |N ε (p, D)| MinPts, and a non-core point q in the neighborhood of a core point is a border point. We denote the set of core points by D core , and the set of border points by D border . The remaining points are noise. In dbscan * [7] border points are not part of a cluster but are considered noise.
Ester et al. [11] define the dbscan clusters based on the concept of density-reachability (a detailed description is given below). Equivalently, we can define the clusters as the connected components of a certain graph. To this end, define the neighborhood graph G(D, E) as the (undirected) graph with node set D and edges connecting pairs of points within distance ε; see Fig. 1. In other words, Note that a point p ∈ D is a core point if and only if its degree in G is at least MinPts − 1, since then its neighborhood contains at least MinPts points (including p itself). Now consider the subgraph G core (D core , E core ) induced by the core points, that is, G core is the graph whose nodes are the core points and whose edges connect two core points when they are within distance ε from each other. We call G core the core graph. The connected components of G core are the clusters in dbscan * . The clusters in dbscan are the same, except that they also contain border points. Formally, a border point q belongs to a cluster C if q has an edge (in G) to a core point p ∈ C. Thus a border point can belong to multiple clusters. The original dbscan algorithm assigns a border point p to the first cluster that finds p (clusters are constructed one by one); we assign border points to the cluster of their nearest core point.

The original definition of the dbscan clustering.
For comparison purposes only, we restate the original definition of the dbscan clustering. Ester et al. [11] define when two points are in the same cluster based on the concept of density-reachability, as explained next. A point q ∈ D is directly density-reachable from a point p ∈ D if q ∈ N ε (p, D) and p is a core point. We denote this by p → q. A core point is always directly density-reachable from itself, since a point p ∈ D is always in its own neighborhood. A point q is density-reachable from a core point p, denoted by p q, if there is a sequence p = r 0 , . . . , r k = q (for some k 0) such that r 0 → r 1 → · · · → r k . Observe that if two points p and q are both core points, then p q if and only if q p. Two points p and q are density-connected if there is a point r such that r p and r q. A cluster is now defined as a subset C ⊆ D such that (i) if a core point p is in C then all points q that are reachable from p are in C, and (ii) any two points in C are density-connected to each other. As observed by Ester et al., a cluster must contain at least one core point, and a cluster is uniquely defined by any of its core points. More precisely, if p ∈ C is a core point then C = {q ∈ D : p q}. Each core point belongs to exactly one dbscan cluster. Under the above definition border points can belong to multiple clusters, however. This is typically undesirable, so the original dbscan algorithm assigns each border point q to only one cluster, namely the cluster from which q is discovered first by their algorithm. This implies that the computed clustering depends on the order in which their algorithm happens to handle the points.

A fast algorithm for dbscan
The original dbscan algorithm reports, while generating and exploring the clusters, for each point p ∈ D all its neighbors. In other words, it spends time on every edge in the neighborhood graph. Our new algorithm avoids this by working with a smaller graph, the box graph G box . Its nodes are disjoint rectangular boxes with a diameter of at most ε that together contain all the points in D, and its edges connect pairs of boxes within distance ε; see Fig. 2. The boxes are generated such that (i) any two points in the same box are in each other's neighborhood, and (ii) the degree of any node in the box graph is O(1). Property (i) allows us to immediately classify all points in a box as width of strips is at most ε/ √ 2 height of boxes is at most ε/ √ 2 distance between bottom edges is more than ε/ √ 2 distance between left strip boundaries is more than ε/ √ 2 core points when it contains at least MinPts points, and property (ii) allows us to quickly retrieve the neighbors of any given point in a box. Next we describe the algorithm, which consists of four easy steps, in detail.
Step 1: Compute the box graph G box . To compute G box , we first construct a collection of vertical strips that together cover all the points. Let p 1 , . . . , p n be the points in D sorted by x-coordinate, with ties broken arbitrarily. The first strip has p 1 on its left boundary. We go through the remaining points from left to right, adding them to the first strip as we go, until we encounter a point p i whose distance to the left strip boundary is more than ε/ √ 2. We then start a new strip with p i on its left boundary, and we add points to that strip until we encounter a point whose distance to the left strip boundary is more than ε/ √ 2, and so on, until we have handled all the points. Constructing the strips takes O(n) time, after sorting the points by x-coordinate.
Within each strip we perform a similar procedure, going over the points within the strip in order of increasing y-coordinate and creating boxes instead of strips. Thus the first box in the strip has the lowest point on its bottom edge, and we keep adding points to this box (enlarging it so that the new point fits, ensuring a tight bounding box) until we encounter a point whose vertical distance to the bottom edge is more than ε/ √ 2. We then start a new box, and so on, until we handled all points in the strip. If the number of points in the j-th strip is n j , then the time needed to handle all the strips is j O(n j log n j ) = O(n log n).
Let m be the number of strips and B j the set of boxes in the j-th strip. We sometimes refer to a set B j as a strip, even though formally B j is a set of boxes. Let B := B 1 ∪ · · · ∪ B m . The nodes of the box graph G box are the boxes in B and there is an We bound the number of neighbors in each of these strips separately.
• The number of neighbors in B j is at most four. Indeed, the boxes in B j can be ordered vertically, and if there are more than two boxes in between b and some other box b ∈ B j , then the vertical distance between b and b is more than ε. • The number of neighbors in B j−1 (and similarly in B j+1 ) is at most five. Suppose for a contradiction that b has six or more neighbors in B j−1 . Let b and b be the lowest and highest of these neighbors, respectively. Then the vertical distance between the top boundary of b and the bottom boundary of b is more than 4ε/ √ 2. Since the height of b is at most ε/ √ 2, this implies that the vertical distance between b and either b or b is more than • The number of neighbors in B j−2 (and similarly in B j+2 ) is at most four. Suppose for a contradiction that b has five or more neighbors in B j−2 . Let b and b be the lowest and highest of these neighbors, respectively. Then the vertical distance between the top boundary of b and the bottom boundary of b is more than 3ε/ √ 2. Since the height of b is at most ε/ √ 2, this implies the vertical distance between b and either b or b is more than The horizontal distance is at least ε/ √ 2 as well, because there is a strip in between. Hence, the total distance between b and either b or b is more than ε, contradicting that both b and b are neighbors of b.
Adding up the maximum number of neighbors in each of the strips gives us a maximum of 22 neighbors in total.
This also gives us an easy way to compute the edge set E box of the box graph, because the edges between boxes in strips B j and B j with |j − j | 2 can be computed in O(|B j | + |B j |) time in total by scanning the boxes in B j and B j in a coordinated manner. The total time to compute all edges of the box graph is thus Adding the time to construct the strips and boxes, we see that Step 1 takes O(n log n) time and we obtain the following lemma.
An alternative for Step 1. An alternative approach is to define the boxes as the non-empty cells in a grid whose cells have height and width ε/ √ 2. If we store the boxes in a hash-table based on the coordinates of their lower left corners, then finding the neighbors of a box b can be done by checking each potential neighbor cell for existence in the hash-table-we do not need to store the box graph explicitly. Creating the boxes (with their corresponding point sets) can be done in O(n) time if the floor function can be computed in O(1) time.
Step 2: Find the core points. The graph G box allows us to determine the core points in a simple and efficient manner. The key observation is that the maximum distance between any two points in the same box is at most ε. Hence, if a box contains more than MinPts points, then all of them are core points. Thus the following simple algorithm suffices to determine the core points. Lemma 3 Given G box , we can find all core points in D in O(n) time.
Proof. The total time spent to handle boxes b with n b MinPts is clearly O(n). The time needed to handle a box b with n b < MinPts is Step 3: Compute the cluster cores. In the previous step, we determined the core points. Next we wish to determine the clusters or, more precisely the cluster cores. The core of a cluster is the set of core points in that cluster. In Step 3 we assign to each core point a cluster-id so that core points in the same cluster have the same cluster-id. Again, this can be done in an efficient manner using G box . To this end, we first remove certain boxes and edges from G box to obtain a reduced box graph G * box . More precisely, we keep only the boxes with at least one core point, and we keep only the edges (b, b ) for which there are core points p ∈ b, p ∈ b with |pp | ε. Because any two core points in a given box b are connected in G core , we have the following lemma.

Lemma 4 The connected components in G *
box correspond one-to-one to the connected components in the core graph G core and, hence, to the dbscan * clusters.
Thus the cluster cores can be computed by computing the connected components in G * box . The latter can be done in O(n) time using DFS [9]. After computing the connected components we give every core point p a cluster-id corresponding to the connected component of the box b that contains p.
To construct G * box , we need to decide for two given boxes b, b whether there are core points p ∈ D(b), p ∈ D(b ) with |pp | ε. If n b < MinPts or n b < MinPts then we simply check all pairs of core points. We can argue as in the proof of Lemma 3 that this takes O(MinPts · n) = O(n) time in total. If n b MinPts and n b MinPts we have to be more careful,since checking all pairs of points can lead to a quadratic running time. For example, if both cells contain n/2 points, then checking all pairs would take Ω(n 2 ) time. When n b MinPts and n b MinPts we therefore compute the Delaunay triangulation of [6], and check whether it has an edge (p, p ) of length at most ε between points p ∈ D(b), p ∈ D(b ). This is sufficient because the pair of points defining the closest distance between D(b) and D(b ) must define an edge in the Delaunay triangulation. This leads to the following lemma.
Lemma 5 Computing the cluster cores can be done in O(n log n) time.
Proof. The most time consuming part of the construction of G * box is to determine for each pair of neighboring boxes in B whether there are core points p ∈ b, p ∈ b with |pp | ε. As mentioned, the total time spent on pairs with n b < MinPts or n b < MinPts is O(n). Let B * be the set of boxes containing at least MinPts points. Then the total time spent on the pairs of boxes from B * is Remark. In practice we can also use a brute-force algorithm when n b MinPts and n b MinPts, because the number of points in boxes with more than MinPts points is typically still not very large. Moreover, if both b and b contain many points, then there are often many pairs of points within distance ε from each other, and we can stop when we find such a pair.
Step 4: Assigning border points to clusters. It remains to decide for non-core points p whether p is a border point or noise. (For dbscan * we can skip Step 4, since in dbscan * border points are considered noise.) If p is a border point, it has to be assigned to the nearest cluster. Again, a brute-force method suffices: for each box b ∈ B and each non-core point p ∈ b, we check all points in b and its neighboring boxes to find p's nearest core point, p . If |pp | ε, then p is a border point in the same cluster as p , otherwise p is noise. We only need to consider boxes b with n b < MinPts-otherwise all points in b are core points-so the argument from the proof of Lemma 3 shows that this takes O(n) time.
Putting it all together. Steps 1 and 3 take O(n log n) time and Steps 2 and 4 take O(n) time. We thus obtain the following theorem.
Theorem 1 Let D be a set of n points in R 2 , and ε and MinPts be given constants. Then we can compute a dbscan clustering on D according to ε and MinPts for the Euclidean metric in O(n log n) time.
Remark: extension to higher dimensions. The algorithm just described can easily be extended to R d for d > 2, as already observed by Gan and Tao [12]. For completeness we describe the extension and the resulting bound. One trivial modification is that in R d we need to use boxes in G box of width at most ε/ √ d along each axis to ensure their diameter is at most ε. The only other difference is in Step 3, where we have to decide for all pairs b, b of neighboring boxes whether there are core points p ∈ D(b) and p ∈ D(b ) with |pp | ε. When n b MinPts and n b MinPts we can no longer use the Delaunay triangulation for this, as it may have quadratic size. Instead we can use a known algorithm for bichromatic closest pair [2], which gives a running time of O(n Preliminaries on hdbscan. Recall that dbscan * is the version of dbscan in which border points are considered noise. The hdbscan hierarchy is a tree structure encoding the clusterings of dbscan * (for a fixed MinPts) that arise as ε increases from ε = 0 to ε = ∞. Initially, when ε = 0, all points are noise. As ε increases, three types of events can happen to the dbscan * clustering: • Type (i): the status of a point changes. In this event, a point changes from being noise to being a core point. The value of ε at which this happens for a point p is called the core distance of p; we denote it by d core (p). • Type (ii): a new cluster starts. This event is triggered by a type (i) event, when a point becoming a core point forms a new (singleton) cluster. • Type (iii): two clusters merge. This event can be triggered by a type (i) event or it can happen when ε = |pq| for core points p, q from different clusters.
Note that all events happen at values of ε such that ε = |pq| for some pair of points p, q ∈ D. Also note that several events may happen simultaneously, not only when two pairwise distances are equal, but also when an event triggers other events. This process can be modeled as a dendrogram: a tree whose leaves correspond to the points in D and whose nodes correspond to clusters arising during the process. This dendrogram, where each node stores the value of ε at which the corresponding cluster was created, is the hdbscan hierarchy. Notice that we can easily extract the dbscan * clustering for any desired value of ε (with respect to the given MinPts) from the dendrogram in linear time. Campello et al. compute the hdbscan hierarchy as follows.
For two points p, q ∈ D, define d mr (p, q) := max (d core (p), d core (q), |pq|) to be the mutual reachability distance of p and q. The mutual reachability graph G mr is defined as the complete graph with node set D in which each edge (p, q) has weight d mr (p, q). Campello et al. observe that hdbscan hierarchy can easily be computed from a minimum spanning tree (mst) on G mr . (Indeed, the cluster-growing process corresponds to the process of computing an mst on G mr using Kruskal's algorithm [9].) Hence, they compute the hdbscan hierarchy as follows.
1. Compute the core distances d core (p) for all points p ∈ D.
2. Compute an mst T of the mutual reachability graph G mr . 3. Convert T into a dendrogram where each internal node stores the value of ε at which the corresponding cluster is formed.
Our planar algorithm. The most time-consuming parts in the algorithm above are Steps 1 and 2; Step 3 takes O(n) time after sorting the edges of T by weight.

For
Step 1 we observe that d core (p) is the distance of point p to its -th nearest neighbor for = MinPts − 1. Hence, to compute all core distances it suffices to compute for each point its k nearest neighbors. This can be done (in any fixed dimension) in O(n log n) time [21]. Since = MinPts − 1 = O(1) this implies that Step 1 takes O(n log n) time.
Step 2 is more difficult to do in subquadratic time. The main problem is that we cannot afford to look at all edges of G mr when computing T . To overcome this problem we need the following generalization of Delaunay triangulations, introduced by Gudmundsson et al. [13]. Recall that a pair of points p, q ∈ D forms an edge in the Delaunay triangulation of D if and only if there is a circle with p and q on its boundary and no points from D in its interior [6]. We say that the pair p, q ∈ D forms a k-th order Delaunay edge, or k-OD edge for short, if and only if there exists a circle with p and q on its boundary and at most k points from D in its interior [13]. (Thus the 0-OD edges are precisely the edges of the Delaunay triangulation.) The k-OD edges are useful for us because of the following lemma.
Lemma 6 Let G mr be the subgraph of G mr that contains only the k-OD edges, where k = max(MinPts − 3, 0). Then an mst of G mr is also an mst of G mr .
Proof. Imagine computing an mst T on G mr using Kruskal's algorithm [9]. This algorithm treats the edges (p, q) of G mr in order of increasing weight, that is, increasing values of d mr (p, q). When it processes (p, q) it checks if p and q are already in the same connected component-in our application this component corresponds to a cluster at the current value of ε-and, if not, merges these components. We will argue that whenever we process an edge (p, q) that is not in G mr , that is, an edge that is not a k-OD edge, then p and q are already in the same connected component. Hence, there is no need to process (p, q), which proves that an mst of G mr is also an mst of G mr .
Let C pq be the circle such that p and q form a diametrical pair of C, and let D(C pq ) ⊂ D be the set of points lying in the interior of C pq . If |D(C pq )| k, then (p, q) is a k-OD edge, so assume |D(C pq )| k + 1. Note that d core (r) < |pq| for all r ∈ D(C pq ). Indeed, since p, q is a diametrical pair of C pq , the distance from r to any other point in C pq (including p and q) is smaller than |pq|. Hence, for ε = |pq| we have |N ε (r, D)| |D(C pq )| + 2 = k + 3 = MinPts. Thus all points r ∈ C pq are core points when we process (p, q). Moreover, for all edges (s, t) with s, t ∈ D(C pq ) ∪ {p, q} we have d mr (s, t) |pq|. Hence, it suffices to prove the following.
Claim: Let C be a circle with two points p, q on its boundary and let D(C) ⊂ D be the set of points from D in the interior of C. Then there is a path from p to q in G mr that uses only points in D(C) ∪ {p, q}.
We prove this claim by induction on |D(C)|. If |D(C)| k then (p, q) is a k-OD edge itself and we are done. Otherwise, pick any point r ∈ D(C). Now shrink C, while keeping p in its boundary, until we obtain a circle C 1 that also has r on its boundary, as shown in Figure 3. By induction, there is a path from p to r in G mr that uses only points in D(C 1 ) ∪ {p, r} ⊂ D(C) ∪ {p, q}. A similar argument shows that there is a path from r to q that uses only points in D(C) ∪ {p, q}. This proves the claim and, hence, the lemma. Gudmundsson et al. showed that the number of k-OD edges is O(n(k + 1)) and that the set of all k-OD edges can be computed in O(n(k + 1) log n) time with a randomized incremental algorithm. Lemma 6 implies that after computing the core distances and the k-OD edges in O(n log n) timerecall that k = max(MinPts − 3, 0) = O(1)-we can compute the mst for G mr by considering only O(n) edges. Thus computing the mst can be done in O(n log n) time [9]. Since the rest of the algorithm takes linear time, we obtain the following theorem.
Theorem 2 Let D be a set of n points in R 2 and MinPts be a given constant. We can compute the hdbscan hierarchy on D for the Euclidean metric with a randomized algorithm in O(n log n) expected time.

Approximate dbscan * and hdbscan
The approach from the previous section for computing the hdbscan hierarchy will not give a subquadratic bound in higher dimensions, since the number of Delaunay edges can be quadratic. The running time of the algorithm from Section 3 to compute a single dbscan clustering is subquadratic in any fixed dimension, but the running time quickly deteriorates as the dimension increases. In this section we introduce an approximate version of dbscan * and hdbscan, both of which can be computed in linear time. An approximation for dbscan * that runs in expected linear time was already provided by Chen et al. [8] Gan and Tao [12], however our approach runs has a slightly better dependency on the approximation factor δ. To the best of our knowledge this is the first linear time approximation algorithm for hdbscan.

Approximate dbscan *
First we define what exactly is an approximate dbscan * clustering. Our definition of approximate dbscan * is essentially the same as the definitions by Chen et al. [8] and Gan and Tao [12]. The main difference is that we base our definition on dbscan * instead of dbscan, which avoids some technical difficulties in the definition.
Let MinPts be a fixed constant. Let C ε (D) denote the set of clusters in the dbscan * clustering for a given value of ε. We call a clustering C 1 a refinement of a clustering C 2 , denoted by C 1 ≺ C 2 , when for every cluster C 1 ∈ C 1 there is a cluster C 2 ∈ C 2 with C 1 ⊆ C 2 . Recall that, as ε increases, the dbscan * clusters merge or expand and new singleton clusters may appear, but clusters do not shrink or disappear. Hence, if ε < ε then 2 C ε (D) ≺ C ε (D). An approximate dbscan * clustering is now defined as follows.
Definition 1 A δ-approximate dbscan * clustering of a data set D, for given parameters ε and MinPts, and a given error δ > 0, is now defined as a clustering C * of D into clusters and noise such that Thus if we choose δ sufficiently small, then a δ-approximate dbscan * clustering is very similar to the exact dbscan * clustering for the given parameter values.
The algorithm. As mentioned in the introduction, both Chen et al. [8] and Gan and Tao [12] already presented algorithms for this. We obtain a slightly better dependency on δ than Gan and Tao [12] by plugging in a better algorithm for approximate bichromatic closest pair.
Recall that the bottleneck in computing a dbscan * clustering lies in checking, for pairs b, b of neighboring boxes, whether there is a pair (p, p ) ∈ D(b) × D(b ) with |pp | ε. We can perform this check approximately by computing an approximate bichromatic closest pair (p, p ) ∈ D(b) × D(b ) such that |pp | (1 + α) · dist(D(b), D(b )) for α = δ/(1 − δ), where dist(D(b), D(b )) denotes the distance between the points of the true closest pair. This can be done in O((1/α) d/3 (n b + n b )) = O((1/δ) d/3 (n b + n b )) time [4]. We add the edge (b, b ) to the box graph G box when |pp | ε. This way we can obtain the following result.
Theorem 3 Let D be a set of n points in R d , and let ε and MinPts be given constants. Then, for any given δ > 0, we can compute a δ-approximate dbscan * clustering on D with respect to ε and MinPts for the Euclidean metric in O(n log n + (1/δ) d/3 n) time.
Proof. Let C be the clustering computed using the approximate bichromatic closest pairs. When dist (D(b), D(b ) ε, then the reported approximate bichromatic closest pair (p, p ) has |pp | ε, so the set of edges added to the box graph is a subset of the edge set of the actual box graph at the given value of ε. Hence, C ≺ C ε (D). On the other hand, when dist(D(b), D(b )) (1 − δ)ε then the approximate closest pair (p, p ) has |pp | (1 + α)(1 − δ)ε = ε. Hence, we are guaranteed to add all edges of the box graph for (1 − δ)ε and so C (1−δ)ε (D) ≺ C. Thus C is a δ-approximate dbscan * clustering.
Recall that the running time of our algorithm is O(n log n), plus the time needed to compute the edges of the box graph. In the approximate version the latter step takes time Approximate hdbscan. Our definition of an approximate hdbscan hierarchy is based on the definition of δ-approximate dbscan * clusterings: we say that a hierarchy is a δ-approximate hdbscan hierarchy if, for any value of ε, the clustering extracted from the hierarchy is a δ-approximate dbscan * clustering for that value of ε. Next we show how to compute a δ-approximate hdbscan hierarchy in O(n log n) time, in any fixed dimension.
As in Section 4 we follow the algorithm by Campello et al. [7], and we speed up Step 2 of the algorithm by computing an mst on a subgraph of the mutual reachability graph G mr rather than on the whole graph. (Steps 1 and 3 can still be done in O(n log n) and O(n) time, respectively.) The difference with the exact algorithm of Section 4 is that we will select the edges of the subgraph in a different manner, using ideas from so-called θ-graphs [18].
Let p ∈ D be a point. We partition R d into simplicial cones with apex p and whose angular diameter is θ, where θ will be specified later. (The angular diameter of a cone c with apex p is the maximum angle between any two vectors emanating from p and inside c.) Let Γ p be the resulting collection of cones and consider a cone c ∈ Γ p . Let D(c) ⊆ D denote the set of points inside c. (If a point lies on the boundaries of several cones we can assign it to one of these cones arbitrarily.) Pick a half-line c with endpoint p that lies inside c. A θ-graph would now be obtained by projecting all points from D(c) orthogonally onto c , and adding an edge from p to the point closest to p in this projection, with ties broken arbitrarily. We do the same, except that we add edges to the k closest points for k := 2 · MinPts − 3. If c contains fewer than k points, we simply connect p to all points in D(c). Doing this for all the cones c ∈ Γ p gives us a set E p of O(k/θ) = O(1/θ) edges for point p. Let E(θ) := p∈D E p . The set E(θ) can be computed by making a straightforward adaptation to the algorithm to compute a θ-graph in R d [18,Chapter 5], leading to the following result.
The set E(θ), where θ is chosen such that cos θ 1 − δ, defines the subgraph G mr (δ) on which we compute the mst in Step 2. Since cos θ > 1 − θ 2 /2, we have cos θ 1 − δ when θ := √ 2δ. Next we show that an mst on G mr (δ) defines a δ-approximate hdbscan clustering. Lemma 8 Let T be an mst of G mr (δ) and let ε > 0. Let C(T , ε) be the clustering induced by T . Then C is a δ-approximate dbscan * clustering for the given ε.
Proof. For a weighted graph G and threshold weight τ , let G[τ ] denote the subgraph obtained by removing all edges of weight greater than τ . In order to show that C(T , ε) ≺ C ε (D) we must show that any connected component of T [ε] is contained in a connected component of G mr [ε]. Since T is a subgraph of G mr this is obviously the case.
Next we prove that C (1−δ)ε (D) ≺ C(T , ε). For this we must prove that any connected component . Since T is an mst of G mr (δ), the connected components of T [ε] are the same as the connected components of G mr (δ) [ε]. It thus suffices to show the following: for any edge (p, q) ∈ G mr [(1 − δ)ε], there is a path from p to q in G mr (δ)[ε]. We show this by induction on |pq|, similarly to the way in which it is shown that a θ-graph has a small dilation.
Let (p, q) be an edge in G mr [(1 − δ)ε]. Consider the set Γ p of cones with apex p that was used to define the edge set E p , and let c ∈ Γ p be the cone containing q. Recall that we added an edge from p to the k points in c that are closest to p when projected onto the half-line c , where k := 2 · MinPts − 3. Hence, when q is one of these k closest points we are done. Otherwise, let r ∈ D(c) be the (MinPts − 1)-th closest point.
In the base case of our inductive proof, where (p, q) is the shortest edge in G mr [(1 − δ)ε], this cannot occur. Thus q must be one of the k closest points in the cone c, and we have an edge between p and q in G mr (δ)[ε] by construction.
If we are not in the base case, then we have a path from r to q in G mr (δ)[ε] by the induction hypothesis. Moreover, (p, r) is an edge in G mr (δ) by construction. Since |pr| ε by part (ii) of the claim, we have a path from p to q in G mr (δ) [ε].
It remains to prove the claim. For this we use the following fact [18,Lemma 4.1.4], which is also used to prove that a θ-graph has small dilation.
Fact: Let s, t be any two points in a cone c ∈ Γ p such that, when projected onto the half-line c , the distance from p to s is smaller than the distance from p to t. Then |ps| |pt|/ cos θ and |st| < |pt|.
Part (iii) of the claim immediately follows from this fact by taking s := r and t := q. Part (ii) follows again by taking s := r and t := q, using that |pq| (1 − δ)ε and that we have chosen δ such that cos θ = 1 − δ. For part (i) we must prove that there are at least MinPts − 1 points within distance (1 − δ)ε from r. Recall that r is the (MinPts − 1)-th closest point to p in the cone c, measured in the projection onto the half-line c . Let r 1 , . . . , r k be the k closest points; thus r = r i for i = MinPts − 1. We distinguish two cases: |pr| (1 − δ)ε and |pr| > (1 − δ)ε. See also Fig. 4.
In the former case we can conclude that |r i r| (1 − δ)ε for all 1 i MinPts − 2 by setting s := r i and t := r and using |pr| (1 − δ)ε. Thus, including the point p, we know that r has at least MinPts − 1 points within distance (1 − δ)ε.
Since we can assume that θ is small enough to ensure 2 sin θ < cos 2 θ, we conclude that, indeed, |rr i | (1 − δ)ε. This finishes the proof for part (i) of the claim and hence, of the lemma.
Combining the previous two lemmas we obtain the following theorem.
Theorem 4 Let D be a set of n points in R d , and let ε and MinPts be given constants. Then, for any given δ > 0, we can compute a δ-approximate hdbscan clustering on D with respect to ε and MinPts for the Euclidean metric in O((n/δ (d−1)/2 ) log d−1 n) time.

Experimental evaluation
In this section we experimentally investigate the efficiency of our new algorithm and compare it to the original algorithm. The only goal of these experiments is to serve as a proof of concept to illustrate that indeed for very basic point distributions the original algorithm has a bad running time, whereas the new algorithm performs much better. We first describe some implementation details and we discuss our implementation of the original algorithm. We then describe the data sets and parameters for the tests. Finally we present the results and conclusions. Implementation details. We implemented two versions of our new algorithm: the strip-based approach for Step 1 and the grid-based approach. In Step 3, where we have to decide for all pairs b, b of neighboring boxes whether there are core points p ∈ D(b) and p ∈ D(b ) with |pp | ε, we use the following randomized brute-force approach. (This is instead of the theoretically better Delaunay triangulations or spherical emptiness queries.) Without loss of generality assume n b n b . For each core point p ∈ D(b) we first test if dist(p, b ) ε. If not, no point of D(b ) can be within distance ε of p. If so, we test each point p ∈ D(b ) to see if |pp | ε. If during this procedure we find two core points within distance ε from each other, we can stop. The randomization is obtained by considering the points in each box in random order; it ensures that if there are many pairs within distance ε, we expect to find such a pair early.
The original dbscan algorithm performs a spherical range query for each point p ∈ D to find all q ∈ D with |pq| ε. To this end an indexing structure such as an R-tree is typically used. In our implementation we use the box-graph to answer the queries. Note that an R-tree also groups the points into boxes at the leaf level; the tree structure is then used find the leaf boxes intersecting the query range, after which the points inside these boxes are tested. The boxes in our box-graph can be seen as being optimized for the radius ε of the query range, and so the box-graph should be at least as efficient as a general-purpose R-tree.
Experimental set-up. We ran the algorithms on several synthetic data sets in 2D and 4D, each consisting of four clusters. (For other numbers of clusters the results are similar.) The clusters either have a uniform or Gaussian distribution, and their centers are placed roughly 700 units apart in a hypercube with edge length 1,000 units. Uniform clusters are generated within a ball of radius 300 around the cluster center, Gaussian clusters are generated with a standard deviation of 100. For several data sets we added 5% noise to the input, uniformly inside a slightly expanded bounding box around the clusters.
Parameters. We analyse the efficiency of the algorithms with respect to two parameters: the input size and the density within the clusters. As a measure of the density we use n(r/ε) d , where r is the cluster radius; thus, for the uniform distribution, the density represents the expected number of points within distance ε from a core point, and also the expected number of points in the boxes of the box-graph within the clusters.
Measurements. We compare the algorithms in two different ways: we measure the actual execution times, and the number of pairs of points for which a distance computation is done. The latter is the main operation for finding the clusters in both the new and the original algorithm, so it provides a good implementation-independent measure. For the original algorithm we also count the sum of neighborhood sizes of all points. Following earlier work, we call this the number of seeds. This is a lower bound for the number of operations needed in the original algorithm, independent of the indexing structure used to find the neighborhoods.
Result for fixed input size. In these experiments we fix the input size and run the algorithms with different values of ε. In 2D we ran the algorithms on a data set of each type-uniform or Gaussian and with or without noise-in which the clusters contain 500,000 points each. In 4D we use the same types of data sets, but with 200,000 points per cluster. The results are shown in Fig. 5.
Result for fixed density. In our second set of experiments we use data sets of different sizes, where for each data set we pick ε such that the density (as defined above) remains constant. For each type of data we generated data sets ranging from 20,000 to 500,000 points per cluster in 2D, and from 20,000 to 300,000 points per cluster for 4D. We ran these experiments for two different values of ε: one that is roughly the smallest value needed to find the four clusters, and one that is roughly the highest value for which the four clusters do not start to merge. The results are given in Fig. 6.
Discussion. The running times in Fig. 5 (top row) show a lot of fluctuation, while the results on the number of distance computations are very stable. We suspect the fluctuation in running time is related to memory issues, or to other processes claiming resources. Hence, most of our conclusions will rely on the number of distance calculations and the sum of neighborhood-sizes.
The results from Fig. 5 clearly show the different dependency on density between the two algorithms. For the original algorithm the number of distance calculations (shown in the second row) grows linearly with the density. The sum of the neighborhood sizes (seeds) is slightly over 50% of the number of distance computations, so the linear dependency is inherent in the original algorithm, which reports the complete neighborhood of all points. The new algorithm, on the other hand, is insensitive to the density. Note that the bottom row in Fig. 5 shows that there is a fairly large range of values of ε at which the correct clusters are found. (The smallest range occurs for Gaussian clusters with noise, and even there the four clusters are still found for ε 2 roughly between 4 and 20.) The original algorithm can compete with the new algorithm only when ε is chosen very near the lower end of the correct range of ε; for a "safe" value of ε in the middle of the range, our algorithm is significantly faster. This is important, as determining the right value of ε is hard and may require running the algorithm several times with different values of ε. Moreover, different clusters may have different densities. Choosing a value for ε that is near the low end of the range  Figure 6: Results of running the algorithms on data-sets of varying sizes, but with a fixed density. The first and third rows are from experiments with a low density and the second and fourth rows with a high density. The x-axis denotes the total number of points in the point set and the y-axis the number of distance calculations. The new method with a strip-based or grid-based construction is denoted by new st and new gr respectively. Note that in the high-density experiments (second and fourth row), the lines for the two new methods almost overlap. However, in the low-density setting, the line for the old method and strip-construction (new st) overlap.
for some clusters may then either fail to find some other clusters or it may lead to an explosion in running time for the other clusters. Fig. 6 shows that the strip-based approach tends to make fewer comparison than the grid-based approach, especially in the low-density setting shown in the upper row in the figure. (In Fig. 5 the low-density setting is in the far left in each graph, and therefore less visible.) This is caused by two things: the boxes are created in a data-driven way in the strip-based approach, and the boxes are smaller (being bounding boxes of the points in it). The former means that points within distance ε end up in the same box more often (in which case they are not compared), while the latter means that some pairs of bounding boxes can be at distance more than ε, while the "corresponding" grid cells have distance just below ε. Except for the low-density setting, the actual computation time (see the top row in Fig. 5) of the grid-based approach is better in 2D (even though the number of comparisons is not), due to the smaller constant factors in the approach.

Concluding remarks
We presented a new algorithm for dbscan in R 2 , which runs in O(n log n) time in the worst case. We also presented an O(n log n) algorithm for hdbscan in R 2 -this is the first subquadratic algorithm for this problem-and we presented near-linear algorithms for approximate hdbscan. It will be interesting to do a more thorough experimental evaluation of our new algorithms. Specifically to compare the approach to other implementations that are current available. It would also be interesting to implement the approximation algorithms and compare them to the exact ones. From the theoretical side, a main open problem is to see if we can compute the exact hdbscan hierarchy in subquadratic time in dimensions d 3.