COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/akos/loader.h @ 132:1ac27e476e25

Last change on this file since 132:1ac27e476e25 was 63:8a39e8b9cdd7, checked in by Akos Ladanyi, 21 years ago

added the loader for the DIMACS file format

File size: 901 bytes
RevLine 
[63]1#ifndef LOADER_H
2#define LOADER_H
3
4#include <map>
5
6#define LINE_LEN 1024
7
8template<typename Graph>
9void LoadGraph(Graph &G, char *filename) {
10  FILE *file;
11  if ((file = fopen(filename, "r")) == NULL) {
12    printf("Could not open %s\n", filename);
13    return;
14  }
15  int i, n1, n2;
16  std::map<int, typename Graph::NodeIt> nmap;
17  char line[LINE_LEN];
18  while (fgets(line, LINE_LEN, file)) {
19    switch (line[0]) {
20      case 'n':
21        nmap[atoi(line + 1)] = G.addNode();
22        break;
23      case 'a':
24        n1 = atoi(line + 1);
25        i = 1;
26        while (isspace(line[i])) i++;
27        while (isdigit(line[i])) i++;
28        n2 = atoi(line + i);
29        if (nmap.find(n1) == nmap.end()) {
30          nmap[n1] = G.addNode();
31        }
32        if (nmap.find(n2) == nmap.end()) {
33          nmap[n2] = G.addNode();
34        }
35        G.addEdge(nmap[n1], nmap[n2]);
36        break;
37    }
38  }
39}
40
41#endif
Note: See TracBrowser for help on using the repository browser.