function [x,reshist] = pr_rich(P,alpha,v,tol)
% PR_RICH Compute PageRank via the Richardson method
% P is a row-stochastic matrix.

maxit = 10000;
reshist = zeros(maxit,1);
x = v;

for iter=1:maxit
    y = (P'*x);
    y = alpha*y;
    w = norm(y,1);
    y = y + (1-w)*v;
    
    x = x - y;
    reshist(iter) = norm(x,1);
    fprintf('iter = %i  res=%8.2e\n', iter, reshist(iter));
    x = y;
    if reshist(iter) < tol, break; end
end
reshist = reshist(1:iter);
if reshist(end) > tol, warning('did not converge'); end