1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* DataStructures for PHP |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/SiroDiaz/DataStructures |
6
|
|
|
* @copyright Copyright (c) 2017 Siro Díaz Palazón |
7
|
|
|
* @license https://github.com/SiroDiaz/DataStructures/blob/master/README.md (MIT License) |
8
|
|
|
*/ |
9
|
|
|
namespace DataStructures\Sets; |
10
|
|
|
|
11
|
|
|
use DataStructures\Trees\Nodes\DisjointNode; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* DisjointSet. |
15
|
|
|
* |
16
|
|
|
* The DisjointSet class represents a disjoint set. |
17
|
|
|
* Operations take in worse case a O(n) except makeSet that takes |
18
|
|
|
* constant time, O(1). |
19
|
|
|
* Basic operations are makeSet, find and union. |
20
|
|
|
* |
21
|
|
|
* @author Siro Diaz Palazon <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class DisjointSet { |
24
|
|
|
private $subsets; |
25
|
|
|
|
26
|
|
|
public function __construct() { |
27
|
|
|
$this->subsets = []; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Creates a new set/tree with zero children and parents. |
32
|
|
|
* Its parent points to itself and the rank is 0 when new |
33
|
|
|
* set is created. |
34
|
|
|
* |
35
|
|
|
* @param mixed $data the data to store. |
36
|
|
|
* @return DataStructures\Trees\Nodes\DisjointNode the node created. |
37
|
|
|
*/ |
38
|
|
|
public function makeSet($data) : DisjointNode { |
39
|
|
|
$newSet = new DisjointNode($data); |
40
|
|
|
$this->subsets[] = $newSet; |
41
|
|
|
|
42
|
|
|
return $newSet; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Returns the representative node (the root of $node in the tree) and |
47
|
|
|
* also applies path compression. |
48
|
|
|
* |
49
|
|
|
* @param DataStructures\Trees\Nodes\DisjointNode $node the node from |
|
|
|
|
50
|
|
|
* where start to search the root. |
51
|
|
|
* @return DataStructures\Trees\Nodes\DisjointNode the parent node. |
52
|
|
|
*/ |
53
|
|
|
public function find($vertex) { |
54
|
|
|
if($this->subsets[$vertex]->parent === null || $this->subsets[$vertex]->parent < 0) { |
55
|
|
|
return $vertex; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$this->subsets[$vertex]->parent = $this->find($this->subsets[$vertex]->parent); |
59
|
|
|
return $this->subsets[$vertex]->parent; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Performs the union of two sets (or trees). First looks for |
64
|
|
|
* the root of $x and $y set. Then, if both are in the same tree |
65
|
|
|
* finalize the method. Else, depending of the rank, will join a |
66
|
|
|
* set to other set (The set with lower rank will be append to higher |
67
|
|
|
* one). If both have the same rank it doesn't matter what tree |
68
|
|
|
* is joined to the other tree but the rank will increase. |
69
|
|
|
* |
70
|
|
|
* @param DataStructures\Trees\Nodes\DisjointNode $x The set. |
|
|
|
|
71
|
|
|
* @param DataStructures\Trees\Nodes\DisjointNode $y The other set. |
|
|
|
|
72
|
|
|
*/ |
73
|
|
|
public function union($vertex1, $vertex2) { |
74
|
|
|
if($this->subsets[$vertex2]->parent < $this->subsets[$vertex1]->parent) { |
75
|
|
|
$this->subsets[$vertex1]->parent = $vertex2; |
76
|
|
|
} else { |
77
|
|
|
if($this->subsets[$vertex1]->parent === $this->subsets[$vertex2]->parent) { |
78
|
|
|
if($this->subsets[$vertex1]->parent === null) { |
79
|
|
|
$this->subsets[$vertex1]->parent = -1; |
80
|
|
|
} else { |
81
|
|
|
$this->subsets[$vertex1]->parent--; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$this->subsets[$vertex2]->parent = $vertex1; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.