Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Graph Algorithms Laravel Package

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.

View on GitHub
Deep Wiki
Context7

nexus-scholar/graph-algorithms

PHP Version License Latest Version on Packagist GitHub Tests Action Status Total Downloads

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.

Role In Nexus Scholar

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.

Features

  • PageRank and degree centrality.
  • Dijkstra and A* pathfinding.
  • Breadth-first and depth-first traversal.
  • Strongly connected components using Tarjan's algorithm.
  • Topological sort with cycle detection.
  • Minimum spanning tree support.
  • Integer-indexed AlgorithmGraph proxy for efficient algorithm execution.
  • Typed value objects such as PathResult and MstResult.
  • Pest test coverage and canonical fixtures.

Requirements

  • PHP 8.2+
  • nexus-scholar/graph-core ^1.0

Installation

composer require nexus-scholar/graph-algorithms

Quick Start

PageRank

use Mbsoft\Graph\Algorithms\Centrality\PageRank;

$pagerank = new PageRank(
    dampingFactor: 0.85,
    maxIterations: 100,
    tolerance: 1e-6,
);

$scores = $pagerank->compute($graph);
arsort($scores);

Shortest Path

use Mbsoft\Graph\Algorithms\Pathfinding\Dijkstra;

$dijkstra = new Dijkstra();
$path = $dijkstra->find($graph, 'start', 'destination');

if ($path !== null) {
    $nodes = $path->nodes;
    $cost = $path->cost;
}

A* With A Heuristic

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');

Traversal

use Mbsoft\Graph\Algorithms\Traversal\Bfs;
use Mbsoft\Graph\Algorithms\Traversal\Dfs;

$bfsOrder = (new Bfs())->traverse($graph, 'startNode');
$dfsOrder = (new Dfs())->traverse($graph, 'startNode');

Strongly Connected Components

use Mbsoft\Graph\Algorithms\Components\StronglyConnected;

$components = (new StronglyConnected())->findComponents($graph);

Custom Weights

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,
);

Error Handling

Algorithms expose failure states through typed returns or meaningful exceptions depending on the operation:

  • Dijkstra rejects negative weights.
  • Topological sort reports cycles.
  • Minimum spanning tree returns no result for disconnected graphs.
  • Pathfinding returns null when no path exists.

Architecture

  • 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.

Testing

composer test
composer stan
composer bench

Use Cases

  • Citation-network analysis.
  • Scholarly influence and discovery graphs.
  • Dependency resolution and build ordering.
  • Workflow orchestration.
  • Route planning and logistics.
  • Infrastructure and network analysis.
  • Game pathfinding and level connectivity.

See Also

License

This library is open-sourced software licensed under the MIT license.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
cadot.eu/make
besmartand-pro/php-quality-config
sentix/ai-chatbot
codifyo/ts-generator-bundle
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky