Crowded Corners of the Map

Classic MLHard+30 XP+30

Crowded Corners of the Map

A delivery company has plotted its drop-off points on a map and wants to find the crowded areas, without saying in advance how many there are. That is DBSCAN's job. It asks the same question at every point: how many other points lie within ε\varepsilonε of me?

Task: write dbscan_roles(points, eps, min_pts), where points is a list of (x, y) pairs. Return a tuple (roles, clusters):

  • roles: one entry per point, in the same order as points, each "core", "border" or "noise";
  • clusters: the number of clusters DBSCAN finds.

Use these rules:

  • A point's neighbours are the other points at a straight-line distance of at most eps. A distance of exactly eps counts.
  • A point is core if it has at least min_pts neighbours. The point itself is not one of its own neighbours.
  • A point that is not core is border if at least one of its neighbours is a core point.
  • Every other point is noise.
  • Clusters grow from core points. Two core points that are neighbours are in the same cluster, and so is every core point reachable from them through a chain of neighbouring core points. A border point joins a cluster but never passes it on: a chain only continues through core points. Noise belongs to no cluster.

scikit-learn's min_samples counts the point itself, so it is always one more than min_pts here. Follow the rules above.

Core, Border, or Noise?

Click any point to reveal what DBSCAN would call it, and why.

CoreBorderNoise

Tap any point above to see its classification.

Examples

points = [(0, 0), (1, 0), (0, 1), (1, 1), (2, 1), (5, 5), (6, 5)]
eps = 1.5
min_pts = 3
returns (['core', 'core', 'core', 'core', 'border', 'noise', 'noise'], 1)
points = [(-2, 0), (-1, 1), (-1, 0), (-1, -1), (0, 0), (1, 1), (1, 0), (1, -1), (2, 0)]
eps = 1
min_pts = 3
returns (['border', 'border', 'core', 'border', 'border', 'border', 'core', 'border', 'border'], 2)

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run