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/mp1/Project_1_Maggioni_Claudio/pagerank2.m

83 lines
1.8 KiB
Mathematica
Raw Normal View History

function x = pagerank2(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
%G = G - diag(diag(G));
% c = out-degree, r = in-degree
[~,n] = size(G);
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);
% ---------------------------- INVERSE ITERATION --------------------------
disp('Using inverse iteration implementation\n');
z = ((1 - p) * (c ~= 0) + (c == 0)) / n;
A = p * G * D + e * z;
x = e/n;
% Check if B will be a singular matrix. If so, change it
2020-10-04 15:49:37 +00:00
alpha = 0.8;
2020-09-27 13:55:37 +00:00
while rcond(A - alpha * I) < eps
alpha = alpha + 0.01;
end
2020-10-04 15:49:37 +00:00
B = A - alpha * I;
old_x = zeros(n, 1);
old_norm = -1;
no = +Inf;
it = 0;
2020-09-27 13:55:37 +00:00
while (old_norm == -1 || old_norm > no) && no > 1e-8
old_norm = no;
old_x = x;
x = B \ x;
2020-09-27 13:55:37 +00:00
x = x/sum(x);
it = it + 1;
2020-09-27 13:55:37 +00:00
no = norm(x - old_x, 1);
end
2020-09-27 13:55:37 +00:00
x = abs(old_x);
display(it);
% -------------------------------------------------------------------------
% Normalize so that sum(x) == 1.
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);
disp(' page-rank in out url')
k = 1;
maxN = length(U);
while (k <= maxN) && (x(q(k)) >= .005)
disp(k)
j = q(k);
temp1 = r(j);
temp2 = c(j);
disp(fprintf(' %3.0f %8.4f %4.0f %4.0f %s', j,x(j),full(temp1),full(temp2),U{j}))
k = k+1;
end
end