Skip to content

Commit 7caf942

Browse files
Merge branch 'TheAlgorithms:master' into master
2 parents 14c5ce6 + 81b5386 commit 7caf942

21 files changed

+816
-22
lines changed

DIRECTORY.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,21 @@
2727
* [Bstnode](./DataStructures/BinarySearchTree/BSTNode.php)
2828
* [Bstree](./DataStructures/BinarySearchTree/BSTree.php)
2929
* [Duplicatekeyexception](./DataStructures/BinarySearchTree/DuplicateKeyException.php)
30+
* Comparebinarytree
31+
* [Binarytreenode](./DataStructures/CompareBinaryTree/BinaryTreeNode.php)
32+
* [Comparebinarytree](./DataStructures/CompareBinaryTree/CompareBinaryTree.php)
3033
* Disjointsets
3134
* [Disjointset](./DataStructures/DisjointSets/DisjointSet.php)
3235
* [Disjointsetnode](./DataStructures/DisjointSets/DisjointSetNode.php)
3336
* [Doublylinkedlist](./DataStructures/DoublyLinkedList.php)
37+
* Invertbinarytree
38+
* [Binarytree](./DataStructures/InvertBinaryTree/BinaryTree.php)
39+
* [Invertbinarytree](./DataStructures/InvertBinaryTree/InvertBinaryTree.php)
3440
* [Node](./DataStructures/Node.php)
3541
* [Queue](./DataStructures/Queue.php)
42+
* Reverselinkedlist
43+
* [Linkedlistitem](./DataStructures/ReverseLinkedList/LinkedListItem.php)
44+
* [Reverselinkedlist](./DataStructures/ReverseLinkedList/ReverseLinkedList.php)
3645
* Segmenttree
3746
* [Segmenttree](./DataStructures/SegmentTree/SegmentTree.php)
3847
* [Segmenttreenode](./DataStructures/SegmentTree/SegmentTreeNode.php)
@@ -50,6 +59,8 @@
5059
* [Bellmanford](./Graphs/BellmanFord.php)
5160
* [Breadthfirstsearch](./Graphs/BreadthFirstSearch.php)
5261
* [Depthfirstsearch](./Graphs/DepthFirstSearch.php)
62+
* [Dijkstras](./Graphs/Dijkstras.php)
63+
* [Graphedge](./Graphs/GraphEdge.php)
5364

5465
## Maths
5566
* [Absolutemax](./Maths/AbsoluteMax.php)
@@ -86,6 +97,10 @@
8697
* [Problem8](./Maths/ProjectEuler/Problem8.php)
8798
* [Problem9](./Maths/ProjectEuler/Problem9.php)
8899

100+
## Neuralnetworks
101+
* Perceptronclassifier
102+
* [Neuralnetworkperceptronclassifier](./NeuralNetworks/PerceptronClassifier/NeuralNetworkPerceptronClassifier.php)
103+
89104
## Searches
90105
* [Binarysearch](./Searches/BinarySearch.php)
91106
* [Exponentialsearch](./Searches/ExponentialSearch.php)
@@ -139,9 +154,12 @@
139154
* Datastructures
140155
* [Avltreetest](./tests/DataStructures/AVLTreeTest.php)
141156
* [Bstreetest](./tests/DataStructures/BSTreeTest.php)
157+
* [Comparebinarytreetest](./tests/DataStructures/CompareBinaryTreeTest.php)
142158
* [Disjointsettest](./tests/DataStructures/DisjointSetTest.php)
143159
* [Doublylinkedlisttest](./tests/DataStructures/DoublyLinkedListTest.php)
160+
* [Invertbinarytreetest](./tests/DataStructures/InvertBinaryTreeTest.php)
144161
* [Queuetest](./tests/DataStructures/QueueTest.php)
162+
* [Reverselinkedlisttest](./tests/DataStructures/ReverseLinkedListTest.php)
145163
* [Segmenttreetest](./tests/DataStructures/SegmentTreeTest.php)
146164
* [Singlylinkedlisttest](./tests/DataStructures/SinglyLinkedListTest.php)
147165
* [Splaytreetest](./tests/DataStructures/SplayTreeTest.php)
@@ -151,10 +169,14 @@
151169
* [Bellmanfordtest](./tests/Graphs/BellmanFordTest.php)
152170
* [Breadthfirstsearchtest](./tests/Graphs/BreadthFirstSearchTest.php)
153171
* [Depthfirstsearchtest](./tests/Graphs/DepthFirstSearchTest.php)
172+
* [Dijkstrastest](./tests/Graphs/DijkstrasTest.php)
154173
* Maths
155174
* [Eratosthenessievetest](./tests/Maths/EratosthenesSieveTest.php)
156175
* [Mathstest](./tests/Maths/MathsTest.php)
157176
* [Projecteulertest](./tests/Maths/ProjectEulerTest.php)
177+
* Neuralnetworks
178+
* Perceptronclassifier
179+
* [Neuralnetworkperceptronclassifiertest](./tests/NeuralNetworks/PerceptronClassifier/NeuralNetworkPerceptronClassifierTest.php)
158180
* Searches
159181
* [Searchestest](./tests/Searches/SearchesTest.php)
160182
* Sorting
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace DataStructures\CompareBinaryTree;
4+
5+
class BinaryTreeNode
6+
{
7+
public function __construct($value, ?BinaryTreeNode $left = null, BinaryTreeNode $right = null)
8+
{
9+
$this->value = $value;
10+
$this->left = $left;
11+
$this->right = $right;
12+
}
13+
14+
public $value;
15+
public ?BinaryTreeNode $left;
16+
public ?BinaryTreeNode $right;
17+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace DataStructures\CompareBinaryTree;
4+
5+
/**
6+
* Recurrent comparison of binary trees based on comparison of left and right branches
7+
* (https://en.wikipedia.org/wiki/Binary_tree).
8+
*
9+
* @author Michał Żarnecki https://github.com/rzarno
10+
*/
11+
class CompareBinaryTree
12+
{
13+
/**
14+
* compare two binary trees
15+
* @param BinaryTreeNode|null $a
16+
* @param BinaryTreeNode|null $b
17+
* @return bool
18+
*/
19+
public function areTreesEqual(?BinaryTreeNode $a, ?BinaryTreeNode $b): bool
20+
{
21+
if (! $a && $b || $a && ! $b) {
22+
return false;
23+
}
24+
25+
if (! $a && ! $b) {
26+
return true;
27+
}
28+
29+
if ($a->value !== $b->value) {
30+
return false;
31+
}
32+
return $this->areTreesEqual($a->left, $b->left)
33+
&& $this->areTreesEqual($a->right, $b->right);
34+
}
35+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace DataStructures\InvertBinaryTree;
4+
5+
class BinaryTree
6+
{
7+
private ?BinaryTree $left = null;
8+
private ?BinaryTree $right = null;
9+
private $value;
10+
11+
public function setLeft(?BinaryTree $left)
12+
{
13+
$this->left = $left;
14+
return $this;
15+
}
16+
17+
public function getLeft(): ?BinaryTree
18+
{
19+
return $this->left;
20+
}
21+
22+
public function setRight(?BinaryTree $right)
23+
{
24+
$this->right = $right;
25+
return $this;
26+
}
27+
28+
public function getRight(): ?BinaryTree
29+
{
30+
return $this->right;
31+
}
32+
33+
public function setValue($value)
34+
{
35+
$this->value = $value;
36+
return $this;
37+
}
38+
39+
public function getValue()
40+
{
41+
return $this->value;
42+
}
43+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace DataStructures\InvertBinaryTree;
4+
5+
/**
6+
* Recurrent algorithm to invert binary tree (mirror)
7+
* (https://medium.com/@kvrware/inverting-binary-tree-b0ff3a5cb0df).
8+
*
9+
* @author Michał Żarnecki https://github.com/rzarno
10+
*/
11+
class InvertBinaryTree
12+
{
13+
public function invert(?BinaryTree $b): void
14+
{
15+
if (! $b) {
16+
return;
17+
}
18+
$tmp = $b->getLeft();
19+
$b->setLeft($b->getRight());
20+
$b->setRight($tmp);
21+
$this->invert($b->getLeft());
22+
$this->invert($b->getRight());
23+
}
24+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace DataStructures\ReverseLinkedList;
4+
5+
class LinkedListItem
6+
{
7+
private ?LinkedListItem $next = null;
8+
private ?LinkedListItem $prev = null;
9+
private $value;
10+
11+
public function setNext(?LinkedListItem $next)
12+
{
13+
$this->next = $next;
14+
return $this;
15+
}
16+
17+
public function getNext(): ?LinkedListItem
18+
{
19+
return $this->next;
20+
}
21+
22+
public function setPrev(?LinkedListItem $prev)
23+
{
24+
$this->prev = $prev;
25+
return $this;
26+
}
27+
28+
public function getPrev(): ?LinkedListItem
29+
{
30+
return $this->prev;
31+
}
32+
33+
public function setValue($value)
34+
{
35+
$this->value = $value;
36+
return $this;
37+
}
38+
39+
public function getValue()
40+
{
41+
return $this->value;
42+
}
43+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace DataStructures\ReverseLinkedList;
4+
5+
/**
6+
* Reverse linked list
7+
* (https://en.wikipedia.org/wiki/Linked_list).
8+
*
9+
* @author Michał Żarnecki https://github.com/rzarno
10+
*/
11+
class ReverseLinkedList
12+
{
13+
public function reverse(LinkedListItem $item): LinkedListItem
14+
{
15+
$next = $item->getNext();
16+
$item->setNext(null);
17+
while (true) {
18+
$item->setPrev($next);
19+
if (! $next) {
20+
return $item;
21+
}
22+
$nextNext = $next->getNext();
23+
$next->setNext($item);
24+
$item = $next;
25+
$next = $nextNext;
26+
}
27+
}
28+
}

Graphs/BellmanFord.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
11
<?php
22

3-
class Edge
4-
{
5-
public $start;
6-
public $end;
7-
public int $weight;
8-
}
9-
103
/**
114
* The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all of the
125
* other vertices in a weighted digraph.
136
* (https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm).
147
*
158
* @author Michał Żarnecki https://github.com/rzarno
16-
* @param array $verticies An array of verticies names
17-
* @param Edge[] $edges An array of edges
18-
* @return string $start The starting vertex
9+
* @param array $verticesNames An array of vertices names
10+
* @param GraphEdge[] $edges An array of edges
11+
* @param string $start The starting vertex
12+
* @return array An array of shortest paths from $start to all other vertices
1913
*/
20-
function bellmanFord(array $verticesNames, array $edges, string $start, bool $verbose = false)
14+
function bellmanFord(array $verticesNames, array $edges, string $start, bool $verbose = false): array
2115
{
2216
$vertices = array_combine($verticesNames, array_fill(0, count($verticesNames), PHP_INT_MAX));
2317

@@ -30,7 +24,7 @@ function bellmanFord(array $verticesNames, array $edges, string $start, bool $ve
3024
$change = false;
3125
foreach ($vertices as $vertice => $minWeight) {
3226
if ($verbose) {
33-
echo "checking vertice $vertice\n";
27+
echo "checking vertex $vertice\n";
3428
}
3529
if ($start === $vertice) {
3630
$vertices[$vertice] = 0;
@@ -39,7 +33,8 @@ function bellmanFord(array $verticesNames, array $edges, string $start, bool $ve
3933
foreach ($edges[$vertice] as $edge) {
4034
if ($vertices[$edge->end] > $vertices[$vertice] + $edge->weight) {
4135
if ($verbose) {
42-
echo "replace $vertice " . $vertices[$edge->end] . " with " . $vertices[$vertice] + $edge->weight . "\n ";
36+
echo "replace $vertice " . $vertices[$edge->end] . " with "
37+
. ($vertices[$vertice] + $edge->weight) . "\n ";
4338
}
4439
$vertices[$edge->end] = $vertices[$vertice] + $edge->weight;
4540
$change = true;

Graphs/Dijkstras.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/**
4+
* The Dijkstra's algorithm is an algorithm for finding the shortest paths between nodes in a weighted graph.
5+
* (https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm).
6+
*
7+
* @author Michał Żarnecki https://github.com/rzarno
8+
* @param array $verticesNames An array of vertices names
9+
* @param GraphEdge[] $edges An array of edges
10+
* @param string $start The starting vertex
11+
* @return array An array of shortest paths from $start to all other vertices
12+
*/
13+
function dijkstras(array $verticesNames, array $edges, string $start): array
14+
{
15+
$vertices = array_combine($verticesNames, array_fill(0, count($verticesNames), PHP_INT_MAX));
16+
$visitedNodes = [];
17+
18+
$nextVertex = $start;
19+
$vertices[$start] = 0;
20+
while (count($visitedNodes) < count($verticesNames)) { //continue until all nodes are visited
21+
foreach ($edges as $edge) {
22+
if ($edge->start == $nextVertex) { //consider only nodes connected to current one
23+
$vertices[$edge->end] = min($vertices[$edge->end], $vertices[$nextVertex] + $edge->weight);
24+
}
25+
}
26+
27+
// find vertex with current lowest value to be starting point in next iteration
28+
$minVertexName = null;
29+
$minVertexWeight = PHP_INT_MAX;
30+
foreach ($vertices as $name => $weight) {
31+
if (in_array($name, $visitedNodes) || $name == $nextVertex) {
32+
continue;
33+
}
34+
if ($weight <= $minVertexWeight) {
35+
$minVertexName = $name;
36+
$minVertexWeight = $weight;
37+
}
38+
}
39+
$visitedNodes[] = $nextVertex;
40+
$nextVertex = $minVertexName;
41+
}
42+
return $vertices;
43+
}

Graphs/GraphEdge.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
class GraphEdge
4+
{
5+
public string $start;
6+
public string $end;
7+
public int $weight;
8+
}

0 commit comments

Comments
 (0)