有A,B,C,D,E五個(gè)網(wǎng)頁(yè)鼻吮,其中
1)A網(wǎng)頁(yè)有鏈接指向B,C,D,E
2)B網(wǎng)頁(yè)有鏈接指向A,D
3)C網(wǎng)頁(yè)有鏈接指向A,D
4)D網(wǎng)頁(yè)有鏈接指向C
5)E網(wǎng)頁(yè)有鏈接指向A,C
思想:
1娄柳、計(jì)算矩陣S
2聘殖、計(jì)算矩陣G
3蛤高、根據(jù)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("計(jì)算次數(shù)" + 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
計(jì)算次數(shù)29
向量:
1.21040 0.40721 1.68064 1.29454 0.40721