actived/graphphp
Composer 安装命令:
composer require actived/graphphp
包简介
A PHP graph theory package.
README 文档
README
A PHP graph theory package that provides structures and algorithms for working with graphs.
Installation
You can install the package via Composer:
composer require actived/graphphp
Features
Graph
The Graph class provides the foundation for working with undirected graphs in PHP. It includes methods for manipulating nodes, edges, and retrieving various properties of the graph.
Creating a Graph
use GraphPHP\Graph\Graph; use GraphPHP\Node\Node; use GraphPHP\Edge\Edge; $graph = new Graph();
Adding Nodes
$nodeA = new Node('A'); $graph->addNode($nodeA);
Adding Edges
$nodeB = new Node('B'); $edge = new Edge($nodeA, $nodeB); $graph->addEdge($edge);
Removing Nodes and Edges
Nodes and edges can be removed from the graph:
$graph->removeNode($nodeA); $graph->removeEdge($edge);
Neighbors
Retrieve the neighbors of a given node:
$neighbors = $graph->getNeighbors($nodeB);
Adjacency Matrix
Get the adjacency matrix of the graph:
$matrix = $graph->getAdjacencyMatrix();
Checking for Cycles
Determine if the graph contains a cycle:
if ($graph->hasCycle()) { echo "The graph has a cycle."; } else { echo "The graph does not have a cycle."; }
Transitive Closure
Compute the transitive closure of the graph using the Floyd-Warshall algorithm:
$closure = $graph->transitiveClosure();
Shortest Path
Compute the shortest path between two nodes using Dijkstra's algorithm:
$pathInfo = $graph->shortestPathDijkstra($nodeA, $nodeC); echo "Shortest path: " . implode(' -> ', $pathInfo['path']); echo "Cost: " . $pathInfo['cost'];
String Representation
To get a string representation of the graph:
echo $graph;
Note: The Graph class assumes an undirected graph. For directed graphs, refer to the DiGraph class documentation.
DiGraph (Directed Graph)
The DiGraph class extends the base Graph class and represents a directed graph. This means all edges in this graph have a direction, going from a source node to a target node.
Creating a Directed Graph
use GraphPHP\Graph\DiGraph; use GraphPHP\Node\Node; use GraphPHP\Edge\DirectedEdge; $diGraph = new DiGraph();
Adding Directed Edges
Only directed edges can be added to a directed graph:
$nodeA = new Node('A'); $nodeB = new Node('B'); $directedEdge = new DirectedEdge($nodeA, $nodeB); $diGraph->addEdge($directedEdge);
Outgoing Neighbors
Retrieve the outgoing neighbors of a given node:
$outgoingNeighbors = $diGraph->getNeighbors($nodeA);
Predecessors
Retrieve the predecessors (nodes with directed edges pointing to the given node) of a node:
$predecessors = $diGraph->getPredecessors($nodeB);
Bellman-Ford Shortest Path
Compute the shortest path between two nodes using the Bellman-Ford algorithm:
$pathInfo = $diGraph->shortestPathBellmanFord($nodeA, $nodeC); echo "Shortest path: " . implode(' -> ', $pathInfo['path']); echo "Cost: " . $pathInfo['cost'];
Checking for Cycles in Directed Graphs
Determine if the directed graph contains a cycle:
if ($diGraph->hasCycle()) { echo "The directed graph has a cycle."; } else { echo "The directed graph does not have a cycle."; }
Adjacency Matrix for Directed Graphs
Get the adjacency matrix of the directed graph:
$matrix = $diGraph->getAdjacencyMatrix();
Note: The DiGraph class is specific to directed graphs. If you need an undirected graph, refer to the base Graph class documentation.
Directed Acyclic Graphs (DAG)
Create and manipulate directed acyclic graphs.
use GraphPHP\Graph\DAG; use GraphPHP\Node\Node; use GraphPHP\Edge\DirectedEdge; $graph = new DAG(); $nodeA = new Node('A'); $nodeB = new Node('B'); $nodeC = new Node('C'); $graph->addNode($nodeA) ->addNode($nodeB) ->addNode($nodeC) ->addEdge(new DirectedEdge($nodeA, $nodeB, 4)) ->addEdge(new DirectedEdge($nodeB, $nodeC, -6)) ->addEdge(new DirectedEdge($nodeA, $nodeC, 2));
Transitive Reduction
Perform transitive reduction on a DAG.
$graph->transitiveReduction(); echo $graph; // Visual representation of the graph
Topological Sort
Get a topological ordering of the nodes in a DAG.
$order = $graph->topologicalSort(); print_r($order);
Roadmap
- Testing: Implement comprehensive tests for the current functionalities.
- Trees: Introduce tree graph structures.
- Directed Trees: Extend the tree structures to support directed trees.
- Binary Trees: Implement binary tree structures and related algorithms.
Contributing
If you have suggestions or improvements, feel free to submit a pull request or open an issue on the GitHub repository.
License
This package is open-sourced software licensed under the MIT license.
actived/graphphp 适用场景与选型建议
actived/graphphp 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 824 次下载、GitHub Stars 达 6, 最近一次更新时间为 2023 年 09 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 actived/graphphp 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 actived/graphphp 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 824
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-09-29