% [x,res,I] = pageRank(P,c,v,s,tolerance)
% 1-c is random jump probability
% v is the personalization vector
% s is the start vector
% for default values, do pageRank(P).  Otherwise, you need to have
% all arguments.  Those that you don't know, put 0, and it will
% give the default values.
% for example pageRank(P,.99,0,0,0) will set the random jump
% probability to .01 and use the default values for the rest.
% x is the PageRank vector
% res is the vector of the L1 residual at each iteration
% I is a vector which counts iterations



function [x,res,I] = pageRank(P,c,v,s,tolerance)

maxiterations=200;


if nargin~=1 & nargin~=5
	disp('you need to have either 1 or 5 arguments');
end

% set defaults
if nargin==1
	c=0;v=0;s=0;tolerance=0;
end

if c==0
	c=.85;
	%c=.9;
	%c=.99; 
end

if tolerance==0,
	%tolerance=1e-8;
	tolerance=1e-4;
end

n=max(size(P));
uniform=ones(n,1)/n;

if v==0,
	v=uniform;
end
 
if s==0,
	s=uniform;
end

% run PageRank

x(1:n,1)=s;%
res=[];
I=[];
i=1;%

%figure
%hold on

%disp('Running PageRank');
while isempty(I) | (res(end) > tolerance & I(end)<maxiterations) 
	[x(1:n,i+1),I,res]=iteratePageRank(P,c,v,x(1:n,i),I,res);%
        i=i+1;%
        disp(i);%
	%plot(I,log(res),'b-');
	%pause(.001)	
end
