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
|
#include "claw/graph.hpp"
#include "claw/graph_algorithm.hpp"
#include <iostream>
#include <fstream>
/*---------------------------------------------------------------------------*/
/**
* \brief A vertex of 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 of 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
/*---------------------------------------------------------------------------*/
class pl_scan_events:
public claw::scan_events< claw::graph<vertex, edge> >
{
public:
pl_scan_events( const vertex& s )
{
distances[s] = 0;
}
void visit_edge( const vertex& source, const vertex& dest )
{
parents[dest] = source;
distances[dest] = distances[source] + 1;
}
std::map<vertex, unsigned int> distances;
std::map<vertex, vertex> parents;
}; // class pl_scan_events
/*---------------------------------------------------------------------------*/
class pp_scan_events:
public claw::scan_events< claw::graph<vertex, edge> >
{
public:
pp_scan_events()
{
m_date = 0;
}
void start(const vertex& s)
{
++m_date;
dates[s].first = m_date;
}
void visit_edge( const vertex& source, const vertex& dest )
{
parents[dest] = source;
}
void end(const vertex& s)
{
++m_date;
dates[s].second = m_date;
}
std::map<vertex, std::pair<unsigned int, unsigned int> > dates;
std::map<vertex, vertex> parents;
private:
unsigned int m_date;
}; // class pp_scan_events
/*---------------------------------------------------------------------------*/
/**
* \brief Output a text representation of a graph.
*/
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 << "S = {";
for (it_s = g.vertex_begin(); it_s != g.vertex_end(); ++it_s)
{
os << " " << *it_s;
}
os << " }" << std::endl;
// edges
os << "A = {" << 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<<
/*---------------------------------------------------------------------------*/
void test_pl(const claw::graph<vertex, edge>& g)
{
vertex s;
std::cout << "Distances, computed from vertex : " << std::endl;
std::cin >> s.val;
pl_scan_events events(s);
claw::breadth_scan<claw::graph<vertex, edge>, pl_scan_events> scan
( g, s, events );
scan();
std::map<vertex, unsigned int>::const_iterator it_d;
for (it_d = events.distances.begin(); it_d!=events.distances.end(); ++it_d)
std::cout << it_d->first.val << " to " << it_d->second << std::endl;
std::map<vertex, vertex>::const_iterator it_s;
for (it_s = events.parents.begin(); it_s!=events.parents.end(); ++it_s)
std::cout << it_s->second.val << " parent of "
<< it_s->first.val << std::endl;
}
/*---------------------------------------------------------------------------*/
void test_pp(const claw::graph<vertex, edge>& g)
{
pp_scan_events events;
claw::depth_scan<claw::graph<vertex, edge>, pp_scan_events> scan( g, events );
scan();
std::map<vertex, std::pair<unsigned int,unsigned int> >::const_iterator it_d;
for (it_d = events.dates.begin(); it_d!=events.dates.end(); ++it_d)
std::cout << it_d->first.val << " : " << it_d->second.first << ","
<< it_d->second.second << std::endl;
std::map<vertex, vertex>::const_iterator it_s;
for (it_s = events.parents.begin(); it_s!=events.parents.end(); ++it_s)
std::cout << it_s->second.val << " parent of "
<< it_s->first.val << std::endl;
}
/*---------------------------------------------------------------------------*/
void test_topological_sort(const claw::graph<vertex, edge>& g)
{
claw::topological_sort< claw::graph<vertex, edge> > sort;
claw::topological_sort< claw::graph<vertex, edge> >::const_iterator it;
sort(g);
for (it = sort.begin(); it!=sort.end(); ++it)
std::cout << *it << std::endl;
} // test_topological_sort()
/*---------------------------------------------------------------------------*/
bool 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 << " : isol" << std::endl;
g.add_vertex(s1);
}
}
f.close();
std::cout << " -------------------------------------- display"
<< std::endl;
std::cout << g << std::endl;
std::cout << " ------------------- breadth scan"
<< std::endl;
test_pl( g );
std::cout << " ------------------- deep scan"
<< std::endl;
test_pp( g );
std::cout << " ------------------- topological order"
<< std::endl;
test_topological_sort( g );
return true;
}
else
return false;
} // test_graphs()
/*---------------------------------------------------------------------------*/
int main(int argc, char* argv[])
{
if (argc != 2)
{
std::cout << argv[0] << " test_file" << std::endl;
return 1;
}
else
{
try
{
if ( !test_graphs(argv[1]) )
std::cerr << "Can't process file '" << argv[1] << '\''
<< std::endl;
}
catch( claw::graph_exception& e )
{
std::cerr << e.what() << std::endl;
}
return 0;
}
}
|