美文网首页
计算PageRank

计算PageRank

作者: keon | 来源:发表于2016-08-02 17:36 被阅读0次

    有A,B,C,D,E五个网页,其中
    1)A网页有链接指向B,C,D,E
    2)B网页有链接指向A,D
    3)C网页有链接指向A,D
    4)D网页有链接指向C
    5)E网页有链接指向A,C

    思想:
    1、计算矩阵S
    2、计算矩阵G
    3、根据q=Gq',迭代

    public class PageRank {
        private static final double ALPHA = 0.85;
        private static final double DISTANCE = 0.0000001;
    
        private static final double[][] matrixS = {
                {0, 1 / 2.0, 1 / 2.0, 0, 1 / 2.0},
                {1 / 4.0, 0, 0, 0, 0},
                {1 / 4.0, 0, 0, 1, 1 / 2.0},
                {1 / 4.0, 1 / 2.0, 1 / 2.0, 0, 0},
                {1 / 4.0, 0, 0, 0, 0}
        };
    
        private static final double[][] matrixU = {
                {1.0, 1.0, 1.0, 1.0, 1.0},
                {1.0, 1.0, 1.0, 1.0, 1.0},
                {1.0, 1.0, 1.0, 1.0, 1.0},
                {1.0, 1.0, 1.0, 1.0, 1.0},
                {1.0, 1.0, 1.0, 1.0, 1.0}
        };
    
        public static void main(String[] args) {
    
            System.out.println("S矩阵:");
            printMatrix(matrixS);
    
            double[][] matrixG = calMatrixG();
    
            System.out.println("G矩阵:");
            printMatrix(matrixG);
    
            double[] vector = {1.0, 1.0, 1.0, 1.0, 1.0};
    
            double[] vectorQ = calVectorQ(matrixG, vector);
    
            System.out.println("向量:");
            printVector(vectorQ);
        }
    
        private static void printMatrix(double[][] matrix) {
            for (int i = 0; i < matrix.length; i++) {
                for (int j = 0; j < matrix[i].length; j++) {
    
    //                String result = String.format("%1$10s", matrix[i][j]);
                    String result = String.format("%.5f", matrix[i][j]);
    
                    System.out.print(result + " ");
                }
                System.out.println();
            }
        }
    
        private static void printVector(double[] vector) {
            for (int i = 0; i < vector.length; i++) {
    
                String result = String.format("%.5f", vector[i]);
    
                System.out.print(result + " ");
            }
        }
    
        private static double[][] calMatrixG() {
            return calMatrixPlus(calMatrixMultiDouble(matrixS, ALPHA),
                    calMatrixMultiDouble(matrixU, (1 - ALPHA) * (1.0 / matrixS.length)));
        }
    
        private static double[][] calMatrixMultiDouble(double[][] matrix, double value) {
            for (int i = 0; i < matrix.length; i++) {
                for (int j = 0; j < matrix[i].length; j++) {
                    matrix[i][j] *= value;
                }
    
            }
            return matrix;
        }
    
        private static double[][] calMatrixPlus(double[][] matrix1, double[][] matrix2) {
            for (int i = 0; i < matrix1.length; i++) {
                for (int j = 0; j < matrix1[i].length; j++) {
                    matrix1[i][j] += matrix2[i][j];
                }
    
            }
            return matrix1;
        }
    
        private static double[] calMatrixMultiVetor(double[][] matrix, double[] vetor) {
            double[] tmp = new double[matrix.length];
    
            for (int i = 0; i < matrix.length; i++) {
                for (int j = 0; j < matrix[i].length; j++) {
                    tmp[i] += matrix[i][j] * vetor[j];
                }
            }
    
            return tmp;
        }
    
        private static double calVectorDistance(double[] vector1, double[] vector2) {
            double sum = 0;
    
            for (int i = 0; i < vector1.length; i++) {
                sum += Math.pow(vector1[i] - vector2[i], 2);
            }
            return Math.sqrt(sum);
        }
    
        private static double[] calVectorQ(double[][] matrixG, double[] vector) {
    
            double[] vector2 = calMatrixMultiVetor(matrixG, vector);
    
            double distance = calVectorDistance(vector, vector2);
    
            int n = 1;
    
    //        System.out.print(distance);
    
            vector = vector2;
            while (distance > DISTANCE) {
                n++;
    
                vector2 = calMatrixMultiVetor(matrixG, vector);
    
                distance = calVectorDistance(vector, vector2);
    
                vector = vector2;
    
    //            System.out.println(distance);
            }
    
            System.out.println("计算次数" + n);
    
            return vector2;
        }
    }
    

    输出
    S矩阵:
    0.00000 0.50000 0.50000 0.00000 0.50000
    0.25000 0.00000 0.00000 0.00000 0.00000
    0.25000 0.00000 0.00000 1.00000 0.50000
    0.25000 0.50000 0.50000 0.00000 0.00000
    0.25000 0.00000 0.00000 0.00000 0.00000
    G矩阵:
    0.03000 0.45500 0.45500 0.03000 0.45500
    0.24250 0.03000 0.03000 0.03000 0.03000
    0.24250 0.03000 0.03000 0.88000 0.45500
    0.24250 0.45500 0.45500 0.03000 0.03000
    0.24250 0.03000 0.03000 0.03000 0.03000
    计算次数29
    向量:
    1.21040 0.40721 1.68064 1.29454 0.40721

    相关文章

      网友评论

          本文标题:计算PageRank

          本文链接:https://www.haomeiwen.com/subject/epafsttx.html