/** * DFS the tree to find the order of leafs to avoid the cross of lines in * the plot. */ private int dfs(int[][] merge, int index, int[] order, int i) { int n = merge.length + 1; if (merge[index][0] > merge.length) { i = dfs(merge, merge[index][0] - n, order, i); } else { order[i++] = merge[index][0]; } if (merge[index][1] > merge.length) { i = dfs(merge, merge[index][1] - n, order, i); } else { order[i++] = merge[index][1]; } return i; }
/** * Create a dendrogram plot. * @param id the id of the plot. * @param merge an n-1 by 2 matrix of which row i describes the merging of clusters at * step i of the clustering. If an element j in the row is less than n, then * observation j was merged at this stage. If j ≥ n then the merge * was with the cluster formed at the (earlier) stage j-n of the algorithm. * @param height a set of n-1 non-decreasing real values, which are the clustering height, * i.e., the value of the criterion associated with the clustering method * for the particular agglomeration. */ public static PlotCanvas plot(String id, int[][] merge, double[] height) { int n = merge.length + 1; Dendrogram dendrogram = new Dendrogram(merge, height); double[] lowerBound = {-n / 100, 0}; double[] upperBound = {n + n / 100, 1.01 * dendrogram.getHeight()}; PlotCanvas canvas = new PlotCanvas(lowerBound, upperBound, false); canvas.getAxis(0).setGridVisible(false); canvas.getAxis(0).setLabelVisible(false); dendrogram.setID(id); canvas.add(dendrogram); return canvas; } }
/** * Create a dendrogram plot. * @param merge an n-1 by 2 matrix of which row i describes the merging of clusters at * step i of the clustering. If an element j in the row is less than n, then * observation j was merged at this stage. If j ≥ n then the merge * was with the cluster formed at the (earlier) stage j-n of the algorithm. * @param height a set of n-1 non-decreasing real values, which are the clustering height, * i.e., the value of the criterion associated with the clustering method * for the particular agglomeration. */ public static PlotCanvas plot(int[][] merge, double[] height) { int n = merge.length + 1; Dendrogram dendrogram = new Dendrogram(merge, height); double[] lowerBound = {-n / 100, 0}; double[] upperBound = {n + n / 100, 1.01 * dendrogram.getHeight()}; PlotCanvas canvas = new PlotCanvas(lowerBound, upperBound, false); canvas.getAxis(0).setGridVisible(false); canvas.getAxis(0).setLabelVisible(false); canvas.add(dendrogram); return canvas; }
@Override public void paint(Graphics g) { Color c = g.getColor(); g.setColor(getColor()); for (int i = 0; i < p1.length; i++) { g.drawLine(p1[i], p2[i]); } g.setColor(c); }
dfs(merge, n - 2, order, 0);