php简单实现pagerank算法
<?phpheader("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 通过html-table形式完成excel下载的功能实现
php身份证识别orc的方法实现
以上就是php实现pagerank的实例的详细内容。
