PageRank的php简单实现
2017-12-25 18:11
246 查看
php简单实现PageRank算法
![](https://img-blog.csdn.net/20171225180944227?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvQUlraWxsZXI=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
<?php header("Content-type:text/html; charset=utf-8"); class PageRank { public $map = []; public $rank = []; public $inputList = []; // example web 'a' (has input link): web 'b' public $size; public $keyValue = 0.85; public function __construct(array $map) { $this->map = $map; $this->size = count($this->map); } //init rank score and transform 'map' format to 'inputList' format public function init() { $size = $this->size; foreach ($this->map as $key => $value) { $this->inputList[$key] = []; } foreach ($this->map as $key => $value) { $this->rank[$key] = 1/$size; foreach ($value as $v) { if (empty($this->inputList[$v])) { $this->inputList[$v][] = $key; } else { array_push($this->inputList[$v], $key); } } } } public function caculate() { $tmp = $this->rank; $keyValue = $this->keyValue; $size = $this->size; foreach ($this->inputList as $key => $value) { $score = (1 - $keyValue)/$size; foreach ($value as $v) { $cc = count($this->map[$v]); if ($cc) { $score += ($keyValue*(1/$cc * $this->rank[$v])); } } $tmp[$key] = $score; } $this->rank = $tmp; } } $map = [ 'a' => ['b', 'c', 'd'],// web 'a' (has out link): web 'b', web 'c', web 'd' 'b' => ['a', 'd'], 'c' => ['b'], 'd' => ['b', 'c'], ]; $example = new PageRank($map); $example->init(); echo '<pre>'; for ($i = 0; $i < 10; $i++) { $example->caculate(); var_dump($example->rank); }
相关文章推荐
- php简单实现socket通信
- PHP实现的简单日历代码
- 简单的php实现用户的注册和登入
- php单例模式的简单实现方法
- PHP简单读取PDF页数的实现方法
- 分享php中四种webservice实现的简单架构方法及实例[转载]
- 一致性哈希算法简单分析及php实现
- PHP实现简单顺序栈
- PHP简单实现MVC框架路由功能模式
- PHP和java结合加解密简单实现方法
- php 实现简单爬虫
- php编写的简单页面跳转功能实现代码
- PHP聊天室简单实现
- 微信网页授权(OAuth2.0) PHP 源码简单实现
- PHP实用篇:简单实现URL二维码生成
- [李景山php]php 实现简单的任务分配系统
- [PHP]进阶教程:服务器端嵌入(SSI)实现多个页面之间的引用与简单案例
- 简单的PHP实现倒计时方法
- PHP实现最简单爬虫原型
- PHP版23种设计模式简单的实现