Skip to content

Instantly share code, notes, and snippets.

Created November 24, 2013 18:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/7630790 to your computer and use it in GitHub Desktop.
Save anonymous/7630790 to your computer and use it in GitHub Desktop.
Graph METISGraphReader::read(std::string path) {
METISParser parser(path);
std::tuple<count, count, index> header = parser.getHeader();
count n = std::get<0>(header);
count m = std::get<1>(header);
index weighted = std::get<2>(header);
//std::tie(n,m,weighted) = header;
// TODO: std::tie(n, m, weighted) = header
Graph G(n);
std::string graphName = Aux::StringTools::split(Aux::StringTools::split(path, '/').back(), '.').front();
G.setName(graphName);
std::cout << "[BEGIN] reading graph G(n=" << n << ", m=" << m << ") from METIS file: " << std::flush; // progress bar follows
double p = 0.0; // percentage for progress bar
node u = 0; // begin with 0
if (weighted == 0) {
while (parser.hasNext()) {
std::vector<node> adjacencies = parser.getNext();
for (index i=0; i < adjacencies.size(); i++) {
node v = adjacencies[i] - 1; // METIS-indices are 1-based
assert (v >= 0);
if (u <= v) { // self-loops are allowed
G.addEdge(u, v);
}
}
u++; // next node
if ((u % ((n + 10)/10)) == 0) {
p = ((double) (u-1) / (double) n) * 100;
std::cout << p << "% " << std::flush;
}
}
std::cout << "[DONE]" << std::endl;
return G;
} else {
while (parser.hasNext()) {
std::vector<std::pair<node,double>> adjacencies = parser.getNextWithWeights();
for (index i=0; i < adjacencies.size(); i++) {
node v = adjacencies[i].first- 1; // METIS-indices are 1-based
assert (v >= 0);
if (u <= v) { // self-loops are allowed
G.addEdge(u, v);
G.setWeight(u, v, adjacencies[i].second);
assert(adjacencies[i].second > 0);
}
}
u += 1; // next node
if ((u % ((n + 10)/10)) == 0) {
p = ((double) (u-1) / (double) n) * 100;
std::cout << p << "% " << std::flush;
}
}
std::cout << "[DONE]" << std::endl;
return G;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment