### Question:
HierarchicalClusterer extends GraphBasedClusteringAlgorithm<T> { @Override public Map<Collection<T>, T> cluster(Collection<Triple<T, T, Double>> similarityGraph) { initialise(similarityGraph); if(numClusters!=null) { for(int i = 0; i < (size - numClusters); i++) { step(); } } else { while(step()) ; } return createClusters(); } HierarchicalClusterer(LinkageMode linkage, int numClusters);  HierarchicalClusterer(LinkageMode linkage, double minSimilarity); Double getIntraClusterDistance(Collection<T> cluster); @Override Map<Collection<T>, T> cluster(Collection<Triple<T, T, Double>> similarityGraph); Map<Collection<T>, T> createClusters();  }### Answer:
@Test public void testCluster() { Collection<Triple<String, String, Double>> data = new LinkedList<>(); data.add(new Triple<>("a", "b", 0.2)); data.add(new Triple<>("a", "c", 1.0)); data.add(new Triple<>("b", "d", 0.8)); HierarchicalClusterer<String> clusterer = new HierarchicalClusterer<>(LinkageMode.Min, 2); Map<Collection<String>, String> clustering = clusterer.cluster(data); Set<String> clu1 = Q.toSet("a", "c"); Set<String> clu2 = Q.toSet("b", "d"); assertTrue(clustering.keySet().contains(clu1)); assertTrue(clustering.keySet().contains(clu2)); assertEquals(2, clustering.keySet().size()); assertTrue(1.0==-clusterer.getIntraClusterDistance(clu1).doubleValue()); assertTrue(0.8==-clusterer.getIntraClusterDistance(clu2).doubleValue()); clusterer = new HierarchicalClusterer<>(LinkageMode.Min, 0.5); clustering = clusterer.cluster(data); assertTrue(clustering.keySet().contains(clu1)); assertTrue(clustering.keySet().contains(clu2)); assertEquals(2, clustering.keySet().size()); assertTrue(1.0==-clusterer.getIntraClusterDistance(clu1).doubleValue()); assertTrue(0.8==-clusterer.getIntraClusterDistance(clu2).doubleValue()); data = new LinkedList<>(); data.add(new Triple<>("a", "b", 0.6)); data.add(new Triple<>("b", "c", 0.6)); data.add(new Triple<>("c", "d", 0.6)); data.add(new Triple<>("a", "c", 0.2)); data.add(new Triple<>("a", "d", 0.0)); data.add(new Triple<>("b", "d", 0.2)); clusterer = new HierarchicalClusterer<>(LinkageMode.Max, 2); clustering = clusterer.cluster(data); clu1 = Q.toSet("a", "b"); clu2 = Q.toSet("c", "d"); assertTrue(clustering.keySet().contains(clu1)); assertTrue(clustering.keySet().contains(clu2)); assertEquals(2, clustering.keySet().size()); assertTrue(0.6==-clusterer.getIntraClusterDistance(clu1).doubleValue()); assertTrue(0.6==-clusterer.getIntraClusterDistance(clu2).doubleValue()); clusterer = new HierarchicalClusterer<>(LinkageMode.Max, 0.5); clustering = clusterer.cluster(data); assertTrue(clustering.keySet().contains(clu1)); assertTrue(clustering.keySet().contains(clu2)); assertEquals(2, clustering.keySet().size()); assertTrue(0.6==-clusterer.getIntraClusterDistance(clu1).doubleValue()); assertTrue(0.6==-clusterer.getIntraClusterDistance(clu2).doubleValue()); }