Last active
May 5, 2017 08:48
-
-
Save philippfranke/d7a374e9dc3ebaae5cce0fc36125a206 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function [ rankings ] = PageRank(a, d, epsilon ) | |
%PAGERANK Computes pagerank, returned as a numeric array. | |
% A is an adjacent sparse matrix representing a graph | |
% d is the dumping factor | |
% epsilon defines the threshold to stop | |
g = digraph(a); | |
out = outdegree(g); | |
n = size(out,1); | |
% Transform adjacency matrix according to WebScience V2, p. 154 | |
% Approaches: | |
% * Loop(in-place calculation) needed approx. 200 hours to finish | |
% * Built-in PageRank does not return expected result | |
% * The following approach could work. | |
a = (spdiags(1./out,0,n,n) * a).'; | |
% Get rid off NaN | |
a(isnan(a)) = 0; | |
% Replace zero rows with 1/n. | |
a(all(a==0,2),:) = 1/n; | |
% Initial PageRank | |
rankings = 1 / n * ones(n,1); | |
% Initial error threshold | |
delta = Inf; | |
while delta > epsilon | |
% Save last PR for comparison. | |
rankings_prev = rankings; | |
rankings = (1 - d) * ones(n,1) + d * a * rankings; | |
% Compute error | |
delta = norm(rankings-rankings_prev,1); | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment