1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
|
/**
* \file main.cpp
* \brief This program shows basic use of the claw::graph class.
* \author Julien Jorge
*/
#include <iostream>
#include <fstream>
#include <algorithm>
#include <list>
#include <claw/graph.hpp>
/*---------------------------------------------------------------------------*/
/**
* \brief This class is a vertex in the graph.
* \author Julien Jorge
*/
class vertex
{
public:
int val;
bool operator<(const vertex& s) const { return val < s.val; }
bool operator==(const vertex& s) const { return val == s.val; }
friend std::ostream& operator<<(std::ostream& os, const vertex& s)
{
return os << s.val;
}
}; // class vertex
/*---------------------------------------------------------------------------*/
/**
* \brief An edge in the graph.
* \author Julien Jorge
*/
class edge
{
public:
char symbol;
int weight;
bool operator<(const edge& a) const { return weight < a.weight; }
bool operator==(const edge& a) const
{
return (symbol == a.symbol) && (weight == a.weight);
}
friend std::ostream& operator<<(std::ostream& os, const edge& a)
{
return os << "[" << a.symbol << ", " << a.weight << "]";
}
}; // class edge
/*---------------------------------------------------------------------------*/
/**
* \brief Write a graph in a stream.
*/
template<class S, class A, class Comp>
std::ostream& operator<<( std::ostream& os, const claw::graph<S, A, Comp>& g)
{
typename claw::graph<S, A, Comp>::vertex_iterator it_s;
typename claw::graph<S, A, Comp>::edge_iterator it_e;
// vertices
os << "V = {";
for (it_s = g.vertex_begin(); it_s != g.vertex_end(); ++it_s)
{
os << " " << *it_s;
}
os << " }" << std::endl;
// edges
os << "E = {" << std::endl;
for (it_e = g.edge_begin(); it_e != g.edge_end(); ++it_e)
os << " [" << it_e->label() << ", " << it_e->source() << ", "
<< it_e->target() << "]" << std::endl;
os << " }" << std::endl;
return os;
} // operator<<
/*---------------------------------------------------------------------------*/
/**
* \brief Write some informations about a graph.
*/
void graph_infos( const claw::graph<vertex, edge>& g )
{
std::vector<vertex> v;
g.vertices(v);
std::cout << g.vertices_count() << " vertices" << std::endl;
std::cout << g.edges_count() << " edges" << std::endl;
for(unsigned int i=0; i!=v.size(); ++i)
std::cout << "vertex: " << v[i].val
<< " outer degree: " << g.outer_degree(v[i])
<< " inner degree: " << g.inner_degree(v[i]) << std::endl;
std::cout << "adjacency list" << std::endl;
for(unsigned int i=0; i!=v.size(); ++i)
{
std::vector<vertex> d;
g.neighbours(v[i], d);
std::cout << v[i].val << " : ";
for (unsigned int j=0; j!=d.size(); ++j)
std::cout << d[j].val << " ("
<< g.label(v[i], d[j]).symbol << "); ";
std::cout << std::endl;
}
} // graph_infos()
/*---------------------------------------------------------------------------*/
/**
* \brief Test the reverse iterator on vertices.
*/
void graph_rit_vertices( const claw::graph<vertex, edge>& g )
{
std::cout << ":: reverse vertex iterator: " << std::endl;
claw::graph<vertex, edge>::reverse_vertex_iterator it_s;
std::cout << "=========== increment ++ (end to begin)" << std::endl;
for (it_s = g.vertex_rbegin(); it_s != g.vertex_rend(); ++it_s)
std::cout << *it_s << ", ";
std::cout << std::endl;
std::cout << "=========== decrement -- (begin to end)" << std::endl;
it_s = g.vertex_rend();
do
{
--it_s;
std::cout << *it_s << ", ";
}
while ( it_s != g.vertex_rbegin() );
std::cout << std::endl;
} // graph_rit_vertices()
/*---------------------------------------------------------------------------*/
/**
* \brief Test the iterator on vertices.
*/
void graph_it_vertices( const claw::graph<vertex, edge>& g )
{
std::cout << ":: vertex iterator: " << std::endl;
claw::graph<vertex, edge>::vertex_iterator it_s;
std::list<vertex> v1, v2;
std::cout << "=========== forward" << std::endl;
for (it_s = g.vertex_begin(); it_s != g.vertex_end(); ++it_s)
{
std::cout << *it_s << ", ";
v1.push_back(*it_s);
}
std::cout << std::endl;
std::cout << "=========== then come back to first" << std::endl;
do
{
--it_s;
std::cout << *it_s << ", ";
v2.push_front(*it_s);
}
while (it_s != g.vertex_begin());
std::cout << std::endl;
if ( std::equal( v1.begin(), v1.end(), v2.begin() ) )
std::cout << "OK" << std::endl;
else
std::cout << "Problem" << std::endl;
v1.clear(); v2.clear();
std::cout << "=========== backward" << std::endl;
it_s = g.vertex_end();
do
{
--it_s;
std::cout << *it_s << ", ";
v1.push_front(*it_s);
}
while (it_s != g.vertex_begin());
std::cout << std::endl;
std::cout << "=========== then come back to the end" << std::endl;
for (; it_s != g.vertex_end(); ++it_s)
{
std::cout << *it_s << ", ";
v2.push_back(*it_s);
}
std::cout << std::endl;
if ( std::equal( v1.begin(), v1.end(), v2.begin() ) )
std::cout << "OK" << std::endl;
else
std::cout << "Problem" << std::endl;
vertex s;
std::cout << "scan from vertex=";
std::cin >> s.val;
std::cout << "=========== to the end" << std::endl;
for (it_s=g.vertex_begin(s); it_s!=g.vertex_end(); ++it_s)
std::cout << *it_s << ", ";
std::cout << std::endl;
std::cout << "=========== to the beginning" << std::endl;
it_s=g.vertex_begin(s);
if (it_s!=g.vertex_begin())
do
{
--it_s;
std::cout << *it_s << ", ";
}
while( it_s != g.vertex_begin() );
std::cout << std::endl;
} // graph_it_vertices()
/*---------------------------------------------------------------------------*/
/**
* \brief Test the reverse iterator on edges.
*/
void graph_rit_edges( const claw::graph<vertex, edge>& g )
{
std::cout << ":: reverse edge iterator: " << std::endl;
claw::graph<vertex, edge>::reverse_edge_iterator it_a;
std::cout << "=========== increment ++ (end to begin)" << std::endl;
for (it_a = g.edge_rbegin(); it_a != g.edge_rend(); ++it_a)
std::cout << "<" << it_a->source() << "; " << it_a->label()
<< it_a->target() << ">, ";
std::cout << std::endl;
std::cout << "=========== decrement -- (begin to end)" << std::endl;
it_a = g.edge_rend();
do
{
--it_a;
std::cout << "<" << it_a->source() << "; " << it_a->label()
<< it_a->target() << ">, ";
}
while ( it_a != g.edge_rbegin() );
std::cout << std::endl;
} // graph_rit_edges()
/*---------------------------------------------------------------------------*/
/**
* \brief Test the iterator on edges.
*/
void graph_it_edges( const claw::graph<vertex, edge>& g )
{
std::cout << ":: edge iterator: " << std::endl;
claw::graph<vertex, edge>::edge_iterator it_a;
std::list<edge> v1, v2;
std::cout << "=========== forward" << std::endl;
for (it_a = g.edge_begin(); it_a != g.edge_end(); ++it_a)
{
std::cout << "<" << it_a->source() << "; " << it_a->label()
<< it_a->target() << ">, ";
v1.push_back(it_a->label());
}
std::cout << std::endl;
std::cout << "=========== then come back to the first" << std::endl;
do
{
--it_a;
std::cout << "<" << it_a->source() << "; " << it_a->label()
<< it_a->target() << ">, ";
v2.push_front(it_a->label());
}
while (it_a != g.edge_begin());
std::cout << std::endl;
if ( std::equal( v1.begin(), v1.end(), v2.begin() ) )
std::cout << "OK" << std::endl;
else
std::cout << "Problem" << std::endl;
v1.clear(); v2.clear();
std::cout << "=========== backward" << std::endl;
it_a = g.edge_end();
do
{
--it_a;
std::cout << "<" << it_a->source() << "; " << it_a->label()
<< it_a->target() << ">, ";
v1.push_front(it_a->label());
}
while (it_a != g.edge_begin());
std::cout << std::endl;
std::cout << "=========== then come back to the end" << std::endl;
for (; it_a != g.edge_end(); ++it_a)
{
std::cout << "<" << it_a->source() << "; " << it_a->label()
<< it_a->target() << ">, ";
v2.push_back(it_a->label());
}
std::cout << std::endl;
if ( std::equal( v1.begin(), v1.end(), v2.begin() ) )
std::cout << "OK" << std::endl;
else
std::cout << "Problem" << std::endl;
vertex s1, s2;
std::cout << "scan from the edge starting at vertex=";
std::cin >> s1.val;
std::cout << "and ending at vertex=";
std::cin >> s2.val;
std::cout << "=========== to the end" << std::endl;
for (it_a=g.edge_begin(s1, s2); it_a!=g.edge_end(); ++it_a)
std::cout << "<" << it_a->source() << "; " << it_a->label()
<< it_a->target() << ">, ";
std::cout << std::endl;
std::cout << "=========== to the begining" << std::endl;
it_a=g.edge_begin(s1, s2);
if (it_a!=g.edge_begin())
do
{
--it_a;
std::cout << "<" << it_a->source() << "; " << it_a->label()
<< it_a->target() << ">, ";
}
while( it_a != g.edge_begin() );
std::cout << std::endl;
} // graph_it_edges()
/*---------------------------------------------------------------------------*/
/**
* \brief Load a graph and call the test methods.
*/
void test_graphs( const std::string& filename )
{
claw::graph<vertex, edge> g;
vertex s1, s2;
edge a;
std::ifstream f(filename.c_str());
if (f)
{
while (f >> s1.val)
{
f >> s2.val;
if (s2.val > 0)
{
f >> a.symbol >> a.weight;
g.add_edge(s1, s2, a);
}
else
{
std::cout << s1 << ": isolated" << std::endl;
g.add_vertex(s1);
}
}
f.close();
std::cout << " ----------------------------------- informations"
<< std::endl;
graph_infos( g );
std::cout << " ------------------------------------------ print"
<< std::endl;
std::cout << g << std::endl;
std::cout << " ------------------------------ iterator (states)"
<< std::endl;
graph_it_vertices( g );
std::cout << " ------------------------------- iterator (edges)"
<< std::endl;
graph_it_edges( g );
std::cout << " ---------------------- reverse iterator (states)"
<< std::endl;
graph_rit_vertices( g );
std::cout << " ----------------------- reverse iterator (edges)"
<< std::endl;
graph_rit_edges( g );
}
} // test_graphs()
int main(int argc, char* argv[])
{
if (argc != 2)
{
std::cout << argv[0] << " test_file" << std::endl;
return 1;
}
else
{
try
{
test_graphs(argv[1]);
}
catch( claw::graph_exception& e )
{
std::cerr << e.what() << std::endl;
}
return 0;
}
}
|