converters/snap.cpp
// Nodes have explicit (and arbitrary) node ids. There is no restriction for node 
// ids to be contiguous integers starting at 0. In TUNGraph and TNGraph edges have no 
// explicit ids -- edges are identified by a pair node ids.
Nodes have explicit (and arbitrary) node ids. There is no restriction for node
ids to be contiguous integers starting at 0. In TUNGraph and TNGraph edges have no
explicit ids -- edges are identified by a pair node ids.
Nodes have explicit ( and arbitrary ) node ids . There is no restriction for node ids to be contiguous integers starting at 0 . In TUNGraph and TNGraph edges have no explicit ids -- edges are identified by a pair node ids .
void loadSNAPFile(Graph *&g, const std::string &fileName, Weight_t wtype)
{
  std::ifstream ifs;
  double t0, t1, t2, t3;
  t0 = mytimer();
  ifs.open(fileName.c_str(), std::ifstream::in);
  if (!ifs) {
    std::cerr << "Error opening SNAP format file: " << fileName << std::endl;
    exit(EXIT_FAILURE);
  }
  std::string line;
  GraphElem numEdges = 0, numVertices;
  size_t place = 0;
  while(1) { 
      std::getline(ifs, line);
      if (line[0] == '#')  { 
          std::size_t found_nodes = line.find("Nodes");
          if (found_nodes != std::string::npos) { 
              std::istringstream iss(line);
              std::string key, val;
              int c = 0;
              while(std::getline(iss, key, ':') >> val) {
                  if (c == 0) 
                      numVertices = std::stol(val);
                  else
                      numEdges = std::stol(val);
                  c++;
              }
          }
          place = ifs.tellg();
      }
      else {
          ifs.seekg(place); 
          break;
      }
  }
  t1 = mytimer();
  std::cout << "Loading SNAP file: " << fileName << ", numvertices: " << numVertices <<
    ", numEdges: " << numEdges << std::endl;
  std::cout << "Time taken to open file and parse number of vertices/edges: " 
      << (t1 - t0) << std::endl;
  t2 = mytimer();
  std::vector<GraphElem> edgeCount(numVertices+1);   
  std::vector<GraphElemTuple> edgeList(numEdges*2);  
  std::map<GraphElem, GraphElem> clusterLocalMap; 
  std::map<GraphElem, GraphElem>::iterator storedAlready;
  GraphElem numUniqueVertices = 0;
  GraphElem nnz = 0;
  for (GraphElem i = 0L; i < numEdges; i++) {
      GraphWeight weight;
      GraphElem v0, v1;
      ifs >> v0 >> v1;
      if (!ifs || ifs.fail()) {
          std::cerr << "Error parsing SNAP edge" << std::endl;
          exit(EXIT_FAILURE);
      }
      storedAlready = clusterLocalMap.find(v0);      
      if( storedAlready != clusterLocalMap.end() ) { 
          v0 = storedAlready->second; 
      } else {
          clusterLocalMap[v0] = numUniqueVertices; 
          v0 = numUniqueVertices; 
          numUniqueVertices++;    
      }
      storedAlready = clusterLocalMap.find(v1);         
      if( storedAlready != clusterLocalMap.end() ) {	
          v1 = storedAlready->second; 
      } else {
          clusterLocalMap[v1] = numUniqueVertices; 
          v1 = numUniqueVertices; 
          numUniqueVertices++; 
      }
      if (wtype == RND_WEIGHT)
          weight = genRandom(RANDOM_MIN_WEIGHT, RANDOM_MAX_WEIGHT);
      if (wtype == ONE_WEIGHT)
          weight = 1.0;
      edgeList.emplace_back(v0, v1, weight);
      edgeList.emplace_back(v1, v0, weight);
      edgeCount[v0 + 1]++;
      edgeCount[v1 + 1]++;
      nnz += 2;
  }
  ifs.close();
  assert((numEdges * 2) == nnz);
  t3 = mytimer();
  std::cout << "Time taken to allocate edgeList/edgeCount and read from file: " 
      << (t3 - t2) << std::endl;
  numEdges = nnz;
  t2 = mytimer();
  g = new Graph(numVertices, numEdges);
  processGraphData(*g, edgeCount, edgeList, numVertices, numEdges);
  t3 = mytimer();  
  std::cout << "Graph populate time: " << (t3 - t2) << std::endl;
  std::cout << "Total I/O time: " << (t3 - t0) << std::endl;
}
void loadSNAPFile ( Graph * & g , const std :: string & fileName , Weight_t wtype ) { std :: ifstream ifs ; double t0 , t1 , t2 , t3 ; t0 = mytimer ( ) ; ifs . open ( fileName . c_str ( ) , std :: ifstream :: in ) ; if ( ! ifs ) { std :: cerr << " " << fileName << std :: endl ; exit ( EXIT_FAILURE ) ; } std :: string line ; GraphElem numEdges = 0 , numVertices ; size_t place = 0 ; while ( 1 ) { std :: getline ( ifs , line ) ; if ( line [ 0 ] == ' ' ) { std :: size_t found_nodes = line . find ( " " ) ; if ( found_nodes != std :: string :: npos ) { std :: istringstream iss ( line ) ; std :: string key , val ; int c = 0 ; while ( std :: getline ( iss , key , ' ' ) >> val ) { if ( c == 0 ) numVertices = std :: stol ( val ) ; else numEdges = std :: stol ( val ) ; c ++ ; } } place = ifs . tellg ( ) ; } else { ifs . seekg ( place ) ; break ; } } t1 = mytimer ( ) ; std :: cout << " " << fileName << " " << numVertices << " " << numEdges << std :: endl ; std :: cout << " " << ( t1 - t0 ) << std :: endl ; t2 = mytimer ( ) ; std :: vector < GraphElem > edgeCount ( numVertices + 1 ) ; std :: vector < GraphElemTuple > edgeList ( numEdges * 2 ) ; std :: map < GraphElem , GraphElem > clusterLocalMap ; std :: map < GraphElem , GraphElem > :: iterator storedAlready ; GraphElem numUniqueVertices = 0 ; GraphElem nnz = 0 ; for ( GraphElem i = 0L ; i < numEdges ; i ++ ) { GraphWeight weight ; GraphElem v0 , v1 ; ifs >> v0 >> v1 ; if ( ! ifs || ifs . fail ( ) ) { std :: cerr << " " << std :: endl ; exit ( EXIT_FAILURE ) ; } storedAlready = clusterLocalMap . find ( v0 ) ; if ( storedAlready != clusterLocalMap . end ( ) ) { v0 = storedAlready -> second ; } else { clusterLocalMap [ v0 ] = numUniqueVertices ; v0 = numUniqueVertices ; numUniqueVertices ++ ; } storedAlready = clusterLocalMap . find ( v1 ) ; if ( storedAlready != clusterLocalMap . end ( ) ) { v1 = storedAlready -> second ; } else { clusterLocalMap [ v1 ] = numUniqueVertices ; v1 = numUniqueVertices ; numUniqueVertices ++ ; } if ( wtype == RND_WEIGHT ) weight = genRandom ( RANDOM_MIN_WEIGHT , RANDOM_MAX_WEIGHT ) ; if ( wtype == ONE_WEIGHT ) weight = 1.0 ; edgeList . emplace_back ( v0 , v1 , weight ) ; edgeList . emplace_back ( v1 , v0 , weight ) ; edgeCount [ v0 + 1 ] ++ ; edgeCount [ v1 + 1 ] ++ ; nnz += 2 ; } ifs . close ( ) ; assert ( ( numEdges * 2 ) == nnz ) ; t3 = mytimer ( ) ; std :: cout << " " << ( t3 - t2 ) << std :: endl ; numEdges = nnz ; t2 = mytimer ( ) ; g = new Graph ( numVertices , numEdges ) ; processGraphData ( * g , edgeCount , edgeList , numVertices , numEdges ) ; t3 = mytimer ( ) ; std :: cout << " " << ( t3 - t2 ) << std :: endl ; std :: cout << " " << ( t3 - t0 ) << std :: endl ; }
Nodes have explicit (and arbitrary) node ids.
Nodes have explicit ( and arbitrary ) node ids .
//Parse the comment lines for problem size //Check if this line has problem sizes // found our line // only interested in parsing nodes/edges // move back // start parsing the data in file // each edge stored twice // complete reading the file  // compute the node renumbering by pushing nodeid on  // a map and updating a temp edgeList //Renumber vertices contiguously from zero // update numEdges, as the input graph is // assumed to be undirected and weights are 1 // for node_0 //Check if it already exists //Already exists //Renumber the cluster id //Does not exist, add to the map //Renumber the vertex id //Increment the number // for node_1 //Check if it already exists //Already exists //Renumber the cluster id //Does not exist, add to the map //Renumber the vertex id //Increment the number // edge counts