added the loader for the DIMACS file format
authorladanyi
Wed, 04 Feb 2004 18:59:07 +0000
changeset 638a39e8b9cdd7
parent 62 aa1700f78754
child 64 72bd463289a9
added the loader for the DIMACS file format
src/work/akos/demo.in
src/work/akos/loader.h
src/work/akos/loader_demo.cc
src/work/akos/makefile
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/work/akos/demo.in	Wed Feb 04 18:59:07 2004 +0000
     1.3 @@ -0,0 +1,17 @@
     1.4 +c
     1.5 +c        This is a simple example file to demonstrate the
     1.6 +c     DIMACS input file format for minimum-cost flow problems.
     1.7 +c 
     1.8 +c problem line :
     1.9 +p min 4 5
    1.10 +c
    1.11 +c node descriptor lines :
    1.12 +n 1 4
    1.13 +n 4 -4
    1.14 +c
    1.15 +c arc descriptor lines :
    1.16 +a 1 2 0 4 2
    1.17 +a 1 3 0 2 2
    1.18 +a 2 3 0 2 1
    1.19 +a 2 4 0 3 3
    1.20 +a 3 4 0 5 1
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/work/akos/loader.h	Wed Feb 04 18:59:07 2004 +0000
     2.3 @@ -0,0 +1,41 @@
     2.4 +#ifndef LOADER_H
     2.5 +#define LOADER_H
     2.6 +
     2.7 +#include <map>
     2.8 +
     2.9 +#define LINE_LEN 1024
    2.10 +
    2.11 +template<typename Graph>
    2.12 +void LoadGraph(Graph &G, char *filename) {
    2.13 +  FILE *file;
    2.14 +  if ((file = fopen(filename, "r")) == NULL) {
    2.15 +    printf("Could not open %s\n", filename);
    2.16 +    return;
    2.17 +  }
    2.18 +  int i, n1, n2;
    2.19 +  std::map<int, typename Graph::NodeIt> nmap;
    2.20 +  char line[LINE_LEN];
    2.21 +  while (fgets(line, LINE_LEN, file)) {
    2.22 +    switch (line[0]) {
    2.23 +      case 'n':
    2.24 +        nmap[atoi(line + 1)] = G.addNode();
    2.25 +        break;
    2.26 +      case 'a':
    2.27 +        n1 = atoi(line + 1);
    2.28 +        i = 1;
    2.29 +        while (isspace(line[i])) i++;
    2.30 +        while (isdigit(line[i])) i++;
    2.31 +        n2 = atoi(line + i);
    2.32 +        if (nmap.find(n1) == nmap.end()) {
    2.33 +          nmap[n1] = G.addNode();
    2.34 +        }
    2.35 +        if (nmap.find(n2) == nmap.end()) {
    2.36 +          nmap[n2] = G.addNode();
    2.37 +        }
    2.38 +        G.addEdge(nmap[n1], nmap[n2]);
    2.39 +        break;
    2.40 +    }
    2.41 +  }
    2.42 +}
    2.43 +
    2.44 +#endif
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/work/akos/loader_demo.cc	Wed Feb 04 18:59:07 2004 +0000
     3.3 @@ -0,0 +1,34 @@
     3.4 +#include <vector>
     3.5 +#include <iostream>
     3.6 +#include <list_graph.hh>
     3.7 +#include <bfs_iterator.hh>
     3.8 +#include <loader.h>
     3.9 +
    3.10 +using namespace marci;
    3.11 +
    3.12 +int main(int, char **) {
    3.13 +  typedef ListGraph::NodeIt NodeIt;
    3.14 +  typedef ListGraph::EdgeIt EdgeIt;
    3.15 +  typedef ListGraph::EachNodeIt EachNodeIt;
    3.16 +  typedef ListGraph::EachEdgeIt EachEdgeIt;
    3.17 +  typedef ListGraph::OutEdgeIt OutEdgeIt;
    3.18 +  typedef ListGraph::InEdgeIt InEdgeIt;
    3.19 +  typedef ListGraph::SymEdgeIt SymEdgeIt;
    3.20 +
    3.21 +  ListGraph G;
    3.22 +  LoadGraph(G, "demo.in");
    3.23 +
    3.24 +  std::cout << "bfs from the first node" << std::endl;
    3.25 +  bfs<ListGraph> bfs_test(G, G.first<EachNodeIt>());
    3.26 +  bfs_test.run();
    3.27 +  std::cout << "reached: ";
    3.28 +  for(EachNodeIt i=G.first<EachNodeIt>(); i.valid(); ++i) {
    3.29 +    std::cout << bfs_test.reached.get(i) << " ";
    3.30 +  }
    3.31 +  std::cout<<std::endl;
    3.32 +  std::cout << "dist: ";
    3.33 +  for(EachNodeIt i=G.first<EachNodeIt>(); i.valid(); ++i) {
    3.34 +    std::cout << bfs_test.dist.get(i) << " ";
    3.35 +  }
    3.36 +  std::cout<<std::endl;
    3.37 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/work/akos/makefile	Wed Feb 04 18:59:07 2004 +0000
     4.3 @@ -0,0 +1,9 @@
     4.4 +CXXFLAGS = -g -Wall -ansi -pedantic
     4.5 +CXX := $(shell type -p g++-3.3 || type -p g++-3.2 || type -p g++-3.0 || type -p g++-3 || echo g++)
     4.6 +
     4.7 +loader_demo: loader_demo.cc ../list_graph.hh ../bfs_iterator.hh loader.h
     4.8 +	$(CXX) $(CXXFLAGS) -I. -I.. loader_demo.cc -o loader_demo 
     4.9 +clean:
    4.10 +	$(RM) loader_demo
    4.11 +
    4.12 +.PHONY: clean