From 7b3eab5f5a932ffbb51d46abfe7e41f1169a18f6 Mon Sep 17 00:00:00 2001 From: miholeus Date: Mon, 19 Mar 2018 19:49:30 +0300 Subject: [PATCH] php example of breadth-firsth search (#58) --- .../php/01_breadth-first_search.php | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 06_breadth-first_search/php/01_breadth-first_search.php diff --git a/06_breadth-first_search/php/01_breadth-first_search.php b/06_breadth-first_search/php/01_breadth-first_search.php new file mode 100644 index 0000000..cf7e97a --- /dev/null +++ b/06_breadth-first_search/php/01_breadth-first_search.php @@ -0,0 +1,46 @@ +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");