首页 > 代码库 > MapReduce原理——PageRank算法Java版
MapReduce原理——PageRank算法Java版
Page Rank就是MapReduce的来源,下文是一个简单的计算PageRank的示例。
import java.text.DecimalFormat; /** * Created by jinsong.sun on 2014/7/15. */ public class PageRankCaculator { public static void main(String[] args) { double[][] g = calcG(genS(), 0.85); double[] q = genQ(); int i = 0; while (i++ < 100000) { q = calcQ(g, q); printQString(q); } } public static double[][] genS() { double[] linkA = {0.00, 0.50, 0.50, 0.00, 0.50}; double[] linkB = {0.25, 0.00, 0.00, 0.00, 0.00}; double[] linkC = {0.25, 0.00, 0.00, 1.00, 0.50}; double[] linkD = {0.25, 0.50, 0.50, 0.00, 0.00}; double[] linkE = {0.25, 0.00, 0.00, 0.00, 0.00}; return new double[][]{linkA, linkB, linkC, linkD, linkE}; } public static double[] genQ() { return new double[] {1.00, 1.00, 1.00, 1.00, 1.00}; } /** * 计算G矩阵。公式:G = α*S + (1-α)*(1/n)*U * * @param s 原始矩阵 * @param alpha 权重 * @return G矩阵 */ public static double[][] calcG(double[][] s, double alpha) { int size = 5; //all one matrix double[][] u = {{1.00, 1.00, 1.00, 1.00, 1.00}, {1.00, 1.00, 1.00, 1.00, 1.00} , {1.00, 1.00, 1.00, 1.00, 1.00}, {1.00, 1.00, 1.00, 1.00, 1.00} , {1.00, 1.00, 1.00, 1.00, 1.00}}; //计算a*S double[][] m1 = new double[size][size]; for (int i = 0; i < s.length; i++) { for (int j = 0; j < s[i].length; j++) { m1[i][j] = s[i][j] * alpha; } } //(1-α)*(1/n)*U double[][] m2 = new double[size][size]; for (int i = 0; i < u.length; i++) { for (int j = 0; j < u[i].length; j++) { DecimalFormat df = new DecimalFormat("#.0000"); m2[i][j] = Double.parseDouble(df.format((1.0 - alpha) * (1.0 / size) * u[i][j])); } } //G = α*S + (1-α)*(1/n)*U double[][] m3 = new double[size][size]; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { m3[i][j] = m1[i][j] + m2[i][j]; DecimalFormat df = new DecimalFormat("#.0000"); m3[i][j] = Double.parseDouble(df.format(m3[i][j])); } } return m3; } /** * 计算特征向量。公式:q_next = G * q_curr * * @param g G矩阵 * @param q 特征向量 * @return */ public static double[] calcQ(double[][] g, double[] q) { double[] qNext = new double[g.length]; for (int i = 0; i < g.length; i++) { for (int j = 0; j < g[i].length; j++) { qNext[i] += g[i][j] * q[j]; } } return qNext; } public static void printQString(double[] m) { String s = "{ {:p00}, {:p10}, {:p20}, {:p30}, {:p40} }"; for (int i = 0; i < 5; i++) { s = s.replace(":p" + i + "0", String.valueOf(m[i])); } System.out.println(s); } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。