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

Method union

kotlin/Graphs/MergingCommunities.kt:7–22  ·  view source on GitHub ↗

With comment

Source from the content-addressed store, hash-verified

5
6 // With comment
7 fun union(x: Int, y: Int) {
8 val repX = find(x)
9 val repY = find(y)
10 if (repX != repY) {
11 // If 'repX' represents a larger community, connect
12 // 'repY 's community to it.
13 if (size[repX] > size[repY]) {
14 parent[repY] = repX
15 size[repX] += size[repY]
16 } else {
17 // Otherwise, connect 'repX's community to 'repY'.
18 parent[repX] = repY
19 size[repY] += size[repX]
20 }
21 }
22 }
23
24 fun find(x: Int): Int {
25 if (x == parent[x]) {

Callers 1

connectMethod · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected