Coding Recipies: Max Spacing for K-order Cluster
Find maximum spacing of a K-clustering of the given Graph (in the given below eg. K=4)
For solving this problem Kruskal Algorithm will be applied , which picks the minimum edges to connect to every vertex and finally in the end generates a MST , but in the process of picking up edges we need to ensure that no cycles are formed , and that we would use the UNION-FIND data structure. Here in this problem we will terminate the process of connecting vertices as soon as out K clusters are formed. Max space between the cluster will be given by the edge that would have actully
decreased the number of clusters to K-1.
Find maximum spacing of a K-clustering of the given Graph (in the given below eg. K=4)
For solving this problem Kruskal Algorithm will be applied , which picks the minimum edges to connect to every vertex and finally in the end generates a MST , but in the process of picking up edges we need to ensure that no cycles are formed , and that we would use the UNION-FIND data structure. Here in this problem we will terminate the process of connecting vertices as soon as out K clusters are formed. Max space between the cluster will be given by the edge that would have actully
decreased the number of clusters to K-1.
public int getMaxSpacing(int clusterCount) { Collections.sort(mEdgeList); UnionFind uf=new UnionFind(mNumVertices); if(clusterCount > uf.getCount()) { try { throw new Exception("Cluster counter is lesser than input"); } catch (Exception e) { System.out.println(e.getMessage()); } } else { for(int i=0;i<mNumVertices;i++) { Edge edge=mEdgeList.get(i); // if parents do not match, consider edge list for MST and , union the two vertex if(!uf.isConnected(edge.src, edge.dest)) { if(uf.getCount()==clusterCount) { uf.printCluster1(); return maxClusterDistance=edge.weight; } int v1 = uf.Find(edge.src); //parent vertex for source int v2 = uf.Find(edge.dest); //parent vertex for destinition uf.Union(v1, v2); } } } return -1; }Read full article from Coding Recipies: Max Spacing for K-order Cluster