PandA-2024.02
test_dsatur2_coloring.cpp
Go to the documentation of this file.
1 /*
2  *
3  * _/_/_/ _/_/ _/ _/ _/_/_/ _/_/
4  * _/ _/ _/ _/ _/_/ _/ _/ _/ _/ _/
5  * _/_/_/ _/_/_/_/ _/ _/_/ _/ _/ _/_/_/_/
6  * _/ _/ _/ _/ _/ _/ _/ _/ _/
7  * _/ _/ _/ _/ _/ _/_/_/ _/ _/
8  *
9  * ***********************************************
10  * PandA Project
11  * URL: http://panda.dei.polimi.it
12  * Politecnico di Milano - DEIB
13  * System Architectures Group
14  * ***********************************************
15  * Copyright (C) 2004-2024 Politecnico di Milano
16  *
17  * This file is part of the PandA framework.
18  *
19  * The PandA framework is free software; you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation; either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program. If not, see <http://www.gnu.org/licenses/>.
31  *
32  */
44 #include "dsatur2_coloring.hpp"
45 #include <boost/graph/adjacency_list.hpp>
46 #include <boost/graph/adjacency_list_io.hpp>
47 #include <cstring>
48 #include <fstream>
49 #include <iosfwd>
50 #include <utility>
51 
52 using namespace boost;
53 int main(int argc, char* argv[])
54 {
55  using Graph1 = adjacency_list<vecS, vecS, undirectedS>;
56  using vertex_descriptor = graph_traits<Graph1>::vertex_descriptor;
57  using vertices_size_type = graph_traits<Graph1>::vertices_size_type;
58  using vertex_index_map = property_map<Graph1, vertex_index_t>::const_type;
59  std::vector<vertex_descriptor> verts;
60  if(argc < 2)
61  {
62  printf("Provide data file in command.\n");
63  exit(1);
64  }
65  // read Graph1
66  Graph1 g1;
67  const int EDGE_FIELDS = 2; /* no of fields in edge line */
68  const int P_FIELDS = 3; /* no of fields in problem line */
69  const char* PROBLEM_TYPE = "edge"; /* name of problem type*/
70  long VERTICES, /*number of vertices*/
71  number_of_edges, /* number of edges and nodes */
72  head, tail;
73 
74  long no_lines = 0, /* no of current input line */
75  no_plines = 0, /* no of problem-lines */
76  no_elines = 0; /* no of edge-lines */
77 
78  std::string in_line; /* for reading input line */
79  char pr_type[5]; /* for reading type of the problem */
80 
81  int err_no; /* no of detected error */
82 
83  /* -------------- error numbers & error messages ---------------- */
84  const int EN1 = 0;
85  const int EN2 = 1;
86  const int EN3 = 2;
87  const int EN4 = 3;
88  // const int EN6 = 4;
89  // const int EN10 = 5;
90  // const int EN7 = 6;
91  const int EN8 = 7;
92  // const int EN9 = 8;
93  // const int EN11 = 9;
94  // const int EN12 = 10;
95  // const int EN13 = 11;
96  // const int EN14 = 12;
97  // const int EN16 = 13;
98  const int EN15 = 14;
99  const int EN17 = 15;
100  const int EN18 = 16;
101  const int EN21 = 17;
102  const int EN19 = 18;
103  // const int EN20 = 19;
104  const int EN22 = 20;
105 
106  static const char* err_message[] = {/* 0*/ "more than one problem line.",
107  /* 1*/ "wrong number of parameters in the problem line.",
108  /* 2*/ "it is not a Max Flow problem line.",
109  /* 3*/ "bad value of a parameter in the problem line.",
110  /* 4*/ "can't obtain enough memory to solve this problem.",
111  /* 5*/ "more than one line with the problem name.",
112  /* 6*/ "can't read problem name.",
113  /* 7*/ "problem description must be before node description.",
114  /* 8*/ "this parser doesn't support multiply sources and sinks.",
115  /* 9*/ "wrong number of parameters in the node line.",
116  /*10*/ "wrong value of parameters in the node line.",
117  /*11*/ " ",
118  /*12*/ "source and sink descriptions must be before arc descriptions.",
119  /*13*/ "too many arcs in the input.",
120  /*14*/ "wrong number of parameters in the arc line.",
121  /*15*/ "wrong value of parameters in the arc line.",
122  /*16*/ "unknown line type in the input.",
123  /*17*/ "reading error.",
124  /*18*/ "not enough arcs in the input.",
125  /*19*/ "source or sink doesn't have incident arcs.",
126  /*20*/ "can't read anything from the input file."};
127  /* --------------------------------------------------------------- */
128 
129  /* The main loop:
130  - reads the line of the input,
131  - analyses its type,
132  - checks correctness of parameters,
133  - puts data to the arrays,
134  - does service functions
135  */
136  std::ifstream in(argv[1]);
137 
138  while(std::getline(in, in_line))
139  {
140  ++no_lines;
141 
142  switch(in_line[0])
143  {
144  case 'c': /* skip lines with comments */
145  case '\n': /* skip empty lines */
146  case '\0': /* skip empty lines at the end of file */
147  break;
148 
149  case 'p': /* problem description */
150  {
151  if(no_plines > 0)
152  /* more than one problem line */
153  {
154  err_no = EN1;
155  goto error;
156  }
157 
158  no_plines = 1;
159  if(
160  /* reading problem line: type of problem, no of nodes, no of arcs */
161  sscanf(in_line.c_str(), "%*c %4s %ld %ld", pr_type, &VERTICES, &number_of_edges) != P_FIELDS)
162  /*wrong number of parameters in the problem line*/
163  {
164  err_no = EN2;
165  goto error;
166  }
167 
168  if(strcmp(pr_type, PROBLEM_TYPE))
169  /*wrong problem type*/
170  {
171  err_no = EN3;
172  goto error;
173  }
174 
175  if(VERTICES <= 0 || number_of_edges <= 0)
176  /*wrong value of no of edges or nodes*/
177  {
178  err_no = EN4;
179  goto error;
180  }
181 
182  {
183  for(long vi = 0; vi < VERTICES; ++vi)
184  {
185  verts.push_back(add_vertex(g1));
186  }
187  }
188  break;
189  }
190 
191  case 'e': /* edge description */
192  {
193  if(no_plines == 0)
194  /* there was not problem line above */
195  {
196  err_no = EN8;
197  goto error;
198  }
199 
200  if(
201  /* reading an edge description */
202  sscanf(in_line.c_str(), "%*c %ld %ld", &tail, &head) != EDGE_FIELDS)
203  /* arc description is not correct */
204  {
205  err_no = EN15;
206  goto error;
207  }
208  --tail; // index from 0, not 1
209  --head;
210  if(tail < 0 || tail > VERTICES || head < 0 || head > VERTICES)
211  /* wrong value of nodes */
212  {
213  err_no = EN17;
214  goto error;
215  }
216  graph_traits<Graph1>::edge_descriptor e1;
217  bool in1;
218  tie(e1, in1) = add_edge(verts[tail], verts[head], g1);
219  if(!in1)
220  {
221  std::cerr << "unable to add edge (" << head << "," << tail << ")" << std::endl;
222  exit(1);
223  }
224 
225  ++no_elines;
226  break;
227  }
228  default:
229  {
230  /* unknown type of line */
231  err_no = EN18;
232  goto error;
233  }
234 
235  } /* end of switch */
236  } /* end of input loop */
237 
238  /* ----- all is read or error while reading ----- */
239 
240  if(!in.eof()) /* reading error */
241  {
242  err_no = EN21;
243  goto error;
244  }
245 
246  if(no_lines == 0) /* empty input */
247  {
248  err_no = EN22;
249  goto error;
250  }
251 
252  if(no_elines < number_of_edges) /* not enough arcs */
253  {
254  err_no = EN19;
255  goto error;
256  }
257 
258  /* Thanks God! all is done */
259 
260  {
261  std::vector<vertices_size_type> color_vec(num_vertices(g1));
262  iterator_property_map<vertices_size_type*, vertex_index_map, vertices_size_type, vertices_size_type&> color(
263  &color_vec.front(), get(vertex_index, g1));
264  vertices_size_type num_colors = dsatur2_coloring(g1, color);
265  std::cout << "Boost colors are: " << num_colors << std::endl;
266  }
267  return (0);
268  /* ---------------------------------- */
269 error: /* error found reading input */
270 
271  printf("\nline %ld of input - %s\n", no_lines, err_message[err_no]);
272 
273  exit(1);
274  return (1); /* to avoid warning */
275 }
This algorithm is to find coloring of a graph Algorithm: Let G = (V,E) be a graph with vertices v_1...
property_traits< ColorMap >::value_type dsatur2_coloring(const VertexListGraph &G, ColorMap color)
coloring of a graph following the DSATUR heuristic (version2)
Boost-based implementation of a heuristic sequential coloring algorithm based on the work of Daniel B...
int main(int argc, char *argv[])

Generated on Mon Feb 12 2024 13:02:50 for PandA-2024.02 by doxygen 1.8.13