graphp/graph
Graphp/graph is a PHP graph data structure library for building and traversing graphs of vertices and edges. Create directed or undirected graphs, attach attributes, and run common algorithms like shortest paths, cycles, and connectivity for analysis and visualization.
## Getting Started
### **Minimal Setup**
1. **Installation**
```bash
composer require graphp/graph
First Use Case: Creating a Graph
use Graphp\Graph\Graph;
$graph = new Graph();
$graph->addVertex('A'); // String IDs work reliably (fixed in v0.9.3)
$graph->addVertex('B');
$graph->addEdge('A', 'B', ['weight' => 5]);
Graph (base graph structure)Vertex (nodes)Edge (connections)Graph\Directed / Graph\Undirected (specialized graphs)Where to Look First
src/Graph/ for core classes (no structural changes).tests/ for usage examples (now CI-verified via GitHub Actions).Building a Graph (PHP 8.1 Optimized)
$graph = new Graph\Directed();
$graph->addVertex('Node1', ['data' => 'custom']); // String IDs now auto-generate correctly
$graph->addEdge('Node1', 'Node2', ['weight' => 3]);
$graph->addEdge('A', 'B', weight: 5, label: 'fast');
Traversal Algorithms (Unchanged)
$bfs = new Graph\Traversal\BreadthFirstSearch($graph, 'A');
$bfs->run();
$path = $bfs->getPathTo('B');
match expressions for traversal state handling:
match ($vertex->getId()) {
'A' => $this->processStart(),
default => $this->processNode($vertex),
};
Graph Analysis (Unchanged)
$components = (new Graph\Analysis\ConnectedComponents($graph))->run();
Serialization (Unchanged)
$serializer = new Graph\IO\Graph6($graph);
$serialized = $serializer->serialize(); // Now PHP 8.1-optimized
Laravel Integration (Updated for PHP 8.1)
// config/app.php (PHP 8.1 syntax)
'graph' => Graph\Directed::class,
// Controller (constructor property promotion)
public function __construct(
private Graph $graph
) {}
Vertex ID Generation (Fixed in v0.9.3)
'A') now auto-generate correctly. Avoid numeric-only IDs if using strings.$graph->addVertex('string_id');
$graph->addVertex(123); // Works, but strings are now preferred for clarity.
PHP 8.1 Breaking Changes
null comparisons may behave differently with PHP 8.1’s strict typing.if ($vertex->getData() === null) { /* ... */ }
Edge Cases in Traversal (Unchanged)
Dijkstra):
foreach ($graph->edges() as $edge) {
assert(is_numeric($edge['weight']), 'Weight must be numeric');
}
Performance (Unchanged)
Graph\Traversal\IterativeDFS and PHP 8.1’s JIT:
$dfs = new IterativeDFS($graph, 'A');
$dfs->run(); // Faster with PHP 8.1 JIT enabled
Inspect Graph Structure (PHP 8.1)
dump(
vertices: $graph->vertices(),
edges: $graph->edges()
);
Visualization (Unchanged)
GraphML with PHP 8.1’s Stringable interface:
$serializer = new Graph\IO\GraphML($graph);
file_put_contents('graph.graphml', $serializer->serialize()->__toString());
Logging (PHP 8.1)
$dfs->setCallback(fn (Vertex $vertex) => logger()->info("Visited: {$vertex->getId()}"));
Custom Traversals (PHP 8.1)
class MyTraversal extends AbstractTraversal {
public function __construct(Graph $graph, string $startVertex) {
parent::__construct($graph, $startVertex);
}
// Use PHP 8.1’s constructor property promotion
}
Graph Serializers (Unchanged)
SerializerInterface with PHP 8.1’s Stringable:
class JSONSerializer implements SerializerInterface, Stringable {
public function __toString(): string { /* ... */ }
}
Laravel Integration (PHP 8.1)
$graph = new Graph();
foreach (User::all() as $user) {
$graph->addVertex($user->id, ['user' => $user]); // PHP 8.1’s union types work here
}
whereIn:
$vertices = Vertex::whereIn('id', $graph->vertices())->get();
PHP 8.1-Specific Features
#[Attribute]
class VertexWeight {
public function __construct(public int $weight) {}
}
// Usage in custom Vertex class
#[VertexWeight(5)]
class WeightedVertex extends Vertex {}
class ImmutableVertex extends Vertex {
public function __construct(
public readonly string $id,
array $data = []
) {
parent::__construct($id, $data);
}
}
NO_UPDATE_NEEDED would **not** apply here—this release introduces meaningful changes (PHP 8.1 support, fixed vertex ID generation) that warrant updates to the **Getting Started**, **Implementation Patterns**, and **Gotchas** sections. The above reflects the revised assessment.
How can I help you explore Laravel packages today?