PHP Classes

PHP Data Structure and Algorithms: Create and search data structures objects

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 48 All time: 10,755 This week: 63Up
Version License PHP version Categories
datastructureandalgo 1.0.0MIT/X Consortium ...5Algorithms, PHP 5, Searching
Description 

Author

This package can create and search data structures objects.

It provides several classes that can store data structure information and can perform several types of algorithms that can

Currently, it provides classes that implement:

- Binary search of nodes with values linked in a tree structure

- Breadth-first search for a graph of nodes with data values with links between each other

- Manipulating and searching linked lists of nodes with the specific values

Picture of Ogbemudia Osayawe
  Performance   Level  
Innovation award
Innovation award
Nominee: 5x

 

Example

<?php declare(strict_types=1);

use
App\BreadthFirstSearch\Graph;
use
App\BreadthFirstSearch\BreadthFirstSearch;
use
App\BreadthFirstSearch\GraphFactory;

require_once
__DIR__ . '/vendor/autoload.php';

ini_set('display_errors', '1');
error_reporting(E_ALL);

$bfsObject = new BreadthFirstSearch(new GraphFactory());
$graph = $bfsObject->getGraph();

if (isset(
$_POST['start'], $_POST['end'])) {
   
bfs($graph, $_POST['start'], $_POST['end']);
}

function
bfs(Graph $graph, string $start, string $end): void
{
   
$result = $graph->search($start, $end);
    if (
null !== $result->getEndNode()) {
        echo
'It found ' . $result->getEndNode()->getValue() . '-';
    }
   
$path = ' Search path: ';
   
$iMax = count($result->getPath());
    foreach (
$result->getPath() as $i => $n) {
       
$path .= $n->getValue();

        if (
$i < $iMax - 1) {
           
$path .= ' --> ';
        }
    }

    echo
$path . '<br />';
}

?>

<h2>Breadth first search:</h2>
<form id="s" method="post">
    <select name="start">
        <?php if (isset($_POST['start'])): ?>
<option value="<?php echo $_POST['start']; ?>"><?php echo $_POST['start']; ?></option>
        <?php else: ?>
<option value="">Select start value</option>
        <?php endif; ?>

        <?php foreach ($graph->getNodes() as $node): ?>
<option value="<?php echo $node->getValue(); ?>"><?php echo $node->getValue(); ?></option>
        <?php endforeach; ?>
</select>

    <select name="end">
        <?php if (isset($_POST['end'])): ?>
<option value="<?php echo $_POST['end']; ?>"><?php echo $_POST['end']; ?></option>
        <?php else: ?>
<option value="">Select end value</option>
        <?php endif; ?>

        <?php foreach ($graph->getNodes() as $node): ?>
<option value="<?php echo $node->getValue(); ?>"><?php echo $node->getValue(); ?></option>
        <?php endforeach; ?>
</select>
    <input type="submit" name="Submit" value="Search">
</form>




Details

Build Status License: MIT

Data Structure and Algorithms PHP

Build the image

docker image build --tag terdia07/php-7.4 .

Run th container

docker container run -d --name dsa --publish 80:80 --mount type=bind,source="$(pwd)",target=/var/www/html terdia07/php-7.4

Enter the container

docker exec -it dsa bash

Install phpunit and setup autoloading by running:

cd /var/www/html && composer install

Run test:

./vendor/bin/phpunit tests --testdox

Visit: http://localhost





  Files folder image Files (26)  
File Role Description
Files folder image.github (1 directory)
Files folder imagedocker (4 files)
Files folder imagesrc (3 directories)
Files folder imagetests (3 directories)
Accessible without login Plain text file bfs.php Example Example script
Accessible without login Plain text file bst.php Example Example script
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file data.json Data Auxiliary data
Accessible without login Plain text file Dockerfile Data Auxiliary data
Accessible without login Plain text file index.php Aux. Auxiliary script
Accessible without login Plain text file README.md Doc. Documentation

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:48
This week:0
All time:10,755
This week:63Up