-
-
Notifications
You must be signed in to change notification settings - Fork 513
Implemented Disjoint Set (Union-Find) Data Structure #160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
643dfa2
Added Disjoint Sets Data structure
Ramy-Badr-Ahmed 7cfc859
Moved DisjointSetTest.php to tests/DataStructures
Ramy-Badr-Ahmed b3a8d7a
Update DataStructures/DisjointSets/DisjointSet.php
Ramy-Badr-Ahmed b6ae103
Update DataStructures/DisjointSets/DisjointSetNode.php
Ramy-Badr-Ahmed 70d981a
Update DataStructures/DisjointSets/DisjointSetNode.php
Ramy-Badr-Ahmed dc93e41
Update tests/DataStructures/DisjointSetTest.php
Ramy-Badr-Ahmed 35aa7d0
Update tests/DataStructures/DisjointSetTest.php
Ramy-Badr-Ahmed 0591a6f
Update tests/DataStructures/DisjointSetTest.php
Ramy-Badr-Ahmed b0086fd
Considered PHPCS remarks. Unit Testing is now working.
Ramy-Badr-Ahmed b2d1160
Remove data type mixed. Considered annotations for php7.4.
Ramy-Badr-Ahmed 926126a
Remove data type mixed. Considered annotations for php7.4.
Ramy-Badr-Ahmed 355c5ba
updating DIRECTORY.md
Ramy-Badr-Ahmed 99e489e
Merge branch 'TheAlgorithms:master' into master
Ramy-Badr-Ahmed File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
use DataStructures\DisjointSets\DisjointSetNode; | ||
|
||
class DisjointSet | ||
{ | ||
/** | ||
* Finds the representative of the set that contains the node. | ||
*/ | ||
public function findSet(DisjointSetNode $node): DisjointSetNode | ||
{ | ||
if ($node !== $node->parent) { | ||
$node->parent = $this->findSet($node->parent); // Path compression: make the parent point directly to the root | ||
} | ||
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; | ||
} | ||
} | ||
} | ||
} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace DataStructures\DisjointSets; | ||
|
||
class DisjointSetNode | ||
{ | ||
public mixed $data; # PHP 8.0^ . Otherwise, define with annotations: @var int|string|float|null | ||
public int $rank; | ||
public DisjointSetNode $parent; | ||
|
||
public function __construct(mixed $data = NULL) { | ||
Ramy-Badr-Ahmed marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$this->data = $data; | ||
$this->rank = 0; | ||
$this->parent = $this; // Initialize parent to itself | ||
} | ||
} | ||
Ramy-Badr-Ahmed marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
namespace DataStructures; | ||
|
||
use DataStructures\DisjointSets\DisjointSetNode; | ||
use DisjointSet; | ||
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]); | ||
|
||
} | ||
Ramy-Badr-Ahmed marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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; | ||
Ramy-Badr-Ahmed marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
return false; | ||
} | ||
|
||
} | ||
Ramy-Badr-Ahmed marked this conversation as resolved.
Show resolved
Hide resolved
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.