function rscore = PageRank(A,epsilon,max_iter)
% rscore = PageRank(A,epsilon)
%
% A        adjacency matrix
% epsilon  reset probability

if nargin < 2
    epsilon = 0.05;
end
if nargin < 3
    max_iter = 1000;
end

[m,n] = size(A);
if m ~= n
    error('Dimension mismatch.');
end

P = transition(A, epsilon);

if max_iter < 1
    opts.disp = 0;
    [rscore,foo] = eigs(P', 1, 'lm', opts);
    rscore = abs(rscore)/norm(rscore);
else
    rscore = ones(n, 1)/sqrt(n);
    rscore_old = rscore;
    for i = 1:max_iter
        rscore = P'*rscore;
        rscore = rscore/norm(rscore);
        if abs(1 - rscore_old'*rscore) < 1e-10
            break
        end
        rscore_old = rscore;
    end
end
