hc-verifier.cc
/**
 *  parse graph in DIMACS graph format into given structures
 */
parse graph in DIMACS graph format into given structures
parse graph in DIMACS graph format into given structures
bool parseGraph( char* filename, vector< vector<int> >& graph, int& nodes, int& edges )
{
  fstream infile ( filename );
  if( !infile ) return false;
  if( verbosity > 0 && !undirected ) cerr  << "c assuming directed graph" << endl;
  vector<char> compact; 
  string line, edgeString;
  int realEdges = 0, realNodes = 0, usedNodes = 0,linecount = 0;
  while( getline( infile, line  ) ) 
  {
    linecount ++;
    if( line.find("c") == 0  ) continue; 
    if( line.find("p") == 0 ) {
      stringstream tmp( line );
      string h1,h2;
      tmp >> h1 >> h2 >> nodes >> edges ;
      if( verbosity > 0 ) cerr << "c found nodes: " << nodes << " and edges: " << edges << endl;
      graph.resize( nodes + 1);
      continue; 
    }
    bool empty = true;
    for( int i = 0 ; i < line.size(); ++ i ) {
      if( line[i] != ' ' ){ empty= false; break;}
    }
    if( empty ) continue;
    int from = -1, to = -1;
    stringstream linestream( line );
    linestream >> edgeString >> from >> to;
    if( edgeString != "e" ) {
      cerr << "c expected 'e' at beginning of line " << linecount << ", found " << edgeString << " instead - ABORT" << endl;
      return 1;
    }
    if( from == -1 || to == -1 ) {
      cerr << "c WARNING invalid edge in line[" << linecount << "]: " << line << endl;
      continue; 
    }
    realEdges ++;
    realNodes = realNodes >= from ? realNodes : from;
    realNodes = realNodes >= to ? realNodes : to;
    if( verbosity > 2 ) cerr << "found edge " << from << " -> " << to << " edge: " << edgeString << endl;
    if( compact.size() <= from ) compact.resize( from + 1);
    if( compact.size() <= to ) compact.resize( to + 1);
    if( compact[from] != 1 )
      { usedNodes ++; compact[from] = 1; }
    if( compact[to] != 1 )  
      { usedNodes ++; compact[to] = 1; }
    if ( graph.size() <= from ) graph.resize(from+1);
    graph[ from ] .push_back( to );
    if( undirected ) { 
      if ( graph.size() <= to ) graph.resize(to+1);
      graph[ to ] .push_back(from);
    }
  }
  infile.close();
  if( realNodes != nodes ) cerr << "c WARNING nodes information mismatch: header: " << nodes << " used: " << realNodes << endl;
  if( realEdges != edges ) cerr << "c WARNING edges information mismatch: header: " << edges << " used: " << realEdges << endl;
  nodes = nodes >= realNodes ? nodes : realNodes;
  edges = edges >= realEdges ? edges : realEdges;
  if( nodes != usedNodes ) cerr << "c WARNING graph is not compact, there are unused nodes (" << usedNodes << " used out of " << nodes << ")" << endl;
  return true;
}
bool parseGraph ( char * filename , vector < vector < int > > & graph , int & nodes , int & edges ) { fstream infile ( filename ) ; if ( ! infile ) return false ; if ( verbosity > 0 && ! undirected ) cerr << " " << endl ; vector < char > compact ; string line , edgeString ; int realEdges = 0 , realNodes = 0 , usedNodes = 0 , linecount = 0 ; while ( getline ( infile , line ) ) { linecount ++ ; if ( line . find ( " " ) == 0 ) continue ; if ( line . find ( " " ) == 0 ) { stringstream tmp ( line ) ; string h1 , h2 ; tmp >> h1 >> h2 >> nodes >> edges ; if ( verbosity > 0 ) cerr << " " << nodes << " " << edges << endl ; graph . resize ( nodes + 1 ) ; continue ; } bool empty = true ; for ( int i = 0 ; i < line . size ( ) ; ++ i ) { if ( line [ i ] != ' ' ) { empty = false ; break ; } } if ( empty ) continue ; int from = -1 , to = -1 ; stringstream linestream ( line ) ; linestream >> edgeString >> from >> to ; if ( edgeString != " " ) { cerr << " " << linecount << " " << edgeString << " " << endl ; return 1 ; } if ( from == -1 || to == -1 ) { cerr << " " << linecount << " " << line << endl ; continue ; } realEdges ++ ; realNodes = realNodes >= from ? realNodes : from ; realNodes = realNodes >= to ? realNodes : to ; if ( verbosity > 2 ) cerr << " " << from << " " << to << " " << edgeString << endl ; if ( compact . size ( ) <= from ) compact . resize ( from + 1 ) ; if ( compact . size ( ) <= to ) compact . resize ( to + 1 ) ; if ( compact [ from ] != 1 ) { usedNodes ++ ; compact [ from ] = 1 ; } if ( compact [ to ] != 1 ) { usedNodes ++ ; compact [ to ] = 1 ; } if ( graph . size ( ) <= from ) graph . resize ( from + 1 ) ; graph [ from ] . push_back ( to ) ; if ( undirected ) { if ( graph . size ( ) <= to ) graph . resize ( to + 1 ) ; graph [ to ] . push_back ( from ) ; } } infile . close ( ) ; if ( realNodes != nodes ) cerr << " " << nodes << " " << realNodes << endl ; if ( realEdges != edges ) cerr << " " << edges << " " << realEdges << endl ; nodes = nodes >= realNodes ? nodes : realNodes ; edges = edges >= realEdges ? edges : realEdges ; if ( nodes != usedNodes ) cerr << " " << usedNodes << " " << nodes << " " << endl ; return true ; }
parse graph in DIMACS graph format into given structures
parse graph in DIMACS graph format into given structures
// check whether graph is compact // parse file line by line // skip comments and empty lines // invalid edge // add parsed edge to graph