close all;
clear all;
clc;

%% Adjacency matrix
A = [   0     1   0 ;
        0     0   1 ;
        1     1   0 ] ;
    

% Create and plot directed graph object
G = digraph(A);
p=plot(G, 'MarkerSize', 10);


%% Initial values
% Initial PageRank
PR = ones(size(A,1), 1);
% Store PageRank at each step (for plotting)
store(1,:) = PR';
% Damping factor
d=0.85;

%% Itearate
for i=2:50

    % For each page
    for p=1:size(A,1)
        
        % T are pages that link to p
        T=find (A(:,p)==1);
        % C is the number of links going out of pages T
        C = sum(A(T,:)')';
        % Update PageRank for page p
        PR(p,1) = (1-d) + d*(sum(PR(T)./C));
        
    end
    store(i,:) = PR';
%    row = sprintf('| %d | %2.3f | %2.3f | %2.3f |', i-1, PR(1,1), PR(2,1), PR(3,1));
%    disp (row);
end

%% Display final PageRank
PR

%% Plot PageRank evolution
figure;
plot (store, '.-'); 
title ('PageRank vs iterations');
xlabel('Steps');
ylabel('PageRank of each page');
