fisharebest/algorithm
Composer 安装命令:
composer require fisharebest/algorithm
包简介
Implementation of standard algorithms in PHP.
关键字:
README 文档
README
fisharebest/algorithm
General purpose algorithms in PHP
- Dijkstra
- Myers’ diff
- Connected/unconnected components of a graph
Installation
Install using composer.
composer require fisharebest/algorithm
Dijkstra
Dijkstra's algorithm finds the shortest path(s) between two nodes in a weighted, directed graph.
Graphs are specified as an array of edges, each with a cost. The example below is an undirected graph (i.e. if D→E is 9, then E→D is also 9.), because it is easy to understand and easy to draw. However, the algorithm works equally well for directed graphs, where links can be one-way only or have different costs in each direction.
D---9---E
/ \ \
14 2 6
/ \ \
A---9---B--11--C
\ / /
7 10 /
\ / /
F-----15 G
Sample code for the above graph.
$graph = array( 'A' => array('B' => 9, 'D' => 14, 'F' => 7), 'B' => array('A' => 9, 'C' => 11, 'D' => 2, 'F' => 10), 'C' => array('B' => 11, 'E' => 6, 'F' => 15), 'D' => array('A' => 14, 'B' => 2, 'E' => 9), 'E' => array('C' => 6, 'D' => 9), 'F' => array('A' => 7, 'B' => 10, 'C' => 15), 'G' => array(), ); $algorithm = new \Fisharebest\Algorithm\Dijkstra($graph); // There can be zero, one or more shortest (i.e. same total cost) paths. // No shortest path. $path = $algorithm->shortestPaths('A', 'G'); // array() // Exactly one shortest path. $path = $algorithm->shortestPaths('A', 'E'); // array(array('A', 'B', 'D', 'E')) // Multiple solutions with the same shortest path. $path = $algorithm->shortestPaths('E', 'F'); // array(array('E', 'D', 'B', 'F'), array('E', 'C', 'F')) // To find next-shortest paths, exclude one or more intermediate nodes from the shortest path. $path = $algorithm->shortestPaths('A', 'E'); // array(array('A', 'B', 'D', 'E')) $path = $algorithm->shortestPaths('A', 'E', array('B')); // array(array('A', 'B', 'D', 'E')) $path = $algorithm->shortestPaths('A', 'E', array('D')); // array(array('A', 'B', 'C', 'E')) $path = $algorithm->shortestPaths('A', 'E', array('B', 'D')); // array(array('A', 'F', 'C', 'E'))
Myers’ diff
Find the difference between two sequences of tokens (characters, words, lines, etc.) using “An O(ND) Difference Algorithm and Its Variations” by Eugene W. Myers.
The output can be interpreted as either:
- A series of instructions to transform the first sequence into the second sequence.
- A list of matches (tokens that appear in both sequences) and mismatches (tokens that appear in just one sequence).
$x = array('a', 'b', 'c', 'a', 'b', 'b', 'a'); $y = array('c', 'b', 'a', 'b', 'a', 'c'); $algorithm = new MyersDiff; $diff = $algorithm->calculate($x, $y); // array( // array('a', MyersDiff::DELETE), i.e. 'a' occurs only in $x // array('b', MyersDiff::DELETE), i.e. 'b' occurs only in $x // array('c', MyersDiff::KEEP), i.e. 'c' occurs both $x and $y // array('b', MyersDiff::INSERT), i.e. 'b' occurs only in $y // array('a', MyersDiff::KEEP), i.e. 'a' occurs in both $x and $y // array('b', MyersDiff::KEEP), i.e. 'b' occurs in both $x and $y // array('b', MyersDiff::DELETE), i.e. 'b' occurs only in $x // array('a', MyersDiff::KEEP), i.e. 'a' occurs in both $x and $y // array('c', MyersDiff::INSERT), i.e. 'c' occurs only in $y // );
Connected components
A depth-first search of a graph to find isolated groups of nodes.
D E
/ \ \
/ \ \
A-----B C
\ /
\ /
F
Sample code for the above graph
$graph = array( 'A' => array('B' => 1, 'D' => 1, 'F' => 1), 'B' => array('A' => 1, 'D' => 1, 'F' => 1), 'C' => array('E' => 1), 'D' => array('A' => 1, 'B' => 1), 'E' => array('C' => 1), 'F' => array('A' => 1, 'B' => 1), ); $algorithm = new \Fisharebest\Algorithm\ConnectedComponent($graph); $components = $algorithm->findConnectedComponents()); // array( // 1 => array('A', 'B', 'D', 'F'), // 2 => array('C', 'E'), // );
fisharebest/algorithm 适用场景与选型建议
fisharebest/algorithm 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 110.08k 次下载、GitHub Stars 达 71, 最近一次更新时间为 2015 年 02 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「diff」 「Algorithm」 「dijkstra」 「myers」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 fisharebest/algorithm 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 fisharebest/algorithm 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 fisharebest/algorithm 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Common mathematical graph algorithms implemented in PHP
A library for comparing two HTML files/snippets and highlighting the differences using simple HTML.
Schema Diff: Show difference between MySQL databases
An ID Generator for PHP based on Snowflake Algorithm (Twitter announced).
Path finder algorithm
统计信息
- 总下载量: 110.08k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 72
- 点击次数: 35
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0-or-later
- 更新时间: 2015-02-15