This repository has been archived on 2021-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
NC/mp2/Project.2.Maggioni.Claudio/pagerank.m

55 lines
1.3 KiB
Mathematica
Raw Normal View History

2020-09-29 11:58:49 +00:00
function x = pagerank(U,G,p)
% PAGERANK Google's PageRank
% pagerank(U,G,p) uses the URLs and adjacency matrix produced by SURFER,
% together with a damping factory p, (default is .85), to compute and plot
% a bar graph of page rank, and print the dominant URLs in page rank order.
% x = pagerank(U,G,p) returns the page ranks instead of printing.
% See also SURFER, SPY.
if nargin < 3, p = .85; end
% Eliminate any self-referential links
2020-10-06 12:43:53 +00:00
%G = G - diag(diag(G));
2020-09-29 11:58:49 +00:00
% c = out-degree, r = in-degree
2020-10-06 12:43:53 +00:00
[~,n] = size(G);
2020-09-29 11:58:49 +00:00
c = sum(G,1);
r = sum(G,2);
% Scale column sums to be 1 (or 0 where there are no out links).
k = find(c~=0);
D = sparse(k,k,1./c(k),n,n);
e = ones(n,1);
I = speye(n,n);
2020-10-06 12:43:53 +00:00
% ---------------------------------- DEFAULT ------------------------------
% Solve (I - p*G*D)*x = e
2020-09-29 11:58:49 +00:00
x = (I - p*G*D)\e;
2020-10-06 12:43:53 +00:00
% -------------------------------------------------------------------------
2020-09-29 11:58:49 +00:00
2020-10-06 12:43:53 +00:00
% Normalize so that sum(x) == 1.
2020-09-29 11:58:49 +00:00
x = x/sum(x);
% Bar graph of page rank.
shg
bar(x)
title('Page Rank')
% Print URLs in page rank order.
if nargout < 1
[~,q] = sort(-x);
2020-10-06 12:43:53 +00:00
disp(' page-rank in out author');
2020-09-29 11:58:49 +00:00
k = 1;
2020-10-06 12:43:53 +00:00
maxN = length(U);
while (k <= maxN) && (x(q(k)) >= .005)
2020-09-29 11:58:49 +00:00
j = q(k);
temp1 = r(j);
temp2 = c(j);
2020-10-06 12:43:53 +00:00
fprintf(' %3.0f %8.4f %4.0f %4.0f %s\n', j,x(j),full(temp1),full(temp2),U{j});
2020-09-29 11:58:49 +00:00
k = k+1;
end
end