Skip to content

Commit 6a35a6a

Browse files
committed
Merge branch 'master' of github.com:mirahman/PHP-Data-Structure-and-Algorithms
2 parents 54e88a3 + 0a7afba commit 6a35a6a

File tree

3 files changed

+80
-71
lines changed

3 files changed

+80
-71
lines changed

.github/FUNDING.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# These are supported funding model platforms
2+
github: [mirahman]
3+
custom: ["paypal.me/mirahman1221", mizanurrahman.com]

Algorithms/Graph/Dijkstra.php

Lines changed: 72 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,73 @@
1-
<?php
2-
3-
/*
4-
* Author: Mizanur rahman <[email protected]>
5-
*
6-
*/
7-
8-
function dijkstra(array $graph, string $source, string $target): array {
9-
$dist = [];
10-
$pred = [];
11-
$Queue = new SplPriorityQueue();
12-
13-
foreach ($graph as $v => $adj) {
14-
$dist[$v] = PHP_INT_MAX;
15-
$pred[$v] = null;
16-
$Queue->insert($v, min($adj));
17-
}
18-
19-
$dist[$source] = 0;
20-
21-
while (!$Queue->isEmpty()) {
22-
$u = $Queue->extract();
23-
if (!empty($graph[$u])) {
24-
foreach ($graph[$u] as $v => $cost) {
25-
if ($dist[$u] + $cost < $dist[$v]) {
26-
$dist[$v] = $dist[$u] + $cost;
27-
$pred[$v] = $u;
28-
}
29-
}
30-
}
31-
}
32-
33-
$S = new SplStack();
34-
$u = $target;
35-
$distance = 0;
36-
37-
while (isset($pred[$u]) && $pred[$u]) {
38-
$S->push($u);
39-
$distance += $graph[$u][$pred[$u]];
40-
$u = $pred[$u];
41-
}
42-
43-
if ($S->isEmpty()) {
44-
return ["distance" => 0, "path" => $S];
45-
} else {
46-
$S->push($source);
47-
return ["distance" => $distance, "path" => $S];
48-
}
49-
}
50-
51-
$graph = [
52-
'A' => ['B' => 3, 'C' => 5, 'D' => 9],
53-
'B' => ['A' => 3, 'C' => 3, 'D' => 4, 'E' => 7],
54-
'C' => ['A' => 5, 'B' => 3, 'D' => 2, 'E' => 6, 'F' => 3],
55-
'D' => ['A' => 9, 'B' => 4, 'C' => 2, 'E' => 2, 'F' => 2],
56-
'E' => ['B' => 7, 'C' => 6, 'D' => 2, 'F' => 5],
57-
'F' => ['C' => 3, 'D' => 2, 'E' => 5],
58-
];
59-
60-
$source = "A";
61-
$target = "F";
62-
63-
$result = dijkstra($graph, $source, $target);
64-
extract($result);
65-
66-
echo "Distance from $source to $target is $distance \n";
67-
echo "Path to follow : ";
68-
69-
while (!$path->isEmpty()) {
70-
echo $path->pop() . "\t";
1+
<?php
2+
3+
/*
4+
* Author: Mizanur rahman <[email protected]>
5+
*
6+
*/
7+
8+
function dijkstra(array $graph, string $source, string $target): array {
9+
$dist = [];
10+
$pred = [];
11+
$Queue = new SplPriorityQueue();
12+
13+
foreach ($graph as $v => $adj) {
14+
$dist[$v] = PHP_INT_MAX;
15+
$pred[$v] = null;
16+
foreach ($adj as $w => $cost) {
17+
$Queue->insert($w, $cost);
18+
}
19+
}
20+
21+
$dist[$source] = 0;
22+
23+
while (!$Queue->isEmpty()) {
24+
$u = $Queue->extract();
25+
if (!empty($graph[$u])) {
26+
foreach ($graph[$u] as $v => $cost) {
27+
if ($dist[$u] + $cost < $dist[$v]) {
28+
$dist[$v] = $dist[$u] + $cost;
29+
$pred[$v] = $u;
30+
}
31+
}
32+
}
33+
}
34+
35+
$S = new SplStack();
36+
$u = $target;
37+
$distance = 0;
38+
39+
while (isset($pred[$u]) && $pred[$u]) {
40+
$S->push($u);
41+
$distance += $graph[$u][$pred[$u]];
42+
$u = $pred[$u];
43+
}
44+
45+
if ($S->isEmpty()) {
46+
return ["distance" => 0, "path" => $S];
47+
} else {
48+
$S->push($source);
49+
return ["distance" => $distance, "path" => $S];
50+
}
51+
}
52+
53+
$graph = [
54+
'A' => ['B' => 3, 'C' => 5, 'D' => 9],
55+
'B' => ['A' => 3, 'C' => 3, 'D' => 4, 'E' => 7],
56+
'C' => ['A' => 5, 'B' => 3, 'D' => 2, 'E' => 6, 'F' => 3],
57+
'D' => ['A' => 9, 'B' => 4, 'C' => 2, 'E' => 2, 'F' => 2],
58+
'E' => ['B' => 7, 'C' => 6, 'D' => 2, 'F' => 5],
59+
'F' => ['C' => 3, 'D' => 2, 'E' => 5],
60+
];
61+
62+
$source = "E";
63+
$target = "F";
64+
65+
$result = dijkstra($graph, $source, $target);
66+
extract($result);
67+
68+
echo "Distance from $source to $target is $distance \n";
69+
echo "Path to follow : ";
70+
71+
while (!$path->isEmpty()) {
72+
echo $path->pop() . "\t";
7173
}

DS/LinkedList/Classes/LinkedList.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ public function insertBefore(string $data = NULL, string $query = NULL) {
6969
while ($currentNode !== NULL) {
7070
if ($currentNode->data === $query) {
7171
$newNode->next = $currentNode;
72-
$previous->next = $newNode;
72+
if ($previous === NULL) {
73+
$this->_firstNode = &$newNode;
74+
} else {
75+
$previous->next = $newNode;
76+
}
7377
$this->_totalNode++;
7478
break;
7579
}

0 commit comments

Comments
 (0)