Skip to content

Instantly share code, notes, and snippets.

@philippfranke
Last active May 5, 2017 08:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philippfranke/d7a374e9dc3ebaae5cce0fc36125a206 to your computer and use it in GitHub Desktop.
Save philippfranke/d7a374e9dc3ebaae5cce0fc36125a206 to your computer and use it in GitHub Desktop.
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