MCPcopy Create free account
hub / github.com/ByteByteGoHq/coding-interview-patterns / union

Method union

java/Graphs/MergingCommunities.java:16–32  ·  view source on GitHub ↗
(int x, int y)

Source from the content-addressed store, hash-verified

14 }
15
16 public void union(int x, int y) {
17 int repX = find(x);
18 int repY = find(y);
19 if (repX != repY) {
20 // If 'repX' represents a larger community, connect
21 // 'repY 's community to it.
22 if (this.size[repX] > this.size[repY]) {
23 this.parent[repY] = repX;
24 this.size[repX] += this.size[repY];
25 }
26 // Otherwise, connect 'rep_x's community to 'rep_y'.
27 else {
28 this.parent[repX] = repY;
29 this.size[repY] += this.size[repX];
30 }
31 }
32 }
33
34 public int find(int x) {
35 if (x == this.parent[x]) {

Callers 1

connectMethod · 0.45

Calls 1

findMethod · 0.95

Tested by

no test coverage detected