# Density-based clustering algorithm: DBSCAN

DBSCAN (Density-Based Spatial Clustering of Applications with Noise) is a clustering algorithm used to partition a dataset into meaningful clusters without needing much prior knowledge about the data. It does not have a strong requirement for convex shapes of clusters of points like k-means do. It is a density-based clustering algorithm that identifies data points tightly packed together and separated from other clusters by low-density regions. This makes it suitable for identifying clusters in datasets with varying densities and nonconvex shapes.

## How does DBSCAN work?

The DBSCAN algorithm works by first defining two parameters.

- Epsilon (ε): This is the maximum distance between two data points that are considered to be in the same cluster.
- MinPts: This is the minimum number of data points that must be in a cluster for it to be considered a valid cluster.

These two parameters essentially define the density.

The algorithm starts with an arbitrary data point, * p*, and looks for all other points within the Epsilon distance of the point. If there are at least a MinPts number of points within that distance, then all of the data points in the region are considered to be in the same cluster, and

*is considered a*

**p***. If there are not at least a MinPts number of points within the Epsilon distance of p, then p is considered a non-core point.*

**core point**The algorithm then moves on to another point and repeats the process of assigning cluster IDs to points that have not yet been explored. Connected density regions of core points form a cluster. A non-core point that does not fall under the epsilon region of a core point is considered an * outlier*.

## Pseudocode for DBSCAN

```
Input:
A dataset D, Epsilon (ε), MinPts
Output:
A set of clusters C
1. Set C to an empty list
2. For each point p in D NOT marked as visited:
2.1 Mark p as visited
2.2 Find all points, points(p), within Epsilon distance from p.
2.3 If |points(p)|>=MinPts
2.3.1 Create a new cluster s, and add p to s
2.3.2 for each point q in points(p)
2.3.2.1 if q is not marked as visited
2.3.2.1.1 mark q as visited
2.3.2.1.2 if |points(q)|>=MinPts then s=union(s, points(q))
2.3.2.2 if q does not yet have a cluster label, add q to s
2.3.3 add s to C
2.4 else mark p as outlier
3. Return C
```

## Advantages of DBSCAN

Some advantages of the DBSCAN algorithm are provided below.

- It can handle datasets with varying densities.
- It can identify clusters of arbitrary shapes.
- It is computationally efficient.

## Disadvantages of DBSCAN

Some disadvantages of the DBSCAN algorithm are provided below.

- It is sensitive to the choice of parameters.
- It is not suitable for high-dimensional data.
- It is difficult to interpret the clusters it produces.

## The time complexity of the DBSCAN algorithm

The time complexity of DBSCAN is O(n^2), where n is the number of data points. This is because the algorithm needs to iterate through all of the data points, and for each data point, it needs to find all of the other data points within the Epsilon distance. As a result, the algorithm has to examine all pairs of data points, resulting in a time complexity of O(n^2).

By using an indexing data structure such as a k-d tree or a ball tree, the time complexity can be reduced to O(nlog(n)). This is because an indexing data structure allows for faster and more efficient searches, reducing the time needed to find the points within the Epsilon distance.