converters/dimacs.cpp
// reading directed files, edges stored twice
reading directed files, edges stored twice
reading directed files edges stored twice
void loadDimacsFile(Graph *&g, const std::string &fileName, Weight_t wtype)
{
  std::ifstream ifs;
  ifs.open(fileName.c_str());
  if (!ifs) {
    std::cerr << "Error opening Dimacs format file: " << fileName << std::endl;
    exit(EXIT_FAILURE);
  }
  std::string line;
  char comment;
  do {
    std::getline(ifs, line);
    comment = line[0];
  } while (comment == 'c');
  GraphElem numVertices, numEdges, value = 0;
  {
    std::string p, sp;
    std::istringstream iss(line);
    iss >> p >> sp >> numVertices >> numEdges;
    if (!iss || iss.fail() || p[0] != 'p') {
      std::cerr << "Error parsing DIMACS format: " << line << std::endl;
      std::cerr << "p: " << p << ", p[0]: " << p[0] << ", sp: " << sp <<
	", numVertices: " << numVertices << ", numEdges: " << numEdges << std::endl;
      exit(EXIT_FAILURE);
    }
  }
  std::cout << "Loading (undirected) DIMACS file: " << fileName << ", initial numvertices: " << numVertices <<
    ", numEdges: " << numEdges << std::endl;
  double t0 = mytimer();
  std::vector<GraphElem> edgeCount(numVertices + 1);
  std::set<GraphElemTuple> edgeSets;
  for (GraphElem i = 0; i < numEdges; i++) {
    std::string label;
    GraphElem source, dest;
    GraphWeight weight;
    ifs >> label >> source >> dest >> weight;
    if (wtype == ONE_WEIGHT)
        weight = 1.0;
    if (wtype == RND_WEIGHT)
        weight = genRandom(RANDOM_MIN_WEIGHT, RANDOM_MAX_WEIGHT);
    if (wtype == ABS_WEIGHT)
        weight = std::fabs(weight);
    if (!ifs || ifs.fail()) {
      std::cerr << "Error parsing DIMACS edge" << std::endl;
      exit(EXIT_FAILURE);
    }
    edgeSets.insert({source-1, dest-1, weight});
    edgeSets.insert({dest-1, source-1, weight});
  }
  ifs.close();
  double t1 = mytimer();
  std::cout << "Time taken to read the file: " << (t1-t0) << " secs." << std::endl;
  for (auto it = edgeSets.begin(); it != edgeSets.end(); ++it)
      edgeCount[it->i_+1]++;
  std::vector<GraphElemTuple> edgeList(edgeSets.begin(), edgeSets.end());
  numEdges = edgeSets.size();
  std::cout << "Adjusted numEdges after removing duplicates: " << numEdges << std::endl; 
  g = new Graph(numVertices, numEdges);
  processGraphData(*g, edgeCount, edgeList, numVertices, numEdges);
}
void loadDimacsFile ( Graph * & g , const std :: string & fileName , Weight_t wtype ) { std :: ifstream ifs ; ifs . open ( fileName . c_str ( ) ) ; if ( ! ifs ) { std :: cerr << " " << fileName << std :: endl ; exit ( EXIT_FAILURE ) ; } std :: string line ; char comment ; do { std :: getline ( ifs , line ) ; comment = line [ 0 ] ; } while ( comment == ' ' ) ; GraphElem numVertices , numEdges , value = 0 ; { std :: string p , sp ; std :: istringstream iss ( line ) ; iss >> p >> sp >> numVertices >> numEdges ; if ( ! iss || iss . fail ( ) || p [ 0 ] != ' ' ) { std :: cerr << " " << line << std :: endl ; std :: cerr << " " << p << " " << p [ 0 ] << " " << sp << " " << numVertices << " " << numEdges << std :: endl ; exit ( EXIT_FAILURE ) ; } } std :: cout << " " << fileName << " " << numVertices << " " << numEdges << std :: endl ; double t0 = mytimer ( ) ; std :: vector < GraphElem > edgeCount ( numVertices + 1 ) ; std :: set < GraphElemTuple > edgeSets ; for ( GraphElem i = 0 ; i < numEdges ; i ++ ) { std :: string label ; GraphElem source , dest ; GraphWeight weight ; ifs >> label >> source >> dest >> weight ; if ( wtype == ONE_WEIGHT ) weight = 1.0 ; if ( wtype == RND_WEIGHT ) weight = genRandom ( RANDOM_MIN_WEIGHT , RANDOM_MAX_WEIGHT ) ; if ( wtype == ABS_WEIGHT ) weight = std :: fabs ( weight ) ; if ( ! ifs || ifs . fail ( ) ) { std :: cerr << " " << std :: endl ; exit ( EXIT_FAILURE ) ; } edgeSets . insert ( { source - 1 , dest - 1 , weight } ) ; edgeSets . insert ( { dest - 1 , source - 1 , weight } ) ; } ifs . close ( ) ; double t1 = mytimer ( ) ; std :: cout << " " << ( t1 - t0 ) << " " << std :: endl ; for ( auto it = edgeSets . begin ( ) ; it != edgeSets . end ( ) ; ++ it ) edgeCount [ it -> i_ + 1 ] ++ ; std :: vector < GraphElemTuple > edgeList ( edgeSets . begin ( ) , edgeSets . end ( ) ) ; numEdges = edgeSets . size ( ) ; std :: cout << " " << numEdges << std :: endl ; g = new Graph ( numVertices , numEdges ) ; processGraphData ( * g , edgeCount , edgeList , numVertices , numEdges ) ; }
reading directed files, edges stored twice
reading directed files edges stored twice
// populate edge set // adjust numEdges and edgeList size // create graph data structure