Skip to content

Instantly share code, notes, and snippets.

@sedera-tax
Created June 9, 2020 07:41
Show Gist options
  • Select an option

  • Save sedera-tax/083241d1651b0779453f3f5a6639ee4a to your computer and use it in GitHub Desktop.

Select an option

Save sedera-tax/083241d1651b0779453f3f5a6639ee4a to your computer and use it in GitHub Desktop.
<?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