function G = google_matrix ( A, p )

%*****************************************************************************80
%
%% google_matrix() converts an adjacency matrix to a Google transition matrix.
%
%  Discussion:
%
%    If the adjacency matrix has a node I with no connectivity,
%    (A(I,1:N) = 0) then we artificially set G(1:N,I)=1/N, so that
%    the transition matrix property of having unit column sums is preserved.
%
%    For nodes with connectivity, we assume that 85 percent of the time
%    we will take a link at random, and 15 percent of the time, we will
%    jump to an arbitrary link.  
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    29 February 2020
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer A(N,N): the adjacency matrix.
%
%    real P: the probability that the user will simply jump to a random
%    web page.
%
%  Output:
%
%    real G(N,N): the Google matrix.
%
  if ( nargin < 2 )
    p = 0.15;
  end

  n = size ( A, 1 );
%
%  s(i) is the number of links on web page i.
%
  s = sum ( A, 2 );
%
%  Allocate G.
%
  G = zeros ( size ( A ) );
%
%  Fill any zero row with 1/n.
%
  for i = 1 : n
    if ( s(i) == 0.0 )
      G(i,:) = 1.0 / n;
    else
      G(i,:) = ( 1.0 - p ) * A(i,:) / s(i) + p / n;
    end
  end
%
%  Transpose so the matrix is a transition/stochastic matrix.
%
  G = G';

  return
end

