src/main/network_main.c
/**
 * \brief Converts matrix from a file to a digraph if the former is (co)network.
 */
\brief Converts matrix from a file to a digraph if the former is (co)network.
\ brief Converts matrix from a file to a digraph if the former is ( co ) network .
CMR_ERROR matrixToDigraph(
  const char* instanceFileName,   
  FileFormat inputFormat,         
  FileFormat outputFormat,        
  bool conetwork,                 
  bool outputNonNetworkElements,  
  bool outputNonNetworkMatrix,    
  bool printStats                 
)
{
  clock_t readClock = clock();
  FILE* instanceFile = strcmp(instanceFileName, "-") ? fopen(instanceFileName, "r") : stdin;
  if (!instanceFile)
    return CMR_ERROR_INPUT;
  CMR* cmr = NULL;
  CMR_CALL( CMRcreateEnvironment(&cmr) );
  CMR_CHRMAT* matrix = NULL;
  if (inputFormat == FILEFORMAT_MATRIX_DENSE)
    CMR_CALL( CMRchrmatCreateFromDenseStream(cmr, instanceFile, &matrix) );
  else if (inputFormat == FILEFORMAT_MATRIX_SPARSE)
    CMR_CALL( CMRchrmatCreateFromSparseStream(cmr, instanceFile, &matrix) );
  if (instanceFile != stdin)
    fclose(instanceFile);
  fprintf(stderr, "Read %lux%lu matrix with %lu nonzeros in %f seconds.\n", matrix->numRows, matrix->numColumns,
    matrix->numNonzeros, (clock() - readClock) * 1.0 / CLOCKS_PER_SEC);
  CMR_CHRMAT* transpose = NULL;
  CMR_CALL( CMRchrmatTranspose(cmr, matrix, &transpose) );
  CMR_CALL( CMRchrmatFree(cmr, &matrix) );
  bool isCoNetwork;
  CMR_GRAPH* digraph = NULL;
  CMR_GRAPH_EDGE* rowEdges = NULL;
  CMR_GRAPH_EDGE* columnEdges = NULL;
  bool* edgesReversed = NULL;
  CMR_NETWORK_STATISTICS stats;
  CMR_CALL( CMRstatsNetworkInit(&stats) );
  if (conetwork)
  {
    CMR_CALL( CMRtestConetworkMatrix(cmr, transpose, &isCoNetwork, &digraph, &rowEdges, &columnEdges, &edgesReversed,
      NULL, &stats) );
  }
  {
    CMR_CALL( CMRtestNetworkMatrix(cmr, transpose, &isCoNetwork, &digraph, &rowEdges, &columnEdges, &edgesReversed,
      NULL, &stats) );
  }
  fprintf(stderr, "Matrix %s%snetwork.\n", isCoNetwork ? "IS " : "IS NOT ", conetwork ? "co" : "");
  if (printStats)
    CMR_CALL( CMRstatsNetworkPrint(stderr, &stats, NULL) );
  if (isCoNetwork)
  {
    if (outputFormat == FILEFORMAT_GRAPH_EDGELIST)
    {
      for (size_t row = 0; row < transpose->numColumns; ++row)
      {
        CMR_GRAPH_EDGE e = rowEdges[row];
        CMR_GRAPH_NODE u = CMRgraphEdgeU(digraph, e);
        CMR_GRAPH_NODE v = CMRgraphEdgeV(digraph, e);
        if (edgesReversed && edgesReversed[e])
        {
          CMR_GRAPH_NODE temp = u;
          u = v;
          v = temp;
        }
        printf("%d %d r%ld\n", u, v, row+1);
      }
      for (size_t column = 0; column < transpose->numRows; ++column)
      {
        CMR_GRAPH_EDGE e = columnEdges[column];
        CMR_GRAPH_NODE u = CMRgraphEdgeU(digraph, e);
        CMR_GRAPH_NODE v = CMRgraphEdgeV(digraph, e);
        if (edgesReversed && edgesReversed[e])
        {
          CMR_GRAPH_NODE temp = u;
          u = v;
          v = temp;
        }
        printf("%d %d c%ld\n", u, v, column+1);
      }
    }
    else if (outputFormat == FILEFORMAT_GRAPH_DOT)
    {
      char buffer[16];
      puts("digraph G {");
      for (size_t row = 0; row < transpose->numColumns; ++row)
      {
        CMR_GRAPH_EDGE e = rowEdges[row];
        CMR_GRAPH_NODE u = CMRgraphEdgeU(digraph, e);
        CMR_GRAPH_NODE v = CMRgraphEdgeV(digraph, e);
        if (edgesReversed && edgesReversed[e])
        {
          CMR_GRAPH_NODE temp = u;
          u = v;
          v = temp;
        }
        printf(" v_%d -> v_%d [label=\"%s\",style=bold,color=red];\n", u, v,
          CMRelementString(CMRrowToElement(row), buffer));
      }
      for (size_t column = 0; column < transpose->numRows; ++column)
      {
        CMR_GRAPH_EDGE e = columnEdges[column];
        CMR_GRAPH_NODE u = CMRgraphEdgeU(digraph, e);
        CMR_GRAPH_NODE v = CMRgraphEdgeV(digraph, e);
        if (edgesReversed && edgesReversed[e])
        {
          CMR_GRAPH_NODE temp = u;
          u = v;
          v = temp;
        }
        printf(" v_%d -> v_%d [label=\"%s\"];\n", u, v, CMRelementString(CMRcolumnToElement(column), buffer));
      }
      puts("}");
    }
    if (edgesReversed)
      CMR_CALL( CMRfreeBlockArray(cmr, &edgesReversed) );
    CMR_CALL( CMRfreeBlockArray(cmr, &rowEdges) );
    CMR_CALL( CMRfreeBlockArray(cmr, &columnEdges) );
    CMR_CALL( CMRgraphFree(cmr, &digraph) );
  }
  CMR_CALL( CMRchrmatFree(cmr, &transpose) );
  CMR_CALL( CMRfreeEnvironment(&cmr) );
  return CMR_OKAY;
}
CMR_ERROR matrixToDigraph ( const char * instanceFileName , FileFormat inputFormat , FileFormat outputFormat , bool conetwork , bool outputNonNetworkElements , bool outputNonNetworkMatrix , bool printStats ) { clock_t readClock = clock ( ) ; FILE * instanceFile = strcmp ( instanceFileName , " " ) ? fopen ( instanceFileName , " " ) : stdin ; if ( ! instanceFile ) return CMR_ERROR_INPUT ; CMR * cmr = NULL ; CMR_CALL ( CMRcreateEnvironment ( & cmr ) ) ; CMR_CHRMAT * matrix = NULL ; if ( inputFormat == FILEFORMAT_MATRIX_DENSE ) CMR_CALL ( CMRchrmatCreateFromDenseStream ( cmr , instanceFile , & matrix ) ) ; else if ( inputFormat == FILEFORMAT_MATRIX_SPARSE ) CMR_CALL ( CMRchrmatCreateFromSparseStream ( cmr , instanceFile , & matrix ) ) ; if ( instanceFile != stdin ) fclose ( instanceFile ) ; fprintf ( stderr , " \n " , matrix -> numRows , matrix -> numColumns , matrix -> numNonzeros , ( clock ( ) - readClock ) * 1.0 / CLOCKS_PER_SEC ) ; CMR_CHRMAT * transpose = NULL ; CMR_CALL ( CMRchrmatTranspose ( cmr , matrix , & transpose ) ) ; CMR_CALL ( CMRchrmatFree ( cmr , & matrix ) ) ; bool isCoNetwork ; CMR_GRAPH * digraph = NULL ; CMR_GRAPH_EDGE * rowEdges = NULL ; CMR_GRAPH_EDGE * columnEdges = NULL ; bool * edgesReversed = NULL ; CMR_NETWORK_STATISTICS stats ; CMR_CALL ( CMRstatsNetworkInit ( & stats ) ) ; if ( conetwork ) { CMR_CALL ( CMRtestConetworkMatrix ( cmr , transpose , & isCoNetwork , & digraph , & rowEdges , & columnEdges , & edgesReversed , NULL , & stats ) ) ; } { CMR_CALL ( CMRtestNetworkMatrix ( cmr , transpose , & isCoNetwork , & digraph , & rowEdges , & columnEdges , & edgesReversed , NULL , & stats ) ) ; } fprintf ( stderr , " \n " , isCoNetwork ? " " : " " , conetwork ? " " : " " ) ; if ( printStats ) CMR_CALL ( CMRstatsNetworkPrint ( stderr , & stats , NULL ) ) ; if ( isCoNetwork ) { if ( outputFormat == FILEFORMAT_GRAPH_EDGELIST ) { for ( size_t row = 0 ; row < transpose -> numColumns ; ++ row ) { CMR_GRAPH_EDGE e = rowEdges [ row ] ; CMR_GRAPH_NODE u = CMRgraphEdgeU ( digraph , e ) ; CMR_GRAPH_NODE v = CMRgraphEdgeV ( digraph , e ) ; if ( edgesReversed && edgesReversed [ e ] ) { CMR_GRAPH_NODE temp = u ; u = v ; v = temp ; } printf ( " \n " , u , v , row + 1 ) ; } for ( size_t column = 0 ; column < transpose -> numRows ; ++ column ) { CMR_GRAPH_EDGE e = columnEdges [ column ] ; CMR_GRAPH_NODE u = CMRgraphEdgeU ( digraph , e ) ; CMR_GRAPH_NODE v = CMRgraphEdgeV ( digraph , e ) ; if ( edgesReversed && edgesReversed [ e ] ) { CMR_GRAPH_NODE temp = u ; u = v ; v = temp ; } printf ( " \n " , u , v , column + 1 ) ; } } else if ( outputFormat == FILEFORMAT_GRAPH_DOT ) { char buffer [ 16 ] ; puts ( " " ) ; for ( size_t row = 0 ; row < transpose -> numColumns ; ++ row ) { CMR_GRAPH_EDGE e = rowEdges [ row ] ; CMR_GRAPH_NODE u = CMRgraphEdgeU ( digraph , e ) ; CMR_GRAPH_NODE v = CMRgraphEdgeV ( digraph , e ) ; if ( edgesReversed && edgesReversed [ e ] ) { CMR_GRAPH_NODE temp = u ; u = v ; v = temp ; } printf ( " \" \" \n " , u , v , CMRelementString ( CMRrowToElement ( row ) , buffer ) ) ; } for ( size_t column = 0 ; column < transpose -> numRows ; ++ column ) { CMR_GRAPH_EDGE e = columnEdges [ column ] ; CMR_GRAPH_NODE u = CMRgraphEdgeU ( digraph , e ) ; CMR_GRAPH_NODE v = CMRgraphEdgeV ( digraph , e ) ; if ( edgesReversed && edgesReversed [ e ] ) { CMR_GRAPH_NODE temp = u ; u = v ; v = temp ; } printf ( " \" \" \n " , u , v , CMRelementString ( CMRcolumnToElement ( column ) , buffer ) ) ; } puts ( " " ) ; } if ( edgesReversed ) CMR_CALL ( CMRfreeBlockArray ( cmr , & edgesReversed ) ) ; CMR_CALL ( CMRfreeBlockArray ( cmr , & rowEdges ) ) ; CMR_CALL ( CMRfreeBlockArray ( cmr , & columnEdges ) ) ; CMR_CALL ( CMRgraphFree ( cmr , & digraph ) ) ; } CMR_CALL ( CMRchrmatFree ( cmr , & transpose ) ) ; CMR_CALL ( CMRfreeEnvironment ( & cmr ) ) ; return CMR_OKAY ; }
\brief Converts matrix from a file to a digraph if the former is (co)network.
\ brief Converts matrix from a file to a digraph if the former is ( co ) network .
/**< File name containing the input matrix (may be `-' for stdin). */ /**< Format of the input matrix. */ /**< Format of the output digraph. */ /**< Whether the input shall be checked for being conetwork instead of network. */ /**< Whether to print the elements of a minimal non-(co)network submatrix. */ /**< Whether to print a minimal non-(co)network submatrix. */ /**< Whether to print statistics to stderr. */ /* Read matrix. */ /* Transpose it. */ /* Test for network. */ /* Cleanup. */