Friday, March 6, 2015

Google's PageRank implementation in Java

Calculating the Google matrix from an adjacency matrix:

public double[][] getGoogleMatrix(int[][] a,double q) {

    int n=a.length;
    double[][] g = new double[n][n];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            g[i][j] = ((q / n) + ((a[j][i] * (1 - q)) / sumOfLine(a[j])));
        }
    }
    return g;
}

Ranking the nodes can be done using the values from the normalized eigenvector of the google matrix:
public double[] normalize(double[] a){

    double s=0;
    for (int i = 0; i < a.length; i++) {
        s+=abs(a[i]);
    }
    double[] n;
    n=multiply(a,1/s);
    for (int i = 0; i < n.length; i++){
        n[i]=(double) round(n[i] * 100000000) / 100000000;
    }
    return n;
}

Power Iteration method to calculate the eigenvector.

This is part of the Matrices project.Refer to Basic matrix calculations in Java for the missing secondary methods.

No comments: