Skip to content

Commit 20a88b2

Browse files
committed
Restructured the code base
1 parent de33c3f commit 20a88b2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+2217
-0
lines changed

Algorithms/Graph/BFS.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/*
4+
* Author: Mizanur rahman <[email protected]>
5+
*
6+
*/
7+
8+
function BFS(array &$graph, int $start, array $visited): SplQueue {
9+
$queue = new SplQueue;
10+
$path = new SplQueue;
11+
12+
$queue->enqueue($start);
13+
$visited[$start] = 1;
14+
15+
while (!$queue->isEmpty()) {
16+
$node = $queue->dequeue();
17+
$path->enqueue($node);
18+
foreach ($graph[$node] as $key => $vertex) {
19+
if (!$visited[$key] && $vertex == 1) {
20+
$visited[$key] = 1;
21+
$queue->enqueue($key);
22+
}
23+
}
24+
}
25+
26+
return $path;
27+
}
28+
29+
$graph = [
30+
0 => [0, 1, 1, 0, 0, 0],
31+
1 => [1, 0, 0, 1, 0, 0],
32+
2 => [1, 0, 0, 1, 0, 0],
33+
3 => [0, 1, 1, 0, 1, 0],
34+
4 => [0, 0, 0, 1, 0, 1],
35+
5 => [0, 0, 0, 0, 1, 0],
36+
];
37+
38+
$graph = [];
39+
$visited = [];
40+
$vertexCount = 6;
41+
42+
for ($i = 1; $i <= $vertexCount; $i++) {
43+
$graph[$i] = array_fill(1, $vertexCount, 0);
44+
$visited[$i] = 0;
45+
}
46+
47+
$graph[1][2] = $graph[2][1] = 1;
48+
$graph[1][5] = $graph[5][1] = 1;
49+
$graph[5][2] = $graph[2][5] = 1;
50+
$graph[5][4] = $graph[4][5] = 1;
51+
$graph[4][3] = $graph[3][4] = 1;
52+
$graph[3][2] = $graph[2][3] = 1;
53+
$graph[6][4] = $graph[4][6] = 1;
54+
55+
$path = BFS($graph, 5, $visited);
56+
57+
while (!$path->isEmpty()) {
58+
echo $path->dequeue() . "\t";
59+
}
60+

Algorithms/Graph/BellmanFord.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
/*
4+
* Author: Mizanur rahman <[email protected]>
5+
*
6+
*/
7+
8+
function bellmanFord(array $graph, int $source): array {
9+
$dist = [];
10+
$len = count($graph);
11+
12+
foreach ($graph as $v => $adj) {
13+
$dist[$v] = PHP_INT_MAX;
14+
}
15+
16+
$dist[$source] = 0;
17+
18+
for ($k = 0; $k < $len - 1; $k++) {
19+
for ($i = 0; $i < $len; $i++) {
20+
for ($j = 0; $j < $len; $j++) {
21+
if ($dist[$i] > $dist[$j] + $graph[$j][$i]) {
22+
$dist[$i] = $dist[$j] + $graph[$j][$i];
23+
}
24+
}
25+
}
26+
}
27+
28+
for ($i = 0; $i < $len; $i++) {
29+
for ($j = 0; $j < $len; $j++) {
30+
if ($dist[$i] > $dist[$j] + $graph[$j][$i]) {
31+
echo 'The graph contains a negative-weight cycle!';
32+
return [];
33+
}
34+
}
35+
}
36+
return $dist;
37+
}
38+
39+
define("I", PHP_INT_MAX);
40+
41+
$graph = [
42+
0 => [I, 3, 5, 9, I, I],
43+
1 => [3, I, 3, 4, 7, I],
44+
2 => [5, 3, I, 2, 6, 3],
45+
3 => [9, 4, 2, I, 2, 2],
46+
4 => [I, 7, 6, 2, I, 5],
47+
5 => [I, I, 3, 2, 5, I]
48+
];
49+
50+
$matrix = array(
51+
0 => array(0, 3, 4),
52+
1 => array(0, 0, 2),
53+
2 => array(0, -2, 0),
54+
);
55+
56+
57+
58+
$source = 0;
59+
60+
$distances = bellmanFord($graph, $source);
61+
62+
foreach($distances as $target => $distance) {
63+
echo "distance from $source to $target is $distance \n";
64+
}

Algorithms/Graph/DFS.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/*
4+
* Author: Mizanur rahman <[email protected]>
5+
*
6+
*/
7+
8+
function DFS(array &$graph, int $start, array $visited): SplQueue {
9+
$stack = new SplStack;
10+
$path = new SplQueue;
11+
12+
$stack->push($start);
13+
$visited[$start] = 1;
14+
15+
while (!$stack->isEmpty()) {
16+
$node = $stack->pop();
17+
$path->enqueue($node);
18+
foreach ($graph[$node] as $key => $vertex) {
19+
if (!$visited[$key] && $vertex == 1) {
20+
$visited[$key] = 1;
21+
$stack->push($key);
22+
}
23+
}
24+
}
25+
26+
return $path;
27+
}
28+
29+
$graph = [
30+
0 => [0, 1, 1, 0, 0, 0],
31+
1 => [1, 0, 0, 1, 0, 0],
32+
2 => [1, 0, 0, 1, 0, 0],
33+
3 => [0, 1, 1, 0, 1, 0],
34+
4 => [0, 0, 0, 1, 0, 1],
35+
5 => [0, 0, 0, 0, 1, 0],
36+
];
37+
38+
$graph = [];
39+
$visited = [];
40+
$vertexCount = 6;
41+
42+
for ($i = 1; $i <= $vertexCount; $i++) {
43+
$graph[$i] = array_fill(1, $vertexCount, 0);
44+
$visited[$i] = 0;
45+
}
46+
47+
$graph[1][2] = $graph[2][1] = 1;
48+
$graph[1][5] = $graph[5][1] = 1;
49+
$graph[5][2] = $graph[2][5] = 1;
50+
$graph[5][4] = $graph[4][5] = 1;
51+
$graph[4][3] = $graph[3][4] = 1;
52+
$graph[3][2] = $graph[2][3] = 1;
53+
$graph[6][4] = $graph[4][6] = 1;
54+
55+
$path = DFS($graph, 1, $visited);
56+
57+
while (!$path->isEmpty()) {
58+
echo $path->dequeue() . "\t";
59+
}
60+

Algorithms/Graph/Dijkstra.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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";
71+
}

Algorithms/Graph/FloydWarshall.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* Author: Mizanur rahman <[email protected]>
5+
*
6+
*/
7+
8+
function floydWarshall(array $graph): array {
9+
$dist = [];
10+
$dist = $graph;
11+
$size = count($dist);
12+
13+
for ($k = 0; $k < $size; $k++)
14+
for ($i = 0; $i < $size; $i++)
15+
for ($j = 0; $j < $size; $j++)
16+
$dist[$i][$j] = min($dist[$i][$j], $dist[$i][$k] + $dist[$k][$j]);
17+
18+
return $dist;
19+
}
20+
21+
$totalVertices = 5;
22+
$graph = [];
23+
for ($i = 0; $i < $totalVertices; $i++) {
24+
for ($j = 0; $j < $totalVertices; $j++) {
25+
$graph[$i][$j] = $i == $j ? 0 : PHP_INT_MAX;
26+
}
27+
}
28+
29+
$graph[0][1] = $graph[1][0] = 10;
30+
$graph[2][1] = $graph[1][2] = 5;
31+
$graph[0][3] = $graph[3][0] = 5;
32+
$graph[3][1] = $graph[1][3] = 5;
33+
$graph[4][1] = $graph[1][4] = 10;
34+
$graph[3][4] = $graph[4][3] = 20;
35+
36+
$distance = floydWarshall($graph);
37+
38+
echo "Shortest distance between A to E is:" . $distance[0][4] . "\n";
39+
echo "Shortest distance between D to C is:" . $distance[3][2] . "\n";

Algorithms/Graph/Kruskal.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
/*
4+
* Example code for: PHP 7 Data Structures and Algorithms
5+
*
6+
* Author: Mizanur rahman <[email protected]>
7+
*
8+
*/
9+
10+
function Kruskal(array $graph): array {
11+
$len = count($graph);
12+
$tree = [];
13+
14+
$set = [];
15+
foreach ($graph as $k => $adj) {
16+
$set[$k] = [$k];
17+
}
18+
19+
$edges = [];
20+
for ($i = 0; $i < $len; $i++) {
21+
for ($j = 0; $j < $i; $j++) {
22+
if ($graph[$i][$j]) {
23+
$edges[$i . ',' . $j] = $graph[$i][$j];
24+
}
25+
}
26+
}
27+
28+
asort($edges);
29+
30+
foreach ($edges as $k => $w) {
31+
list($i, $j) = explode(',', $k);
32+
33+
$iSet = findSet($set, $i);
34+
$jSet = findSet($set, $j);
35+
if ($iSet != $jSet) {
36+
$tree[] = ["from" => $i, "to" => $j, "cost" => $graph[$i][$j]];
37+
unionSet($set, $iSet, $jSet);
38+
}
39+
}
40+
41+
return $tree;
42+
}
43+
44+
function findSet(array &$set, int $index) {
45+
foreach ($set as $k => $v) {
46+
if (in_array($index, $v)) {
47+
return $k;
48+
}
49+
}
50+
51+
return false;
52+
}
53+
54+
function unionSet(array &$set, int $i, int $j) {
55+
$a = $set[$i];
56+
$b = $set[$j];
57+
unset($set[$i], $set[$j]);
58+
$set[] = array_merge($a, $b);
59+
}
60+
61+
$graph = [
62+
[0, 3, 1, 6, 0, 0],
63+
[3, 0, 5, 0, 3, 0],
64+
[1, 5, 0, 5, 6, 4],
65+
[6, 0, 5, 0, 0, 2],
66+
[0, 3, 6, 0, 0, 6],
67+
[0, 0, 4, 2, 6, 0]
68+
];
69+
70+
$mst = Kruskal($graph);
71+
72+
$minimumCost = 0;
73+
74+
75+
foreach($mst as $v) {
76+
echo "From {$v['from']} to {$v['to']} cost is {$v['cost']} \n";
77+
$minimumCost += $v['cost'];
78+
}
79+
80+
echo "Minimum cost: $minimumCost \n";
81+
82+
83+

0 commit comments

Comments
 (0)