DBSCANClusterer { public Set<Set<T>> cluster(double epsilon, int minPoints, boolean includeNoise) { LOG.debug("cluster: epsilon=" + epsilon + ", minPoints=" + minPoints + ", includeNoise=" + includeNoise); Set<Set<T>> clusters = new HashSet<Set<T>>(); Set<T> noise = new HashSet<T>(); this.visited = new boolean[dataSet.size()]; this.clusterList = new ArrayList<Set<T>>(dataSet.size()); for (int i = 0; i < dataSet.size(); i++) this.clusterList.add(null); Set<T> cluster = null; for (int index = 0; index < dataSet.size(); index++) { if (visited[index]) continue; visited[index] = true; Set<Integer> neighbours = this.getNeighbours(index, epsilon); if (neighbours.size() < minPoints - 1) noise.add(objectSet.get(index)); else { cluster = new HashSet<T>(); expandCluster(index, neighbours, cluster, epsilon, minPoints); clusters.add(cluster); } } LOG.debug("Found " + clusters.size() + " clusters"); LOG.debug("Found " + noise.size() + " noise"); if (includeNoise) { for (T object : noise) { Set<T> oneObject = new HashSet<T>(); oneObject.add(object); clusters.add(oneObject); } } return clusters; } DBSCANClusterer(List<T> objectSet, List<double[]> dataSet); Set<Set<T>> cluster(double epsilon, int minPoints, boolean includeNoise);  }
@Test public void testCluster() { List<Integer> objectSet = new ArrayList<Integer>(); List<double[]> dataSet = new ArrayList<double[]>(); objectSet.add(1); objectSet.add(2); objectSet.add(3); objectSet.add(4); objectSet.add(5); objectSet.add(6); dataSet.add(new double[] { 3.8 }); dataSet.add(new double[] { 4.0 }); dataSet.add(new double[] { 4.2 }); dataSet.add(new double[] { 8.0 }); dataSet.add(new double[] { 8.1 }); dataSet.add(new double[] { 12.0 }); DBSCANClusterer<Integer> clusterer = new DBSCANClusterer<Integer>(objectSet, dataSet); Set<Set<Integer>> clusters = clusterer.cluster(0.3, 2, false); assertEquals(2, clusters.size()); for (Set<Integer> cluster : clusters) { if (cluster.contains(1)) { assertTrue(cluster.contains(2)); assertTrue(cluster.contains(3)); } else if (cluster.contains(4)) { assertTrue(cluster.contains(5)); } else { fail("Unknown cluster"); } } }