function [pR,pRValues] = PageRank(G)
% G is an n-by-n connectivity array.
% pR(i) is the page rank of page i.

% Generate the transition matrix

  [n,n] = size(G);
  p = .85;
  P = zeros(n,n);
  for j=1:n
      nOutlinks = sum(G(:,j));
      if nOutlinks==0
          P(:,j) = ones(n,1)/n;
      else
          for i=1:n
              if G(i,j)==1
                  P(i,j) = p/nOutlinks + (1-p)/n;
              else
                  P(i,j) = (1-p)/n;
              end
          end
      end
  end
% Compute the stationary vector
  [v,err,its] = StatVec(P,ones(n,1),.00001,1000);
  pRValues = v/sum(v);
% Determine the page ranks
  [w,q] = sort(-v);
  pR = zeros(n,1);
  for k=1:n
      % Index of the kth most important webpage...
      j = q(k);
      pR(j) = k;
  end
  
  