PHP code example of mbsoft31 / graph-core
1. Go to this page and download the library: Download mbsoft31/graph-core library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mbsoft31 / graph-core example snippets
use Mbsoft\Graph\Domain\Graph;
// Create a directed graph
$graph = new Graph(directed: true);
// Add nodes with attributes
$graph->addNode('A', ['label' => 'Node A', 'color' => 'red']);
$graph->addNode('B', ['label' => 'Node B', 'color' => 'blue']);
$graph->addNode('C', ['label' => 'Node C', 'color' => 'green']);
// Add edges with weights
$graph->addEdge('A', 'B', ['weight' => 1.5]);
$graph->addEdge('B', 'C', ['weight' => 2.0]);
$graph->addEdge('C', 'A', ['weight' => 0.5]);
// Query the graph
echo count($graph->nodes()); // 3
echo count($graph->edges()); // 3
// Check connections
if ($graph->hasEdge('A', 'B')) {
$weight = $graph->edgeAttrs('A', 'B')['weight'];
echo "Edge A->B has weight: $weight\n";
}
// Get neighbors
$successors = $graph->successors('A'); // ['B']
$predecessors = $graph->predecessors('C'); // ['B']
use Mbsoft\Graph\Domain\Graph;
// Create an undirected graph
$graph = new Graph(directed: false);
$graph->addEdge('A', 'B', ['type' => 'friendship']);
$graph->addEdge('B', 'C', ['type' => 'friendship']);
// In undirected graphs, edges work both ways
$graph->hasEdge('A', 'B'); // true
$graph->hasEdge('B', 'A'); // true (same edge)
// Successors and predecessors are the same (neighbors)
$graph->successors('B'); // ['A', 'C']
$graph->predecessors('B'); // ['A', 'C']
use Mbsoft\Graph\Domain\Graph;
$edges = [
['A', 'B', ['weight' => 1.0]],
['B', 'C', ['weight' => 2.0]],
['C', 'D', ['weight' => 1.5]],
['D', 'A', ['weight' => 3.0]],
];
$graph = Graph::fromEdgeList($edges, directed: true);
use Mbsoft\Graph\Domain\Graph;
use Mbsoft\Graph\Domain\SubgraphView;
// Create a graph
$graph = new Graph();
$graph->addEdge('A', 'B');
$graph->addEdge('B', 'C');
$graph->addEdge('C', 'D');
$graph->addEdge('D', 'E');
// Create a view containing only nodes A, B, and C
$subgraph = new SubgraphView($graph, ['A', 'B', 'C']);
// The view only shows edges within the selected nodes
$subgraph->nodes(); // ['A', 'B', 'C']
$subgraph->edges(); // Only A->B and B->C edges
$subgraph->hasEdge('C', 'D'); // false (D not in view)
// Update node attributes (merge with existing)
$graph->addNode('A', ['new_attr' => 'value']);
// Replace all node attributes
$graph->setNodeAttrs('A', ['only' => 'this']);
// Update edge attributes
$graph->setEdgeAttrs('A', 'B', ['weight' => 5.0, 'label' => 'Strong']);
use Mbsoft\Graph\IO\CytoscapeJsonExporter;
$exporter = new CytoscapeJsonExporter();
$json = $exporter->export($graph);
// Result structure:
// [
// 'elements' => [
// 'nodes' => [
// ['data' => ['id' => 'A', 'label' => 'Node A', ...]],
// ...
// ],
// 'edges' => [
// ['data' => ['source' => 'A', 'target' => 'B', 'weight' => 1.5]],
// ...
// ]
// ]
// ]
file_put_contents('graph.json', json_encode($json));
use Mbsoft\Graph\IO\GraphMLExporter;
$exporter = new GraphMLExporter();
$xml = $exporter->export($graph);
file_put_contents('graph.graphml', $xml);
use Mbsoft\Graph\IO\GexfExporter;
$exporter = new GexfExporter();
$xml = $exporter->export($graph);
file_put_contents('graph.gexf', $xml);
$socialNetwork = new Graph(directed: false);
// Add users
$socialNetwork->addNode('alice', ['name' => 'Alice', 'age' => 28]);
$socialNetwork->addNode('bob', ['name' => 'Bob', 'age' => 32]);
$socialNetwork->addNode('charlie', ['name' => 'Charlie', 'age' => 25]);
// Add friendships
$socialNetwork->addEdge('alice', 'bob', ['since' => '2020']);
$socialNetwork->addEdge('bob', 'charlie', ['since' => '2019']);
// Find friends of friends
$bobsFriends = $socialNetwork->successors('bob'); // ['alice', 'charlie']
$tasks = new Graph(directed: true);
// Add tasks
$tasks->addNode('compile', ['duration' => 30]);
$tasks->addNode('test', ['duration' => 45]);
$tasks->addNode('package', ['duration' => 15]);
$tasks->addNode('deploy', ['duration' => 20]);
// Add dependencies
$tasks->addEdge('compile', 'test');
$tasks->addEdge('test', 'package');
$tasks->addEdge('package', 'deploy');
// Find what needs to be done before deployment
$deployPrereqs = $tasks->predecessors('deploy'); // ['package']
bash
composer analyse