From d422be50318ad0773ff230da7b8cb634631253de Mon Sep 17 00:00:00 2001 From: mirahman Date: Tue, 12 Dec 2017 14:11:42 -0500 Subject: [PATCH 1/7] fixing dijkstra's algorithm --- Algorithms/Graph/Dijkstra.php | 142 +++++++++++++++++----------------- 1 file changed, 72 insertions(+), 70 deletions(-) diff --git a/Algorithms/Graph/Dijkstra.php b/Algorithms/Graph/Dijkstra.php index 77b3dbc..864205e 100644 --- a/Algorithms/Graph/Dijkstra.php +++ b/Algorithms/Graph/Dijkstra.php @@ -1,71 +1,73 @@ - - * - */ - -function dijkstra(array $graph, string $source, string $target): array { - $dist = []; - $pred = []; - $Queue = new SplPriorityQueue(); - - foreach ($graph as $v => $adj) { - $dist[$v] = PHP_INT_MAX; - $pred[$v] = null; - $Queue->insert($v, min($adj)); - } - - $dist[$source] = 0; - - while (!$Queue->isEmpty()) { - $u = $Queue->extract(); - if (!empty($graph[$u])) { - foreach ($graph[$u] as $v => $cost) { - if ($dist[$u] + $cost < $dist[$v]) { - $dist[$v] = $dist[$u] + $cost; - $pred[$v] = $u; - } - } - } - } - - $S = new SplStack(); - $u = $target; - $distance = 0; - - while (isset($pred[$u]) && $pred[$u]) { - $S->push($u); - $distance += $graph[$u][$pred[$u]]; - $u = $pred[$u]; - } - - if ($S->isEmpty()) { - return ["distance" => 0, "path" => $S]; - } else { - $S->push($source); - return ["distance" => $distance, "path" => $S]; - } -} - -$graph = [ - 'A' => ['B' => 3, 'C' => 5, 'D' => 9], - 'B' => ['A' => 3, 'C' => 3, 'D' => 4, 'E' => 7], - 'C' => ['A' => 5, 'B' => 3, 'D' => 2, 'E' => 6, 'F' => 3], - 'D' => ['A' => 9, 'B' => 4, 'C' => 2, 'E' => 2, 'F' => 2], - 'E' => ['B' => 7, 'C' => 6, 'D' => 2, 'F' => 5], - 'F' => ['C' => 3, 'D' => 2, 'E' => 5], -]; - -$source = "A"; -$target = "F"; - -$result = dijkstra($graph, $source, $target); -extract($result); - -echo "Distance from $source to $target is $distance \n"; -echo "Path to follow : "; - -while (!$path->isEmpty()) { - echo $path->pop() . "\t"; + + * + */ + +function dijkstra(array $graph, string $source, string $target): array { + $dist = []; + $pred = []; + $Queue = new SplPriorityQueue(); + + foreach ($graph as $v => $adj) { + $dist[$v] = PHP_INT_MAX; + $pred[$v] = null; + foreach ($adj as $w => $cost) { + $Queue->insert($w, $cost); + } + } + + $dist[$source] = 0; + + while (!$Queue->isEmpty()) { + $u = $Queue->extract(); + if (!empty($graph[$u])) { + foreach ($graph[$u] as $v => $cost) { + if ($dist[$u] + $cost < $dist[$v]) { + $dist[$v] = $dist[$u] + $cost; + $pred[$v] = $u; + } + } + } + } + + $S = new SplStack(); + $u = $target; + $distance = 0; + + while (isset($pred[$u]) && $pred[$u]) { + $S->push($u); + $distance += $graph[$u][$pred[$u]]; + $u = $pred[$u]; + } + + if ($S->isEmpty()) { + return ["distance" => 0, "path" => $S]; + } else { + $S->push($source); + return ["distance" => $distance, "path" => $S]; + } +} + +$graph = [ + 'A' => ['B' => 3, 'C' => 5, 'D' => 9], + 'B' => ['A' => 3, 'C' => 3, 'D' => 4, 'E' => 7], + 'C' => ['A' => 5, 'B' => 3, 'D' => 2, 'E' => 6, 'F' => 3], + 'D' => ['A' => 9, 'B' => 4, 'C' => 2, 'E' => 2, 'F' => 2], + 'E' => ['B' => 7, 'C' => 6, 'D' => 2, 'F' => 5], + 'F' => ['C' => 3, 'D' => 2, 'E' => 5], +]; + +$source = "E"; +$target = "F"; + +$result = dijkstra($graph, $source, $target); +extract($result); + +echo "Distance from $source to $target is $distance \n"; +echo "Path to follow : "; + +while (!$path->isEmpty()) { + echo $path->pop() . "\t"; } \ No newline at end of file From 9cf0fb69221b850d9faeb49c421acb069d95ec8d Mon Sep 17 00:00:00 2001 From: Stanislav Karassyov Date: Mon, 22 Jan 2018 00:00:13 +0600 Subject: [PATCH 2/7] fix: You can't insert before the first element with method insertBefore Resolves: #4 --- DS/LinkedList/Classes/LinkedList.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/DS/LinkedList/Classes/LinkedList.php b/DS/LinkedList/Classes/LinkedList.php index 1d6bfa3..405e7f7 100644 --- a/DS/LinkedList/Classes/LinkedList.php +++ b/DS/LinkedList/Classes/LinkedList.php @@ -69,6 +69,11 @@ public function insertBefore(string $data = NULL, string $query = NULL) { while ($currentNode !== NULL) { if ($currentNode->data === $query) { $newNode->next = $currentNode; + if ($previous === NULL) { + $this->_firstNode = &$newNode; + } else { + $previous->next = $newNode; + } $previous->next = $newNode; $this->_totalNode++; break; From 676602d384cbe81594234258f3b1dc3fb9cc0bd6 Mon Sep 17 00:00:00 2001 From: Stanislav Karassyov Date: Mon, 22 Jan 2018 00:30:09 +0600 Subject: [PATCH 3/7] fix: You can't insert before the first element with method insertBefore --- DS/LinkedList/Classes/LinkedList.php | 1 - 1 file changed, 1 deletion(-) diff --git a/DS/LinkedList/Classes/LinkedList.php b/DS/LinkedList/Classes/LinkedList.php index 405e7f7..40dd272 100644 --- a/DS/LinkedList/Classes/LinkedList.php +++ b/DS/LinkedList/Classes/LinkedList.php @@ -74,7 +74,6 @@ public function insertBefore(string $data = NULL, string $query = NULL) { } else { $previous->next = $newNode; } - $previous->next = $newNode; $this->_totalNode++; break; } From 0a7afbaad737b0ff66632241f3fef92f0ea9a55f Mon Sep 17 00:00:00 2001 From: Mizanur Rahman Date: Mon, 13 Jan 2020 23:18:16 +0900 Subject: [PATCH 4/7] Create FUNDING.yml --- .github/FUNDING.yml | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .github/FUNDING.yml diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..53253a8 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,3 @@ +# These are supported funding model platforms +github: [mirahman] +custom: ["paypal.me/mirahman1221", mizanurrahman.com] From 54e88a3f7dad9d51254ea0cb27732c7e9fc1a554 Mon Sep 17 00:00:00 2001 From: Mizanur rahman Date: Thu, 16 Jan 2020 19:15:25 +0600 Subject: [PATCH 5/7] adding TRIE data structure in PHP --- DS/Tree/.DS_Store | Bin 0 -> 6148 bytes DS/Tree/Classes/Trie.php | 49 +++++++++++++++++++++++++++++++++++ DS/Tree/Classes/TrieNode.php | 21 +++++++++++++++ DS/Tree/Examples/trie.php | 30 +++++++++++++++++++++ 4 files changed, 100 insertions(+) create mode 100644 DS/Tree/.DS_Store create mode 100644 DS/Tree/Classes/Trie.php create mode 100644 DS/Tree/Classes/TrieNode.php create mode 100644 DS/Tree/Examples/trie.php diff --git a/DS/Tree/.DS_Store b/DS/Tree/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..1ad25cbe90a6572f8b80a99612b73f93e916116c GIT binary patch literal 6148 zcmeHKJ5B>Z41I=3M50MaIalBYqYzHO1t0{fm9|*XZ^gNCG@d_-6gJS&py!qSW<0h> z`-=4%fNehx_rM0gn(By8H%swz*M(e_$I+t3gf~ow>2=tTi|ES^&%MC|2E1V6bIKnF zmi&J7&f5Dik1p4fR}6>&F(3xSfEf6l0dKIB^=+c87!U(u;KYD`9~#xM7tV?C>7a=b zfViSNgzK0kh|L|uUN|SxL$ORHW~$YQVVO>UtGHe`CuTYV!&3rr`?vXRK0caa^7nj^_l81N2&|gkwO#g lm>BJt8*j&V5tMPw*F5iqb7G{^AL&H>3^*?mG4R(6d;tOm8=L?D literal 0 HcmV?d00001 diff --git a/DS/Tree/Classes/Trie.php b/DS/Tree/Classes/Trie.php new file mode 100644 index 0000000..2fe2292 --- /dev/null +++ b/DS/Tree/Classes/Trie.php @@ -0,0 +1,49 @@ + + * + */ + +namespace DS\Tree\Classes; + +use DS\Tree\Classes\TrieNode; + +class Trie +{ + public $root; + public function __construct() + { + $this->root = new TrieNode; + } + + public function insert(string $key) + { + $key = strtolower($key); + $length = strlen($key); + $pCrawl = $this->root; + for ($level = 0; $level < $length; $level++) { + $index = ord($key[$level]) - ord('a'); + if (is_null($pCrawl->children[$index])) + $pCrawl->children[$index] = new TrieNode(); + + $pCrawl = $pCrawl->children[$index]; + } + $pCrawl->isEndOfWord = true; + } + + public function search(string $key) + { + $key = strtolower($key); + $length = strlen($key); + $pCrawl = $this->root; + for ($level = 0; $level < $length; $level++) { + $index = ord($key[$level]) - ord('a'); + if (is_null($pCrawl->children[$index])) + return false; + + $pCrawl = $pCrawl->children[$index]; + } + return (!is_null($pCrawl) && $pCrawl->isEndOfWord); + } +} diff --git a/DS/Tree/Classes/TrieNode.php b/DS/Tree/Classes/TrieNode.php new file mode 100644 index 0000000..ee49527 --- /dev/null +++ b/DS/Tree/Classes/TrieNode.php @@ -0,0 +1,21 @@ + + * + */ + +namespace DS\Tree\Classes; + +class TrieNode +{ + public $children; + public $isEndOfWord; + const ALPHABETSIZE = 26; + + public function __construct() + { + $this->isEndOfWord = false; + $this->children = array_fill(0,self::ALPHABETSIZE, null); + } +} \ No newline at end of file diff --git a/DS/Tree/Examples/trie.php b/DS/Tree/Examples/trie.php new file mode 100644 index 0000000..23e9ab0 --- /dev/null +++ b/DS/Tree/Examples/trie.php @@ -0,0 +1,30 @@ + + * + */ + +include __DIR__ . '/../../../Vendor/Autoload.php'; + +use \DS\Tree\Classes\Trie; + +try { + $keys = ["dhaka", "bangladesh", "there", "answer", "any", "by", "bye", "their"]; + + $trie = new Trie(); + // Construct trie + for ($i = 0; $i < count($keys); $i++) + $trie->insert($keys[$i]); + + $searches = ["dhaka", "three", "these", "there", "the", "any", "DHAKA", "anyone", "desh"]; + foreach ($searches as $search) { + if ($trie->search($search)) { + echo "$search - is present in the trie \n"; + } else { + echo "$search - is not present in the trie \n"; + } + } +} catch (Exception $e) { + echo $e->getMessage(); +} From 7574f25f96c6b77bf036fcf2a8ef560719d0e21e Mon Sep 17 00:00:00 2001 From: Mizanur rahman Date: Thu, 16 Jan 2020 19:26:03 +0600 Subject: [PATCH 6/7] added Trie in our list of solved DS --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 45417a4..057c7ab 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ Any feedback, bugs or suggestion will be welcomed. - Binary Tree - Binary Search Tree - Tree Traversal (In-order, pre-order, post-order) + - Trie (Simple insert and search operation) 5. Heaps From eee6c1b6164c2a74295bd75ed38f13cbbc903971 Mon Sep 17 00:00:00 2001 From: Mizanur Rahman Date: Fri, 21 Feb 2020 05:20:12 +0600 Subject: [PATCH 7/7] Create .sonarcloud.properties --- .sonarcloud.properties | 1 + 1 file changed, 1 insertion(+) create mode 100644 .sonarcloud.properties diff --git a/.sonarcloud.properties b/.sonarcloud.properties new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/.sonarcloud.properties @@ -0,0 +1 @@ +