function A = sparse_from_index_file ( index_file )

%*****************************************************************************80
%
%% sparse_from_index_file() creates a sparse matrix from an index file.
%
%  Discussion:
%
%    The index file contains N lines.  Line I describes the adjacency 
%    information for node I of a directed graph.  The first entry of
%    line I is always the integer I.  Zero or more integers follow.
%
%    If line I includes value J, then A(I,J) is set to 1, and this is
%    interpreted as "there is a link or path from node I to node J".
%
%  Licensing:
%
%    This code is distributed under the MIT license. 
%
%  Modified:
%
%    01 March 2019
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    string index_file, the name of the index file.
%
%  Output:
%
%    sparse A(), the sparse matrix created from the index file 
%    information.
%
  input = fopen ( index_file, 'rt' );

  if ( input < 0 ) 
    fprintf ( 1, '\n' );
    fprintf ( 1, 'sparse_from_index_file - Error!\n' );
    fprintf ( 1, '  Could not open the file "%s".\n', filename1 );
    error ( 'sparse_from_index_file - Error!' );
  end

  row = [ ];
  col = [ ];

  i = 0;
  nz = 0;  

  while ( true )

    line = fgets ( input );

    if ( line == -1 )
      break;
    end

    [ x, count ] = sscanf ( line, '%d' );

    i = i + 1;
%
%  Let's assume that the I->I link is not interesting!
%
    self_links_legal = false;
    if ( self_links_legal )
      nz = nz + 1;
      row(nz) = i;
      col(nz) = i;
    end

    for j = 2 : count
      nz = nz + 1;
      row(nz) = i;
      col(nz) = x(j);
    end

  end

  s = ones ( 1, nz );
  m = i; 
  n = i;
  A = sparse ( row, col, s, m, n, nz );

  fclose ( input );

  return
end
