function tiny_page_test ( )

%*****************************************************************************80
%
%% tiny_page_test() carries out the page rank algorithm for the "tiny" dataset.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    02 March 2019
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'tiny_page_test():\n' );
  fprintf ( 1, '  Pagerank algorithm for the "tiny" dataset.\n' );
%
%  Open the index file, and count the number of nodes ( = rows ).
%
  index_file = 'tiny_index.txt';
  n = file_row_count ( index_file );
  fprintf ( 1, '  Number of nodes = %d\n', n );  
%
%  Create some sparse matrices.
%  A is "adjacency":  A(I,J) = 1 means there is a link from I to J.
%
  A = sparse_from_index_file ( index_file );
  nonzeros = nnz ( A );
  fprintf ( 1, '  Adjacency matrix:\n' );
  fprintf ( 1, '  Number of nonzero elements = %d\n', nonzeros );
  full ( A )
%
%  Write the sparse matrix to a sparse matrix file.
%
  [ i, j, s ] = find ( A );
  Adata = [ i, j, s ];
  if ( A(n,n) == 0 )
    Adata = [ Adata; n, n, 0.0 ];
  end
  save ( 'tiny_sparse.txt', 'Adata', '-ascii' );
%
%  Create some sparse matrices.
%  C is "connectivity": C(I,J) = 1 means there is a link from J to I.
%
  C = A';

  fprintf ( 1, '  Connectivity matrix:\n' );
  spy ( C );
  print ( '-dpng', 'tiny_connectivity.png' );
  full ( C )
%
%  T is transition matrix: 
%  T(I,J) = 1/k, the probability of moving from J to I
%  assuming C(I,J) is nonzero and there are k nonzeros in column J.
%
%  A random walk using T may cause walkers to disappear.
%
  T = A';

  for j = 1 : n
    csum = sum ( T(:,j) );
    if ( 0 < csum )
      T(:,j) = T(:,j) / csum;
    end
  end

  fprintf ( 1, '  Transition matrix:\n' );
  full ( T )
%
%  S: The selfloop transition matrix with selfloops for sinks.
%  We just compute and print this as a verification.
%  A random walk with S will cause walkers to wander around until
%  reaching a deadend, but we will never "lose" a walker.
%
  S = A';

  for j = 1 : n
    csum = sum ( S(:,j) );
    if ( 0 < csum )
      S(:,j) = S(:,j) / csum;
    else
      S(j,j) = 1.0;
    end
  end

  fprintf ( 1, '  Transition matrix with deadend loops:\n' );
  full ( S )
%
%  G: The transition matrix modified by the random jump probability.
%  A random walk with G will never get stuck.
%
  p = 0.85;

  G = A';

  for j = 1 : n
    csum = sum ( G(:,j) );
    if ( csum == 0.0 )
      G(:,j) = 1.0 / n;
    else
      G(:,j) = p * G(:,j) / csum;
      i = find ( G(:,j) == 0.0 );
      zsum = length ( i );
      if ( 0 < zsum )
        G(i,j) = ( 1.0 - p ) / zsum;
      end
    end
  end

  fprintf ( 1, '  Transition matrix with random jumps:\n' );
  full ( G )

  sum ( G )
%
%  Solve G*x = x.
%  x is the eigenvector corresponding to eigenvalue 1.
%  1 is the maximal eigenvalue.
%  eigs will return in column 1 of V the eigenvector corresponding to 
%  the largest eigenvalue.
%
  [ V, D ] = eigs ( G );

  ranking = abs ( V(:,1) ) / norm ( V(:,1), 1 );

  label = 1 : n;
  r8vec2_print ( label, ranking, '  Page rankings:' );

  return
end
