Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. // A class to represent a subset for union-find
  2. class subset
  3. {
  4. int parent, rank;
  5. };
  6.  
  7.  
  8. int find(subset subsets[], int i)
  9. {
  10. // find root and make root as parent of i (path compression)
  11. if (subsets[i].parent != i)
  12. subsets[i].parent = find(subsets, subsets[i].parent);
  13.  
  14. return subsets[i].parent;
  15. }
  16.  
  17.  
  18. void Union(subset subsets[], int x, int y)
  19. {
  20. int xroot = find(subsets, x);
  21. int yroot = find(subsets, y);
  22.  
  23. // Attach smaller rank tree under root of high rank tree
  24. // (Union by Rank)
  25. if (subsets[xroot].rank < subsets[yroot].rank)
  26. subsets[xroot].parent = yroot;
  27. else if (subsets[xroot].rank > subsets[yroot].rank)
  28. subsets[yroot].parent = xroot;
  29.  
  30. // If ranks are same, then make one as root and increment
  31. // its rank by one
  32. else
  33. {
  34. subsets[yroot].parent = xroot;
  35. subsets[xroot].rank++;
  36. }
  37. }
  38.  
  39.  
  40. public Vector<PathSegment> minSpanningTree()
  41. throws GraphException{
  42.  
  43. Edge result[] = new Edge[vertices.size()];
  44. int e = 0;
  45. int i = 0; // An index variable, used for sorted edges
  46.  
  47. Vector<Edge> newEdgeList = (Vector<Edge>) edges.clone();
  48. Collections.sort(newEdgeList);
  49.  
  50. subset subsets[] = new subset[vertices.size()];
  51. for(i=0; i<vertices.size(); i++)
  52. subsets[i]=new subset();
  53.  
  54. for (int v = 0; v < vertices.size(); ++v)
  55. {
  56. subsets[v].parent = v;
  57. subsets[v].rank = 0;
  58. }
  59.  
  60. // loop over edges
  61. for (Edge e1 : newEdgeList) {
  62.  
  63. // if pair of vertices in each edge don't share a disjoint set
  64. // union their path segments sets and repeat
  65. }
  66. return null;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement