os/ossrv/ossrv_pub/boost_apis/boost/graph/read_dimacs.hpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
//=======================================================================
sl@0
     2
// Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
sl@0
     3
// Authors: Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee
sl@0
     4
//
sl@0
     5
// Distributed under the Boost Software License, Version 1.0. (See
sl@0
     6
// accompanying file LICENSE_1_0.txt or copy at
sl@0
     7
// http://www.boost.org/LICENSE_1_0.txt)
sl@0
     8
//=======================================================================
sl@0
     9
sl@0
    10
/*
sl@0
    11
  Reads maximal flow problem in extended DIMACS format.
sl@0
    12
  
sl@0
    13
  Reads from stdin. 
sl@0
    14
sl@0
    15
  This works, but could use some polishing. 
sl@0
    16
*/
sl@0
    17
sl@0
    18
/* ----------------------------------------------------------------- */
sl@0
    19
sl@0
    20
#include <vector>
sl@0
    21
#include <stdio.h>
sl@0
    22
sl@0
    23
namespace boost {
sl@0
    24
sl@0
    25
template <class Graph, class CapacityMap, class ReverseEdgeMap>
sl@0
    26
int read_dimacs_max_flow(Graph& g,
sl@0
    27
                         CapacityMap capacity, 
sl@0
    28
                         ReverseEdgeMap reverse_edge,
sl@0
    29
                         typename graph_traits<Graph>::vertex_descriptor& src,
sl@0
    30
                         typename graph_traits<Graph>::vertex_descriptor& sink)
sl@0
    31
{
sl@0
    32
  //  const int MAXLINE = 100;      /* max line length in the input file */
sl@0
    33
  const int ARC_FIELDS = 3;     /* no of fields in arc line  */
sl@0
    34
  const int NODE_FIELDS = 2;    /* no of fields in node line  */
sl@0
    35
  const int P_FIELDS = 3;       /* no of fields in problem line */
sl@0
    36
  const char* PROBLEM_TYPE = "max"; /* name of problem type*/
sl@0
    37
sl@0
    38
  typedef typename graph_traits<Graph>::vertices_size_type vertices_size_type;
sl@0
    39
  typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
sl@0
    40
  typedef typename graph_traits<Graph>::edge_descriptor edge_descriptor;
sl@0
    41
  
sl@0
    42
  std::vector<vertex_descriptor> verts;
sl@0
    43
sl@0
    44
  long m, n,                    /*  number of edges and nodes */
sl@0
    45
    i, head, tail, cap;
sl@0
    46
sl@0
    47
  long no_lines=0,              /* no of current input line */
sl@0
    48
    no_plines=0,                /* no of problem-lines */
sl@0
    49
    no_nslines=0,               /* no of node-source-lines */
sl@0
    50
    no_nklines=0,               /* no of node-source-lines */
sl@0
    51
    no_alines=0;                /* no of arc-lines */
sl@0
    52
  
sl@0
    53
  std::string in_line;          /* for reading input line */
sl@0
    54
  char pr_type[3];              /* for reading type of the problem */
sl@0
    55
  char nd;                      /* source (s) or sink (t) */
sl@0
    56
  
sl@0
    57
  int k,                        /* temporary */
sl@0
    58
    err_no;                     /* no of detected error */
sl@0
    59
sl@0
    60
  /* -------------- error numbers & error messages ---------------- */
sl@0
    61
  const int EN1   = 0;
sl@0
    62
  const int EN2   = 1;
sl@0
    63
  const int EN3   = 2;
sl@0
    64
  const int EN4   = 3;
sl@0
    65
  //  const int EN6   = 4;
sl@0
    66
  //  const int EN10  = 5;
sl@0
    67
  //  const int EN7   = 6;
sl@0
    68
  const int EN8   = 7;
sl@0
    69
  const int EN9   = 8;
sl@0
    70
  const int EN11  = 9;
sl@0
    71
  const int EN12 = 10;
sl@0
    72
  //  const int EN13 = 11;
sl@0
    73
  const int EN14 = 12;
sl@0
    74
  const int EN16 = 13;
sl@0
    75
  const int EN15 = 14;
sl@0
    76
  const int EN17 = 15;
sl@0
    77
  const int EN18 = 16;
sl@0
    78
  const int EN21 = 17;
sl@0
    79
  const int EN19 = 18;
sl@0
    80
  const int EN20 = 19;
sl@0
    81
  const int EN22 = 20;
sl@0
    82
  
sl@0
    83
  static char *err_message[] = 
sl@0
    84
  { 
sl@0
    85
    /* 0*/    "more than one problem line.",
sl@0
    86
    /* 1*/    "wrong number of parameters in the problem line.",
sl@0
    87
    /* 2*/    "it is not a Max Flow problem line.",
sl@0
    88
    /* 3*/    "bad value of a parameter in the problem line.",
sl@0
    89
    /* 4*/    "can't obtain enough memory to solve this problem.",
sl@0
    90
    /* 5*/    "more than one line with the problem name.",
sl@0
    91
    /* 6*/    "can't read problem name.",
sl@0
    92
    /* 7*/    "problem description must be before node description.",
sl@0
    93
    /* 8*/    "this parser doesn't support multiply sources and sinks.",
sl@0
    94
    /* 9*/    "wrong number of parameters in the node line.",
sl@0
    95
    /*10*/    "wrong value of parameters in the node line.",
sl@0
    96
    /*11*/    " ",
sl@0
    97
    /*12*/    "source and sink descriptions must be before arc descriptions.",
sl@0
    98
    /*13*/    "too many arcs in the input.",
sl@0
    99
    /*14*/    "wrong number of parameters in the arc line.",
sl@0
   100
    /*15*/    "wrong value of parameters in the arc line.",
sl@0
   101
    /*16*/    "unknown line type in the input.",
sl@0
   102
    /*17*/    "reading error.",
sl@0
   103
    /*18*/    "not enough arcs in the input.",
sl@0
   104
    /*19*/    "source or sink doesn't have incident arcs.",
sl@0
   105
    /*20*/    "can't read anything from the input file."
sl@0
   106
  };
sl@0
   107
  /* --------------------------------------------------------------- */
sl@0
   108
sl@0
   109
  /* The main loop:
sl@0
   110
     -  reads the line of the input,
sl@0
   111
     -  analyses its type,
sl@0
   112
     -  checks correctness of parameters,
sl@0
   113
     -  puts data to the arrays,
sl@0
   114
     -  does service functions
sl@0
   115
  */
sl@0
   116
sl@0
   117
  while (std::getline(std::cin, in_line)) {
sl@0
   118
    ++no_lines;
sl@0
   119
sl@0
   120
    switch (in_line[0]) {
sl@0
   121
    case 'c':                  /* skip lines with comments */
sl@0
   122
    case '\n':                 /* skip empty lines   */
sl@0
   123
    case '\0':                 /* skip empty lines at the end of file */
sl@0
   124
      break;
sl@0
   125
      
sl@0
   126
    case 'p':                  /* problem description      */
sl@0
   127
      if ( no_plines > 0 )
sl@0
   128
        /* more than one problem line */
sl@0
   129
        { err_no = EN1 ; goto error; }
sl@0
   130
      
sl@0
   131
      no_plines = 1;
sl@0
   132
      
sl@0
   133
      if (
sl@0
   134
          /* reading problem line: type of problem, no of nodes, no of arcs */
sl@0
   135
          sscanf ( in_line.c_str(), "%*c %3s %ld %ld", pr_type, &n, &m )
sl@0
   136
          != P_FIELDS
sl@0
   137
          )
sl@0
   138
        /*wrong number of parameters in the problem line*/
sl@0
   139
        { err_no = EN2; goto error; }
sl@0
   140
      
sl@0
   141
      if ( strcmp ( pr_type, PROBLEM_TYPE ) )
sl@0
   142
        /*wrong problem type*/
sl@0
   143
        { err_no = EN3; goto error; }
sl@0
   144
      
sl@0
   145
      if ( n <= 0  || m <= 0 )
sl@0
   146
        /*wrong value of no of arcs or nodes*/
sl@0
   147
        { err_no = EN4; goto error; }
sl@0
   148
sl@0
   149
      {
sl@0
   150
        for (long vi = 0; vi < n; ++vi)
sl@0
   151
          verts.push_back(add_vertex(g));
sl@0
   152
      }
sl@0
   153
      break;
sl@0
   154
      
sl@0
   155
    case 'n':                    /* source(s) description */
sl@0
   156
      if ( no_plines == 0 )
sl@0
   157
        /* there was not problem line above */
sl@0
   158
        { err_no = EN8; goto error; }
sl@0
   159
      
sl@0
   160
      /* reading source  or sink */
sl@0
   161
      k = sscanf ( in_line.c_str(),"%*c %ld %c", &i, &nd );
sl@0
   162
      --i; // index from 0
sl@0
   163
      if ( k < NODE_FIELDS )
sl@0
   164
        /* node line is incorrect */
sl@0
   165
        { err_no = EN11; goto error; }
sl@0
   166
      
sl@0
   167
      if ( i < 0 || i > n )
sl@0
   168
        /* wrong value of node */
sl@0
   169
        { err_no = EN12; goto error; }
sl@0
   170
      
sl@0
   171
      switch (nd) {
sl@0
   172
      case 's':  /* source line */
sl@0
   173
        
sl@0
   174
        if ( no_nslines != 0)
sl@0
   175
          /* more than one source line */ 
sl@0
   176
          { err_no = EN9; goto error; }
sl@0
   177
        
sl@0
   178
        no_nslines = 1;
sl@0
   179
        src = verts[i];
sl@0
   180
        break;
sl@0
   181
        
sl@0
   182
      case 't':  /* sink line */
sl@0
   183
        
sl@0
   184
        if ( no_nklines != 0)
sl@0
   185
          /* more than one sink line */
sl@0
   186
          { err_no = EN9; goto error; }
sl@0
   187
        
sl@0
   188
        no_nklines = 1;
sl@0
   189
        sink = verts[i];
sl@0
   190
        break;
sl@0
   191
        
sl@0
   192
      default:
sl@0
   193
        /* wrong type of node-line */
sl@0
   194
        err_no = EN12; goto error; 
sl@0
   195
      }
sl@0
   196
      break;
sl@0
   197
      
sl@0
   198
    case 'a':                    /* arc description */
sl@0
   199
      if ( no_nslines == 0 || no_nklines == 0 ) 
sl@0
   200
        /* there was not source and sink description above */
sl@0
   201
        { err_no = EN14; goto error; }
sl@0
   202
      
sl@0
   203
      if ( no_alines >= m )
sl@0
   204
        /*too many arcs on input*/
sl@0
   205
        { err_no = EN16; goto error; }
sl@0
   206
      
sl@0
   207
      if (
sl@0
   208
          /* reading an arc description */
sl@0
   209
          sscanf ( in_line.c_str(),"%*c %ld %ld %ld",
sl@0
   210
                   &tail, &head, &cap )
sl@0
   211
          != ARC_FIELDS
sl@0
   212
          ) 
sl@0
   213
        /* arc description is not correct */
sl@0
   214
        { err_no = EN15; goto error; }
sl@0
   215
sl@0
   216
      --tail; // index from 0, not 1
sl@0
   217
      --head;
sl@0
   218
      if ( tail < 0  ||  tail > n  ||
sl@0
   219
           head < 0  ||  head > n  
sl@0
   220
           )
sl@0
   221
        /* wrong value of nodes */
sl@0
   222
        { err_no = EN17; goto error; }
sl@0
   223
sl@0
   224
      {      
sl@0
   225
        edge_descriptor e1, e2; 
sl@0
   226
        bool in1, in2;
sl@0
   227
        tie(e1, in1) = add_edge(verts[tail], verts[head], g);
sl@0
   228
        tie(e2, in2) = add_edge(verts[head], verts[tail], g);
sl@0
   229
        if (!in1 || !in2) {
sl@0
   230
          std::cerr << "unable to add edge (" << head << "," << tail << ")" 
sl@0
   231
                    << std::endl;
sl@0
   232
          return -1;
sl@0
   233
        }
sl@0
   234
        capacity[e1] = cap;
sl@0
   235
        capacity[e2] = 0;
sl@0
   236
        reverse_edge[e1] = e2;
sl@0
   237
        reverse_edge[e2] = e1;
sl@0
   238
      }
sl@0
   239
      ++no_alines;
sl@0
   240
      break;
sl@0
   241
      
sl@0
   242
    default:
sl@0
   243
      /* unknown type of line */
sl@0
   244
      err_no = EN18; goto error;
sl@0
   245
      
sl@0
   246
    } /* end of switch */
sl@0
   247
  }     /* end of input loop */
sl@0
   248
  
sl@0
   249
  /* ----- all is red  or  error while reading ----- */ 
sl@0
   250
  
sl@0
   251
  if ( feof (stdin) == 0 ) /* reading error */
sl@0
   252
    { err_no=EN21; goto error; } 
sl@0
   253
  
sl@0
   254
  if ( no_lines == 0 ) /* empty input */
sl@0
   255
    { err_no = EN22; goto error; } 
sl@0
   256
  
sl@0
   257
  if ( no_alines < m ) /* not enough arcs */
sl@0
   258
    { err_no = EN19; goto error; } 
sl@0
   259
  
sl@0
   260
  if ( out_degree(src, g) == 0 || out_degree(sink, g) == 0  ) 
sl@0
   261
    /* no arc goes out of the source */
sl@0
   262
    { err_no = EN20; goto error; }
sl@0
   263
  
sl@0
   264
  /* Thanks God! all is done */
sl@0
   265
  return (0);
sl@0
   266
  
sl@0
   267
  /* ---------------------------------- */
sl@0
   268
 error:  /* error found reading input */
sl@0
   269
  
sl@0
   270
  printf ( "\nline %ld of input - %s\n", 
sl@0
   271
           no_lines, err_message[err_no] );
sl@0
   272
  
sl@0
   273
  exit (1);
sl@0
   274
  return (0); /* to avoid warning */
sl@0
   275
}
sl@0
   276
/* --------------------   end of parser  -------------------*/
sl@0
   277
sl@0
   278
} // namespace boost