PHP code example of php-architecture-kit / graph

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\GraphNavigator;
use PhpArchitecture\Graph\Vertex\VertexInterface;

$navigator = new GraphNavigator($graph);

$vertexContext = $navigator->selectVertex($a->id());
$edgeContext = $navigator->selectEdge($ab->id());

$allVertexContexts = $navigator->selectVertices();
$apiVertices = $navigator->selectVertices(
    static fn(VertexInterface $vertex): bool => ($vertex->metadata['type'] ?? null) === 'api',
);

// VertexContext
$neighborContexts = $vertexContext->neighbors();
$incidentEdges = $vertexContext->edges();

// EdgeContext
$isDirected = $edgeContext->isDirected();
$u = $edgeContext->u();
$v = $edgeContext->v();

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\EdgeInterface;

$path = $navigator->shortestPathTo(
    sourceId: $a->id(),
    targetId: $c->id(),
    edgeFilter: static fn(EdgeInterface $edge): bool =>
        ($edge->metadata['blocked'] ?? false) === false,
);

$pathEdgeIds = array_map(
    static fn($context): string => $context->edge->id()->toString(),
    $path,
);

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