MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / unionBySize

Method unionBySize

KruskalsAlgorithm.java:51–67  ·  view source on GitHub ↗
(int node1, int node2)

Source from the content-addressed store, hash-verified

49 return parent[node];
50 }
51 public boolean unionBySize(int node1, int node2){
52 //1. find the root parent
53 int rootParent1 = findRootParent(node1);
54 int rootParent2 = findRootParent(node2);
55 if(rootParent1==rootParent2){
56 return false;
57 }
58 // 2, union of components
59 if(size[rootParent1]<size[rootParent2]){
60 parent[rootParent1] = rootParent2;
61 size[rootParent2] += size[rootParent1];
62 }else {
63 parent[rootParent2] = rootParent1;
64 size[rootParent1] += size[rootParent2];
65 }
66 return true;
67 }
68}

Callers 1

spanningTreeMethod · 0.95

Calls 1

findRootParentMethod · 0.95

Tested by

no test coverage detected