PandA-2024.02
test_maxclique_dsatur_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  */
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  typedef adjacency_list<vecS, vecS, undirectedS> Graph1;
56  typedef graph_traits<Graph1>::vertex_descriptor vertex_descriptor;
57  typedef graph_traits<Graph1>::vertices_size_type vertices_size_type;
58  typedef property_map<Graph1, vertex_index_t>::const_type vertex_index_map;
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  verts.push_back(add_vertex(g1));
185  }
186  break;
187  }
188 
189  case 'e': /* edge description */
190  {
191  if(no_plines == 0)
192  /* there was not problem line above */
193  {
194  err_no = EN8;
195  goto error;
196  }
197 
198  if(
199  /* reading an edge description */
200  sscanf(in_line.c_str(), "%*c %ld %ld", &tail, &head) != EDGE_FIELDS)
201  /* arc description is not correct */
202  {
203  err_no = EN15;
204  goto error;
205  }
206  --tail; // index from 0, not 1
207  --head;
208  if(tail < 0 || tail > VERTICES || head < 0 || head > VERTICES)
209  /* wrong value of nodes */
210  {
211  err_no = EN17;
212  goto error;
213  }
214  graph_traits<Graph1>::edge_descriptor e1;
215  bool in1;
216  tie(e1, in1) = add_edge(verts[tail], verts[head], g1);
217  if(!in1)
218  {
219  std::cerr << "unable to add edge (" << head << "," << tail << ")" << std::endl;
220  exit(1);
221  }
222 
223  ++no_elines;
224  break;
225  }
226  default:
227  {
228  /* unknown type of line */
229  err_no = EN18;
230  goto error;
231  }
232 
233  } /* end of switch */
234  } /* end of input loop */
235 
236  /* ----- all is read or error while reading ----- */
237 
238  if(!in.eof()) /* reading error */
239  {
240  err_no = EN21;
241  goto error;
242  }
243 
244  if(no_lines == 0) /* empty input */
245  {
246  err_no = EN22;
247  goto error;
248  }
249 
250  if(no_elines < number_of_edges) /* not enough arcs */
251  {
252  err_no = EN19;
253  goto error;
254  }
255 
256  /* Thanks God! all is done */
257 
258  {
259  std::vector<vertices_size_type> color_vec(num_vertices(g1));
260  iterator_property_map<vertices_size_type*, vertex_index_map, vertices_size_type, vertices_size_type&> color(
261  &color_vec.front(), get(vertex_index, g1));
262  vertices_size_type num_colors = maxclique_dsatur_coloring(g1, color);
263  std::cout << "Boost colors are: " << num_colors << std::endl;
264  }
265  return (0);
266  /* ---------------------------------- */
267 error: /* error found reading input */
268 
269  printf("\nline %ld of input - %s\n", no_lines, err_message[err_no]);
270 
271  exit(1);
272  return (1); /* to avoid warning */
273 }
This algorithm is to find coloring of a graph Algorithm: Let G = (V,E) be a graph with vertices v_1...
Boost-based implementation of an exact sequential coloring algorithm based on the work of Olivier Cou...
property_traits< ColorMap >::value_type maxclique_dsatur_coloring(const VertexListGraph &G, ColorMap color)
int main(int argc, char *argv[])

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