% Script P2
% Checks out PageRank

% A small example to check correctness...
rand('seed',0);
% Generate the link matrix...
n = 50; p = .1; G = sparse(n,n);
for i=1:n
for j=1:n
if rand(1,1)<=p
G(i,j) = 1;
end
end
end
alpha = .85;
% Generate the A-matrix explicitly and compute its stationary vector...
A = zeros(n,n);
for j=1:n
tau = sum(G(:,j));
if tau>0
A(:,j) = alpha*G(:,j)/tau + (1-alpha)*ones(n,1)/n;
else
A(:,j) = ones(n,1)/n;
end
end
[X,D] = eig(A); [c,k] = max(diag(D)); x = abs(X(:,k));
% Compute the page ranks...
[z,idx] = sort(x,'descend');
PR_Exact = zeros(n,1);
PR_Exact(idx) = (1:n)';
% Compare and display..
PR = PageRank(G,alpha);
clc
disp(' k PR(k) PR_Exact(k) x(k)')
disp('---------------------------------------------')
for k=1:n
fprintf(' %2d %2d %2d %6.4f\n',k,PR(k),PR_Exact(k),x(k))
end