src/main/network_main.c
/**
 * \brief Converts the given digraph file to the corresponding (co)network matrix.
 */
\brief Converts the given digraph file to the corresponding (co)network matrix.
\ brief Converts the given digraph file to the corresponding ( co ) network matrix .
CMR_ERROR digraphToMatrix(
  const char* instanceFileName, 
  FileFormat inputFormat,       
  FileFormat outputFormat,      
  bool conetwork                
)
{
  FILE* instanceFile = strcmp(instanceFileName, "-") ? fopen(instanceFileName, "r") : stdin;
  if (!instanceFile)
    return CMR_ERROR_INPUT;
  CMR* cmr = NULL;
  CMR_CALL( CMRcreateEnvironment(&cmr) );
  CMR_GRAPH* digraph = NULL;
  CMR_ELEMENT* edgeElements = NULL;
  if (inputFormat == FILEFORMAT_GRAPH_EDGELIST)
  {
    CMR_CALL( CMRgraphCreateFromEdgeList(cmr, &digraph, &edgeElements, NULL, instanceFile) );
  }
  if (instanceFile != stdin)
    fclose(instanceFile);
  size_t numForestEdges = 0;
  size_t numCoforestEdges = 0;
  for (CMR_GRAPH_ITER i = CMRgraphEdgesFirst(digraph); CMRgraphEdgesValid(digraph, i); i = CMRgraphEdgesNext(digraph, i))
  {
    CMR_GRAPH_EDGE e = CMRgraphEdgesEdge(digraph, i);
    CMR_ELEMENT element = edgeElements[e];
    if (CMRelementIsRow(element))
      numForestEdges++;
    else if (CMRelementIsColumn(element))
      numCoforestEdges++;
  }
  CMR_GRAPH_EDGE* forestEdges = NULL;
  CMR_CALL( CMRallocBlockArray(cmr, &forestEdges, numForestEdges) );
  for (size_t i = 0; i < numForestEdges; ++i)
    forestEdges[i] = -1;
  CMR_GRAPH_EDGE* coforestEdges = NULL;
  CMR_CALL( CMRallocBlockArray(cmr, &coforestEdges, numCoforestEdges) );
  for (size_t i = 0; i < numCoforestEdges; ++i)
    coforestEdges[i] = -1;
  for (CMR_GRAPH_ITER i = CMRgraphEdgesFirst(digraph); CMRgraphEdgesValid(digraph, i); i = CMRgraphEdgesNext(digraph, i))
  {
    CMR_GRAPH_EDGE e = CMRgraphEdgesEdge(digraph, i);
    CMR_ELEMENT element = edgeElements[e];
    if (CMRelementIsRow(element))
    {
      size_t rowIndex = CMRelementToRowIndex(element);
      if (rowIndex < numForestEdges)
        forestEdges[rowIndex] = e;
    }
    else if (CMRelementIsColumn(element))
    {
      size_t columnIndex = CMRelementToColumnIndex(element);
      if (columnIndex < numCoforestEdges)
        coforestEdges[columnIndex] = e;
    }
  }
  CMR_CHRMAT* matrix = NULL;
  bool isCorrectForest = false;
  clock_t startTime = clock();
  if (conetwork)
  {
    CMR_CALL( CMRcomputeNetworkMatrix(cmr, digraph, NULL, &matrix, NULL, numForestEdges, forestEdges, numCoforestEdges,
      coforestEdges, &isCorrectForest) );
  }
  else
  {
    CMR_CALL( CMRcomputeNetworkMatrix(cmr, digraph, &matrix, NULL, NULL, numForestEdges, forestEdges, numCoforestEdges,
      coforestEdges, &isCorrectForest) );
  }
  clock_t endTime = clock();
  fprintf(stderr, "Time: %f\n", (endTime - startTime) * 1.0 / CLOCKS_PER_SEC);
  if (outputFormat == FILEFORMAT_MATRIX_DENSE)
    CMR_CALL( CMRchrmatPrintDense(cmr, matrix, stdout, '0', false) );
  else if (outputFormat == FILEFORMAT_MATRIX_SPARSE)
    CMR_CALL( CMRchrmatPrintSparse(cmr, matrix, stdout) );
  else
    assert(false);
  CMR_CALL( CMRchrmatFree(cmr, &matrix) );
  free(coforestEdges);
  free(forestEdges);
  free(edgeElements);
  CMR_CALL( CMRgraphFree(cmr, &digraph) );
  CMR_CALL( CMRfreeEnvironment(&cmr) );
  return CMR_OKAY;
}
CMR_ERROR digraphToMatrix ( const char * instanceFileName , FileFormat inputFormat , FileFormat outputFormat , bool conetwork ) { FILE * instanceFile = strcmp ( instanceFileName , " " ) ? fopen ( instanceFileName , " " ) : stdin ; if ( ! instanceFile ) return CMR_ERROR_INPUT ; CMR * cmr = NULL ; CMR_CALL ( CMRcreateEnvironment ( & cmr ) ) ; CMR_GRAPH * digraph = NULL ; CMR_ELEMENT * edgeElements = NULL ; if ( inputFormat == FILEFORMAT_GRAPH_EDGELIST ) { CMR_CALL ( CMRgraphCreateFromEdgeList ( cmr , & digraph , & edgeElements , NULL , instanceFile ) ) ; } if ( instanceFile != stdin ) fclose ( instanceFile ) ; size_t numForestEdges = 0 ; size_t numCoforestEdges = 0 ; for ( CMR_GRAPH_ITER i = CMRgraphEdgesFirst ( digraph ) ; CMRgraphEdgesValid ( digraph , i ) ; i = CMRgraphEdgesNext ( digraph , i ) ) { CMR_GRAPH_EDGE e = CMRgraphEdgesEdge ( digraph , i ) ; CMR_ELEMENT element = edgeElements [ e ] ; if ( CMRelementIsRow ( element ) ) numForestEdges ++ ; else if ( CMRelementIsColumn ( element ) ) numCoforestEdges ++ ; } CMR_GRAPH_EDGE * forestEdges = NULL ; CMR_CALL ( CMRallocBlockArray ( cmr , & forestEdges , numForestEdges ) ) ; for ( size_t i = 0 ; i < numForestEdges ; ++ i ) forestEdges [ i ] = -1 ; CMR_GRAPH_EDGE * coforestEdges = NULL ; CMR_CALL ( CMRallocBlockArray ( cmr , & coforestEdges , numCoforestEdges ) ) ; for ( size_t i = 0 ; i < numCoforestEdges ; ++ i ) coforestEdges [ i ] = -1 ; for ( CMR_GRAPH_ITER i = CMRgraphEdgesFirst ( digraph ) ; CMRgraphEdgesValid ( digraph , i ) ; i = CMRgraphEdgesNext ( digraph , i ) ) { CMR_GRAPH_EDGE e = CMRgraphEdgesEdge ( digraph , i ) ; CMR_ELEMENT element = edgeElements [ e ] ; if ( CMRelementIsRow ( element ) ) { size_t rowIndex = CMRelementToRowIndex ( element ) ; if ( rowIndex < numForestEdges ) forestEdges [ rowIndex ] = e ; } else if ( CMRelementIsColumn ( element ) ) { size_t columnIndex = CMRelementToColumnIndex ( element ) ; if ( columnIndex < numCoforestEdges ) coforestEdges [ columnIndex ] = e ; } } CMR_CHRMAT * matrix = NULL ; bool isCorrectForest = false ; clock_t startTime = clock ( ) ; if ( conetwork ) { CMR_CALL ( CMRcomputeNetworkMatrix ( cmr , digraph , NULL , & matrix , NULL , numForestEdges , forestEdges , numCoforestEdges , coforestEdges , & isCorrectForest ) ) ; } else { CMR_CALL ( CMRcomputeNetworkMatrix ( cmr , digraph , & matrix , NULL , NULL , numForestEdges , forestEdges , numCoforestEdges , coforestEdges , & isCorrectForest ) ) ; } clock_t endTime = clock ( ) ; fprintf ( stderr , " \n " , ( endTime - startTime ) * 1.0 / CLOCKS_PER_SEC ) ; if ( outputFormat == FILEFORMAT_MATRIX_DENSE ) CMR_CALL ( CMRchrmatPrintDense ( cmr , matrix , stdout , ' ' , false ) ) ; else if ( outputFormat == FILEFORMAT_MATRIX_SPARSE ) CMR_CALL ( CMRchrmatPrintSparse ( cmr , matrix , stdout ) ) ; else assert ( false ) ; CMR_CALL ( CMRchrmatFree ( cmr , & matrix ) ) ; free ( coforestEdges ) ; free ( forestEdges ) ; free ( edgeElements ) ; CMR_CALL ( CMRgraphFree ( cmr , & digraph ) ) ; CMR_CALL ( CMRfreeEnvironment ( & cmr ) ) ; return CMR_OKAY ; }
\brief Converts the given digraph file to the corresponding (co)network matrix.
\ brief Converts the given digraph file to the corresponding ( co ) network matrix .
/**< File name containing the input digraph (may be `-' for stdin). */ /**< Format of the input digraph. */ /**< Format of the output matrix. */ /**< Whether the output shall be the conetwork matrix instead of the network matrix. */ /* Read edge list. */ /* Scan edges for (co)forest edges. */ /* Create list of (co)forest edges. */