-
-
Notifications
You must be signed in to change notification settings - Fork 491
/
Copy pathDisjointSet.php
49 lines (43 loc) · 1.34 KB
/
DisjointSet.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
/*
* Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) in Pull Request: #160
* https://github.com/TheAlgorithms/PHP/pull/160
*
* Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request addressing bugs/corrections to this file.
* Thank you!
*/
namespace DataStructures\DisjointSets;
class DisjointSet
{
/**
* Finds the representative of the set that contains the node.
*/
public function findSet(DisjointSetNode $node): DisjointSetNode
{
if ($node !== $node->parent) {
// Path compression: make the parent point directly to the root
$node->parent = $this->findSet($node->parent);
}
return $node->parent;
}
/**
* Unites the sets that contain x and y.
*/
public function unionSet(DisjointSetNode $nodeX, DisjointSetNode $nodeY): void
{
$rootX = $this->findSet($nodeX);
$rootY = $this->findSet($nodeY);
if ($rootX === $rootY) {
return; // They are already in the same set
}
// Union by rank: attach the smaller tree under the larger tree
if ($rootX->rank > $rootY->rank) {
$rootY->parent = $rootX;
} else {
$rootX->parent = $rootY;
if ($rootX->rank === $rootY->rank) {
$rootY->rank += 1;
}
}
}
}