Python exercise · about 5 minutes · free
DBSCAN: Clusters From Density
K-means needs k and likes round blobs. DBSCAN finds any shape, and marks lonely points as noise.
DBSCAN groups points that are densely packed. A point is a core point if at least min_pts points (itself included) lie within distance eps. Clusters grow by chaining from core point to core point; border points join a nearby cluster; everything else is noise (label -1). It needs no k, finds odd shapes and detects outliers, but struggles when density varies a lot.
Prices, versions and service names last checked 2026-11-19. They change, so confirm on the official pages before you rely on them.
Your quest: Write
dbscan(points, eps, min_pts) for 1D numbers (distance abs, neighbour if distance <= eps + 1e-9). Return a label per point: clusters numbered from 0 in the order they are discovered (scan points left to right in list order), noise -1.Try it now Runs in your browser. No sign-in needed. Ctrl+Enter to run.
Hint
Precompute neighbours. Loop over unvisited points; if not core mark noise, else start a cluster and expand with a queue, adding the neighbours of every core point.Output appears here.