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

Method unionSets

cpp/Graphs/connect_the_dots.cpp:18–34  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

16 }
17
18 bool unionSets(int x, int y) {
19 int repX = findSet(x);
20 int repY = findSet(y);
21 if (repX != repY) {
22 if (size[repX] > size[repY]) {
23 parent[repY] = repX;
24 size[repX] += size[repY];
25 } else {
26 parent[repX] = repY;
27 size[repY] += size[repX];
28 }
29 // Return true if both groups were merged.
30 return true;
31 }
32 // Return false if the points belong to the same group.
33 return false;
34 }
35
36 int findSet(int x) {
37 if (x == parent[x]) {

Callers 1

connectTheDotsFunction · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected