1. Go to this page and download the library: Download php-architecture-kit/graph library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
php-architecture-kit / graph example snippets
use PhpArchitecture\Graph\Graph;
use PhpArchitecture\Graph\GraphNavigator;
use PhpArchitecture\Graph\Edge\DirectedEdge;
use PhpArchitecture\Graph\Edge\UndirectedEdge;
use PhpArchitecture\Graph\Vertex\Vertex;
$graph = new Graph();
$a = new Vertex(metadata: ['name' => 'A']);
$b = new Vertex(metadata: ['name' => 'B']);
$c = new Vertex(metadata: ['name' => 'C']);
$graph->vertexStore->addVertex($a);
$graph->vertexStore->addVertex($b);
$graph->vertexStore->addVertex($c);
$ab = new DirectedEdge($a, $b);
$bc = new DirectedEdge($b, $c);
$ac = new UndirectedEdge($a, $c);
$graph->edgeStore->addEdge($ab);
$graph->edgeStore->addEdge($bc);
$graph->edgeStore->addEdge($ac);
$isAdjacent = $graph->edgeStore->areAdjacent($a->id(), $b->id()); // true
$degreeOfB = $graph->edgeStore->degree($b->id()); // 2
$navigator = new GraphNavigator($graph);
$path = $navigator->shortestPathTo($a->id(), $c->id()); // EdgeContext[]
use PhpArchitecture\Graph\Graph;
use PhpArchitecture\Graph\Config\GraphConfig;
use PhpArchitecture\Graph\Edge\DirectedEdge;
use PhpArchitecture\Graph\Edge\UndirectedEdge;
use PhpArchitecture\Graph\Edge\Weight\Config\WeightConfig;
$graph = new Graph(config: new GraphConfig(
allowSelfLoop: false,
allowMultiEdge: false,
allowCyclicEdge: false,
weightConfig: new WeightConfig([
DirectedEdge::class => ['cost' => 1.0, 'latency' => 10.0],
UndirectedEdge::class => ['cost' => 0.5],
]),
));
use PhpArchitecture\Graph\Tools\Navigation\Traversal\VertexVisitorInterface;
use PhpArchitecture\Graph\Tools\Navigation\Traversal\VisitAction;
use PhpArchitecture\Graph\Tools\Navigation\Traversal\VisitResult;
use PhpArchitecture\Graph\Vertex\VertexInterface;
final class CollectVertexIdsVisitor implements VertexVisitorInterface
{
/** @var list<string> */
public array $visited = [];
public function visit(VertexInterface $vertex): VisitResult
{
$this->visited[] = $vertex->id()->toString();
return new VisitResult(VisitAction::Continue);
}
}
$visitor = new CollectVertexIdsVisitor();
$result = $navigator->traverseVertices([$visitor]);
$visitedByVisitor = $result->getByVisitor(CollectVertexIdsVisitor::class);
use PhpArchitecture\Graph\Edge\DirectedEdge;
use PhpArchitecture\Graph\Edge\Weight\EdgeWeights;
use PhpArchitecture\Graph\Edge\Weight\Weight;
$edge = new DirectedEdge($a, $b);
$graph->edgeStore->addEdge(
$edge,
new EdgeWeights($edge->id(), [
'cost' => new Weight('cost', 3.0),
'latency' => new Weight('latency', 25.0),
]),
);
$cost = $navigator->selectEdge($edge->id())->weights()->value('cost');
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.