Git Product home page Git Product logo

graph-tools-php's Introduction

graph-tools

Packagist PHP Version Support Scrutinizer Code Quality Coverage Status Build and test License: MIT

Tools for working with graphs

How to install to your project

composer require smoren/graph-tools

Unit testing

composer install
composer test-init
composer test

Usage

Working with preloaded graph repository

Basic graph
use Smoren\GraphTools\Models\Edge;
use Smoren\GraphTools\Models\Vertex;
use Smoren\GraphTools\Traverse\Traverse;
use Smoren\GraphTools\Traverse\TraverseDirect;
use Smoren\GraphTools\Traverse\TraverseReverse;
use Smoren\GraphTools\Filters\TransparentTraverseFilter;
use Smoren\GraphTools\Store\PreloadedGraphRepository;
use Smoren\GraphTools\Structs\FilterConfig;

$vertexes = [
    new Vertex(1, 1, null), // id, type, extra data
    new Vertex(2, 1, null),
    new Vertex(3, 1, null),
];
$connections = [
    new Edge(1, 1, 1, 2), // id, type, from id, to id
    new Edge(2, 1, 2, 3),
];

// Creating repository
$repo = new PreloadedGraphRepository($vertexes, $connections);

// Creating direct traverse model
$traverse = new TraverseDirect($repo);
$contexts = $traverse->generate(
    $repo->getVertexById(1),
    new TransparentTraverseFilter([FilterConfig::PREVENT_LOOP_PASS])
);

// Let's go traverse
$vertexIds = [];
foreach($contexts as $context) {
    $vertexIds[] = $context->getVertex()->getId();
}
print_r($vertexIds); // [1, 2, 3]

// Creating reverse traverse model
$traverse = new TraverseReverse($repo);
$contexts = $traverse->generate(
    $repo->getVertexById(3),
    new TransparentTraverseFilter([FilterConfig::PREVENT_LOOP_PASS])
);

// Let's go traverse
$vertexIds = [];
foreach($contexts as $context) {
    $vertexIds[] = $context->getVertex()->getId();
}
print_r($vertexIds); // [3, 2, 1]

$traverse = new Traverse($repo);

// Creating non-directed traverse model
$contexts = $traverse->generate(
    $repo->getVertexById(2),
    new TransparentTraverseFilter([FilterConfig::PREVENT_LOOP_PASS])
);

// Let's go traverse
$vertexIds = [];
$loopsCount = 0;
foreach($contexts as $context) {
    if($context->isLoop()) {
        $contexts->send(Traverse::STOP_BRANCH);
        ++$loopsCount;
    } else {
        $vertexIds[] = $context->getVertex()->getId();
    }
}
print_r($vertexIds); // [2, 3, 1]
var_dump($loopsCount); // 2

// Creating non-directed traverse model with loop prevent control
$contexts = $traverse->generate(
    $repo->getVertexById(2),
    new TransparentTraverseFilter([FilterConfig::PREVENT_LOOP_PASS, FilterConfig::PREVENT_LOOP_HANDLE])
);

// Let's go traverse
$vertexIds = [];
foreach($contexts as $context) {
    $vertexIds[] = $context->getVertex()->getId();
}
print_r($vertexIds); // [2, 3, 1]

Look for more examples in tests.

graph-tools-php's People

Contributors

astrahov90 avatar peter279k avatar smoren avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.