# 从赌钱游戏看PageRank算法

double x1=1.0,x2=1.0,x3=1.0;

double x2 income =x1/2.0;
double x3
income =x1/2.0+x2;
double x1_ income =x3;

// Gamble单机程序
public class Gamble
{
public static double x1=1.0,x2=1.0,x3=1.0;

public static void playgame(){
double x2_income=x1/2.0;        double x3_income=x1/2.0+x2;        double x1_income=x3;        x1=x1_income;        x2=x2_income;        x3=x3_income;        System.out.println("x1:"+x1+", x2:"+x2+", x3:"+x3); }                public static void main(String[] args){        for(int i=0;i<500;i++){        System.out.print("第"+i+"轮 ");        playgame();        } } }

x1:1.2000000000000002, x2:0.6000000000000001, x3:1.2000000000000002
…...

double x2_income=x1/2.0+x3/2.0;
double x3_income=x1/2.0+x2;
double x1_income=x3/2.0;

x1:0.6666666666666667, x2:1.0, x3:1.3333333333333333

double x2_income=x1/2.0+x3/2.0;
double x3_income=x1/2.0+x2;
double x1_income=0;

A包含B和C的链接
B包含C的链接
C包含A的链接

PR(B)=PR(A)/2;
PR(B)=PR(A)/2+PR(C);
PR(A)=PR(C);

PR(x1)+ PR(x2)+ …+PR(xn)
=( (PR(x2)/ L(x2)+ … )q+1-q) + … + ( (PR(x1)/ L(x1)+ … )q+1-q)
=(PR(x1) L(x1)/L(x1) + PR(x2) L(x2)/L(x2) + … + PR(xn) L(xn)/L(xn))q + n(1-q)
=( PR(x1) + PR(x2) + … + PR(xn))
q + n - nq
=n
q + n – n*q
= n

#### 程序实现：

PageRankWorker：是一个PageRank工人实现，为了方便演示，它通过一个字符串数组代表包括的链接（实际上应该从本地网页文件里获取）

pagepr = pagepr+(Double)prwh.getObj(page);

#### 运行步骤：

1、  启动ParkServerDemo（它的IP端口已经在配置文件指定）

java -cp fourinone.jar; ParkServerDemo

2、运行A、B、C三个PageRankWorker，传入不同的IP和端口号
java  -cp fourinone.jar; PageRankWorker localhost 2008 A

java  -cp fourinone.jar; PageRankWorker localhost 2009 B

java  -cp fourinone.jar; PageRankWorker localhost 2010 C

3、运行PageRankCtor
java -cp fourinone.jar; PageRankCtor

// ParkServerDemo
import com.fourinone.BeanContext;
public class ParkServerDemo{
public static void main(String[] args){
BeanContext.startPark();
}
} // PageRankWorker import com.fourinone.MigrantWorker; import com.fourinone.WareHouse; import com.fourinone.Workman;public class PageRankWorker extends MigrantWorker{        public String page = null;        public String[] links = null;        public PageRankWorker(String page, String[] links){               this.page = page;               this.links = links;        }              public WareHouse doTask(WareHouse inhouse) {               Double pr = (Double)inhouse.getObj(page);               System.out.println(pr);               WareHouse outhouse = new WareHouse();               for(String p:links)                   outhouse.setObj(p, pr/links.length);//对包括的链接PR投票                  return outhouse;        }        public static void main(String[] args){               String[] links = null;               if(args[2].equals("A"))                   links = new String[]{"B","C"};//A页面包括的链接               else if(args[2].equals("B"))                   links = new String[]{"C"};               else if(args[2].equals("C"))                   links = new String[]{"A"};                                               PageRankWorker mw = new PageRankWorker(args[2],links);               mw.waitWorking(args[0],Integer.parseInt(args[1]),"pagerankworker");         } }// PageRankCtor import com.fourinone.Contractor; import com.fourinone.WareHouse; import com.fourinone.WorkerLocal; import java.util.Iterator;public class PageRankCtor extends Contractor{        public WareHouse giveTask(WareHouse inhouse){               WorkerLocal[] wks = getWaitingWorkers("pagerankworker");               System.out.println("wks.length:"+wks.length);                                    for(int i=0;i<500;i++){//500轮                   WareHouse[] hmarr = doTaskBatch(wks, inhouse);                   WareHouse prwh = new WareHouse();                       for(WareHouse result:hmarr){                           for(Iterator iter=result.keySet().iterator();iter.hasNext();){                               String page = (String)iter.next();                               Double pagepr = (Double)result.getObj(page);                               if(prwh.containsKey(page))                                   pagepr = pagepr+(Double)prwh.getObj(page);                                   prwh.setObj(page,pagepr);                             }                         }                    inhouse = prwh;//迭代                    System.out.println("No."+i+":"+inhouse);                 }                 return inhouse;               }                public static void main(String[] args){              PageRankCtor a = new PageRankCtor();              WareHouse inhouse = new WareHouse();              inhouse.setObj("A",1.00d);//A的pr初始值              inhouse.setObj("B",1.00d);//B的pr初始值              inhouse.setObj("C",1.00d);//C的pr初始值              a.giveTask(inhouse);              a.exit();        } }

• 本文已收录于以下专栏：

## 从赌钱游戏看pagerank算法

http://www.infoq.com/cn/articles/gamble-and-pagerank
• echojeison
• 2013年04月16日 15:30
• 525

• joshualiunsw
• 2016年08月08日 23:52
• 837

## 机器学习（十九）——PageRank算法, KNN, loss function详解

• antkillerfarm
• 2017年05月22日 09:50
• 746

## PageRank算法和HITS算法

• wuzhongdehua1
• 2016年03月09日 15:24
• 1314

## PageRank算法的实现一

PageRank算法的思想比较容易理解，基于被许多好的网页链接的网页一定是好的网页的思想。 PageRank算法 用邻接矩阵来表示网页的连接状态。 矩阵A=(1-d)*q+d*p; A：网页的...
• lskyne
• 2012年12月29日 14:34
• 5067

## pagerank算法的Matlab實現

• DS_agent
• 2016年01月18日 22:43
• 1722

## PageRank算法我的java代码的实现

• qq_26468069
• 2015年04月08日 17:16
• 1482

## PageRank算法原理详解

﻿﻿ 转自：http://blog.csdn.net/hguisu/article/details/7996185 1. PageRank算法概述          PageRank,即网页排名...
• u014731752
• 2015年04月23日 17:49
• 2056

## PageRank算法（python实现）

Python 实现的PageRank算法，纯粹使用python原生模块，没有使用numpy、scipy。
• nersleGmail
• 2015年03月02日 21:46
• 2840