nexus-scholar/graph-core
Lightweight PHP 8.2+ graph data-structure for directed/undirected graphs with node/edge attributes, fast adjacency via integer indexing, read-only subgraph views, and exporters for Cytoscape.js JSON, GraphML, and GEXF. Ideal base for graph analytics.
graph-core is a lightweight PHP graph data-structure package for directed and undirected graphs. It provides node and edge attributes, efficient adjacency lookups, subgraph views, and exporters for common graph-visualization formats.
This package is the graph foundation for Nexus Scholar citation-network work. It intentionally stays generic: scholarly concepts such as papers, citations, co-citation, bibliographic coupling, screening, and exports live in nexus-scholar/core, while graph storage, traversal primitives, attributes, and serialization live here.
Use it directly for PHP graph modeling, or pair it with nexus-scholar/graph-algorithms when you need centrality, pathfinding, traversal, components, ordering, or minimum-spanning-tree algorithms.
ext-dom for XML export formats.ext-dom for GraphML and GEXF exportscomposer require nexus-scholar/graph-core
use Mbsoft\Graph\Domain\Graph;
$graph = new Graph(directed: true);
$graph->addNode('A', ['label' => 'Node A']);
$graph->addNode('B', ['label' => 'Node B']);
$graph->addNode('C', ['label' => 'Node C']);
$graph->addEdge('A', 'B', ['weight' => 1.5]);
$graph->addEdge('B', 'C', ['weight' => 2.0]);
$graph->addEdge('C', 'A', ['weight' => 0.5]);
count($graph->nodes()); // 3
count($graph->edges()); // 3
if ($graph->hasEdge('A', 'B')) {
$weight = $graph->edgeAttrs('A', 'B')['weight'];
}
$successors = $graph->successors('A');
$predecessors = $graph->predecessors('C');
use Mbsoft\Graph\Domain\Graph;
$graph = new Graph(directed: false);
$graph->addEdge('A', 'B', ['type' => 'friendship']);
$graph->addEdge('B', 'C', ['type' => 'friendship']);
$graph->hasEdge('A', 'B'); // true
$graph->hasEdge('B', 'A'); // true
$graph->successors('B');
$graph->predecessors('B');
use Mbsoft\Graph\Domain\Graph;
$edges = [
['A', 'B', ['weight' => 1.0]],
['B', 'C', ['weight' => 2.0]],
['C', 'D', ['weight' => 1.5]],
];
$graph = Graph::fromEdgeList($edges, directed: true);
use Mbsoft\Graph\Domain\Graph;
use Mbsoft\Graph\Domain\SubgraphView;
$graph = new Graph();
$graph->addEdge('A', 'B');
$graph->addEdge('B', 'C');
$graph->addEdge('C', 'D');
$subgraph = new SubgraphView($graph, ['A', 'B', 'C']);
$subgraph->nodes();
$subgraph->edges();
$subgraph->hasEdge('C', 'D'); // false
Export for browser visualization with Cytoscape.js:
use Mbsoft\Graph\IO\CytoscapeJsonExporter;
$exporter = new CytoscapeJsonExporter();
$json = $exporter->export($graph);
Export GraphML for tools such as Gephi, yEd, or NetworkX:
use Mbsoft\Graph\IO\GraphMLExporter;
$exporter = new GraphMLExporter();
$xml = $exporter->export($graph);
Export GEXF for Gephi and compatible network-analysis tools:
use Mbsoft\Graph\IO\GexfExporter;
$exporter = new GexfExporter();
$xml = $exporter->export($graph);
Core interfaces and classes include:
GraphInterface: read-only graph operations.MutableGraphInterface: graph operations with mutation methods.ExporterInterface: common export contract.Graph: mutable graph implementation.SubgraphView: efficient filtered graph view.Node and Edge: value objects for graph elements.IndexMap: bidirectional mapping between external node IDs and internal integer indexes.composer test
composer test:coverage
composer analyse
This library is open-sourced software licensed under the MIT license.
How can I help you explore Laravel packages today?