mirror of
https://github.com/TheAlgorithms/PHP.git
synced 2025-07-20 08:21:17 +02:00
* Added Disjoint Sets Data structure * Moved DisjointSetTest.php to tests/DataStructures * Update DataStructures/DisjointSets/DisjointSet.php Co-authored-by: Brandon Johnson <bbj1979@gmail.com> * Update DataStructures/DisjointSets/DisjointSetNode.php Co-authored-by: Brandon Johnson <bbj1979@gmail.com> * Update DataStructures/DisjointSets/DisjointSetNode.php Co-authored-by: Brandon Johnson <bbj1979@gmail.com> * Update tests/DataStructures/DisjointSetTest.php Co-authored-by: Brandon Johnson <bbj1979@gmail.com> * Update tests/DataStructures/DisjointSetTest.php Co-authored-by: Brandon Johnson <bbj1979@gmail.com> * Update tests/DataStructures/DisjointSetTest.php Co-authored-by: Brandon Johnson <bbj1979@gmail.com> * Considered PHPCS remarks. Unit Testing is now working. * Remove data type mixed. Considered annotations for php7.4. * Remove data type mixed. Considered annotations for php7.4. * updating DIRECTORY.md * Implemented Trie DataStructure * Added Trie to DIRECTORY.md * updating DIRECTORY.md * Implemented AVLTree DataStructure * updating DIRECTORY.md * Implemented AVLTree DataStructure * Implemented SegmentTreeNode.php * Implementing SegmentTree * Implementing SegmentTree with updateTree * Implementing SegmentTree with rangeUpdateTree * Implementing SegmentTree with query and queryTree * Added serializing and deserializing of the SegmentTree * Adding unit tests SegmentTree implementation * Added unit tests for SegmentTree updates and range updates * considering PHPCS for Added unit tests for SegmentTree updates and range updates * Added unit tests for SegmentTree serialization/deserialization and array updates reflections * Added unit tests for SegmentTree Edge Cases * Added unit tests for SegmentTree Exceptions (OutOfBoundsException, InvalidArgumentException) * Added SegmentTree to DIRECTORY.md * Implemented Segment Tree Data Structure * Added some comments to my files in: #160, #162, #163, #166. Implemented Segment Tree Data Structure. * Added some comments to my files in: #160, #162, #163, #166. Implemented Segment Tree Data Structure. * Added comments time complexity for query(), update() and buildTree() --------- Co-authored-by: Brandon Johnson <bbj1979@gmail.com> Co-authored-by: Ramy-Badr-Ahmed <Ramy-Badr-Ahmed@users.noreply.github.com>
96 lines
3.0 KiB
PHP
96 lines
3.0 KiB
PHP
<?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;
|
|
|
|
require_once __DIR__ . '/../../DataStructures/DisjointSets/DisjointSet.php';
|
|
require_once __DIR__ . '/../../DataStructures/DisjointSets/DisjointSetNode.php';
|
|
|
|
use DataStructures\DisjointSets\DisjointSet;
|
|
use DataStructures\DisjointSets\DisjointSetNode;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class DisjointSetTest extends TestCase
|
|
{
|
|
private DisjointSet $ds;
|
|
private array $nodes;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->ds = new DisjointSet();
|
|
$this->nodes = [];
|
|
|
|
// Create 20 nodes
|
|
for ($i = 0; $i < 20; $i++) {
|
|
$this->nodes[$i] = new DisjointSetNode($i);
|
|
}
|
|
|
|
// Perform union operations to form several disjoint sets
|
|
$this->ds->unionSet($this->nodes[0], $this->nodes[1]);
|
|
$this->ds->unionSet($this->nodes[1], $this->nodes[2]);
|
|
|
|
$this->ds->unionSet($this->nodes[3], $this->nodes[4]);
|
|
$this->ds->unionSet($this->nodes[4], $this->nodes[5]);
|
|
|
|
$this->ds->unionSet($this->nodes[6], $this->nodes[7]);
|
|
$this->ds->unionSet($this->nodes[7], $this->nodes[8]);
|
|
|
|
$this->ds->unionSet($this->nodes[9], $this->nodes[10]);
|
|
$this->ds->unionSet($this->nodes[10], $this->nodes[11]);
|
|
|
|
$this->ds->unionSet($this->nodes[12], $this->nodes[13]);
|
|
$this->ds->unionSet($this->nodes[13], $this->nodes[14]);
|
|
|
|
$this->ds->unionSet($this->nodes[15], $this->nodes[16]);
|
|
$this->ds->unionSet($this->nodes[16], $this->nodes[17]);
|
|
|
|
$this->ds->unionSet($this->nodes[18], $this->nodes[19]);
|
|
}
|
|
|
|
public function testFindSet(): void
|
|
{
|
|
// Nodes in the same sets should have the same root
|
|
for ($i = 0; $i < 6; $i++) {
|
|
for ($j = 0; $j < 6; $j++) {
|
|
$setI = $this->ds->findSet($this->nodes[$i]);
|
|
$setJ = $this->ds->findSet($this->nodes[$j]);
|
|
|
|
if ($this->inSameSet($i, $j)) {
|
|
$this->assertSame($setI, $setJ, "Nodes $i and $j should be in the same set");
|
|
} else {
|
|
$this->assertNotSame($setI, $setJ, "Nodes $i and $j should be in different sets");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private function inSameSet(int $i, int $j): bool
|
|
{
|
|
// Define which nodes should be in the same set based on union operations
|
|
$sets = [
|
|
[0, 1, 2], // Set A
|
|
[3, 4, 5], // Set B
|
|
[6, 7, 8], // Set C
|
|
[9, 10, 11], // Set D
|
|
[12, 13, 14], // Set E
|
|
[15, 16, 17], // Set F
|
|
[18, 19] // Set G
|
|
];
|
|
|
|
foreach ($sets as $set) {
|
|
if (in_array($i, $set) && in_array($j, $set)) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|