Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // A class to represent a subset for union-find
- class subset
- {
- int parent, rank;
- };
- int find(subset subsets[], int i)
- {
- // find root and make root as parent of i (path compression)
- if (subsets[i].parent != i)
- subsets[i].parent = find(subsets, subsets[i].parent);
- return subsets[i].parent;
- }
- void Union(subset subsets[], int x, int y)
- {
- int xroot = find(subsets, x);
- int yroot = find(subsets, y);
- // Attach smaller rank tree under root of high rank tree
- // (Union by Rank)
- if (subsets[xroot].rank < subsets[yroot].rank)
- subsets[xroot].parent = yroot;
- else if (subsets[xroot].rank > subsets[yroot].rank)
- subsets[yroot].parent = xroot;
- // If ranks are same, then make one as root and increment
- // its rank by one
- else
- {
- subsets[yroot].parent = xroot;
- subsets[xroot].rank++;
- }
- }
- public Vector<PathSegment> minSpanningTree()
- throws GraphException{
- Edge result[] = new Edge[vertices.size()];
- int e = 0;
- int i = 0; // An index variable, used for sorted edges
- Vector<Edge> newEdgeList = (Vector<Edge>) edges.clone();
- Collections.sort(newEdgeList);
- subset subsets[] = new subset[vertices.size()];
- for(i=0; i<vertices.size(); i++)
- subsets[i]=new subset();
- for (int v = 0; v < vertices.size(); ++v)
- {
- subsets[v].parent = v;
- subsets[v].rank = 0;
- }
- // loop over edges
- for (Edge e1 : newEdgeList) {
- // if pair of vertices in each edge don't share a disjoint set
- // union their path segments sets and repeat
- }
- return null;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement