MCPcopy Create free account
hub / github.com/TheAlgorithms/JavaScript / DisjointSetTree

Class DisjointSetTree

Graphs/KruskalMST.js:10–46  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8}
9
10class DisjointSetTree {
11 // Disjoint Set DataStructure
12 constructor() {
13 // map to from node name to the node object
14 this.map = {}
15 }
16
17 makeSet(x) {
18 // Function to create a new set with x as its member
19 this.map[x] = new DisjointSetTreeNode(x)
20 }
21
22 findSet(x) {
23 // Function to find the set x belongs to (with path-compression)
24 if (this.map[x] !== this.map[x].parent) {
25 this.map[x].parent = this.findSet(this.map[x].parent.key)
26 }
27 return this.map[x].parent
28 }
29
30 union(x, y) {
31 // Function to merge 2 disjoint sets
32 this.link(this.findSet(x), this.findSet(y))
33 }
34
35 link(x, y) {
36 // Helper function for union operation
37 if (x.rank > y.rank) {
38 y.parent = x
39 } else {
40 x.parent = y
41 if (x.rank === y.rank) {
42 y.rank += 1
43 }
44 }
45 }
46}
47
48class GraphWeightedUndirectedAdjacencyList {
49 // Weighted Undirected Graph class

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected