mbsoft31/graph-algorithms
PHP 8.2+ graph algorithms built on nexus-scholar/graph-core. Includes PageRank and degree centrality, Dijkstra and A* shortest paths, BFS/DFS traversal, Tarjan SCC, topological sort with cycle detection, and minimum spanning tree utilities via typed APIs.
graph-algorithms is a PHP package of graph algorithms built on nexus-scholar/graph-core. It provides centrality, pathfinding, traversal, component analysis, topological ordering, and minimum-spanning-tree algorithms through typed, reusable APIs.
This package is the algorithm layer for Nexus Scholar citation-network analysis. graph-core owns graph storage and export primitives; graph-algorithms owns reusable graph computations; nexus-scholar/core applies those capabilities to scholarly workflows such as citation graphs, co-citation analysis, bibliographic coupling, snowballing, and review exports.
The package is generic enough to use outside research tooling, but its main public value is showing that Nexus Scholar's graph work is package-quality PHP, not only application code.
AlgorithmGraph proxy for efficient algorithm execution.PathResult and MstResult.nexus-scholar/graph-core ^1.0composer require nexus-scholar/graph-algorithms
use Mbsoft\Graph\Algorithms\Centrality\PageRank;
$pagerank = new PageRank(
dampingFactor: 0.85,
maxIterations: 100,
tolerance: 1e-6,
);
$scores = $pagerank->compute($graph);
arsort($scores);
use Mbsoft\Graph\Algorithms\Pathfinding\Dijkstra;
$dijkstra = new Dijkstra();
$path = $dijkstra->find($graph, 'start', 'destination');
if ($path !== null) {
$nodes = $path->nodes;
$cost = $path->cost;
}
use Mbsoft\Graph\Algorithms\Pathfinding\AStar;
$astar = new AStar(
heuristicCallback: fn (string $from, string $to): float => manhattanDistance($from, $to),
);
$path = $astar->find($graph, 'start', 'destination');
use Mbsoft\Graph\Algorithms\Traversal\Bfs;
use Mbsoft\Graph\Algorithms\Traversal\Dfs;
$bfsOrder = (new Bfs())->traverse($graph, 'startNode');
$dfsOrder = (new Dfs())->traverse($graph, 'startNode');
use Mbsoft\Graph\Algorithms\Components\StronglyConnected;
$components = (new StronglyConnected())->findComponents($graph);
Pathfinding algorithms can read weights from arbitrary edge attributes:
use Mbsoft\Graph\Algorithms\Pathfinding\Dijkstra;
$distanceOptimized = new Dijkstra(
fn (array $attrs, string $from, string $to): float => $attrs['distance'] ?? 1.0,
);
Algorithms expose failure states through typed returns or meaningful exceptions depending on the operation:
null when no path exists.AlgorithmGraph: optimized integer-indexed graph proxy used during algorithm execution.IndexMap: bidirectional string-to-integer mapping.CentralityAlgorithmInterface: centrality algorithm contract.PathfindingAlgorithmInterface: pathfinding contract returning PathResult.TraversalAlgorithmInterface: traversal contract.PathResult: immutable shortest-path result.MstResult: minimum-spanning-tree result.composer test
composer stan
composer bench
This library is open-sourced software licensed under the MIT license.
How can I help you explore Laravel packages today?