Websurfing

%Author:minster
% modified websurf.m in class
close all
clear all

numsims = 2000; % Parameter controlling the number of simulations

%G is representing the figure 3.14 pag 70
%ask question in class
G = [0 0 0 1 0 0; 1 0 0 0 0 0; 1 0 0 0 0 0; 0 1 1 0 1 0; 0 0 1 0 0 1; 0 0 0 0 0 0];
n = length(G);

%monte carlos simulation with G

state = 1; % initial state of the system

M = zeros(n,n);
for i = 1:n;
rowSum = sum(G(i,:));
if (rowSum ~= 0)
M(i,:) = 1/rowSum*G(i,:);
end
end

pages = zeros(1,n); % vector to hold number of times page visited
fprintf(‘\n\n’);
fprintf(‘============================================================\n’);
fprintf(‘Computed Probabilities from %d simulations\n\n’,numsims);
fprintf(‘Sims Page 1 Page 2 Page 3 Page 4 Page 5 \n’);
fprintf(‘============================================================\n’);
% Simulate a surfer’s session
computed_prob = [];
for i=1:numsims
prob = rand;
accumulated_prob = 0;
flag = -1;

for j = 1:n-1
accumulated_prob = accumulated_prob + M(state,j);
if prob < accumulated_prob state = j; flag = j; break; end end; if flag==-1 % coded in case of round-off error state = n; end pages(state) = pages(state) + 1; if ~mod(i,numsims/5) computed_prob = [computed_prob; pages/sum(pages)]; % fprintf('%6d %5.3e %5.3e %5.3e %5.3e %5.3e \n',i,pages(1:5)/sum(pages)); ifprint = mod(i,numsims/5); if ifprint == 0 fprintf('%6d %5.3f %5.3f %5.3f %5.3f %5.3f %5.3f\n',i,pages/sum(pages)); fprintf('\n') end end end %histogram bar(computed_prob(end,:)) convergence = max(abs(computed_prob(end,:)-computed_prob(end-1,:))); text = sprintf('Convergence to %4.2e in all entries',convergence); title(text); axis tight ranking = pages/sum(pages); [sortedRanking, indices] = sort(ranking,'descend'); fprintf('\n****************************\n') fprintf(' PageRank results\n') fprintf('****************************\n') fprintf('Web Page PageRank\n') fprintf('----------------------------\n') for i=1:length(sortedRanking) fprintf('%5d %5.3f \n',indices(i),sortedRanking(i)); end fprintf('\nNote your connectivity matrix was: ')