Created
June 9, 2020 07:41
-
-
Save sedera-tax/083241d1651b0779453f3f5a6639ee4a to your computer and use it in GitHub Desktop.
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
| <?php | |
| function bfsComponentSize($matrix) { | |
| $visited = []; | |
| $queue = []; | |
| $componentSize = 0; | |
| for ($i = 0; $i < count($matrix); $i++) { | |
| $visited[] = false; | |
| } | |
| $visited[1] = true; | |
| $queue[] = 1; | |
| while (count($queue) > 0) { | |
| $currentVertex = array_shift($queue); | |
| $visited[$currentVertex] = true; | |
| $componentSize++; | |
| for ($nextVertex = 0; $nextVertex < count($matrix); $nextVertex++) { | |
| if ($matrix[$currentVertex][$nextVertex] && !$visited[$nextVertex]) { | |
| $visited[$nextVertex] = true; | |
| $queue[] = $nextVertex; | |
| } | |
| } | |
| } | |
| return $componentSize; | |
| } | |
| $tab = [ | |
| [true, false, false], | |
| [true, false, false], | |
| [false, false, false] | |
| ]; | |
| echo bfsComponentSize($tab); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment