mbsoft31/graph-algorithms
High-performance PHP 8.2+ graph algorithms library (mbsoft/graph-core). Includes PageRank and degree centrality, Dijkstra and A* pathfinding, BFS/DFS traversal, Tarjan SCC, Kahn topological sort, and Prim MST. Type-safe, tested, zero extra deps.
Install the package via Composer (composer require mbsoft/graph-algorithms) and ensure mbsoft/graph-core ^1.0 is also present. Start with simple use cases like computing PageRank centrality or finding shortest paths using Dijkstra—both are demonstrated in the README with clear, minimal setup. Begin by constructing a graph using the mbsoft/graph-core package (the library’s only dependency), then instantiate an algorithm class (e.g., PageRank, Dijkstra) and call its compute() or find() method directly. No heavy configuration is required upfront—algorithms default to sensible values (e.g., damping factor 0.85 for PageRank), making prototyping quick and intuitive.
Leverage the library’s strategy-based design by passing callbacks for custom weight extraction or heuristics (e.g., lambda functions to compute travel time or Euclidean distance). Use AlgorithmGraph implicitly—no manual conversion needed, but understand that algorithms internally convert your graph once for high-performance operations. Common patterns include:
AStar with a heuristic in grid-based games, fallback to Dijkstra when needed, and handle null paths via null checks or try/catch for unreachable targets.CycleDetectedException for topological sorts, handle null returns from Prim for disconnected graphs, and validate edge weights before running Dijkstra.StronglyConnected to decompose a graph into SCCs, then apply TopologicalSort to the condensation DAG.InvalidArgumentException or pre-check weights; fallback to Bellman-Ford (outside this package) if needed.null, not an exception, for disconnected graphs: Explicitly check for null before accessing totalWeight—this avoids unexpected Error exceptions.GraphInterface, but performance relies on the internal AlgorithmGraph conversion. Avoid frequent graph recreations—reuse the base graph or batch operations.SplQueue (O(1) enqueue/dequeue) and DFS is iterative (prevents stack overflow), but large graphs may still strain memory—consider chunking for huge networks.PathfindingAlgorithmInterface, etc.).How can I help you explore Laravel packages today?