php example of breadth-firsth search (#58)
This commit is contained in:
committed by
Aditya Bhargava
parent
2f939182ae
commit
7b3eab5f5a
46
06_breadth-first_search/php/01_breadth-first_search.php
Normal file
46
06_breadth-first_search/php/01_breadth-first_search.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
function personIsSeller(string $name): bool {
|
||||
return $name[-1] == "m";
|
||||
}
|
||||
|
||||
$graph = [];
|
||||
$graph["you"] = ["alice", "bob", "claire"];
|
||||
$graph["bob"] = ["anuj", "peggy"];
|
||||
$graph["alice"] = ["peggy"];
|
||||
$graph["claire"] = ["thom", "jonny"];
|
||||
$graph["anuj"] = [];
|
||||
$graph["peggy"] = [];
|
||||
$graph["thom"] = [];
|
||||
$graph["jonny"] = [];
|
||||
|
||||
function enqueue(\SplQueue $queue, array $persons) {
|
||||
foreach ($persons as $person) {
|
||||
$queue->enqueue($person);
|
||||
}
|
||||
}
|
||||
|
||||
function search(string $name): bool {
|
||||
global $graph;
|
||||
$searchQueue = new \SplQueue();
|
||||
enqueue($searchQueue, $graph[$name]);
|
||||
# This array is how you keep track of which people you've searched before.
|
||||
$searched = [];
|
||||
while (!$searchQueue->isEmpty()) {
|
||||
$person = $searchQueue->dequeue();
|
||||
# Only search this person if you haven't already searched them.
|
||||
if (!isset($searched[$person])) {
|
||||
if (personIsSeller($person)) {
|
||||
printf("%s is a mango seller", $person);
|
||||
return true;
|
||||
} else {
|
||||
enqueue($searchQueue, $graph[$person]);
|
||||
# Marks this person as searched
|
||||
$searched[$person] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
search("you");
|
||||
Reference in New Issue
Block a user