clear all; close all; clc;
load('adjency')
p = 0.85;
T = zeros(size(G));
n = size(G,1);

for i=1:n
    if(sum(G(i,:))==0)
        T(i,:) = ones(size(G(i,:)))/n;
    else
        T(i,:) = p*G(i,:)/sum(G(i,:)) + (1-p)/n;
    end
end

iterations = 100;
v = rand(n,1);
values = zeros(iterations,1);
for k=1:iterations
    u = (v'*T)';
    u = u/norm(u);
    values(k) = log( norm(u-v) );
    v = u;
end

semilogy(1:iterations,exp(values));
xlabel('k');
ylabel('||v_k - v_{k-1}||');
title('Convergence of the power method')
[maxVals,indexes] = sort(v,'descend');
fprintf('The indexes of the three webpages with largest indexes are: %d, %d and %d.\n',indexes(1),indexes(2),indexes(3));