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.
The nexus-scholar/graph-algorithms package is a specialized, high-performance solution for graph-heavy applications, particularly those requiring centrality metrics, pathfinding, or component analysis. Its architecture is optimized for large-scale directed/undirected graphs (1,000+ nodes) with:
AlgorithmGraph proxy: Accelerates algorithm execution by reducing string-based node lookups, but locks into nexus-scholar/graph-core’s data model.PathResult, MstResult): Enables clean integration with Laravel’s type system and service containers.Fit for:
Misalignment:
Stack Compatibility:
require installation with zero Laravel-specific friction.app()->bind(PathfindingAlgorithmInterface::class, fn() => new AStar(...))).Migration Path:
// Before
$path = customDijkstra($graph, 'A', 'B');
// After
$dijkstra = app(Dijkstra::class);
$path = $dijkstra->find($graph, 'A', 'B');
nexus-scholar/graph-core if not already using it. Leverage AlgorithmGraph for performance-critical paths.GraphInterface (e.g., string node IDs, array edge attributes).Compatibility Caveats:
nexus-scholar/graph-core ^1.0. If your project uses a different graph library (e.g., undergraph/undergraph), evaluate adapter costs.$attrs['distance']). Custom attribute formats need mapping logic.AlgorithmGraph for computation).| Risk | Impact | Mitigation |
|---|---|---|
| Performance Overhead | Medium | Benchmark AlgorithmGraph conversion vs. raw graph operations. Cache proxies for repeated use. |
| Dependency Bloat | High | Audit graph-core for Laravel conflicts (e.g., service provider collisions). Use composer why to trace dependencies. |
| Error Handling Gaps | Medium | Wrap algorithm calls in try-catch blocks; normalize exceptions to Laravel’s Problem contracts. |
| Memory Leaks | High (Large Graphs) | Monitor SplQueue/SplStack usage in traversal algorithms. Implement GC hooks for long-running processes. |
| Laravel Service Container | Low | Register algorithms as contextual bindings to avoid singleton pitfalls (e.g., shared AlgorithmGraph state). |
| Graph Data Model Lock-in | Critical | Design adapter interfaces early to abstract GraphInterface (e.g., GraphAdapter trait). |
| Algorithm Limitations | Medium | Extend interfaces (e.g., PathfindingAlgorithmInterface) for custom logic (e.g., bidirectional Dijkstra). |
Key Questions for the TPM:
nexus-scholar/graph-core’s GraphInterface? If not, what’s the adapter effort?nexus-scholar/graph-core actively maintained? What’s the deprecation policy for breaking changes?The package is optimized for Laravel/PHP 8.2+ environments with the following integrations:
| Laravel Component | Integration Strategy | Example |
|---|---|---|
| Service Container | Register algorithms as contextual bindings with typed interfaces. Use tagging to group related algorithms (e.g., pathfinding). |
```php |
// config/services.php
'graph.algorithms' => [
'pathfinding' => [
'dijkstra' => Dijkstra::class,
'astar' => AStar::class,
],
];
``` |
| Dependency Injection | Inject PathfindingAlgorithmInterface into controllers/services. Use constructor injection for testability. | php public function __construct( private PathfindingAlgorithmInterface $pathfinder ) {} |
| Queue Workers | Offload heavy computations (e.g., PageRank on large graphs) to Laravel Queues. Serialize graphs to JSON for inter-process communication. | php // Job public function handle(): void { $graph = Graph::fromJson($this->graphJson); $scores = (new PageRank())->compute($graph); // Store results... } |
| Testing | Leverage Pest for algorithm unit tests. Mock GraphInterface to isolate logic. | php it('computes shortest path', function () { $graph = Mockery::mock(GraphInterface::class); $graph->shouldReceive('edgesForNode')->andReturn([...]); $dijkstra = new Dijkstra(); $result = $dijkstra->find($graph, 'A', 'B'); expect($result)->not->toBeNull(); }); |
| API Routes | Expose algorithms via Laravel API Resources or GraphQL (e.g., shortestPath mutation). | php // routes/api.php Route::post('/graph/path', [PathController::class, 'findPath']); |
| Database Storage | Store graphs as JSON columns (PostgreSQL) or serialized blobs. Use graph-core’s export/import for migration. | php // Migration Schema::table('graphs', function (Blueprint $table) { $table->json('nodes')->nullable(); $table->json('edges')->nullable(); }); |
Assessment Phase (1–2 weeks)
microtime for 1,000-node graphs).GraphInterface.Pilot Phase (2–4 weeks)
nexus-scholar/graph-core to test dependency conflicts.How can I help you explore Laravel packages today?