% PageRank simple example

% This is the example we saw in class

N = 5 % number of pages in the whole WWW

A = [0 1 0 0 0;...  % modified adjacency matrix (sinks link to everyone)
     0 0 1 1 1;...
     0 1 0 0 0;...
     1 1 1 1 1;...
     0 1 0 1 0]

% This is another, pathological example with a 2-vertex cycle where the
% surfer gets "stuck". It shows the necessity of adding a damping factor,
% which we mentioned in class but didn't treat in the formulas below.
% (See e.g. https://de.wikipedia.org/wiki/PageRank)
%
% N = 3
% 
% A = [0 1 0;...
%      0 0 1;...
%      0 1 0]
 
D = diag(A*ones(N,1))

M = (inv(D)*A)' % Google-Matrix without damping

[EV, EW] = eig(M)

p = EV(:,1)

p_normalized = p/(ones(1,N)*p)

% Power iterations
p_0 = (1/N)*ones(N,1)
p_k = p_0;
for k = 1:20
    k
    p_k = M*p_k
end

