src/main/k_modular_main.c
/**
 * \brief Tests matrix from a file for total unimodularity.
 */
\brief Tests matrix from a file for total unimodularity.
\ brief Tests matrix from a file for total unimodularity .
static
CMR_ERROR testKmodularity(
  const char* instanceFileName, 
  FileFormat inputFormat,       
  bool transpose,               
  bool strong,                  
  bool unimodular               
)
{
  assert(!transpose || !strong);
  clock_t startClock, endTime;
  FILE* instanceFile = strcmp(instanceFileName, "-") ? fopen(instanceFileName, "r") : stdin;
  if (!instanceFile)
    return CMR_ERROR_INPUT;
  CMR* cmr = NULL;
  CMR_CALL( CMRcreateEnvironment(&cmr) );
  startClock = clock();
  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.\n", matrix->numRows, matrix->numColumns,
    matrix->numNonzeros);
  bool propertyOriginal, propertyTranspose;
  size_t kOriginal, kTranspose;
  bool checkOriginal = !transpose;
  bool checkTranspose = transpose || strong;
  if (checkOriginal)
  {
    startClock = clock();
    if (unimodular)
    {
      CMR_CALL( CMRtestUnimodularity(cmr, matrix, &propertyOriginal) );
      fprintf(stderr, "Determined in %f seconds that it is %sunimodular.\n",
        (clock() - startClock) * 1.0 / CLOCKS_PER_SEC, propertyOriginal ? "" : "NOT ");
    }
    else
    {
      CMR_CALL( CMRtestKmodularity(cmr, matrix, &propertyOriginal, &kOriginal) );
      fprintf(stderr, "Determined in %f seconds that it is ", (clock() - startClock) * 1.0 / CLOCKS_PER_SEC);
      if (propertyOriginal)
        fprintf(stderr, "%lu-modular.\n", kOriginal);
      else
        fprintf(stderr, "NOT k-modular.\n");
    }
  }
  if (checkTranspose)
  {
    startClock = clock();
    CMR_CHRMAT* transposed = NULL;
    CMR_CALL( CMRchrmatTranspose(cmr, matrix, &transposed) );
    if (unimodular)
    {
      CMR_CALL( CMRtestUnimodularity(cmr, transposed, &propertyTranspose) );
      fprintf(stderr, "Determined in %f seconds that its transpose is %sunimodular.\n",
        (clock() - startClock) * 1.0 / CLOCKS_PER_SEC, propertyTranspose ? "" : "NOT ");
    }
    else
    {
      CMR_CALL( CMRtestKmodularity(cmr, transposed, &propertyOriginal, &kTranspose) );
      fprintf(stderr, "Determined in %f seconds that its transpose is ", (clock() - startClock) * 1.0 / CLOCKS_PER_SEC);
      if (propertyOriginal)
        fprintf(stderr, "%lu-modular.\n", kTranspose);
      else
        fprintf(stderr, "NOT k-modular.\n");
    }
    CMR_CALL( CMRchrmatFree(cmr, &transposed));
  }
  if (strong)
  {
    fprintf(stderr, "The matrix is %s%smodular.\n", propertyOriginal && propertyTranspose ? "" : "NOT ",
      unimodular ? "uni" : "k-");
  }
  CMR_CALL( CMRchrmatFree(cmr, &matrix) );
  CMR_CALL( CMRfreeEnvironment(&cmr) );
  return CMR_OKAY;
}
static CMR_ERROR testKmodularity ( const char * instanceFileName , FileFormat inputFormat , bool transpose , bool strong , bool unimodular ) { assert ( ! transpose || ! strong ) ; clock_t startClock , endTime ; FILE * instanceFile = strcmp ( instanceFileName , " " ) ? fopen ( instanceFileName , " " ) : stdin ; if ( ! instanceFile ) return CMR_ERROR_INPUT ; CMR * cmr = NULL ; CMR_CALL ( CMRcreateEnvironment ( & cmr ) ) ; startClock = clock ( ) ; 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 ) ; bool propertyOriginal , propertyTranspose ; size_t kOriginal , kTranspose ; bool checkOriginal = ! transpose ; bool checkTranspose = transpose || strong ; if ( checkOriginal ) { startClock = clock ( ) ; if ( unimodular ) { CMR_CALL ( CMRtestUnimodularity ( cmr , matrix , & propertyOriginal ) ) ; fprintf ( stderr , " \n " , ( clock ( ) - startClock ) * 1.0 / CLOCKS_PER_SEC , propertyOriginal ? " " : " " ) ; } else { CMR_CALL ( CMRtestKmodularity ( cmr , matrix , & propertyOriginal , & kOriginal ) ) ; fprintf ( stderr , " " , ( clock ( ) - startClock ) * 1.0 / CLOCKS_PER_SEC ) ; if ( propertyOriginal ) fprintf ( stderr , " \n " , kOriginal ) ; else fprintf ( stderr , " \n " ) ; } } if ( checkTranspose ) { startClock = clock ( ) ; CMR_CHRMAT * transposed = NULL ; CMR_CALL ( CMRchrmatTranspose ( cmr , matrix , & transposed ) ) ; if ( unimodular ) { CMR_CALL ( CMRtestUnimodularity ( cmr , transposed , & propertyTranspose ) ) ; fprintf ( stderr , " \n " , ( clock ( ) - startClock ) * 1.0 / CLOCKS_PER_SEC , propertyTranspose ? " " : " " ) ; } else { CMR_CALL ( CMRtestKmodularity ( cmr , transposed , & propertyOriginal , & kTranspose ) ) ; fprintf ( stderr , " " , ( clock ( ) - startClock ) * 1.0 / CLOCKS_PER_SEC ) ; if ( propertyOriginal ) fprintf ( stderr , " \n " , kTranspose ) ; else fprintf ( stderr , " \n " ) ; } CMR_CALL ( CMRchrmatFree ( cmr , & transposed ) ) ; } if ( strong ) { fprintf ( stderr , " \n " , propertyOriginal && propertyTranspose ? " " : " " , unimodular ? " " : " " ) ; } CMR_CALL ( CMRchrmatFree ( cmr , & matrix ) ) ; CMR_CALL ( CMRfreeEnvironment ( & cmr ) ) ; return CMR_OKAY ; }
\brief Tests matrix from a file for total unimodularity.
\ brief Tests matrix from a file for total unimodularity .
/**< File name containing the input matrix (may be `-' for stdin). */ /**< Format of the input matrix. */ /**< Whether to test the transpose matrix. */ /**< Whether to test for strong k-modularity. */ /**< Whether to only test for unimodularity. */ /* Read matrix. */ /* Actual test. */ /* Cleanup. */