Code Monkey home page Code Monkey logo

graph's Introduction

graph

A high-level abstraction of Dijkstra's algorithm in PHP

This code has been extracted from doctrine/orientdb-odm, which has been marked inactive, and stripped down to the bare essentials.

The original LICENSE file has been retained, unchanged.

Below is a slightly modified snippet from the original README that demonstrates Graph's usage:

<?php

require "vendor/graph/autoloader.php";

use Sixpeteunder\Graph\Graph;
use Sixpeteunder\Graph\Vertex;
use Sixpeteunder\Graph\Algorithm\Dijkstra;

$graph = new Graph();

$rome = new Vertex('Rome');
$paris = new Vertex('Paris');
$london = new Vertex('London');

$rome->connect($paris, 2);
$rome->connect($london, 3);
$paris->connect($london, 1);

$graph->add($rome);
$graph->add($paris);
$graph->add($london);

$algorithm = new Dijkstra($graph);
$algorithm->setStartingVertex($rome);
$algorithm->setEndingVertex($london);

var_dump($algorithm->solve());

And here is a slightly more practical example of my own:

<?php

require "vendor/graph/autoloader.php";

use Sixpeteunder\Graph\Vertex;
use Sixpeteunder\Graph\Graph;
use Sixpeteunder\Graph\Algorithm\Dijkstra;

// Dummy points
$pointList = [
    (object) ['name' => 'Rome', 'latitude' => -0.212354, 'longitude' => 12.233423],
    (object) ['name' => 'Paris', 'latitude' => -0.316739, 'longitude' => 46.096325],
    (object) ['name' => 'London', 'latitude' => -0.544479, 'longitude' => 31.965335],
];

/** 
 * @param mixed $pointA
 * @param mixed $pointB
 * @return int
 * @todo Replace this with actual distance function 
 */
function getDistanceBetween($pointA, $pointB)
{
    return random_int(1, 10);
}

/**
 * Gets shortest path from a starting point to a finishing point.
 * 
 * @param array $points 
 * @param int $startingPointIndex
 * @param int $finishingPointIndex
 * @return Dijkstra
 */
function getShortestPath(array $points, int $startingPointIndex, int $finishingPointIndex)
{
    $graph = new Graph();

    $vertices = [];
    foreach ($points as $point) array_push($vertices, new Vertex($point->name));

    $count = count($vertices);
    for ($i = 0; $i < $count - 1; $i++)
        for ($j = $i + 1; $j < $count; $j++)
            $vertices[$i]->connect($vertices[$j], getDistanceBetween($points[$i], $points[$j]));

    foreach ($vertices as $vertex) $graph->add($vertex);

    $algorithm = new Dijkstra($graph);
    $algorithm->setStartingVertex($vertices[$startingPointIndex]);
    $algorithm->setEndingVertex($vertices[$finishingPointIndex]);

    $algorithm->solve();
    return $algorithm;
}

$result = getShortestPath($pointList, 0, 2);
var_dump($result->getShortestPath());
echo $result->getLiteralShortestPath();
echo "Distance is: " . $result->getDistance();

graph's People

Watchers

 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.