Merge branch 'whitespace_and_formatting' of https://github.com/anadon/graph into pr171_2

Resolved Conflicts:
 include/boost/graph/one_bit_color_map.hpp
 include/boost/graph/two_bit_color_map.hpp
This commit is contained in:
jzmaddock 2019-08-26 18:09:24 +01:00
commit fa6871ef4d
743 changed files with 76329 additions and 71081 deletions

View File

@ -1,5 +1,5 @@
BasedOnStyle: WebKit
Standard: Cpp11
Standard: Cpp03
AlignAfterOpenBracket: false
AlignEscapedNewlinesLeft: true
AlwaysBreakAfterDefinitionReturnType: None
@ -13,4 +13,3 @@ SortIncludes: false
AlignTrailingComments: false
SpacesInAngles: true

View File

@ -30,9 +30,28 @@
using namespace boost;
enum { a, b, c, d, e, f, g, h };
enum { _1, _2, _3, _4, _5, _6, _7, _8 };
enum
{
a,
b,
c,
d,
e,
f,
g,
h
};
enum
{
_1,
_2,
_3,
_4,
_5,
_6,
_7,
_8
};
void test_isomorphism()
{
@ -74,23 +93,22 @@ void test_isomorphism()
add_edge(_8, _1, Gb);
add_edge(_8, _7, Gb);
std::vector< std::size_t > in_degree_A(num_vertices(Ga));
boost::detail::compute_in_degree(Ga, &in_degree_A[0]);
std::vector< std::size_t > in_degree_B(num_vertices(Gb));
boost::detail::compute_in_degree(Gb, &in_degree_B[0]);
degree_vertex_invariant<std::size_t*, GraphA>
invariantA(&in_degree_A[0], Ga);
degree_vertex_invariant<std::size_t*, GraphB>
invariantB(&in_degree_B[0], Gb);
degree_vertex_invariant< std::size_t*, GraphA > invariantA(
&in_degree_A[0], Ga);
degree_vertex_invariant< std::size_t*, GraphB > invariantB(
&in_degree_B[0], Gb);
std::vector<graph_traits<GraphB>::vertex_descriptor> f(num_vertices(Ga));
std::vector< graph_traits< GraphB >::vertex_descriptor > f(
num_vertices(Ga));
bool ret = isomorphism(Ga, Gb, &f[0], invariantA, invariantB,
(invariantB.max)(),
get(vertex_index, Ga), get(vertex_index, Gb));
(invariantB.max)(), get(vertex_index, Ga), get(vertex_index, Gb));
assert(ret == true);
for (std::size_t i = 0; i < num_vertices(Ga); ++i)

View File

@ -26,7 +26,10 @@ namespace std
namespace boost
{
enum vertex_compile_cost_t { vertex_compile_cost };
enum vertex_compile_cost_t
{
vertex_compile_cost
};
BOOST_INSTALL_PROPERTY(vertex, compile_cost);
}
@ -47,8 +50,7 @@ typedef adjacency_list< listS, // Store out-edges of each vertex in a std::list
typedef graph_traits< file_dep_graph2 >::vertex_descriptor vertex_t;
typedef graph_traits< file_dep_graph2 >::edge_descriptor edge_t;
int
main(int argc, const char** argv)
int main(int argc, const char** argv)
{
std::ifstream file_in(argc >= 2 ? argv[1] : "makefile-dependencies.dat");
typedef graph_traits< file_dep_graph2 >::vertices_size_type size_type;
@ -64,8 +66,9 @@ main(int argc, const char** argv)
while (file_in >> p)
add_edge(id2vertex[p.first], id2vertex[p.second], g);
#else
std::istream_iterator<std::pair<size_type, size_type> >
input_begin(file_in), input_end;
std::istream_iterator< std::pair< size_type, size_type > > input_begin(
file_in),
input_end;
file_dep_graph2 g(input_begin, input_end, n_vertices);
#endif
@ -77,19 +80,21 @@ main(int argc, const char** argv)
compile_cost_map_t compile_cost_map = get(vertex_compile_cost, g);
std::ifstream name_in(argc >= 3 ? argv[2] : "makefile-target-names.dat");
std::ifstream compile_cost_in(argc >= 4 ? argv[3] : "target-compile-costs.dat");
std::ifstream compile_cost_in(
argc >= 4 ? argv[3] : "target-compile-costs.dat");
graph_traits< file_dep_graph2 >::vertex_iterator vi, vi_end;
for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) {
for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
{
name_in >> name_map[*vi];
compile_cost_in >> compile_cost_map[*vi];
}
graph_property_iter_range< file_dep_graph2,
vertex_compile_cost_t >::iterator ci, ci_end;
vertex_compile_cost_t >::iterator ci,
ci_end;
boost::tie(ci, ci_end) = get_property_iter_range(g, vertex_compile_cost);
std::cout << "total (sequential) compile time: "
<< std::accumulate(ci, ci_end, 0.0) << std::endl;
return 0;
}

View File

@ -35,7 +35,8 @@ struct Actor
};
typedef adjacency_list< vecS, vecS, undirectedS, Actor,
property<edge_centrality_t, double> > ActorGraph;
property< edge_centrality_t, double > >
ActorGraph;
typedef graph_traits< ActorGraph >::vertex_descriptor Vertex;
typedef graph_traits< ActorGraph >::edge_descriptor Edge;
@ -44,48 +45,58 @@ void load_actor_graph(std::istream& in, ActorGraph& g)
std::map< int, Vertex > actors;
std::string line;
while (getline(in, line)) {
while (getline(in, line))
{
std::vector< Vertex > actors_in_movie;
// Map from the actor numbers on this line to the actor vertices
typedef tokenizer< char_separator< char > > Tok;
Tok tok(line, char_separator< char >(" "));
for (Tok::iterator id = tok.begin(); id != tok.end(); ++id) {
for (Tok::iterator id = tok.begin(); id != tok.end(); ++id)
{
int actor_id = lexical_cast< int >(*id);
std::map< int, Vertex >::iterator v = actors.find(actor_id);
if (v == actors.end()) {
if (v == actors.end())
{
Vertex new_vertex = add_vertex(Actor(actor_id), g);
actors[actor_id] = new_vertex;
actors_in_movie.push_back(new_vertex);
} else {
}
else
{
actors_in_movie.push_back(v->second);
}
}
for (std::vector< Vertex >::iterator i = actors_in_movie.begin();
i != actors_in_movie.end(); ++i) {
i != actors_in_movie.end(); ++i)
{
for (std::vector< Vertex >::iterator j = i + 1;
j != actors_in_movie.end(); ++j) {
if (!edge(*i, *j, g).second) add_edge(*i, *j, g);
j != actors_in_movie.end(); ++j)
{
if (!edge(*i, *j, g).second)
add_edge(*i, *j, g);
}
}
}
}
template < typename Graph, typename VertexIndexMap, typename VertexNameMap >
std::ostream&
write_pajek_graph(std::ostream& out, const Graph& g,
std::ostream& write_pajek_graph(std::ostream& out, const Graph& g,
VertexIndexMap vertex_index, VertexNameMap vertex_name)
{
out << "*Vertices " << num_vertices(g) << '\n';
typedef typename graph_traits< Graph >::vertex_iterator vertex_iterator;
for (vertex_iterator v = vertices(g).first; v != vertices(g).second; ++v) {
out << get(vertex_index, *v)+1 << " \"" << get(vertex_name, *v) << "\"\n";
for (vertex_iterator v = vertices(g).first; v != vertices(g).second; ++v)
{
out << get(vertex_index, *v) + 1 << " \"" << get(vertex_name, *v)
<< "\"\n";
}
out << "*Edges\n";
typedef typename graph_traits< Graph >::edge_iterator edge_iterator;
for (edge_iterator e = edges(g).first; e != edges(g).second; ++e) {
for (edge_iterator e = edges(g).first; e != edges(g).second; ++e)
{
out << get(vertex_index, source(*e, g)) + 1 << ' '
<< get(vertex_index, target(*e, g)) + 1 << " 1.0\n"; // HACK!
}
@ -97,14 +108,17 @@ class actor_clustering_threshold : public bc_clustering_threshold<double>
typedef bc_clustering_threshold< double > inherited;
public:
actor_clustering_threshold(double threshold, const ActorGraph& g,
bool normalize)
: inherited(threshold, g, normalize), iter(1) { }
actor_clustering_threshold(
double threshold, const ActorGraph& g, bool normalize)
: inherited(threshold, g, normalize), iter(1)
{
}
bool operator()(double max_centrality, Edge e, const ActorGraph& g)
{
std::cout << "Iter: " << iter << " Max Centrality: "
<< (max_centrality / dividend) << std::endl;
std::cout << "Iter: " << iter
<< " Max Centrality: " << (max_centrality / dividend)
<< std::endl;
++iter;
return inherited::operator()(max_centrality, e, g);
}
@ -123,27 +137,41 @@ int main(int argc, char* argv[])
// Parse command-line options
{
int on_arg = 1;
while (on_arg < argc) {
while (on_arg < argc)
{
std::string arg(argv[on_arg]);
if (arg == "-in") {
++on_arg; assert(on_arg < argc);
if (arg == "-in")
{
++on_arg;
assert(on_arg < argc);
in_file = argv[on_arg];
} else if (arg == "-out") {
++on_arg; assert(on_arg < argc);
}
else if (arg == "-out")
{
++on_arg;
assert(on_arg < argc);
out_file = argv[on_arg];
} else if (arg == "-threshold") {
++on_arg; assert(on_arg < argc);
}
else if (arg == "-threshold")
{
++on_arg;
assert(on_arg < argc);
threshold = lexical_cast< double >(argv[on_arg]);
} else if (arg == "-normalize") {
}
else if (arg == "-normalize")
{
normalize = true;
} else {
}
else
{
std::cerr << "Unrecognized parameter \"" << arg << "\".\n";
return -1;
}
++on_arg;
}
if (in_file.empty() || out_file.empty() || threshold < 0) {
if (in_file.empty() || out_file.empty() || threshold < 0)
{
std::cerr << "error: syntax is actor_clustering [options]\n\n"
<< "options are:\n"
<< "\t-in <infile>\tInput file\n"
@ -160,8 +188,10 @@ int main(int argc, char* argv[])
{
std::cout << "Building graph." << std::endl;
std::ifstream in(in_file.c_str());
if (!in) {
std::cerr << "Unable to open file \"" << in_file << "\" for input.\n";
if (!in)
{
std::cerr << "Unable to open file \"" << in_file
<< "\" for input.\n";
return -2;
}
load_actor_graph(in, g);
@ -177,8 +207,10 @@ int main(int argc, char* argv[])
{
std::cout << "Writing graph to file: " << out_file << std::endl;
std::ofstream out(out_file.c_str());
if (!out) {
std::cerr << "Unable to open file \"" << out_file << "\" for output.\n";
if (!out)
{
std::cerr << "Unable to open file \"" << out_file
<< "\" for output.\n";
return -3;
}
write_pajek_graph(out, g, get(vertex_index, g), get(&Actor::id, g));

View File

@ -11,12 +11,12 @@
#include <boost/graph/adjacency_list.hpp>
#include <iostream>
int
main()
int main()
{
using namespace boost;
typedef adjacency_list< vecS, vecS, bidirectionalS, no_property,
property<int, edge_weight_t>, no_property, vecS> Graph;
property< int, edge_weight_t >, no_property, vecS >
Graph;
const std::size_t n = 3;
typedef std::pair< std::size_t, std::size_t > E;
@ -25,7 +25,8 @@ main()
Graph g(edge_array, edge_array + m, n);
graph_traits< Graph >::edge_iterator edge_iterator;
for (std::size_t i = 0; i < m; ++i) {
for (std::size_t i = 0; i < m; ++i)
{
const graph_traits< Graph >::edge_iterator e = edges(g).first + i;
std::cout << *e << " ";
}

View File

@ -37,32 +37,34 @@
*/
struct EdgeProperties {
struct EdgeProperties
{
EdgeProperties(const std::string& n) : name(n) {}
std::string name;
};
struct VertexProperties {
struct VertexProperties
{
std::size_t index;
boost::default_color_type color;
};
int main(int, char*[])
{
using namespace boost;
using namespace std;
typedef adjacency_list<vecS, listS, undirectedS,
VertexProperties, EdgeProperties> Graph;
typedef adjacency_list< vecS, listS, undirectedS, VertexProperties,
EdgeProperties >
Graph;
const int V = 5;
Graph g(V);
property_map<Graph, std::size_t VertexProperties::*>::type
id = get(&VertexProperties::index, g);
property_map<Graph, std::string EdgeProperties::*>::type
name = get(&EdgeProperties::name, g);
property_map< Graph, std::size_t VertexProperties::* >::type id
= get(&VertexProperties::index, g);
property_map< Graph, std::string EdgeProperties::* >::type name
= get(&EdgeProperties::name, g);
boost::graph_traits< Graph >::vertex_iterator vi, viend;
int vnum = 0;
@ -78,7 +80,8 @@ int main(int , char* [])
graph_traits< Graph >::vertex_iterator i, end;
graph_traits< Graph >::out_edge_iterator ei, edge_end;
for (boost::tie(i,end) = vertices(g); i != end; ++i) {
for (boost::tie(i, end) = vertices(g); i != end; ++i)
{
cout << id[*i] << " ";
for (boost::tie(ei, edge_end) = out_edges(*i, g); ei != edge_end; ++ei)
cout << " --" << name[*ei] << "--> " << id[target(*ei, g)] << " ";
@ -90,11 +93,12 @@ int main(int , char* [])
remove_edge(vertex(1, g), vertex(3, g), g);
ei = out_edges(vertex(1, g), g).first;
cout << "removing edge (" << id[source(*ei, g)]
<< "," << id[target(*ei, g)] << ")" << endl;
cout << "removing edge (" << id[source(*ei, g)] << "," << id[target(*ei, g)]
<< ")" << endl;
remove_edge(ei, g);
for(boost::tie(i,end) = vertices(g); i != end; ++i) {
for (boost::tie(i, end) = vertices(g); i != end; ++i)
{
cout << id[*i] << " ";
for (boost::tie(ei, edge_end) = out_edges(*i, g); ei != edge_end; ++ei)
cout << " --" << name[*ei] << "--> " << id[target(*ei, g)] << " ";

View File

@ -15,7 +15,10 @@
using namespace boost;
//======== my data structure
struct MyStruct { double value; };
struct MyStruct
{
double value;
};
std::ostream& operator<<(std::ostream& out, const MyStruct& s)
{
out << s.value << " ";
@ -28,58 +31,74 @@ std::istream& operator >> ( std::istream& in, MyStruct& s )
}
//======== vertex properties
struct n1_t { enum { num = 23063}; typedef vertex_property_tag kind; };
struct n2_t { enum { num = 23062}; typedef vertex_property_tag kind; };
struct n3_t { enum { num = 23061}; typedef vertex_property_tag kind; };
struct n1_t
{
enum
{
num = 23063
};
typedef vertex_property_tag kind;
};
struct n2_t
{
enum
{
num = 23062
};
typedef vertex_property_tag kind;
};
struct n3_t
{
enum
{
num = 23061
};
typedef vertex_property_tag kind;
};
typedef property< n1_t, int,
property< n2_t, double,
property< n3_t, MyStruct > > > VertexProperty;
property< n2_t, double, property< n3_t, MyStruct > > >
VertexProperty;
//====== edge properties
struct e1_t { enum { num = 23064}; typedef edge_property_tag kind; };
struct e1_t
{
enum
{
num = 23064
};
typedef edge_property_tag kind;
};
typedef property< e1_t, double > EdgeProperty;
//===== graph types
typedef
adjacency_list<vecS, listS, directedS, no_property, no_property>
typedef adjacency_list< vecS, listS, directedS, no_property, no_property >
Graph1;
typedef
adjacency_list<setS, setS, bidirectionalS, VertexProperty, EdgeProperty>
typedef adjacency_list< setS, setS, bidirectionalS, VertexProperty,
EdgeProperty >
Graph2;
int
main()
int main()
{
// read Graph1
Graph1 g1;
std::ifstream readFile1("data1.txt");
readFile1 >> read(g1);
std::cout << "graph g1 from file data1.txt:\n"
<< write( g1 )
<< std::endl;
std::cout << "graph g1 from file data1.txt:\n" << write(g1) << std::endl;
// read Graph2 and all internal properties
Graph2 g2;
std::ifstream readFile2("data2.txt");
readFile2 >> read(g2);
std::cout << "graph g2 from file data2.txt:\n"
<< write( g2 )
<< std::endl;
std::cout << "graph g2 from file data2.txt:\n" << write(g2) << std::endl;
// read Graph2, no property given. Write no property.
Graph2 g21;
std::ifstream readFile21("data1.txt");
readFile21 >> read(g21, no_property(), no_property());
std::cout << "graph g21 from file data1.txt:\n"
<< write(g21, no_property(), no_property())
<< std::endl;
<< write(g21, no_property(), no_property()) << std::endl;
// read Graph2, incomplete data in a different order. Write it diffently.
Graph2 g31;
@ -90,8 +109,5 @@ main()
<< write(g31, property< n3_t, MyStruct >(), EdgeProperty())
<< std::endl;
return 0;
}

View File

@ -11,11 +11,19 @@
#include <boost/graph/adjacency_matrix.hpp>
#include <boost/graph/graph_utility.hpp>
int main()
{
using namespace boost;
enum { A, B, C, D, E, F, N };
enum
{
A,
B,
C,
D,
E,
F,
N
};
const char* name = "ABCDEF";
// A directed graph

View File

@ -10,7 +10,6 @@
//=======================================================================
//
#include <boost/graph/astar_search.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/random.hpp>
@ -26,7 +25,6 @@
using namespace boost;
using namespace std;
// auxiliary types
struct location
{
@ -34,23 +32,31 @@ struct location
};
typedef float cost;
template <class Name, class LocMap>
class city_writer {
template < class Name, class LocMap > class city_writer
{
public:
city_writer(Name n, LocMap l, float _minx, float _maxx,
float _miny, float _maxy,
unsigned int _ptx, unsigned int _pty)
: name(n), loc(l), minx(_minx), maxx(_maxx), miny(_miny),
maxy(_maxy), ptx(_ptx), pty(_pty) {}
city_writer(Name n, LocMap l, float _minx, float _maxx, float _miny,
float _maxy, unsigned int _ptx, unsigned int _pty)
: name(n)
, loc(l)
, minx(_minx)
, maxx(_maxx)
, miny(_miny)
, maxy(_maxy)
, ptx(_ptx)
, pty(_pty)
{
}
template < class Vertex >
void operator()(ostream& out, const Vertex& v) const {
void operator()(ostream& out, const Vertex& v) const
{
float px = 1 - (loc[v].x - minx) / (maxx - minx);
float py = (loc[v].y - miny) / (maxy - miny);
out << "[label=\"" << name[v] << "\", pos=\""
<< static_cast< unsigned int >(ptx * px) << ","
<< static_cast<unsigned int>(pty * py)
<< "\", fontsize=\"11\"]";
<< static_cast< unsigned int >(pty * py) << "\", fontsize=\"11\"]";
}
private:
Name name;
LocMap loc;
@ -58,40 +64,41 @@ private:
unsigned int ptx, pty;
};
template <class WeightMap>
class time_writer {
template < class WeightMap > class time_writer
{
public:
time_writer(WeightMap w) : wm(w) {}
template <class Edge>
void operator()(ostream &out, const Edge& e) const {
template < class Edge > void operator()(ostream& out, const Edge& e) const
{
out << "[label=\"" << wm[e] << "\", fontsize=\"11\"]";
}
private:
WeightMap wm;
};
// euclidean distance heuristic
template < class Graph, class CostType, class LocMap >
class distance_heuristic : public astar_heuristic< Graph, CostType >
{
public:
typedef typename graph_traits< Graph >::vertex_descriptor Vertex;
distance_heuristic(LocMap l, Vertex goal)
: m_location(l), m_goal(goal) {}
distance_heuristic(LocMap l, Vertex goal) : m_location(l), m_goal(goal) {}
CostType operator()(Vertex u)
{
CostType dx = m_location[m_goal].x - m_location[u].x;
CostType dy = m_location[m_goal].y - m_location[u].y;
return ::sqrt(dx * dx + dy * dy);
}
private:
LocMap m_location;
Vertex m_goal;
};
struct found_goal {}; // exception for termination
struct found_goal
{
}; // exception for termination
// visitor that terminates when we find the goal
template < class Vertex >
@ -99,116 +106,123 @@ class astar_goal_visitor : public boost::default_astar_visitor
{
public:
astar_goal_visitor(Vertex goal) : m_goal(goal) {}
template <class Graph>
void examine_vertex(Vertex u, Graph& g) {
template < class Graph > void examine_vertex(Vertex u, Graph& g)
{
if (u == m_goal)
throw found_goal();
}
private:
Vertex m_goal;
};
int main(int argc, char** argv)
{
// specify some types
typedef adjacency_list< listS, vecS, undirectedS, no_property,
property<edge_weight_t, cost> > mygraph_t;
property< edge_weight_t, cost > >
mygraph_t;
typedef property_map< mygraph_t, edge_weight_t >::type WeightMap;
typedef mygraph_t::vertex_descriptor vertex;
typedef mygraph_t::edge_descriptor edge_descriptor;
typedef std::pair< int, int > edge;
// specify data
enum nodes {
Troy, LakePlacid, Plattsburgh, Massena, Watertown, Utica,
Syracuse, Rochester, Buffalo, Ithaca, Binghamton, Woodstock,
NewYork, N
};
const char *name[] = {
"Troy", "Lake Placid", "Plattsburgh", "Massena",
"Watertown", "Utica", "Syracuse", "Rochester", "Buffalo",
"Ithaca", "Binghamton", "Woodstock", "New York"
enum nodes
{
Troy,
LakePlacid,
Plattsburgh,
Massena,
Watertown,
Utica,
Syracuse,
Rochester,
Buffalo,
Ithaca,
Binghamton,
Woodstock,
NewYork,
N
};
const char* name[] = { "Troy", "Lake Placid", "Plattsburgh", "Massena",
"Watertown", "Utica", "Syracuse", "Rochester", "Buffalo", "Ithaca",
"Binghamton", "Woodstock", "New York" };
location locations[] = { // lat/long
{42.73, 73.68}, {44.28, 73.99}, {44.70, 73.46},
{44.93, 74.89}, {43.97, 75.91}, {43.10, 75.23},
{43.04, 76.14}, {43.17, 77.61}, {42.89, 78.86},
{42.44, 76.50}, {42.10, 75.91}, {42.04, 74.11},
{ 42.73, 73.68 }, { 44.28, 73.99 }, { 44.70, 73.46 }, { 44.93, 74.89 },
{ 43.97, 75.91 }, { 43.10, 75.23 }, { 43.04, 76.14 }, { 43.17, 77.61 },
{ 42.89, 78.86 }, { 42.44, 76.50 }, { 42.10, 75.91 }, { 42.04, 74.11 },
{ 40.67, 73.94 }
};
edge edge_array[] = {
edge(Troy,Utica), edge(Troy,LakePlacid),
edge(Troy,Plattsburgh), edge(LakePlacid,Plattsburgh),
edge(Plattsburgh,Massena), edge(LakePlacid,Massena),
edge(Massena,Watertown), edge(Watertown,Utica),
edge(Watertown,Syracuse), edge(Utica,Syracuse),
edge(Syracuse,Rochester), edge(Rochester,Buffalo),
edge(Syracuse,Ithaca), edge(Ithaca,Binghamton),
edge(Ithaca,Rochester), edge(Binghamton,Troy),
edge(Binghamton,Woodstock), edge(Binghamton,NewYork),
edge(Syracuse,Binghamton), edge(Woodstock,Troy),
edge(Woodstock,NewYork)
};
edge edge_array[]
= { edge(Troy, Utica), edge(Troy, LakePlacid), edge(Troy, Plattsburgh),
edge(LakePlacid, Plattsburgh), edge(Plattsburgh, Massena),
edge(LakePlacid, Massena), edge(Massena, Watertown),
edge(Watertown, Utica), edge(Watertown, Syracuse),
edge(Utica, Syracuse), edge(Syracuse, Rochester),
edge(Rochester, Buffalo), edge(Syracuse, Ithaca),
edge(Ithaca, Binghamton), edge(Ithaca, Rochester),
edge(Binghamton, Troy), edge(Binghamton, Woodstock),
edge(Binghamton, NewYork), edge(Syracuse, Binghamton),
edge(Woodstock, Troy), edge(Woodstock, NewYork) };
unsigned int num_edges = sizeof(edge_array) / sizeof(edge);
cost weights[] = { // estimated travel time (mins)
96, 134, 143, 65, 115, 133, 117, 116, 74, 56,
84, 73, 69, 70, 116, 147, 173, 183, 74, 71, 124
96, 134, 143, 65, 115, 133, 117, 116, 74, 56, 84, 73, 69, 70, 116, 147,
173, 183, 74, 71, 124
};
// create graph
mygraph_t g(N);
WeightMap weightmap = get(edge_weight, g);
for(std::size_t j = 0; j < num_edges; ++j) {
edge_descriptor e; bool inserted;
boost::tie(e, inserted) = add_edge(edge_array[j].first,
edge_array[j].second, g);
for (std::size_t j = 0; j < num_edges; ++j)
{
edge_descriptor e;
bool inserted;
boost::tie(e, inserted)
= add_edge(edge_array[j].first, edge_array[j].second, g);
weightmap[e] = weights[j];
}
// pick random start/goal
boost::mt19937 gen(std::time(0));
vertex start = random_vertex(g, gen);
vertex goal = random_vertex(g, gen);
cout << "Start vertex: " << name[start] << endl;
cout << "Goal vertex: " << name[goal] << endl;
ofstream dotfile;
dotfile.open("test-astar-cities.dot");
write_graphviz(dotfile, g,
city_writer<const char **, location*>
(name, locations, 73.46, 78.86, 40.67, 44.93,
480, 400),
city_writer< const char**, location* >(
name, locations, 73.46, 78.86, 40.67, 44.93, 480, 400),
time_writer< WeightMap >(weightmap));
vector< mygraph_t::vertex_descriptor > p(num_vertices(g));
vector< cost > d(num_vertices(g));
try {
try
{
// call astar named parameter interface
astar_search_tree
(g, start,
distance_heuristic<mygraph_t, cost, location*>
(locations, goal),
predecessor_map(make_iterator_property_map(p.begin(), get(vertex_index, g))).
distance_map(make_iterator_property_map(d.begin(), get(vertex_index, g))).
visitor(astar_goal_visitor<vertex>(goal)));
} catch(found_goal fg) { // found a path to the goal
astar_search_tree(g, start,
distance_heuristic< mygraph_t, cost, location* >(locations, goal),
predecessor_map(
make_iterator_property_map(p.begin(), get(vertex_index, g)))
.distance_map(
make_iterator_property_map(d.begin(), get(vertex_index, g)))
.visitor(astar_goal_visitor< vertex >(goal)));
}
catch (found_goal fg)
{ // found a path to the goal
list< vertex > shortest_path;
for(vertex v = goal;; v = p[v]) {
for (vertex v = goal;; v = p[v])
{
shortest_path.push_front(v);
if (p[v] == v)
break;
}
cout << "Shortest path from " << name[start] << " to "
<< name[goal] << ": ";
cout << "Shortest path from " << name[start] << " to " << name[goal]
<< ": ";
list< vertex >::iterator spi = shortest_path.begin();
cout << name[start];
for (++spi; spi != shortest_path.end(); ++spi)
@ -217,8 +231,7 @@ int main(int argc, char **argv)
return 0;
}
cout << "Didn't find a path from " << name[start] << "to"
<< name[goal] << "!" << endl;
cout << "Didn't find a path from " << name[start] << "to" << name[goal]
<< "!" << endl;
return 0;
}

View File

@ -4,7 +4,6 @@
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// This program uses the A-star search algorithm in the Boost Graph Library to
// solve a maze. It is an example of how to apply Boost Graph Library
// algorithms to implicit graphs.
@ -26,11 +25,11 @@
IMPORTANT:
~~~~~~~~~~
This example appears to be broken and crashes at runtime, see https://github.com/boostorg/graph/issues/148
This example appears to be broken and crashes at runtime, see
https://github.com/boostorg/graph/issues/148
*/
#include <boost/graph/astar_search.hpp>
#include <boost/graph/filtered_graph.hpp>
#include <boost/graph/grid_graph.hpp>
@ -54,10 +53,12 @@ typedef boost::graph_traits<grid>::vertex_descriptor vertex_descriptor;
typedef boost::graph_traits< grid >::vertices_size_type vertices_size_type;
// A hash function for vertices.
struct vertex_hash {
struct vertex_hash
{
typedef vertex_descriptor argument_type;
typedef std::size_t result_type;
std::size_t operator()(vertex_descriptor const& u) const {
std::size_t operator()(vertex_descriptor const& u) const
{
std::size_t seed = 0;
boost::hash_combine(seed, u[0]);
boost::hash_combine(seed, u[1]);
@ -84,44 +85,51 @@ typedef boost::vertex_subset_complement_filter<grid, vertex_set>::type
// A-star search is used to find a path through the maze. Each edge has a
// weight of one, so the total path length is equal to the number of edges
// traversed.
class maze {
class maze
{
public:
friend std::ostream& operator<<(std::ostream&, const maze&);
friend void random_maze(maze&);
maze():m_grid(create_grid(0, 0)),m_barrier_grid(create_barrier_grid()) {};
maze(std::size_t x, std::size_t y):m_grid(create_grid(x, y)),
m_barrier_grid(create_barrier_grid()) {};
maze()
: m_grid(create_grid(0, 0)), m_barrier_grid(create_barrier_grid()) {};
maze(std::size_t x, std::size_t y)
: m_grid(create_grid(x, y)), m_barrier_grid(create_barrier_grid()) {};
// The length of the maze along the specified dimension.
vertices_size_type length(std::size_t d) const { return m_grid.length(d); }
bool has_barrier(vertex_descriptor u) const {
bool has_barrier(vertex_descriptor u) const
{
return m_barriers.find(u) != m_barriers.end();
}
// Try to find a path from the lower-left-hand corner source (0,0) to the
// upper-right-hand corner goal (x-1, y-1).
vertex_descriptor source() const { return vertex(0, m_grid); }
vertex_descriptor goal() const {
vertex_descriptor goal() const
{
return vertex(num_vertices(m_grid) - 1, m_grid);
}
bool solve();
bool solved() const { return !m_solution.empty(); }
bool solution_contains(vertex_descriptor u) const {
bool solution_contains(vertex_descriptor u) const
{
return m_solution.find(u) != m_solution.end();
}
private:
// Create the underlying rank-2 grid with the specified dimensions.
grid create_grid(std::size_t x, std::size_t y) {
grid create_grid(std::size_t x, std::size_t y)
{
boost::array< std::size_t, GRID_RANK > lengths = { { x, y } };
return grid(lengths);
}
// Filter the barrier vertices out of the underlying grid.
filtered_grid create_barrier_grid() {
filtered_grid create_barrier_grid()
{
return boost::make_vertex_subset_complement_filter(m_grid, m_barriers);
}
@ -137,19 +145,20 @@ private:
distance m_solution_length;
};
// Euclidean heuristic for a grid
//
// This calculates the Euclidean distance between a vertex and a goal
// vertex.
class euclidean_heuristic:
public boost::astar_heuristic<filtered_grid, double>
class euclidean_heuristic
: public boost::astar_heuristic< filtered_grid, double >
{
public:
euclidean_heuristic(vertex_descriptor goal) : m_goal(goal) {};
double operator()(vertex_descriptor v) {
return sqrt(pow(double(m_goal[0] - v[0]), 2) + pow(double(m_goal[1] - v[1]), 2));
double operator()(vertex_descriptor v)
{
return sqrt(pow(double(m_goal[0] - v[0]), 2)
+ pow(double(m_goal[1] - v[1]), 2));
}
private:
@ -157,13 +166,17 @@ private:
};
// Exception thrown when the goal vertex is found
struct found_goal {};
struct found_goal
{
};
// Visitor that terminates when we find the goal vertex
struct astar_goal_visitor:public boost::default_astar_visitor {
struct astar_goal_visitor : public boost::default_astar_visitor
{
astar_goal_visitor(vertex_descriptor goal) : m_goal(goal) {};
void examine_vertex(vertex_descriptor u, const filtered_grid&) {
void examine_vertex(vertex_descriptor u, const filtered_grid&)
{
if (u == m_goal)
throw found_goal();
}
@ -173,18 +186,18 @@ private:
};
// Solve the maze using A-star search. Return true if a solution was found.
bool maze::solve() {
bool maze::solve()
{
boost::static_property_map< distance > weight(1);
// The predecessor map is a vertex-to-vertex mapping.
typedef boost::unordered_map<vertex_descriptor,
vertex_descriptor,
vertex_hash> pred_map;
typedef boost::unordered_map< vertex_descriptor, vertex_descriptor,
vertex_hash >
pred_map;
pred_map predecessor;
boost::associative_property_map< pred_map > pred_pmap(predecessor);
// The distance map is a vertex-to-distance mapping.
typedef boost::unordered_map<vertex_descriptor,
distance,
vertex_hash> dist_map;
typedef boost::unordered_map< vertex_descriptor, distance, vertex_hash >
dist_map;
dist_map distance;
boost::associative_property_map< dist_map > dist_pmap(distance);
@ -193,13 +206,16 @@ bool maze::solve() {
euclidean_heuristic heuristic(g);
astar_goal_visitor visitor(g);
try {
try
{
astar_search(m_barrier_grid, s, heuristic,
boost::weight_map(weight).
predecessor_map(pred_pmap).
distance_map(dist_pmap).
visitor(visitor) );
} catch(found_goal fg) {
boost::weight_map(weight)
.predecessor_map(pred_pmap)
.distance_map(dist_pmap)
.visitor(visitor));
}
catch (found_goal fg)
{
// Walk backwards from the goal through the predecessor chain adding
// vertices to the solution path.
for (vertex_descriptor u = g; u != s; u = predecessor[u])
@ -212,21 +228,23 @@ bool maze::solve() {
return false;
}
#define BARRIER "#"
// Print the maze as an ASCII map.
std::ostream& operator<<(std::ostream& output, const maze& m) {
std::ostream& operator<<(std::ostream& output, const maze& m)
{
// Header
for (vertices_size_type i = 0; i < m.length(0) + 2; i++)
output << BARRIER;
output << std::endl;
// Body
for (int y = m.length(1)-1; y >= 0; y--) {
for (int y = m.length(1) - 1; y >= 0; y--)
{
// Enumerate rows in reverse order and columns in regular order so that
// (0,0) appears in the lower left-hand corner. This requires that y be
// int and not the unsigned vertices_size_type because the loop exit
// condition is y==-1.
for (vertices_size_type x = 0; x < m.length(0); x++) {
for (vertices_size_type x = 0; x < m.length(0); x++)
{
// Put a barrier on the left-hand side.
if (x == 0)
output << BARRIER;
@ -254,34 +272,40 @@ std::ostream& operator<<(std::ostream& output, const maze& m) {
}
// Return a random integer in the interval [a, b].
std::size_t random_int(std::size_t a, std::size_t b) {
std::size_t random_int(std::size_t a, std::size_t b)
{
if (b < a)
b = a;
boost::uniform_int<> dist(a, b);
boost::variate_generator<boost::mt19937&, boost::uniform_int<> >
generate(random_generator, dist);
boost::variate_generator< boost::mt19937&, boost::uniform_int<> > generate(
random_generator, dist);
return generate();
}
// Generate a maze with a random assignment of barriers.
void random_maze(maze& m) {
void random_maze(maze& m)
{
vertices_size_type n = num_vertices(m.m_grid);
vertex_descriptor s = m.source();
vertex_descriptor g = m.goal();
// One quarter of the cells in the maze should be barriers.
int barriers = n / 4;
while (barriers > 0) {
while (barriers > 0)
{
// Choose horizontal or vertical direction.
std::size_t direction = random_int(0, 1);
// Walls range up to one quarter the dimension length in this direction.
vertices_size_type wall = random_int(1, m.length(direction) / 4);
// Create the wall while decrementing the total barrier count.
vertex_descriptor u = vertex(random_int(0, n - 1), m.m_grid);
while (wall) {
while (wall)
{
// Start and goal spaces should never be barriers.
if (u != s && u != g) {
if (u != s && u != g)
{
wall--;
if (!m.has_barrier(u)) {
if (!m.has_barrier(u))
{
m.m_barriers.insert(u);
barriers--;
}
@ -295,13 +319,15 @@ void random_maze(maze& m) {
}
}
int main (int argc, char const *argv[]) {
int main(int argc, char const* argv[])
{
// The default maze size is 20x10. A different size may be specified on
// the command line.
std::size_t x = 20;
std::size_t y = 10;
if (argc == 3) {
if (argc == 3)
{
x = boost::lexical_cast< std::size_t >(argv[1]);
y = boost::lexical_cast< std::size_t >(argv[2]);
}

View File

@ -15,20 +15,16 @@
using namespace boost;
template < typename Graph, typename ParentMap >
struct edge_writer
template < typename Graph, typename ParentMap > struct edge_writer
{
edge_writer(const Graph & g, const ParentMap & p)
: m_g(g), m_parent(p)
{
}
edge_writer(const Graph& g, const ParentMap& p) : m_g(g), m_parent(p) {}
template < typename Edge >
void operator()(std::ostream& out, const Edge& e) const
{
out << "[label=\"" << get(edge_weight, m_g, e) << "\"";
typename graph_traits < Graph >::vertex_descriptor
u = source(e, m_g), v = target(e, m_g);
typename graph_traits< Graph >::vertex_descriptor u = source(e, m_g),
v = target(e, m_g);
if (m_parent[v] == u)
out << ", color=\"black\"";
else
@ -39,29 +35,36 @@ struct edge_writer
ParentMap m_parent;
};
template < typename Graph, typename Parent >
edge_writer < Graph, Parent >
make_edge_writer(const Graph & g, const Parent & p)
edge_writer< Graph, Parent > make_edge_writer(const Graph& g, const Parent& p)
{
return edge_writer< Graph, Parent >(g, p);
}
struct EdgeProperties {
struct EdgeProperties
{
int weight;
};
int
main()
int main()
{
enum { u, v, x, y, z, N };
enum
{
u,
v,
x,
y,
z,
N
};
char name[] = { 'u', 'v', 'x', 'y', 'z' };
typedef std::pair< int, int > E;
const int n_edges = 10;
E edge_array[] = { E(u, y), E(u, x), E(u, v), E(v, u),
E(x, y), E(x, v), E(y, v), E(y, z), E(z, u), E(z,x) };
E edge_array[] = { E(u, y), E(u, x), E(u, v), E(v, u), E(x, y), E(x, v),
E(y, v), E(y, z), E(z, u), E(z, x) };
int weight[n_edges] = { -4, 8, 5, -2, 9, -3, 7, 2, 6, 7 };
typedef adjacency_list < vecS, vecS, directedS,
no_property, EdgeProperties> Graph;
typedef adjacency_list< vecS, vecS, directedS, no_property, EdgeProperties >
Graph;
#if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
// VC++ can't handle the iterator constructor
Graph g(N);
@ -71,8 +74,8 @@ main()
Graph g(edge_array, edge_array + n_edges, N);
#endif
graph_traits< Graph >::edge_iterator ei, ei_end;
property_map<Graph, int EdgeProperties::*>::type
weight_pmap = get(&EdgeProperties::weight, g);
property_map< Graph, int EdgeProperties::* >::type weight_pmap
= get(&EdgeProperties::weight, g);
int i = 0;
for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei, ++i)
weight_pmap[*ei] = weight[i];
@ -84,19 +87,20 @@ main()
distance[z] = 0;
#if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
bool r = bellman_ford_shortest_paths
(g, int(N), weight_pmap, &parent[0], &distance[0],
closed_plus<int>(), std::less<int>(), default_bellman_visitor());
bool r = bellman_ford_shortest_paths(g, int(N), weight_pmap, &parent[0],
&distance[0], closed_plus< int >(), std::less< int >(),
default_bellman_visitor());
#else
bool r = bellman_ford_shortest_paths
(g, int (N), weight_map(weight_pmap).distance_map(&distance[0]).
predecessor_map(&parent[0]));
bool r = bellman_ford_shortest_paths(g, int(N),
weight_map(weight_pmap)
.distance_map(&distance[0])
.predecessor_map(&parent[0]));
#endif
if (r)
for (i = 0; i < N; ++i)
std::cout << name[i] << ": " << std::setw(3) << distance[i]
<< " " << name[parent[i]] << std::endl;
std::cout << name[i] << ": " << std::setw(3) << distance[i] << " "
<< name[parent[i]] << std::endl;
else
std::cout << "negative cycle" << std::endl;
@ -105,17 +109,19 @@ main()
<< " rankdir=LR\n"
<< " size=\"5,3\"\n"
<< " ratio=\"fill\"\n"
<< " edge[style=\"bold\"]\n" << " node[shape=\"circle\"]\n";
<< " edge[style=\"bold\"]\n"
<< " node[shape=\"circle\"]\n";
{
for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) {
for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)
{
graph_traits< Graph >::edge_descriptor e = *ei;
graph_traits < Graph >::vertex_descriptor
u = source(e, g), v = target(e, g);
graph_traits< Graph >::vertex_descriptor u = source(e, g),
v = target(e, g);
// VC++ doesn't like the 3-argument get function, so here
// we workaround by using 2-nested get()'s.
dot_file << name[u] << " -> " << name[v]
<< "[label=\"" << get(get(&EdgeProperties::weight, g), e) << "\"";
dot_file << name[u] << " -> " << name[v] << "[label=\""
<< get(get(&EdgeProperties::weight, g), e) << "\"";
if (parent[v] == u)
dot_file << ", color=\"black\"";
else

View File

@ -9,30 +9,38 @@
#include <boost/graph/edge_list.hpp>
#include <boost/graph/bellman_ford_shortest_paths.hpp>
int
main()
int main()
{
using namespace boost;
// ID numbers for the routers (vertices).
enum
{ A, B, C, D, E, F, G, H, n_vertices };
{
A,
B,
C,
D,
E,
F,
G,
H,
n_vertices
};
const int n_edges = 11;
typedef std::pair< int, int > Edge;
// The list of connections between routers stored in an array.
Edge edges[] = {
Edge(A, B), Edge(A, C),
Edge(B, D), Edge(B, E), Edge(C, E), Edge(C, F), Edge(D, H),
Edge(D, E), Edge(E, H), Edge(F, G), Edge(G, H)
};
Edge edges[] = { Edge(A, B), Edge(A, C), Edge(B, D), Edge(B, E), Edge(C, E),
Edge(C, F), Edge(D, H), Edge(D, E), Edge(E, H), Edge(F, G),
Edge(G, H) };
// Specify the graph type and declare a graph object
typedef edge_list < Edge*, Edge, std::ptrdiff_t, std::random_access_iterator_tag> Graph;
typedef edge_list< Edge*, Edge, std::ptrdiff_t,
std::random_access_iterator_tag >
Graph;
Graph g(edges, edges + n_edges);
// The transmission delay values for each edge.
float delay[] =
{5.0, 1.0, 1.3, 3.0, 10.0, 2.0, 6.3, 0.4, 1.3, 1.2, 0.5};
float delay[] = { 5.0, 1.0, 1.3, 3.0, 10.0, 2.0, 6.3, 0.4, 1.3, 1.2, 0.5 };
// Declare some storage for some "external" vertex properties.
char name[] = "ABCDEFGH";
@ -40,22 +48,21 @@ main()
for (int i = 0; i < n_vertices; ++i)
parent[i] = i;
float distance[n_vertices];
std::fill(distance, distance + n_vertices, (std::numeric_limits < float >::max)());
std::fill(
distance, distance + n_vertices, (std::numeric_limits< float >::max)());
// Specify A as the source vertex
distance[A] = 0;
bool r = bellman_ford_shortest_paths(g, int(n_vertices),
weight_map(make_iterator_property_map
(&delay[0],
get(edge_index, g),
delay[0])).
distance_map(&distance[0]).
predecessor_map(&parent[0]));
weight_map(
make_iterator_property_map(&delay[0], get(edge_index, g), delay[0]))
.distance_map(&distance[0])
.predecessor_map(&parent[0]));
if (r)
for (int i = 0; i < n_vertices; ++i)
std::cout << name[i] << ": " << distance[i]
<< " " << name[parent[i]] << std::endl;
std::cout << name[i] << ": " << distance[i] << " "
<< name[parent[i]] << std::endl;
else
std::cout << "negative cycle" << std::endl;

View File

@ -13,8 +13,11 @@
#include <iostream>
using namespace boost;
template < typename TimeMap > class bfs_time_visitor:public default_bfs_visitor {
template < typename TimeMap >
class bfs_time_visitor : public default_bfs_visitor
{
typedef typename property_traits< TimeMap >::value_type T;
public:
bfs_time_visitor(TimeMap tmap, T& t) : m_timemap(tmap), m_time(t) {}
template < typename Vertex, typename Graph >
@ -26,21 +29,29 @@ public:
T& m_time;
};
int
main()
int main()
{
using namespace boost;
// Select the graph type we wish to use
typedef adjacency_list< vecS, vecS, undirectedS > graph_t;
// Set up the vertex IDs and names
enum { r, s, t, u, v, w, x, y, N };
enum
{
r,
s,
t,
u,
v,
w,
x,
y,
N
};
const char* name = "rstuvwxy";
// Specify the edges in the graph
typedef std::pair< int, int > E;
E edge_array[] = { E(r, s), E(r, v), E(s, w), E(w, r), E(w, t),
E(w, x), E(x, t), E(t, u), E(x, y), E(u, y)
};
E edge_array[] = { E(r, s), E(r, v), E(s, w), E(w, r), E(w, t), E(w, x),
E(x, t), E(t, u), E(x, y), E(u, y) };
// Create the graph object
const int n_edges = sizeof(edge_array) / sizeof(E);
#if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
@ -58,8 +69,7 @@ main()
// a vector to hold the discover time property for each vertex
std::vector< Size > dtime(num_vertices(g));
typedef
iterator_property_map<std::vector<Size>::iterator,
typedef iterator_property_map< std::vector< Size >::iterator,
property_map< graph_t, vertex_index_t >::const_type >
dtime_pm_type;
dtime_pm_type dtime_pm(dtime.begin(), get(vertex_index, g));
@ -69,7 +79,8 @@ main()
breadth_first_search(g, vertex(s, g), visitor(vis));
// Use std::sort to order the vertices by their discover time
std::vector<graph_traits<graph_t>::vertices_size_type > discover_order(N);
std::vector< graph_traits< graph_t >::vertices_size_type > discover_order(
N);
integer_range< int > range(0, N);
std::copy(range.begin(), range.end(), discover_order.begin());
std::sort(discover_order.begin(), discover_order.end(),

View File

@ -14,8 +14,11 @@
#include <iostream>
using namespace boost;
template < typename TimeMap > class bfs_time_visitor:public default_bfs_visitor {
template < typename TimeMap >
class bfs_time_visitor : public default_bfs_visitor
{
typedef typename property_traits< TimeMap >::value_type T;
public:
bfs_time_visitor(TimeMap tmap, T& t) : m_timemap(tmap), m_time(t) {}
template < typename Vertex, typename Graph >
@ -27,28 +30,36 @@ public:
T& m_time;
};
struct VertexProps {
struct VertexProps
{
boost::default_color_type color;
std::size_t discover_time;
unsigned int index;
};
int
main()
int main()
{
using namespace boost;
// Select the graph type we wish to use
typedef adjacency_list < listS, listS, undirectedS,
VertexProps> graph_t;
typedef adjacency_list< listS, listS, undirectedS, VertexProps > graph_t;
// Set up the vertex IDs and names
enum { r, s, t, u, v, w, x, y, N };
enum
{
r,
s,
t,
u,
v,
w,
x,
y,
N
};
const char* name = "rstuvwxy";
// Specify the edges in the graph
typedef std::pair< int, int > E;
E edge_array[] = { E(r, s), E(r, v), E(s, w), E(w, r), E(w, t),
E(w, x), E(x, t), E(t, u), E(x, y), E(u, y)
};
E edge_array[] = { E(r, s), E(r, v), E(s, w), E(w, r), E(w, t), E(w, x),
E(x, t), E(t, u), E(x, y), E(u, y) };
// Create the graph object
const int n_edges = sizeof(edge_array) / sizeof(E);
#if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
@ -68,36 +79,36 @@ main()
typedef graph_traits< graph_t >::vertices_size_type Size;
Size time = 0;
typedef property_map<graph_t, std::size_t VertexProps::*>::type dtime_map_t;
typedef property_map< graph_t, std::size_t VertexProps::* >::type
dtime_map_t;
dtime_map_t dtime_map = get(&VertexProps::discover_time, g);
bfs_time_visitor< dtime_map_t > vis(dtime_map, time);
breadth_first_search(g, vertex(s, g), color_map(get(&VertexProps::color, g)).
visitor(vis));
breadth_first_search(
g, vertex(s, g), color_map(get(&VertexProps::color, g)).visitor(vis));
// a vector to hold the discover time property for each vertex
std::vector< Size > dtime(num_vertices(g));
typedef
iterator_property_map<std::vector<Size>::iterator,
typedef iterator_property_map< std::vector< Size >::iterator,
property_map< graph_t, unsigned int VertexProps::* >::type >
dtime_pm_type;
graph_traits< graph_t >::vertex_iterator vi, vi_end;
std::size_t c = 0;
for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi, ++c) {
for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi, ++c)
{
dtime[c] = dtime_map[*vi];
put(&VertexProps::index, g, *vi, c);
}
dtime_pm_type dtime_pm(dtime.begin(), get(&VertexProps::index, g));
// Use std::sort to order the vertices by their discover time
std::vector<graph_traits<graph_t>::vertices_size_type > discover_order(N);
std::vector< graph_traits< graph_t >::vertices_size_type > discover_order(
N);
integer_range< int > range(0, N);
std::copy(range.begin(), range.end(), discover_order.begin());
std::sort(discover_order.begin(), discover_order.end(),
make_indirect_cmp(
std::less<Size>(),
make_indirect_cmp(std::less< Size >(),
make_iterator_property_map(
dtime.begin(),
typed_identity_property_map<std::size_t>())));
dtime.begin(), typed_identity_property_map< std::size_t >())));
std::cout << "order of discovery: ";
for (int i = 0; i < N; ++i)

View File

@ -13,9 +13,8 @@
using namespace boost;
template < typename Graph, typename VertexNameMap, typename TransDelayMap >
void
build_router_network(Graph & g, VertexNameMap name_map,
TransDelayMap delay_map)
void build_router_network(
Graph& g, VertexNameMap name_map, TransDelayMap delay_map)
{
typename graph_traits< Graph >::vertex_descriptor a, b, c, d, e;
a = add_vertex(g);
@ -48,33 +47,33 @@ build_router_network(Graph & g, VertexNameMap name_map,
delay_map[ed] = 3.3;
}
template < typename VertexNameMap >
class bfs_name_printer : public default_bfs_visitor {
class bfs_name_printer : public default_bfs_visitor
{
// inherit default (empty) event point actions
public:
bfs_name_printer(VertexNameMap n_map) : m_name_map(n_map) {
}
bfs_name_printer(VertexNameMap n_map) : m_name_map(n_map) {}
template < typename Vertex, typename Graph >
void discover_vertex(Vertex u, const Graph&) const
{
std::cout << get(m_name_map, u) << ' ';
}
private:
VertexNameMap m_name_map;
};
struct VP {
struct VP
{
char name;
};
struct EP {
struct EP
{
double weight;
};
int
main()
int main()
{
typedef adjacency_list< listS, vecS, directedS, VP, EP > graph_t;
graph_t g;
@ -90,5 +89,4 @@ main()
std::cout << "BFS vertex discover order: ";
breadth_first_search(g, a, visitor(vis));
std::cout << std::endl;
}

View File

@ -56,17 +56,16 @@
*/
template <class ParentDecorator>
struct print_parent {
template < class ParentDecorator > struct print_parent
{
print_parent(const ParentDecorator& p_) : p(p_) {}
template <class Vertex>
void operator()(const Vertex& v) const {
template < class Vertex > void operator()(const Vertex& v) const
{
std::cout << "parent[" << v << "] = " << p[v] << std::endl;
}
ParentDecorator p;
};
template < class NewGraph, class Tag >
struct graph_copier
: public boost::base_visitor< graph_copier< NewGraph, Tag > >
@ -75,29 +74,30 @@ struct graph_copier
graph_copier(NewGraph& graph) : new_g(graph) {}
template <class Edge, class Graph>
void operator()(Edge e, Graph& g) {
template < class Edge, class Graph > void operator()(Edge e, Graph& g)
{
boost::add_edge(boost::source(e, g), boost::target(e, g), new_g);
}
private:
NewGraph& new_g;
};
template < class NewGraph, class Tag >
inline graph_copier<NewGraph, Tag>
copy_graph(NewGraph& g, Tag) {
inline graph_copier< NewGraph, Tag > copy_graph(NewGraph& g, Tag)
{
return graph_copier< NewGraph, Tag >(g);
}
int main(int, char*[])
{
typedef boost::adjacency_list<
boost::mapS, boost::vecS, boost::bidirectionalS,
typedef boost::adjacency_list< boost::mapS, boost::vecS,
boost::bidirectionalS,
boost::property< boost::vertex_color_t, boost::default_color_type,
boost::property< boost::vertex_degree_t, int,
boost::property< boost::vertex_in_degree_t, int,
boost::property<boost::vertex_out_degree_t, int> > > >
> Graph;
boost::property< boost::vertex_out_degree_t, int > > > > >
Graph;
Graph G(5);
boost::add_edge(0, 2, G);
@ -131,19 +131,18 @@ int main(int , char* [])
// The source vertex
Vertex s = *(boost::vertices(G).first);
p[s] = s;
boost::breadth_first_search
(G, s,
boost::visitor(boost::make_bfs_visitor
(std::make_pair(boost::record_distances(d, boost::on_tree_edge()),
std::make_pair
(boost::record_predecessors(&p[0],
boost::on_tree_edge()),
boost::breadth_first_search(G, s,
boost::visitor(boost::make_bfs_visitor(
std::make_pair(boost::record_distances(d, boost::on_tree_edge()),
std::make_pair(
boost::record_predecessors(&p[0], boost::on_tree_edge()),
copy_graph(G_copy, boost::on_examine_edge()))))));
boost::print_graph(G);
boost::print_graph(G_copy);
if (boost::num_vertices(G) < 11) {
if (boost::num_vertices(G) < 11)
{
std::cout << "distances: ";
#ifdef BOOST_OLD_STREAM_ITERATORS
std::copy(d, d + 5, std::ostream_iterator< int, char >(std::cout, " "));

View File

@ -51,17 +51,16 @@
*/
template <class ParentDecorator>
struct print_parent {
template < class ParentDecorator > struct print_parent
{
print_parent(const ParentDecorator& p_) : p(p_) {}
template <class Vertex>
void operator()(const Vertex& v) const {
template < class Vertex > void operator()(const Vertex& v) const
{
std::cout << "parent[" << v << "] = " << p[v] << std::endl;
}
ParentDecorator p;
};
template < class NewGraph, class Tag >
struct graph_copier
: public boost::base_visitor< graph_copier< NewGraph, Tag > >
@ -70,29 +69,30 @@ struct graph_copier
graph_copier(NewGraph& graph) : new_g(graph) {}
template <class Edge, class Graph>
void operator()(Edge e, Graph& g) {
template < class Edge, class Graph > void operator()(Edge e, Graph& g)
{
boost::add_edge(boost::source(e, g), boost::target(e, g), new_g);
}
private:
NewGraph& new_g;
};
template < class NewGraph, class Tag >
inline graph_copier<NewGraph, Tag>
copy_graph(NewGraph& g, Tag) {
inline graph_copier< NewGraph, Tag > copy_graph(NewGraph& g, Tag)
{
return graph_copier< NewGraph, Tag >(g);
}
int main(int, char*[])
{
typedef boost::adjacency_list<
boost::mapS, boost::vecS, boost::bidirectionalS,
typedef boost::adjacency_list< boost::mapS, boost::vecS,
boost::bidirectionalS,
boost::property< boost::vertex_color_t, boost::default_color_type,
boost::property< boost::vertex_degree_t, int,
boost::property< boost::vertex_in_degree_t, int,
boost::property<boost::vertex_out_degree_t, int> > > >
> Graph;
boost::property< boost::vertex_out_degree_t, int > > > > >
Graph;
Graph G(5);
boost::add_edge(0, 2, G);
@ -125,16 +125,15 @@ int main(int , char* [])
// The source vertex
Vertex s = *(boost::vertices(G).first);
p[s] = s;
boost::neighbor_breadth_first_search
(G, s,
boost::visitor(boost::make_neighbor_bfs_visitor
(std::make_pair(boost::record_distances(d, boost::on_tree_edge()),
boost::record_predecessors(&p[0],
boost::on_tree_edge())))));
boost::neighbor_breadth_first_search(G, s,
boost::visitor(boost::make_neighbor_bfs_visitor(
std::make_pair(boost::record_distances(d, boost::on_tree_edge()),
boost::record_predecessors(&p[0], boost::on_tree_edge())))));
boost::print_graph(G);
if (boost::num_vertices(G) < 11) {
if (boost::num_vertices(G) < 11)
{
std::cout << "distances: ";
#ifdef BOOST_OLD_STREAM_ITERATORS
std::copy(d, d + 5, std::ostream_iterator< int, char >(std::cout, " "));

View File

@ -18,18 +18,19 @@ namespace boost
struct edge_component_t
{
enum
{ num = 555 };
{
num = 555
};
typedef edge_property_tag kind;
}
edge_component;
} edge_component;
}
int
main()
int main()
{
using namespace boost;
typedef adjacency_list < vecS, vecS, undirectedS,
no_property, property < edge_component_t, std::size_t > >graph_t;
typedef adjacency_list< vecS, vecS, undirectedS, no_property,
property< edge_component_t, std::size_t > >
graph_t;
typedef graph_traits< graph_t >::vertex_descriptor vertex_t;
graph_t g(9);
add_edge(0, 5, g);
@ -44,8 +45,8 @@ main()
add_edge(6, 7, g);
add_edge(7, 8, g);
property_map < graph_t, edge_component_t >::type
component = get(edge_component, g);
property_map< graph_t, edge_component_t >::type component
= get(edge_component, g);
std::size_t num_comps = biconnected_components(g, component);
std::cerr << "Found " << num_comps << " biconnected components.\n";
@ -54,19 +55,20 @@ main()
articulation_points(g, std::back_inserter(art_points));
std::cerr << "Found " << art_points.size() << " articulation points.\n";
std::cout << "graph A {\n" << " node[shape=\"circle\"]\n";
std::cout << "graph A {\n"
<< " node[shape=\"circle\"]\n";
for (std::size_t i = 0; i < art_points.size(); ++i) {
for (std::size_t i = 0; i < art_points.size(); ++i)
{
std::cout << (char)(art_points[i] + 'A')
<< " [ style=\"filled\", fillcolor=\"red\" ];"
<< std::endl;
<< " [ style=\"filled\", fillcolor=\"red\" ];" << std::endl;
}
graph_traits< graph_t >::edge_iterator ei, ei_end;
for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)
std::cout << (char)(source(*ei, g) + 'A') << " -- "
<< (char)(target(*ei, g) + 'A')
<< "[label=\"" << component[*ei] << "\"]\n";
<< (char)(target(*ei, g) + 'A') << "[label=\""
<< component[*ei] << "\"]\n";
std::cout << "}\n";
return 0;

View File

@ -18,8 +18,7 @@ using namespace boost;
/// Example to test for bipartiteness and print the certificates.
template <typename Graph>
void print_bipartite (const Graph& g)
template < typename Graph > void print_bipartite(const Graph& g)
{
typedef graph_traits< Graph > traits;
typename traits::vertex_iterator vertex_iter, vertex_end;
@ -31,25 +30,35 @@ void print_bipartite (const Graph& g)
if (bipartite)
{
typedef std::vector< default_color_type > partition_t;
typedef typename property_map <Graph, vertex_index_t>::type index_map_t;
typedef iterator_property_map <partition_t::iterator, index_map_t> partition_map_t;
typedef
typename property_map< Graph, vertex_index_t >::type index_map_t;
typedef iterator_property_map< partition_t::iterator, index_map_t >
partition_map_t;
partition_t partition(num_vertices(g));
partition_map_t partition_map(partition.begin(), get(vertex_index, g));
/// A second interface yields a bipartition in a color map, if the graph is bipartite.
/// A second interface yields a bipartition in a color map, if the graph
/// is bipartite.
is_bipartite(g, get(vertex_index, g), partition_map);
for (boost::tie (vertex_iter, vertex_end) = vertices (g); vertex_iter != vertex_end; ++vertex_iter)
for (boost::tie(vertex_iter, vertex_end) = vertices(g);
vertex_iter != vertex_end; ++vertex_iter)
{
std::cout << "Vertex " << *vertex_iter << " has color " << (get (partition_map, *vertex_iter) == color_traits <
default_color_type>::white () ? "white" : "black") << std::endl;
std::cout
<< "Vertex " << *vertex_iter << " has color "
<< (get(partition_map, *vertex_iter)
== color_traits< default_color_type >::white()
? "white"
: "black")
<< std::endl;
}
}
else
{
typedef std::vector <typename traits::vertex_descriptor> vertex_vector_t;
typedef std::vector< typename traits::vertex_descriptor >
vertex_vector_t;
vertex_vector_t odd_cycle;
/// A third interface yields an odd-cycle if the graph is not bipartite.
@ -82,8 +91,9 @@ int main (int argc, char **argv)
* 8 - 9 - 10
**/
E bipartite_edges[] = { E (0, 1), E (0, 4), E (1, 2), E (2, 6), E (3, 4), E (3, 8), E (4, 5), E (4, 7), E (5, 6), E (
6, 7), E (7, 10), E (8, 9), E (9, 10) };
E bipartite_edges[]
= { E(0, 1), E(0, 4), E(1, 2), E(2, 6), E(3, 4), E(3, 8), E(4, 5),
E(4, 7), E(5, 6), E(6, 7), E(7, 10), E(8, 9), E(9, 10) };
vector_graph_t bipartite_vector_graph(&bipartite_edges[0],
&bipartite_edges[0] + sizeof(bipartite_edges) / sizeof(E), 11);
@ -100,10 +110,10 @@ int main (int argc, char **argv)
*
**/
E non_bipartite_edges[] = { E (0, 1), E (0, 4), E (1, 2), E (2, 6), E (3, 6), E (3, 8), E (4, 5), E (4, 7), E (5, 6),
E (6, 7), E (7, 9), E (8, 9) };
vector_graph_t non_bipartite_vector_graph (&non_bipartite_edges[0], &non_bipartite_edges[0]
+ sizeof(non_bipartite_edges) / sizeof(E), 10);
E non_bipartite_edges[] = { E(0, 1), E(0, 4), E(1, 2), E(2, 6), E(3, 6),
E(3, 8), E(4, 5), E(4, 7), E(5, 6), E(6, 7), E(7, 9), E(8, 9) };
vector_graph_t non_bipartite_vector_graph(&non_bipartite_edges[0],
&non_bipartite_edges[0] + sizeof(non_bipartite_edges) / sizeof(E), 10);
/// Call test routine for a bipartite and a non-bipartite graph.

View File

@ -19,7 +19,6 @@
#include <boost/graph/breadth_first_search.hpp>
#include <boost/graph/depth_first_search.hpp>
template < class Distance >
class calc_distance_visitor : public boost::bfs_visitor<>
{
@ -27,28 +26,27 @@ public:
calc_distance_visitor(Distance d) : distance(d) {}
template < class Graph >
void tree_edge(typename boost::graph_traits<Graph>::edge_descriptor e,
Graph& g)
void tree_edge(
typename boost::graph_traits< Graph >::edge_descriptor e, Graph& g)
{
typename boost::graph_traits< Graph >::vertex_descriptor u, v;
u = boost::source(e, g);
v = boost::target(e, g);
distance[v] = distance[u] + 1;
}
private:
Distance distance;
};
template < class VertexNameMap, class DistanceMap >
class print_tree_visitor : public boost::dfs_visitor<>
{
public:
print_tree_visitor(VertexNameMap n, DistanceMap d) : name(n), distance(d) {}
template < class Graph >
void
discover_vertex(typename boost::graph_traits<Graph>::vertex_descriptor v,
Graph&)
void discover_vertex(
typename boost::graph_traits< Graph >::vertex_descriptor v, Graph&)
{
typedef typename boost::property_traits< DistanceMap >::value_type Dist;
// indentation based on depth
@ -58,8 +56,8 @@ public:
}
template < class Graph >
void tree_edge(typename boost::graph_traits<Graph>::edge_descriptor e,
Graph& g)
void tree_edge(
typename boost::graph_traits< Graph >::edge_descriptor e, Graph& g)
{
distance[boost::target(e, g)] = distance[boost::source(e, g)] + 1;
}
@ -69,13 +67,13 @@ private:
DistanceMap distance;
};
int
main(int argc, const char** argv)
int main(int argc, const char** argv)
{
using namespace boost;
std::ifstream datafile(argc >= 2 ? argv[1] : "./boost_web.dat");
if (!datafile) {
if (!datafile)
{
std::cerr << "No ./boost_web.dat file" << std::endl;
return -1;
}
@ -86,8 +84,8 @@ main(int argc, const char** argv)
typedef adjacency_list< vecS, vecS, directedS,
property< vertex_name_t, std::string,
property< vertex_color_t, default_color_type > >,
property<edge_name_t, std::string, property<edge_weight_t, int> >
> Graph;
property< edge_name_t, std::string, property< edge_weight_t, int > > >
Graph;
typedef graph_traits< Graph > Traits;
typedef Traits::vertex_descriptor Vertex;
@ -105,7 +103,8 @@ main(int argc, const char** argv)
// Read the data file and construct the graph.
std::string line;
while (std::getline(datafile,line)) {
while (std::getline(datafile, line))
{
std::list< std::string > line_toks;
boost::stringtok(line_toks, line, "|");
@ -116,28 +115,35 @@ main(int argc, const char** argv)
std::list< std::string >::iterator i = line_toks.begin();
boost::tie(pos, inserted) = name2vertex.insert(std::make_pair(*i, Vertex()));
if (inserted) {
boost::tie(pos, inserted)
= name2vertex.insert(std::make_pair(*i, Vertex()));
if (inserted)
{
u = add_vertex(g);
put(node_name, u, *i);
pos->second = u;
} else
}
else
u = pos->second;
++i;
std::string hyperlink_name = *i++;
boost::tie(pos, inserted) = name2vertex.insert(std::make_pair(*i, Vertex()));
if (inserted) {
boost::tie(pos, inserted)
= name2vertex.insert(std::make_pair(*i, Vertex()));
if (inserted)
{
v = add_vertex(g);
put(node_name, v, *i);
pos->second = v;
} else
}
else
v = pos->second;
Edge e;
boost::tie(e, inserted) = add_edge(u, v, g);
if (inserted) {
if (inserted)
{
put(link_name, e, hyperlink_name);
}
}
@ -149,11 +155,12 @@ main(int argc, const char** argv)
typedef std::vector< size_type > IntVector;
// Create N x N matrix for storing the shortest distances
// between each vertex. Initialize all distances to zero.
std::vector<IntVector> d_matrix(num_vertices(g),
IntVector(num_vertices(g), 0));
std::vector< IntVector > d_matrix(
num_vertices(g), IntVector(num_vertices(g), 0));
size_type i;
for (i = 0; i < num_vertices(g); ++i) {
for (i = 0; i < num_vertices(g); ++i)
{
calc_distance_visitor< size_type* > vis(&d_matrix[i][0]);
Traits::vertex_descriptor src = vertices(g).first[i];
breadth_first_search(g, src, boost::visitor(vis));
@ -162,11 +169,12 @@ main(int argc, const char** argv)
size_type diameter = 0;
BOOST_USING_STD_MAX();
for (i = 0; i < num_vertices(g); ++i)
diameter = max BOOST_PREVENT_MACRO_SUBSTITUTION(diameter, *std::max_element(d_matrix[i].begin(),
d_matrix[i].end()));
diameter = max BOOST_PREVENT_MACRO_SUBSTITUTION(diameter,
*std::max_element(d_matrix[i].begin(), d_matrix[i].end()));
std::cout << "The diameter of the boost web-site graph is " << diameter
<< std::endl << std::endl;
<< std::endl
<< std::endl;
std::cout << "Number of clicks from the home page: " << std::endl;
Traits::vertex_iterator vi, vi_end;
@ -185,10 +193,9 @@ main(int argc, const char** argv)
// Do a BFS starting at the home page, recording the parent of each
// vertex (where parent is with respect to the search tree).
Traits::vertex_descriptor src = vertices(g).first[0];
breadth_first_search
(g, src,
boost::visitor(make_bfs_visitor(record_predecessors(&parent[0],
on_tree_edge()))));
breadth_first_search(g, src,
boost::visitor(
make_bfs_visitor(record_predecessors(&parent[0], on_tree_edge()))));
// Add all the search tree edges into a new graph
Graph search_tree(num_vertices(g));
@ -203,8 +210,8 @@ main(int argc, const char** argv)
// the tree nodes in the order that we want to print out:
// a directory-structure like format.
std::vector< size_type > dfs_distances(num_vertices(g), 0);
print_tree_visitor<NameMap, size_type*>
tree_printer(node_name, &dfs_distances[0]);
print_tree_visitor< NameMap, size_type* > tree_printer(
node_name, &dfs_distances[0]);
for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
get(vertex_color, g)[*vi] = white_color;
depth_first_visit(search_tree, src, tree_printer, get(vertex_color, g));

View File

@ -76,17 +76,19 @@ int main()
property< vertex_index_t, long,
property< vertex_color_t, boost::default_color_type,
property< vertex_distance_t, long,
property < vertex_predecessor_t, Traits::edge_descriptor > > > > >,
property< vertex_predecessor_t,
Traits::edge_descriptor > > > > >,
property< edge_capacity_t, long,
property< edge_residual_capacity_t, long,
property < edge_reverse_t, Traits::edge_descriptor > > > > Graph;
property< edge_reverse_t, Traits::edge_descriptor > > > >
Graph;
Graph g;
property_map < Graph, edge_capacity_t >::type
capacity = get(edge_capacity, g);
property_map < Graph, edge_residual_capacity_t >::type
residual_capacity = get(edge_residual_capacity, g);
property_map< Graph, edge_capacity_t >::type capacity
= get(edge_capacity, g);
property_map< Graph, edge_residual_capacity_t >::type residual_capacity
= get(edge_residual_capacity, g);
property_map< Graph, edge_reverse_t >::type rev = get(edge_reverse, g);
Traits::vertex_descriptor s, t;
read_dimacs_max_flow(g, capacity, rev, s, t);
@ -105,7 +107,8 @@ int main()
for (boost::tie(ei, e_end) = out_edges(*u_iter, g); ei != e_end; ++ei)
if (capacity[*ei] > 0)
std::cout << "f " << *u_iter << " " << target(*ei, g) << " "
<< (capacity[*ei] - residual_capacity[*ei]) << std::endl;
<< (capacity[*ei] - residual_capacity[*ei])
<< std::endl;
return EXIT_SUCCESS;
}

View File

@ -22,8 +22,7 @@ typedef undirected_graph<> Graph;
typedef graph_traits< Graph >::vertex_descriptor Vertex;
typedef graph_traits< Graph >::edge_descriptor Edge;
int
main(int argc, char *argv[])
int main(int argc, char* argv[])
{
// Create the graph and read it from standard input.
Graph g;

View File

@ -17,19 +17,17 @@ using namespace boost;
// The clique_printer is a visitor that will print the vertices that comprise
// a clique. Note that the vertices are not given in any specific order.
template <typename OutputStream>
struct clique_printer
template < typename OutputStream > struct clique_printer
{
clique_printer(OutputStream& stream)
: os(stream)
{ }
clique_printer(OutputStream& stream) : os(stream) {}
template < typename Clique, typename Graph >
void clique(const Clique& c, const Graph& g)
{
// Iterate over the clique and print each vertex within it.
typename Clique::const_iterator i, end = c.end();
for(i = c.begin(); i != end; ++i) {
for (i = c.begin(); i != end; ++i)
{
os << g[*i].name << " ";
}
os << endl;
@ -52,8 +50,7 @@ typedef graph_traits<Graph>::edge_descriptor Edge;
// each vertex. This is used during graph creation.
typedef property_map< Graph, string Actor::* >::type NameMap;
int
main(int argc, char *argv[])
int main(int argc, char* argv[])
{
// Create the graph and and its name map accessor.
Graph g;

View File

@ -14,39 +14,47 @@
#include <stdlib.h>
#include <boost/pending/bucket_sorter.hpp>
int main() {
int main()
{
using namespace std;
using boost::bucket_sorter;
const std::size_t N = 10;
vector< std::size_t > bucket(N);
for (std::size_t i=0; i<N; i++) {
for (std::size_t i = 0; i < N; i++)
{
bucket[i] = rand() % N;
cout.width(6);
cout << "Number " << i << " is in bucket " << bucket[i] << endl;
}
typedef boost::identity_property_map ID;
typedef bucket_sorter<std::size_t, int,
vector<std::size_t>::iterator, ID> BS;
typedef bucket_sorter< std::size_t, int, vector< std::size_t >::iterator,
ID >
BS;
BS my_bucket_sorter(N, N, bucket.begin());
for (std::size_t ii = 0; ii < N; ii++)
my_bucket_sorter.push(ii);
std::size_t j;
for (j=0; j<N; j++) {
for (j = 0; j < N; j++)
{
cout << "The bucket " << j;
if ( ! my_bucket_sorter[j].empty() ) {
if (!my_bucket_sorter[j].empty())
{
cout << " has number ";
do {
do
{
int v = my_bucket_sorter[j].top();
my_bucket_sorter[j].pop();
cout << v << " ";
} while (!my_bucket_sorter[j].empty());
cout << endl;
} else {
}
else
{
cout << " has no number associated with it." << endl;
}
}
@ -59,25 +67,32 @@ int main() {
cout << "After removing numbers 5 and 7, check correctness again." << endl;
for (j=0; j<N; j++) {
for (j = 0; j < N; j++)
{
cout << "The bucket " << j;
if ( ! my_bucket_sorter[j].empty() ) {
if (!my_bucket_sorter[j].empty())
{
cout << " has number ";
do {
do
{
int v = my_bucket_sorter[j].top();
my_bucket_sorter[j].pop();
cout << v << " ";
} while (!my_bucket_sorter[j].empty());
cout << endl;
} else {
}
else
{
cout << " has no number associated with it." << endl;
}
}
std::size_t iii;
for (iii=0; iii<N; iii++) {
for (iii = 0; iii < N; iii++)
{
std::size_t current = rand() % N;
if ( ! my_bucket_sorter[current].empty() ) {
if (!my_bucket_sorter[current].empty())
{
int v = my_bucket_sorter[current].top();
my_bucket_sorter[current].pop();
bucket[v] = rand() % N;
@ -85,9 +100,11 @@ int main() {
}
}
for (iii=0; iii<N; iii++) {
for (iii = 0; iii < N; iii++)
{
std::size_t current = rand() % N;
if ( ! my_bucket_sorter[current].empty() ) {
if (!my_bucket_sorter[current].empty())
{
int v = my_bucket_sorter[current].top();
bucket[v] = rand() % N;
my_bucket_sorter.update(v);

View File

@ -16,20 +16,13 @@
#include <boost/graph/planar_canonical_ordering.hpp>
#include <boost/graph/boyer_myrvold_planar_test.hpp>
using namespace boost;
int main(int argc, char** argv)
{
typedef adjacency_list
< vecS,
vecS,
undirectedS,
property<vertex_index_t, int>,
property<edge_index_t, int>
>
typedef adjacency_list< vecS, vecS, undirectedS,
property< vertex_index_t, int >, property< edge_index_t, int > >
graph;
// Create a maximal planar graph on 6 vertices
@ -57,17 +50,13 @@ int main(int argc, char** argv)
for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)
put(e_index, *ei, edge_count++);
// Test for planarity - we know it is planar, we just want to
// compute the planar embedding as a side-effect
typedef std::vector< graph_traits< graph >::edge_descriptor > vec_t;
std::vector< vec_t > embedding(num_vertices(g));
if (boyer_myrvold_planarity_test(boyer_myrvold_params::graph = g,
boyer_myrvold_params::embedding =
make_iterator_property_map(
embedding.begin(), get(vertex_index, g))
)
)
boyer_myrvold_params::embedding = make_iterator_property_map(
embedding.begin(), get(vertex_index, g))))
std::cout << "Input graph is planar" << std::endl;
else
std::cout << "Input graph is not planar" << std::endl;
@ -77,8 +66,7 @@ int main(int argc, char** argv)
ordering_storage_t ordering;
planar_canonical_ordering(g,
make_iterator_property_map(
embedding.begin(), get(vertex_index, g)),
make_iterator_property_map(embedding.begin(), get(vertex_index, g)),
std::back_inserter(ordering));
ordering_storage_t::iterator oi, oi_end;

View File

@ -9,8 +9,8 @@
/*
IMPORTANT!!!
~~~~~~~~~~~~
This example uses interfaces that have been deprecated and removed from Boost.Grpah.
Someone needs to update it, as it does NOT compile.
This example uses interfaces that have been deprecated and removed from
Boost.Grpah. Someone needs to update it, as it does NOT compile.
*/
#include <boost/config.hpp>
@ -20,8 +20,7 @@
#include <boost/graph/connected_components.hpp>
#include <boost/graph/graphviz.hpp>
int
main()
int main()
{
using namespace boost;
GraphvizGraph g;
@ -29,21 +28,20 @@ main()
std::vector< int > component(num_vertices(g));
connected_components
(g, make_iterator_property_map(component.begin(),
get(vertex_index, g), component[0]));
connected_components(g,
make_iterator_property_map(
component.begin(), get(vertex_index, g), component[0]));
property_map < GraphvizGraph, vertex_attribute_t >::type
vertex_attr_map = get(vertex_attribute, g);
std::string color[] = {
"white", "gray", "black", "lightgray"};
property_map< GraphvizGraph, vertex_attribute_t >::type vertex_attr_map
= get(vertex_attribute, g);
std::string color[] = { "white", "gray", "black", "lightgray" };
graph_traits< GraphvizGraph >::vertex_iterator vi, vi_end;
for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) {
for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
{
vertex_attr_map[*vi]["color"] = color[component[*vi]];
vertex_attr_map[*vi]["style"] = "filled";
if (vertex_attr_map[*vi]["color"] == "black")
vertex_attr_map[*vi]["fontcolor"] = "white";
}
write_graphviz("figs/cc-internet-out.dot", g);
}

View File

@ -19,7 +19,6 @@
#include <boost/property_map/property_map.hpp>
#include <boost/graph/graph_utility.hpp> // for boost::make_list
/*
Example of using a visitor with the depth first search
and breadth first search algorithm
@ -54,14 +53,15 @@
using namespace std;
using namespace boost;
struct city_arrival : public base_visitor< city_arrival >
{
city_arrival(string* n) : names(n) {}
typedef on_discover_vertex event_filter;
template < class Vertex, class Graph >
inline void operator()(Vertex u, Graph&) {
cout << endl << "arriving at " << names[u] << endl
inline void operator()(Vertex u, Graph&)
{
cout << endl
<< "arriving at " << names[u] << endl
<< " neighboring cities are: ";
}
string* names;
@ -72,7 +72,8 @@ struct neighbor_cities : public base_visitor<neighbor_cities>
neighbor_cities(string* n) : names(n) {}
typedef on_examine_edge event_filter;
template < class Edge, class Graph >
inline void operator()(Edge e, Graph& g) {
inline void operator()(Edge e, Graph& g)
{
cout << names[target(e, g)] << ", ";
}
string* names;
@ -83,7 +84,8 @@ struct finish_city : public base_visitor<finish_city>
finish_city(string* n) : names(n) {}
typedef on_finish_vertex event_filter;
template < class Vertex, class Graph >
inline void operator()(Vertex u, Graph&) {
inline void operator()(Vertex u, Graph&)
{
cout << endl << "finished with " << names[u] << endl;
}
string* names;
@ -92,20 +94,30 @@ struct finish_city : public base_visitor<finish_city>
int main(int, char*[])
{
enum { SanJose, SanFran, LA, SanDiego, Fresno, LasVegas, Reno,
Sacramento, SaltLake, Phoenix, N };
enum
{
SanJose,
SanFran,
LA,
SanDiego,
Fresno,
LasVegas,
Reno,
Sacramento,
SaltLake,
Phoenix,
N
};
string names[] = { "San Jose", "San Francisco", "Los Angeles", "San Diego",
"Fresno", "Las Vegas", "Reno", "Sacramento",
"Salt Lake City", "Phoenix" };
string names[]
= { "San Jose", "San Francisco", "Los Angeles", "San Diego", "Fresno",
"Las Vegas", "Reno", "Sacramento", "Salt Lake City", "Phoenix" };
typedef std::pair< int, int > E;
E edge_array[] = { E(Sacramento, Reno), E(Sacramento, SanFran),
E(Reno, SaltLake),
E(SanFran, SanJose),
E(SanJose, Fresno), E(SanJose, LA),
E(LA, LasVegas), E(LA, SanDiego),
E(LasVegas, Phoenix) };
E edge_array[]
= { E(Sacramento, Reno), E(Sacramento, SanFran), E(Reno, SaltLake),
E(SanFran, SanJose), E(SanJose, Fresno), E(SanJose, LA),
E(LA, LasVegas), E(LA, SanDiego), E(LasVegas, Phoenix) };
/* Create the graph type we want. */
typedef adjacency_list< vecS, vecS, undirectedS > Graph;
@ -119,22 +131,18 @@ int main(int, char*[])
#endif
cout << "*** Depth First ***" << endl;
depth_first_search
(G,
visitor(make_dfs_visitor(boost::make_list(city_arrival(names),
neighbor_cities(names),
finish_city(names)))));
depth_first_search(G,
visitor(make_dfs_visitor(boost::make_list(
city_arrival(names), neighbor_cities(names), finish_city(names)))));
cout << endl;
/* Get the source vertex */
boost::graph_traits<Graph>::vertex_descriptor
s = vertex(SanJose,G);
boost::graph_traits< Graph >::vertex_descriptor s = vertex(SanJose, G);
cout << "*** Breadth First ***" << endl;
breadth_first_search
(G, s, visitor(make_bfs_visitor(boost::make_list(city_arrival(names),
neighbor_cities(names),
finish_city(names)))));
breadth_first_search(G, s,
visitor(make_bfs_visitor(boost::make_list(
city_arrival(names), neighbor_cities(names), finish_city(names)))));
return 0;
}

View File

@ -49,8 +49,7 @@ typedef boost::exterior_vertex_property<Graph, float> ClosenessProperty;
typedef ClosenessProperty::container_type ClosenessContainer;
typedef ClosenessProperty::map_type ClosenessMap;
int
main(int argc, char *argv[])
int main(int argc, char* argv[])
{
// Create the graph and a property map that provides access to[
// tha actor names.
@ -75,9 +74,10 @@ main(int argc, char *argv[])
// Print the closeness centrality of each vertex.
graph_traits< Graph >::vertex_iterator i, end;
for(boost::tie(i, end) = vertices(g); i != end; ++i) {
cout << setw(12) << setiosflags(ios::left)
<< g[*i].name << get(cm, *i) << endl;
for (boost::tie(i, end) = vertices(g); i != end; ++i)
{
cout << setw(12) << setiosflags(ios::left) << g[*i].name << get(cm, *i)
<< endl;
}
return 0;

View File

@ -4,7 +4,6 @@
// Boost Software License, Version 1.0 (See accompanying file
// LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
//[code_clustering_coefficient
#include <iostream>
#include <iomanip>
@ -38,8 +37,7 @@ typedef exterior_vertex_property<Graph, float> ClusteringProperty;
typedef ClusteringProperty::container_type ClusteringContainer;
typedef ClusteringProperty::map_type ClusteringMap;
int
main(int argc, char *argv[])
int main(int argc, char* argv[])
{
// Create the graph and a name map that provides access to
// then actor names.
@ -58,9 +56,10 @@ main(int argc, char *argv[])
// Print the clustering coefficient of each vertex.
graph_traits< Graph >::vertex_iterator i, end;
for(boost::tie(i, end) = vertices(g); i != end; ++i) {
cout << setw(12) << setiosflags(ios::left)
<< g[*i].name << get(cm, *i) << endl;
for (boost::tie(i, end) = vertices(g); i != end; ++i)
{
cout << setw(12) << setiosflags(ios::left) << g[*i].name << get(cm, *i)
<< endl;
}
cout << "mean clustering coefficient: " << cc << endl;

View File

@ -48,7 +48,6 @@
*/
using namespace std;
using boost::tie;
@ -61,9 +60,8 @@ int main(int , char* [])
const int E = 4;
Edge edgelist[] = { Edge(0, 1), Edge(1, 4), Edge(4, 0), Edge(2, 5) };
edge_list<Edge*,Edge,ptrdiff_t,std::random_access_iterator_tag> g(edgelist, edgelist + E);
edge_list< Edge*, Edge, ptrdiff_t, std::random_access_iterator_tag > g(
edgelist, edgelist + E);
cout << "An undirected graph (edge list):" << endl;
print_edges(g, identity_property_map());
cout << endl;
@ -71,19 +69,20 @@ int main(int , char* [])
disjoint_sets_with_storage<> ds(N);
incremental_components(g, ds);
component_index<int> components(&ds.parents()[0],
&ds.parents()[0] + ds.parents().size());
component_index< int > components(
&ds.parents()[0], &ds.parents()[0] + ds.parents().size());
cout << "Total number of components: " << components.size() << endl;
for (int k = 0; k != N; ++k)
cout << "Vertex " << k << " is in the component who's representative is "
cout << "Vertex " << k
<< " is in the component who's representative is "
<< ds.find_set(k) << endl;
cout << endl;
for (std::size_t i = 0; i < components.size(); ++i) {
for (std::size_t i = 0; i < components.size(); ++i)
{
cout << "component " << i << " contains: ";
component_index<int>::component_iterator
j = components[i].first,
component_index< int >::component_iterator j = components[i].first,
jend = components[i].second;
for (; j != jend; ++j)
cout << *j << " ";

View File

@ -11,8 +11,7 @@
#include <boost/graph/connected_components.hpp>
#include <boost/graph/adjacency_list.hpp>
int
main()
int main()
{
using namespace boost;
typedef adjacency_list< vecS, vecS, undirectedS > Graph;
@ -25,15 +24,15 @@ main()
add_edge(2, 5, G);
std::vector< int > c(num_vertices(G));
int num = connected_components
(G, make_iterator_property_map(c.begin(), get(vertex_index, G), c[0]));
int num = connected_components(
G, make_iterator_property_map(c.begin(), get(vertex_index, G), c[0]));
std::cout << std::endl;
std::vector< int >::iterator i;
std::cout << "Total number of components: " << num << std::endl;
for (i = c.begin(); i != c.end(); ++i)
std::cout << "Vertex " << i - c.begin()
<< " is in component " << *i << std::endl;
std::cout << "Vertex " << i - c.begin() << " is in component " << *i
<< std::endl;
std::cout << std::endl;
return EXIT_SUCCESS;
}

View File

@ -54,9 +54,9 @@ int main(int , char* [])
std::vector< int >::size_type i;
cout << "Total number of components: " << num << endl;
for (i = 0; i != component.size(); ++i)
cout << "Vertex " << i <<" is in component " << component[i] << endl;
cout << "Vertex " << i << " is in component " << component[i]
<< endl;
cout << endl;
}
return 0;
}

View File

@ -8,19 +8,24 @@
//=======================================================================
#include <boost/graph/adjacency_list.hpp>
#if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(BOOST_NO_STD_ALLOCATOR)
#if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) \
&& !defined(BOOST_NO_STD_ALLOCATOR)
template <class Allocator>
struct list_with_allocatorS { };
template < class Allocator > struct list_with_allocatorS
{
};
namespace boost {
namespace boost
{
template < class Alloc, class ValueType >
struct container_gen<list_with_allocatorS<Alloc>, ValueType> {
struct container_gen< list_with_allocatorS< Alloc >, ValueType >
{
typedef typename Alloc::template rebind< ValueType >::other Allocator;
typedef std::list< ValueType, Allocator > type;
};
template < class Alloc >
struct parallel_edge_traits< list_with_allocatorS<Alloc> > {
struct parallel_edge_traits< list_with_allocatorS< Alloc > >
{
typedef allow_parallel_edge_tag type;
};
@ -28,7 +33,8 @@ namespace boost {
// now you can define a graph using std::list and a specific allocator
typedef boost::adjacency_list< list_with_allocatorS< std::allocator< int > >,
boost::vecS, boost::directedS> MyGraph;
boost::vecS, boost::directedS >
MyGraph;
int main(int, char*[])
{
@ -39,9 +45,6 @@ int main(int, char*[])
#else
int main(int, char*[])
{
return 0;
}
int main(int, char*[]) { return 0; }
#endif

View File

@ -11,27 +11,34 @@
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_utility.hpp>
int
main()
int main()
{
using namespace boost;
typedef adjacency_list< vecS, vecS, directedS,
property < vertex_name_t, char > > graph_t;
property< vertex_name_t, char > >
graph_t;
enum
{ a, b, c, d, e, f, g, N };
{
a,
b,
c,
d,
e,
f,
g,
N
};
graph_t G(N);
property_map < graph_t, vertex_name_t >::type
name_map = get(vertex_name, G);
property_map< graph_t, vertex_name_t >::type name_map = get(vertex_name, G);
char name = 'a';
graph_traits< graph_t >::vertex_iterator v, v_end;
for (boost::tie(v, v_end) = vertices(G); v != v_end; ++v, ++name)
name_map[*v] = name;
typedef std::pair< int, int > E;
E edges[] = { E(a, c), E(a, d), E(b, a), E(b, d), E(c, f),
E(d, c), E(d, e), E(d, f), E(e, b), E(e, g), E(f, e), E(f, g)
};
E edges[] = { E(a, c), E(a, d), E(b, a), E(b, d), E(c, f), E(d, c), E(d, e),
E(d, f), E(e, b), E(e, g), E(f, e), E(f, g) };
for (int i = 0; i < 12; ++i)
add_edge(edges[i].first, edges[i].second, G);

View File

@ -35,11 +35,11 @@ int main()
};
E the_edges[] = { E(0, 1), E(0, 2), E(0, 3), E(1, 0), E(1, 3), E(1, 5),
E(2, 0), E(2, 5), E(3, 1), E(3, 4), E(4, 1), E(5, 0),
E(5, 2) };
E(2, 0), E(2, 5), E(3, 1), E(3, 4), E(4, 1), E(5, 0), E(5, 2) };
typedef compressed_sparse_row_graph< directedS, WebPage > WebGraph;
WebGraph g(boost::edges_are_sorted, &the_edges[0], &the_edges[0] + sizeof(the_edges)/sizeof(E), 6);
WebGraph g(boost::edges_are_sorted, &the_edges[0],
&the_edges[0] + sizeof(the_edges) / sizeof(E), 6);
// Set the URLs of each vertex
int index = 0;

View File

@ -35,7 +35,8 @@ int main(int , char* [])
using namespace std;
typedef adjacency_list< vecS, vecS, undirectedS,
property< vertex_color_t, default_color_type,
property<vertex_degree_t,int> > > Graph;
property< vertex_degree_t, int > > >
Graph;
typedef graph_traits< Graph >::vertex_descriptor Vertex;
typedef graph_traits< Graph >::vertices_size_type size_type;
@ -65,8 +66,8 @@ int main(int , char* [])
for (boost::tie(ui, ui_end) = vertices(G); ui != ui_end; ++ui)
deg[*ui] = degree(*ui, G);
property_map<Graph, vertex_index_t>::type
index_map = get(vertex_index, G);
property_map< Graph, vertex_index_t >::type index_map
= get(vertex_index, G);
std::cout << "original bandwidth: " << bandwidth(G) << std::endl;
@ -87,7 +88,9 @@ int main(int , char* [])
for (size_type c = 0; c != inv_perm.size(); ++c)
perm[index_map[inv_perm[c]]] = c;
std::cout << " bandwidth: "
<< bandwidth(G, make_iterator_property_map(&perm[0], index_map, perm[0]))
<< bandwidth(G,
make_iterator_property_map(
&perm[0], index_map, perm[0]))
<< std::endl;
}
{
@ -105,14 +108,16 @@ int main(int , char* [])
for (size_type c = 0; c != inv_perm.size(); ++c)
perm[index_map[inv_perm[c]]] = c;
std::cout << " bandwidth: "
<< bandwidth(G, make_iterator_property_map(&perm[0], index_map, perm[0]))
<< bandwidth(G,
make_iterator_property_map(
&perm[0], index_map, perm[0]))
<< std::endl;
}
{
// reverse cuthill_mckee_ordering
cuthill_mckee_ordering(G, inv_perm.rbegin(), get(vertex_color, G),
make_degree_map(G));
cuthill_mckee_ordering(
G, inv_perm.rbegin(), get(vertex_color, G), make_degree_map(G));
cout << "Reverse Cuthill-McKee ordering:" << endl;
cout << " ";
@ -124,7 +129,9 @@ int main(int , char* [])
for (size_type c = 0; c != inv_perm.size(); ++c)
perm[index_map[inv_perm[c]]] = c;
std::cout << " bandwidth: "
<< bandwidth(G, make_iterator_property_map(&perm[0], index_map, perm[0]))
<< bandwidth(G,
make_iterator_property_map(
&perm[0], index_map, perm[0]))
<< std::endl;
}
return 0;

View File

@ -27,29 +27,30 @@ namespace std
typedef adjacency_list< listS, // Store out-edges of each vertex in a std::list
vecS, // Store vertex set in a std::vector
directedS // The file dependency graph is directed
> file_dep_graph;
>
file_dep_graph;
typedef graph_traits< file_dep_graph >::vertex_descriptor vertex_t;
typedef graph_traits< file_dep_graph >::edge_descriptor edge_t;
bool
has_cycle_dfs(const file_dep_graph & g, vertex_t u,
default_color_type * color)
bool has_cycle_dfs(
const file_dep_graph& g, vertex_t u, default_color_type* color)
{
color[u] = gray_color;
graph_traits< file_dep_graph >::adjacency_iterator vi, vi_end;
for (boost::tie(vi, vi_end) = adjacent_vertices(u, g); vi != vi_end; ++vi)
if (color[*vi] == white_color) {
if (color[*vi] == white_color)
{
if (has_cycle_dfs(g, *vi, color))
return true; // cycle detected, return immediately
} else if (color[*vi] == gray_color) // *vi is an ancestor!
}
else if (color[*vi] == gray_color) // *vi is an ancestor!
return true;
color[u] = black_color;
return false;
}
bool
has_cycle(const file_dep_graph & g)
bool has_cycle(const file_dep_graph& g)
{
std::vector< default_color_type > color(num_vertices(g), white_color);
graph_traits< file_dep_graph >::vertex_iterator vi, vi_end;
@ -60,20 +61,20 @@ has_cycle(const file_dep_graph & g)
return false;
}
int
main(int argc, const char** argv)
int main(int argc, const char** argv)
{
std::ifstream file_in(argc >= 2 ? argv[1] : "makefile-dependencies.dat");
typedef graph_traits< file_dep_graph >::vertices_size_type size_type;
size_type n_vertices;
file_in >> n_vertices; // read in number of vertices
std::istream_iterator < std::pair < size_type,
size_type > > input_begin(file_in), input_end;
std::istream_iterator< std::pair< size_type, size_type > > input_begin(
file_in),
input_end;
#if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
// VC++ has trouble with the edge iterator constructor
file_dep_graph g(n_vertices);
while (input_begin != input_end) {
while (input_begin != input_end)
{
size_type i, j;
boost::tie(i, j) = *input_begin++;
add_edge(i, j, g);

View File

@ -16,37 +16,40 @@
// we get conflict with boost::default_dfs_visitor.
using namespace boost;
namespace std {
namespace std
{
template < typename T >
std::istream& operator>>(std::istream& in, std::pair< T, T >& p)
{
in >> p.first >> p.second;
return
in;
return in;
}
}
typedef adjacency_list<
listS, // Store out-edges of each vertex in a std::list
typedef adjacency_list< listS, // Store out-edges of each vertex in a std::list
vecS, // Store vertex set in a std::vector
directedS // The file dependency graph is directed
> file_dep_graph;
>
file_dep_graph;
typedef graph_traits< file_dep_graph >::vertex_descriptor vertex_t;
typedef graph_traits< file_dep_graph >::edge_descriptor edge_t;
template < typename Visitor > void
dfs_v1(const file_dep_graph & g, vertex_t u, default_color_type * color,
Visitor vis)
template < typename Visitor >
void dfs_v1(
const file_dep_graph& g, vertex_t u, default_color_type* color, Visitor vis)
{
color[u] = gray_color;
vis.discover_vertex(u, g);
graph_traits< file_dep_graph >::out_edge_iterator ei, ei_end;
for (boost::tie(ei, ei_end) = out_edges(u, g); ei != ei_end; ++ei) {
if (color[target(*ei, g)] == white_color) {
for (boost::tie(ei, ei_end) = out_edges(u, g); ei != ei_end; ++ei)
{
if (color[target(*ei, g)] == white_color)
{
vis.tree_edge(*ei, g);
dfs_v1(g, target(*ei, g), color, vis);
} else if (color[target(*ei, g)] == gray_color)
}
else if (color[target(*ei, g)] == gray_color)
vis.back_edge(*ei, g);
else
vis.forward_or_cross_edge(*ei, g);
@ -55,12 +58,13 @@ dfs_v1(const file_dep_graph & g, vertex_t u, default_color_type * color,
vis.finish_vertex(u, g);
}
template < typename Visitor > void
generic_dfs_v1(const file_dep_graph & g, Visitor vis)
template < typename Visitor >
void generic_dfs_v1(const file_dep_graph& g, Visitor vis)
{
std::vector< default_color_type > color(num_vertices(g), white_color);
graph_traits< file_dep_graph >::vertex_iterator vi, vi_end;
for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) {
for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
{
if (color[*vi] == white_color)
dfs_v1(g, *vi, &color[0], vis);
}
@ -68,48 +72,27 @@ generic_dfs_v1(const file_dep_graph & g, Visitor vis)
struct dfs_visitor_default
{
template <typename V, typename G > void
discover_vertex(V, const G &)
template < typename V, typename G > void discover_vertex(V, const G&) {}
template < typename E, typename G > void tree_edge(E, const G&) {}
template < typename E, typename G > void back_edge(E, const G&) {}
template < typename E, typename G > void forward_or_cross_edge(E, const G&)
{
}
template <typename E, typename G > void
tree_edge(E, const G &)
{
}
template < typename E, typename G > void
back_edge(E, const G &)
{
}
template < typename E, typename G > void
forward_or_cross_edge(E, const G &)
{
}
template < typename V, typename G > void
finish_vertex(V, const G &)
{
}
template < typename V, typename G > void finish_vertex(V, const G&) {}
};
struct cycle_detector : public dfs_visitor_default
{
cycle_detector(bool & cycle):
has_cycle(cycle)
{
}
void
back_edge(edge_t, const file_dep_graph &)
{
has_cycle = true;
}
cycle_detector(bool& cycle) : has_cycle(cycle) {}
void back_edge(edge_t, const file_dep_graph&) { has_cycle = true; }
bool& has_cycle;
};
bool
has_cycle(const file_dep_graph & g)
bool has_cycle(const file_dep_graph& g)
{
bool has_cycle = false;
cycle_detector vis(has_cycle);
@ -117,20 +100,20 @@ has_cycle(const file_dep_graph & g)
return has_cycle;
}
int
main(int argc, const char** argv)
int main(int argc, const char** argv)
{
std::ifstream file_in(argc >= 2 ? argv[1] : "makefile-dependencies.dat");
typedef graph_traits< file_dep_graph >::vertices_size_type size_type;
size_type n_vertices;
file_in >> n_vertices; // read in number of vertices
std::istream_iterator < std::pair < size_type,
size_type > >input_begin(file_in), input_end;
std::istream_iterator< std::pair< size_type, size_type > > input_begin(
file_in),
input_end;
#if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
// VC++ has trouble with the edge iterator constructor
file_dep_graph g(n_vertices);
while (input_begin != input_end) {
while (input_begin != input_end)
{
size_type i, j;
boost::tie(i, j) = *input_begin++;
add_edge(i, j, g);

View File

@ -12,8 +12,8 @@
#include "../test/min_cost_max_flow_utils.hpp"
int main() {
int main()
{
boost::SampleGraph::vertex_descriptor s, t;
boost::SampleGraph::Graph g;
boost::SampleGraph::getSampleGraph(g, s, t);
@ -25,4 +25,3 @@ int main() {
assert(cost == 29);
return 0;
}

View File

@ -18,26 +18,24 @@
*/
using namespace boost;
typedef adjacency_list<
listS, listS, directedS,
typedef adjacency_list< listS, listS, directedS,
property< vertex_index_t, int >,
property<
edge_weight_t, double, property<edge_weight2_t, double>
>
> grap_real_t;
property< edge_weight_t, double, property< edge_weight2_t, double > > >
grap_real_t;
template <typename TG>
void gen_rand_graph(TG &g, size_t nV, size_t nE)
template < typename TG > void gen_rand_graph(TG& g, size_t nV, size_t nE)
{
g.clear();
mt19937 rng;
rng.seed(uint32_t(time(0)));
boost::generate_random_graph(g, nV, nE, rng, true, true);
boost::uniform_real<> ur(-1, 10);
boost::variate_generator<boost::mt19937&, boost::uniform_real<> > ew1rg(rng, ur);
boost::variate_generator< boost::mt19937&, boost::uniform_real<> > ew1rg(
rng, ur);
randomize_property< edge_weight_t >(g, ew1rg);
boost::uniform_int< size_t > uint(1, 5);
boost::variate_generator<boost::mt19937&, boost::uniform_int<size_t> > ew2rg(rng, uint);
boost::variate_generator< boost::mt19937&, boost::uniform_int< size_t > >
ew2rg(rng, uint);
randomize_property< edge_weight2_t >(g, ew2rg);
}
@ -47,20 +45,25 @@ int main(int argc, char* argv[])
using std::endl;
const double epsilon = 0.0000001;
double min_cr, max_cr; /// Minimum and maximum cycle ratio
typedef std::vector<graph_traits<grap_real_t>::edge_descriptor> ccReal_t;
typedef std::vector< graph_traits< grap_real_t >::edge_descriptor >
ccReal_t;
ccReal_t cc; /// critical cycle
grap_real_t tgr;
property_map<grap_real_t, vertex_index_t>::type vim = get(vertex_index, tgr);
property_map<grap_real_t, edge_weight_t>::type ew1 = get(edge_weight, tgr);
property_map<grap_real_t, edge_weight2_t>::type ew2 = get(edge_weight2, tgr);
property_map< grap_real_t, vertex_index_t >::type vim
= get(vertex_index, tgr);
property_map< grap_real_t, edge_weight_t >::type ew1
= get(edge_weight, tgr);
property_map< grap_real_t, edge_weight2_t >::type ew2
= get(edge_weight2, tgr);
gen_rand_graph(tgr, 1000, 30000);
cout << "Vertices number: " << num_vertices(tgr) << endl;
cout << "Edges number: " << num_edges(tgr) << endl;
int i = 0;
graph_traits< grap_real_t >::vertex_iterator vi, vi_end;
for (boost::tie(vi, vi_end) = vertices(tgr); vi != vi_end; vi++) {
for (boost::tie(vi, vi_end) = vertices(tgr); vi != vi_end; vi++)
{
vim[*vi] = i++; /// Initialize vertex index property
}
max_cr = maximum_cycle_ratio(tgr, vim, ew1, ew2);
@ -73,11 +76,10 @@ int main(int argc, char* argv[])
{
cr.first += ew1[*itr];
cr.second += ew2[*itr];
std::cout << "(" << vim[source(*itr, tgr)] << "," <<
vim[target(*itr, tgr)] << ") ";
std::cout << "(" << vim[source(*itr, tgr)] << ","
<< vim[target(*itr, tgr)] << ") ";
}
cout << endl;
assert(std::abs(cr.first / cr.second - min_cr) < epsilon * 2);
return EXIT_SUCCESS;
}

View File

@ -26,9 +26,18 @@ int main()
{
using namespace boost;
typedef adjacency_list< vecS, vecS, directedS,
property<vertex_distance_t, int>, property<edge_weight_t, int> > graph_t;
property< vertex_distance_t, int >, property< edge_weight_t, int > >
graph_t;
graph_t g(6);
enum verts { r, s, t, u, v, x };
enum verts
{
r,
s,
t,
u,
v,
x
};
char name[] = "rstuvx";
add_edge(r, s, 5, g);
add_edge(r, t, 3, g);
@ -41,8 +50,8 @@ int main()
add_edge(u, x, 1, g);
add_edge(v, x, -2, g);
property_map<graph_t, vertex_distance_t>::type
d_map = get(vertex_distance, g);
property_map< graph_t, vertex_distance_t >::type d_map
= get(vertex_distance, g);
#if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
// VC++ has trouble with the named-parameter mechanism, so
@ -53,8 +62,8 @@ int main()
std::less< int > compare;
closed_plus< int > combine;
property_map< graph_t, edge_weight_t >::type w_map = get(edge_weight, g);
dag_shortest_paths(g, s, d_map, w_map, &color[0], &pred[0],
vis, compare, combine, (std::numeric_limits<int>::max)(), 0);
dag_shortest_paths(g, s, d_map, w_map, &color[0], &pred[0], vis, compare,
combine, (std::numeric_limits< int >::max)(), 0);
#else
dag_shortest_paths(g, s, distance_map(d_map));
#endif

View File

@ -72,52 +72,50 @@ b(14); d a
*/
typedef property< vertex_color_t, default_color_type,
property<vertex_distance_t,int> > VProperty;
property< vertex_distance_t, int > >
VProperty;
typedef int weight_t;
typedef property< edge_weight_t, weight_t > EProperty;
typedef adjacency_list< vecS, vecS, directedS, VProperty, EProperty > Graph;
template < class Tag >
struct endl_printer
: public boost::base_visitor< endl_printer<Tag> >
struct endl_printer : public boost::base_visitor< endl_printer< Tag > >
{
typedef Tag event_filter;
endl_printer(std::ostream& os) : m_os(os) {}
template <class T, class Graph>
void operator()(T, Graph&) { m_os << std::endl; }
template < class T, class Graph > void operator()(T, Graph&)
{
m_os << std::endl;
}
std::ostream& m_os;
};
template <class Tag>
endl_printer<Tag> print_endl(std::ostream& os, Tag) {
template < class Tag > endl_printer< Tag > print_endl(std::ostream& os, Tag)
{
return endl_printer< Tag >(os);
}
template < class PA, class Tag >
struct edge_printer
: public boost::base_visitor< edge_printer<PA, Tag> >
struct edge_printer : public boost::base_visitor< edge_printer< PA, Tag > >
{
typedef Tag event_filter;
edge_printer(PA pa, std::ostream& os) : m_pa(pa), m_os(os) {}
template <class T, class Graph>
void operator()(T x, Graph& g) {
m_os << "(" << get(m_pa, source(x, g)) << ","
<< get(m_pa, target(x, g)) << ") ";
template < class T, class Graph > void operator()(T x, Graph& g)
{
m_os << "(" << get(m_pa, source(x, g)) << "," << get(m_pa, target(x, g))
<< ") ";
}
PA m_pa;
std::ostream& m_os;
};
template < class PA, class Tag >
edge_printer<PA, Tag>
print_edge(PA pa, std::ostream& os, Tag) {
edge_printer< PA, Tag > print_edge(PA pa, std::ostream& os, Tag)
{
return edge_printer< PA, Tag >(pa, os);
}
template < class NewGraph, class Tag >
struct graph_copier
: public boost::base_visitor< graph_copier< NewGraph, Tag > >
@ -126,44 +124,53 @@ struct graph_copier
graph_copier(NewGraph& graph) : new_g(graph) {}
template <class Edge, class Graph>
void operator()(Edge e, Graph& g) {
template < class Edge, class Graph > void operator()(Edge e, Graph& g)
{
add_edge(source(e, g), target(e, g), new_g);
}
private:
NewGraph& new_g;
};
template < class NewGraph, class Tag >
inline graph_copier<NewGraph, Tag>
copy_graph(NewGraph& g, Tag) {
inline graph_copier< NewGraph, Tag > copy_graph(NewGraph& g, Tag)
{
return graph_copier< NewGraph, Tag >(g);
}
template <class Graph, class Name>
void print(Graph& G, Name name)
template < class Graph, class Name > void print(Graph& G, Name name)
{
typename boost::graph_traits< Graph >::vertex_iterator ui, uiend;
for (boost::tie(ui, uiend) = vertices(G); ui != uiend; ++ui) {
for (boost::tie(ui, uiend) = vertices(G); ui != uiend; ++ui)
{
cout << name[*ui] << " --> ";
typename boost::graph_traits< Graph >::adjacency_iterator vi, viend;
for(boost::tie(vi, viend) = adjacent_vertices(*ui, G); vi != viend; ++vi)
for (boost::tie(vi, viend) = adjacent_vertices(*ui, G); vi != viend;
++vi)
cout << name[*vi] << " ";
cout << endl;
}
}
int
main(int , char* [])
int main(int, char*[])
{
// Name and ID numbers for the vertices
char name[] = "abcdefg";
enum { a, b, c, d, e, f, g, N};
enum
{
a,
b,
c,
d,
e,
f,
g,
N
};
Graph G(N);
boost::property_map<Graph, vertex_index_t>::type
vertex_id = get(vertex_index, G);
boost::property_map< Graph, vertex_index_t >::type vertex_id
= get(vertex_index, G);
std::vector< weight_t > distance(N, (numeric_limits< weight_t >::max)());
typedef boost::graph_traits< Graph >::vertex_descriptor Vertex;
@ -171,19 +178,10 @@ main(int , char* [])
typedef std::pair< int, int > E;
E edges[] = { E(a,c), E(a,d),
E(b,a), E(b,d),
E(c,f),
E(d,c), E(d,e), E(d,f),
E(e,b), E(e,g),
E(f,e), E(f,g) };
E edges[] = { E(a, c), E(a, d), E(b, a), E(b, d), E(c, f), E(d, c), E(d, e),
E(d, f), E(e, b), E(e, g), E(f, e), E(f, g) };
int weight[] = { 3, 4,
6, 8,
12,
7, 0, 5,
10, 3,
1, 2 };
int weight[] = { 3, 4, 6, 8, 12, 7, 0, 5, 10, 3, 1, 2 };
for (int i = 0; i < 12; ++i)
add_edge(edges[i].first, edges[i].second, weight[i], G);
@ -191,7 +189,8 @@ main(int , char* [])
print(G, name);
adjacency_list< listS, vecS, directedS,
property<vertex_color_t, default_color_type> > G_copy(N);
property< vertex_color_t, default_color_type > >
G_copy(N);
cout << "Starting graph:" << endl;
@ -199,19 +198,14 @@ main(int , char* [])
std::ostream_iterator< char > cout_char(std::cout, " ");
boost::queue< Vertex > Q;
boost::breadth_first_search
(G, vertex(a, G), Q,
make_bfs_visitor(
boost::make_list
(write_property(make_iterator_property_map(name, vertex_id,
name[0]),
boost::breadth_first_search(G, vertex(a, G), Q,
make_bfs_visitor(boost::make_list(
write_property(make_iterator_property_map(name, vertex_id, name[0]),
cout_char, on_examine_vertex()),
write_property(make_iterator_property_map(distance.begin(),
vertex_id,
distance[0]),
write_property(make_iterator_property_map(
distance.begin(), vertex_id, distance[0]),
cout_int, on_examine_vertex()),
print_edge(make_iterator_property_map(name, vertex_id,
name[0]),
print_edge(make_iterator_property_map(name, vertex_id, name[0]),
std::cout, on_examine_edge()),
print_endl(std::cout, on_finish_vertex()))),
get(vertex_color, G));
@ -219,29 +213,24 @@ main(int , char* [])
std::cout << "about to call dijkstra's" << std::endl;
parent[vertex(a, G)] = vertex(a, G);
boost::dijkstra_shortest_paths
(G, vertex(a, G),
distance_map(make_iterator_property_map(distance.begin(), vertex_id,
distance[0])).
predecessor_map(make_iterator_property_map(parent.begin(), vertex_id,
parent[0])).
visitor(make_dijkstra_visitor(copy_graph(G_copy, on_examine_edge()))));
boost::dijkstra_shortest_paths(G, vertex(a, G),
distance_map(make_iterator_property_map(
distance.begin(), vertex_id, distance[0]))
.predecessor_map(make_iterator_property_map(
parent.begin(), vertex_id, parent[0]))
.visitor(
make_dijkstra_visitor(copy_graph(G_copy, on_examine_edge()))));
cout << endl;
cout << "Result:" << endl;
boost::breadth_first_search
(G, vertex(a, G),
visitor(make_bfs_visitor(
boost::make_list
(write_property(make_iterator_property_map(name, vertex_id,
name[0]),
boost::breadth_first_search(G, vertex(a, G),
visitor(make_bfs_visitor(boost::make_list(
write_property(make_iterator_property_map(name, vertex_id, name[0]),
cout_char, on_examine_vertex()),
write_property(make_iterator_property_map(distance.begin(),
vertex_id,
distance[0]),
write_property(make_iterator_property_map(
distance.begin(), vertex_id, distance[0]),
cout_int, on_examine_vertex()),
print_edge(make_iterator_property_map(name, vertex_id,
name[0]),
print_edge(make_iterator_property_map(name, vertex_id, name[0]),
std::cout, on_examine_edge()),
print_endl(std::cout, on_finish_vertex())))));

View File

@ -11,8 +11,7 @@
using namespace boost;
template < typename Graph > void
read_graph_file(std::istream & in, Graph & g)
template < typename Graph > void read_graph_file(std::istream& in, Graph& g)
{
typedef typename graph_traits< Graph >::vertices_size_type size_type;
size_type n_vertices;
@ -27,14 +26,14 @@ read_graph_file(std::istream & in, Graph & g)
break;
}
int
main(int argc, const char** argv)
int main(int argc, const char** argv)
{
typedef adjacency_list < listS, // Store out-edges of each vertex in a std::list
typedef adjacency_list< listS, // Store out-edges of each vertex in a
// std::list
vecS, // Store vertex set in a std::vector
directedS // The graph is directed
> graph_type;
>
graph_type;
graph_type g; // use default constructor to create empty graph
std::ifstream file_in(argc >= 2 ? argv[1] : "makefile-dependencies.dat");

View File

@ -11,8 +11,7 @@
using namespace boost;
template < typename Graph > void
read_graph_file(std::istream & in, Graph & g)
template < typename Graph > void read_graph_file(std::istream& in, Graph& g)
{
typedef typename graph_traits< Graph >::vertex_descriptor Vertex;
typedef typename graph_traits< Graph >::vertices_size_type size_type;
@ -30,14 +29,14 @@ read_graph_file(std::istream & in, Graph & g)
break;
}
int
main(int argc, const char** argv)
int main(int argc, const char** argv)
{
typedef adjacency_list < listS, // Store out-edges of each vertex in a std::list
typedef adjacency_list< listS, // Store out-edges of each vertex in a
// std::list
vecS, // Store vertex set in a std::vector
directedS // The graph is directed
> graph_type;
>
graph_type;
graph_type g; // use default constructor to create empty graph
std::ifstream file_in(argc >= 2 ? argv[1] : "makefile-dependencies.dat");

View File

@ -4,7 +4,6 @@
// Boost Software License, Version 1.0 (See accompanying file
// LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
//[degree_centrality_example
#include <iostream>
#include <iomanip>
@ -39,8 +38,7 @@ typedef exterior_vertex_property<Graph, unsigned> CentralityProperty;
typedef CentralityProperty::container_type CentralityContainer;
typedef CentralityProperty::map_type CentralityMap;
int
main(int argc, char *argv[])
int main(int argc, char* argv[])
{
// Create the graph and a property map that provides access
// to the actor names.
@ -57,9 +55,10 @@ main(int argc, char *argv[])
// Print the degree centrality of each vertex.
graph_traits< Graph >::vertex_iterator i, end;
for(boost::tie(i, end) = vertices(g); i != end; ++i) {
cout << setiosflags(ios::left) << setw(12)
<< g[*i].name << cm[*i] << endl;
for (boost::tie(i, end) = vertices(g); i != end; ++i)
{
cout << setiosflags(ios::left) << setw(12) << g[*i].name << cm[*i]
<< endl;
}
return 0;

View File

@ -13,11 +13,15 @@
#include <iostream>
using namespace boost;
template < typename TimeMap > class dfs_time_visitor:public default_dfs_visitor {
template < typename TimeMap >
class dfs_time_visitor : public default_dfs_visitor
{
typedef typename property_traits< TimeMap >::value_type T;
public:
dfs_time_visitor(TimeMap dmap, TimeMap fmap, T& t)
: m_dtimemap(dmap), m_ftimemap(fmap), m_time(t) {
: m_dtimemap(dmap), m_ftimemap(fmap), m_time(t)
{
}
template < typename Vertex, typename Graph >
void discover_vertex(Vertex u, const Graph& g) const
@ -34,22 +38,27 @@ public:
T& m_time;
};
int
main()
int main()
{
// Select the graph type we wish to use
typedef adjacency_list< vecS, vecS, directedS > graph_t;
typedef graph_traits< graph_t >::vertices_size_type size_type;
// Set up the vertex names
enum
{ u, v, w, x, y, z, N };
{
u,
v,
w,
x,
y,
z,
N
};
char name[] = { 'u', 'v', 'w', 'x', 'y', 'z' };
// Specify the edges in the graph
typedef std::pair< int, int > E;
E edge_array[] = { E(u, v), E(u, x), E(x, v), E(y, x),
E(v, y), E(w, y), E(w, z), E(z, z)
};
E edge_array[] = { E(u, v), E(u, x), E(x, v), E(y, x), E(v, y), E(w, y),
E(w, z), E(z, z) };
#if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
graph_t g(N);
for (std::size_t j = 0; j < sizeof(edge_array) / sizeof(E); ++j)
@ -61,8 +70,7 @@ main()
// discover time and finish time properties
std::vector< size_type > dtime(num_vertices(g));
std::vector< size_type > ftime(num_vertices(g));
typedef
iterator_property_map<std::vector<size_type>::iterator,
typedef iterator_property_map< std::vector< size_type >::iterator,
property_map< graph_t, vertex_index_t >::const_type >
time_pm_type;
time_pm_type dtime_pm(dtime.begin(), get(vertex_index, g));

View File

@ -9,8 +9,8 @@
/*
IMPORTANT!!!
~~~~~~~~~~~~
This example uses interfaces that have been deprecated and removed from Boost.Grpah.
Someone needs to update it, as it does NOT compile.
This example uses interfaces that have been deprecated and removed from
Boost.Grpah. Someone needs to update it, as it does NOT compile.
*/
#include <boost/graph/graphviz.hpp>
@ -20,35 +20,33 @@ char name[] = "abcdefghij";
struct parenthesis_visitor : public boost::default_dfs_visitor
{
template <class Vertex, class Graph> void
start_vertex(Vertex v, const Graph &)
template < class Vertex, class Graph >
void start_vertex(Vertex v, const Graph&)
{
std::cout << ' ';
}
template <class Vertex, class Graph> void
discover_vertex(Vertex v, const Graph &)
template < class Vertex, class Graph >
void discover_vertex(Vertex v, const Graph&)
{
std::cout << "(" << name[v] << ' ';
}
template <class Vertex, class Graph> void
finish_vertex(Vertex v, const Graph &)
template < class Vertex, class Graph >
void finish_vertex(Vertex v, const Graph&)
{
std::cout << ' ' << name[v] << ")";
}
};
int
main()
int main()
{
using namespace boost;
GraphvizGraph g;
read_graphviz("figs/dfs-example.dot", g);
graph_traits< GraphvizGraph >::edge_iterator e, e_end;
for (boost::tie(e, e_end) = edges(g); e != e_end; ++e)
std::cout << '(' << name[source(*e, g)] << ' '
<< name[target(*e, g)] << ')' << std::endl;
parenthesis_visitor
paren_vis;
std::cout << '(' << name[source(*e, g)] << ' ' << name[target(*e, g)]
<< ')' << std::endl;
parenthesis_visitor paren_vis;
depth_first_search(g, visitor(paren_vis));
std::cout << std::endl;
return 0;

View File

@ -14,7 +14,6 @@
#include <algorithm>
#include <utility>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/depth_first_search.hpp>
#include <boost/graph/visitors.hpp>
@ -50,46 +49,46 @@
using namespace boost;
using namespace std;
template < class VisitorList >
struct edge_categorizer : public dfs_visitor<VisitorList> {
struct edge_categorizer : public dfs_visitor< VisitorList >
{
typedef dfs_visitor< VisitorList > Base;
edge_categorizer(const VisitorList& v = null_visitor()) : Base(v) {}
template <class Edge, class Graph>
void tree_edge(Edge e, Graph& G) {
cout << "Tree edge: " << source(e, G) <<
" --> " << target(e, G) << endl;
template < class Edge, class Graph > void tree_edge(Edge e, Graph& G)
{
cout << "Tree edge: " << source(e, G) << " --> " << target(e, G)
<< endl;
Base::tree_edge(e, G);
}
template <class Edge, class Graph>
void back_edge(Edge e, Graph& G) {
cout << "Back edge: " << source(e, G)
<< " --> " << target(e, G) << endl;
template < class Edge, class Graph > void back_edge(Edge e, Graph& G)
{
cout << "Back edge: " << source(e, G) << " --> " << target(e, G)
<< endl;
Base::back_edge(e, G);
}
template < class Edge, class Graph >
void forward_or_cross_edge(Edge e, Graph& G) {
cout << "Forward or cross edge: " << source(e, G)
<< " --> " << target(e, G) << endl;
void forward_or_cross_edge(Edge e, Graph& G)
{
cout << "Forward or cross edge: " << source(e, G) << " --> "
<< target(e, G) << endl;
Base::forward_or_cross_edge(e, G);
}
template <class Edge, class Graph>
void finish_edge(Edge e, Graph& G) {
cout << "Finish edge: " << source(e, G) <<
" --> " << target(e, G) << endl;
template < class Edge, class Graph > void finish_edge(Edge e, Graph& G)
{
cout << "Finish edge: " << source(e, G) << " --> " << target(e, G)
<< endl;
Base::finish_edge(e, G);
}
};
template < class VisitorList >
edge_categorizer<VisitorList>
categorize_edges(const VisitorList& v) {
edge_categorizer< VisitorList > categorize_edges(const VisitorList& v)
{
return edge_categorizer< VisitorList >(v);
}
int
main(int , char* [])
int main(int, char*[])
{
using namespace boost;
@ -112,7 +111,8 @@ main(int , char* [])
std::vector< size_type > d(num_vertices(G));
std::vector< size_type > f(num_vertices(G));
int t = 0;
depth_first_search(G, visitor(categorize_edges(
depth_first_search(G,
visitor(categorize_edges(
make_pair(stamp_times(&d[0], t, on_discover_vertex()),
stamp_times(&f[0], t, on_finish_vertex())))));
@ -122,4 +122,3 @@ main(int , char* [])
return 0;
}

View File

@ -27,35 +27,32 @@
using namespace boost;
using namespace std;
struct open_paren : public base_visitor<open_paren> {
struct open_paren : public base_visitor< open_paren >
{
typedef on_discover_vertex event_filter;
template <class Vertex, class Graph>
void operator()(Vertex v, Graph&) {
template < class Vertex, class Graph > void operator()(Vertex v, Graph&)
{
std::cout << "(" << v;
}
};
struct close_paren : public base_visitor<close_paren> {
struct close_paren : public base_visitor< close_paren >
{
typedef on_finish_vertex event_filter;
template <class Vertex, class Graph>
void operator()(Vertex v, Graph&) {
template < class Vertex, class Graph > void operator()(Vertex v, Graph&)
{
std::cout << v << ")";
}
};
int
main(int, char*[])
int main(int, char*[])
{
using namespace boost;
typedef adjacency_list<> Graph;
typedef std::pair< int, int > E;
E edge_array[] = { E(0, 2),
E(1, 1), E(1, 3),
E(2, 1), E(2, 3),
E(3, 1), E(3, 4),
E(4, 0), E(4, 1) };
E edge_array[] = { E(0, 2), E(1, 1), E(1, 3), E(2, 1), E(2, 3), E(3, 1),
E(3, 4), E(4, 0), E(4, 1) };
#if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
Graph G(5);
for (std::size_t j = 0; j < sizeof(edge_array) / sizeof(E); ++j)
@ -65,9 +62,8 @@ main(int, char*[])
#endif
std::cout << "DFS parenthesis:" << std::endl;
depth_first_search(G, visitor(make_dfs_visitor(std::make_pair(open_paren(),
close_paren()))));
depth_first_search(G,
visitor(make_dfs_visitor(std::make_pair(open_paren(), close_paren()))));
std::cout << std::endl;
return 0;
}

View File

@ -15,54 +15,64 @@
using namespace boost;
int
main(int, char *[])
int main(int, char*[])
{
typedef adjacency_list_traits<listS, listS,
directedS>::vertex_descriptor vertex_descriptor;
typedef adjacency_list_traits< listS, listS, directedS >::vertex_descriptor
vertex_descriptor;
typedef adjacency_list< listS, listS, directedS,
property< vertex_index_t, int,
property< vertex_name_t, char,
property< vertex_distance_t, int,
property< vertex_predecessor_t, vertex_descriptor > > > >,
property<edge_weight_t, int> > graph_t;
property< edge_weight_t, int > >
graph_t;
typedef std::pair< int, int > Edge;
const int num_nodes = 5;
enum nodes { A, B, C, D, E };
Edge edge_array[] = { Edge(A, C), Edge(B, B), Edge(B, D), Edge(B, E),
Edge(C, B), Edge(C, D), Edge(D, E), Edge(E, A), Edge(E, B)
enum nodes
{
A,
B,
C,
D,
E
};
Edge edge_array[] = { Edge(A, C), Edge(B, B), Edge(B, D), Edge(B, E),
Edge(C, B), Edge(C, D), Edge(D, E), Edge(E, A), Edge(E, B) };
int weights[] = { 1, 2, 1, 2, 7, 3, 1, 1, 1 };
int num_arcs = sizeof(edge_array) / sizeof(Edge);
graph_traits< graph_t >::vertex_iterator i, iend;
graph_t g(edge_array, edge_array + num_arcs, weights, num_nodes);
property_map<graph_t, edge_weight_t>::type weightmap = get(edge_weight, g);
property_map< graph_t, edge_weight_t >::type weightmap
= get(edge_weight, g);
// Manually intialize the vertex index and name maps
property_map<graph_t, vertex_index_t>::type indexmap = get(vertex_index, g);
property_map< graph_t, vertex_index_t >::type indexmap
= get(vertex_index, g);
property_map< graph_t, vertex_name_t >::type name = get(vertex_name, g);
int c = 0;
for (boost::tie(i, iend) = vertices(g); i != iend; ++i, ++c) {
for (boost::tie(i, iend) = vertices(g); i != iend; ++i, ++c)
{
indexmap[*i] = c;
name[*i] = 'A' + c;
}
vertex_descriptor s = vertex(A, g);
property_map<graph_t, vertex_distance_t>::type
d = get(vertex_distance, g);
property_map<graph_t, vertex_predecessor_t>::type
p = get(vertex_predecessor, g);
property_map< graph_t, vertex_distance_t >::type d
= get(vertex_distance, g);
property_map< graph_t, vertex_predecessor_t >::type p
= get(vertex_predecessor, g);
dijkstra_shortest_paths(g, s, predecessor_map(p).distance_map(d));
std::cout << "distances and parents:" << std::endl;
graph_traits< graph_t >::vertex_iterator vi, vend;
for (boost::tie(vi, vend) = vertices(g); vi != vend; ++vi) {
for (boost::tie(vi, vend) = vertices(g); vi != vend; ++vi)
{
std::cout << "distance(" << name[*vi] << ") = " << d[*vi] << ", ";
std::cout << "parent(" << name[*vi] << ") = " << name[p[*vi]] << std::
endl;
std::cout << "parent(" << name[*vi] << ") = " << name[p[*vi]]
<< std::endl;
}
std::cout << std::endl;
@ -71,15 +81,17 @@ main(int, char *[])
<< " rankdir=LR\n"
<< " size=\"4,3\"\n"
<< " ratio=\"fill\"\n"
<< " edge[style=\"bold\"]\n" << " node[shape=\"circle\"]\n";
<< " edge[style=\"bold\"]\n"
<< " node[shape=\"circle\"]\n";
graph_traits< graph_t >::edge_iterator ei, ei_end;
for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) {
for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)
{
graph_traits< graph_t >::edge_descriptor e = *ei;
graph_traits < graph_t >::vertex_descriptor
u = source(e, g), v = target(e, g);
dot_file << name[u] << " -> " << name[v]
<< "[label=\"" << get(weightmap, e) << "\"";
graph_traits< graph_t >::vertex_descriptor u = source(e, g),
v = target(e, g);
dot_file << name[u] << " -> " << name[v] << "[label=\""
<< get(weightmap, e) << "\"";
if (p[v] == u)
dot_file << ", color=\"black\"";
else

View File

@ -16,38 +16,48 @@
using namespace boost;
int
main(int, char *[])
int main(int, char*[])
{
typedef adjacency_list < listS, vecS, directedS,
no_property, property < edge_weight_t, int > > graph_t;
typedef adjacency_list< listS, vecS, directedS, no_property,
property< edge_weight_t, int > >
graph_t;
typedef graph_traits< graph_t >::vertex_descriptor vertex_descriptor;
typedef std::pair< int, int > Edge;
const int num_nodes = 5;
enum nodes { A, B, C, D, E };
enum nodes
{
A,
B,
C,
D,
E
};
char name[] = "ABCDE";
Edge edge_array[] = { Edge(A, C), Edge(B, B), Edge(B, D), Edge(B, E),
Edge(C, B), Edge(C, D), Edge(D, E), Edge(E, A), Edge(E, B)
};
Edge(C, B), Edge(C, D), Edge(D, E), Edge(E, A), Edge(E, B) };
int weights[] = { 1, 2, 1, 2, 7, 3, 1, 1, 1 };
int num_arcs = sizeof(edge_array) / sizeof(Edge);
graph_t g(edge_array, edge_array + num_arcs, weights, num_nodes);
property_map<graph_t, edge_weight_t>::type weightmap = get(edge_weight, g);
property_map< graph_t, edge_weight_t >::type weightmap
= get(edge_weight, g);
std::vector< vertex_descriptor > p(num_vertices(g));
std::vector< int > d(num_vertices(g));
vertex_descriptor s = vertex(A, g);
dijkstra_shortest_paths(g, s,
predecessor_map(boost::make_iterator_property_map(p.begin(), get(boost::vertex_index, g))).
distance_map(boost::make_iterator_property_map(d.begin(), get(boost::vertex_index, g))));
predecessor_map(boost::make_iterator_property_map(
p.begin(), get(boost::vertex_index, g)))
.distance_map(boost::make_iterator_property_map(
d.begin(), get(boost::vertex_index, g))));
std::cout << "distances and parents:" << std::endl;
graph_traits< graph_t >::vertex_iterator vi, vend;
for (boost::tie(vi, vend) = vertices(g); vi != vend; ++vi) {
for (boost::tie(vi, vend) = vertices(g); vi != vend; ++vi)
{
std::cout << "distance(" << name[*vi] << ") = " << d[*vi] << ", ";
std::cout << "parent(" << name[*vi] << ") = " << name[p[*vi]] << std::
endl;
std::cout << "parent(" << name[*vi] << ") = " << name[p[*vi]]
<< std::endl;
}
std::cout << std::endl;
@ -57,15 +67,17 @@ main(int, char *[])
<< " rankdir=LR\n"
<< " size=\"4,3\"\n"
<< " ratio=\"fill\"\n"
<< " edge[style=\"bold\"]\n" << " node[shape=\"circle\"]\n";
<< " edge[style=\"bold\"]\n"
<< " node[shape=\"circle\"]\n";
graph_traits< graph_t >::edge_iterator ei, ei_end;
for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) {
for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)
{
graph_traits< graph_t >::edge_descriptor e = *ei;
graph_traits < graph_t >::vertex_descriptor
u = source(e, g), v = target(e, g);
dot_file << name[u] << " -> " << name[v]
<< "[label=\"" << get(weightmap, e) << "\"";
graph_traits< graph_t >::vertex_descriptor u = source(e, g),
v = target(e, g);
dot_file << name[u] << " -> " << name[v] << "[label=\""
<< get(weightmap, e) << "\"";
if (p[v] == u)
dot_file << ", color=\"black\"";
else

View File

@ -20,38 +20,48 @@
using namespace boost;
int
main(int, char *[])
int main(int, char*[])
{
typedef adjacency_list < listS, vecS, directedS,
no_property, property < edge_weight_t, int > > graph_t;
typedef adjacency_list< listS, vecS, directedS, no_property,
property< edge_weight_t, int > >
graph_t;
typedef graph_traits< graph_t >::vertex_descriptor vertex_descriptor;
typedef std::pair< int, int > Edge;
const int num_nodes = 5;
enum nodes { A, B, C, D, E };
enum nodes
{
A,
B,
C,
D,
E
};
char name[] = "ABCDE";
Edge edge_array[] = { Edge(A, C), Edge(B, B), Edge(B, D), Edge(B, E),
Edge(C, B), Edge(C, D), Edge(D, E), Edge(E, A), Edge(E, B)
};
Edge(C, B), Edge(C, D), Edge(D, E), Edge(E, A), Edge(E, B) };
int weights[] = { 1, 2, 1, 2, 7, 3, 1, 1, 1 };
int num_arcs = sizeof(edge_array) / sizeof(Edge);
graph_t g(edge_array, edge_array + num_arcs, weights, num_nodes);
property_map<graph_t, edge_weight_t>::type weightmap = get(edge_weight, g);
property_map< graph_t, edge_weight_t >::type weightmap
= get(edge_weight, g);
std::vector< vertex_descriptor > p(num_vertices(g));
std::vector< int > d(num_vertices(g));
vertex_descriptor s = vertex(A, g);
dijkstra_shortest_paths_no_color_map(g, s,
predecessor_map(boost::make_iterator_property_map(p.begin(), get(boost::vertex_index, g))).
distance_map(boost::make_iterator_property_map(d.begin(), get(boost::vertex_index, g))));
predecessor_map(boost::make_iterator_property_map(
p.begin(), get(boost::vertex_index, g)))
.distance_map(boost::make_iterator_property_map(
d.begin(), get(boost::vertex_index, g))));
std::cout << "distances and parents:" << std::endl;
graph_traits< graph_t >::vertex_iterator vi, vend;
for (boost::tie(vi, vend) = vertices(g); vi != vend; ++vi) {
for (boost::tie(vi, vend) = vertices(g); vi != vend; ++vi)
{
std::cout << "distance(" << name[*vi] << ") = " << d[*vi] << ", ";
std::cout << "parent(" << name[*vi] << ") = " << name[p[*vi]] << std::
endl;
std::cout << "parent(" << name[*vi] << ") = " << name[p[*vi]]
<< std::endl;
}
std::cout << std::endl;
@ -61,15 +71,17 @@ main(int, char *[])
<< " rankdir=LR\n"
<< " size=\"4,3\"\n"
<< " ratio=\"fill\"\n"
<< " edge[style=\"bold\"]\n" << " node[shape=\"circle\"]\n";
<< " edge[style=\"bold\"]\n"
<< " node[shape=\"circle\"]\n";
graph_traits< graph_t >::edge_iterator ei, ei_end;
for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) {
for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)
{
graph_traits< graph_t >::edge_descriptor e = *ei;
graph_traits < graph_t >::vertex_descriptor
u = source(e, g), v = target(e, g);
dot_file << name[u] << " -> " << name[v]
<< "[label=\"" << get(weightmap, e) << "\"";
graph_traits< graph_t >::vertex_descriptor u = source(e, g),
v = target(e, g);
dot_file << name[u] << " -> " << name[v] << "[label=\""
<< get(weightmap, e) << "\"";
if (p[v] == u)
dot_file << ", color=\"black\"";
else

View File

@ -11,9 +11,10 @@
int main(int, char*[])
{
// directed_graph is a subclass of adjacency_list which gives you object oriented access to functions
// like add_vertex and add_edge, which makes the code easier to understand. However, it hard codes many
// of the template parameters, so it is much less flexible.
// directed_graph is a subclass of adjacency_list which gives you object
// oriented access to functions like add_vertex and add_edge, which makes
// the code easier to understand. However, it hard codes many of the
// template parameters, so it is much less flexible.
typedef boost::directed_graph<> Graph;
Graph g;

View File

@ -4,7 +4,6 @@
// Boost Software License, Version 1.0 (See accompanying file
// LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
//[eccentricity_example
#include <iostream>
#include <iomanip>
@ -49,8 +48,7 @@ typedef boost::exterior_vertex_property<Graph, int> EccentricityProperty;
typedef EccentricityProperty::container_type EccentricityContainer;
typedef EccentricityProperty::map_type EccentricityMap;
int
main(int argc, char *argv[])
int main(int argc, char* argv[])
{
// Create the graph and a name map that provides access to
// then actor names.
@ -77,9 +75,10 @@ main(int argc, char *argv[])
// Print the closeness centrality of each vertex.
graph_traits< Graph >::vertex_iterator i, end;
for(boost::tie(i, end) = vertices(g); i != end; ++i) {
cout << setw(12) << setiosflags(ios::left)
<< g[*i].name << get(em, *i) << endl;
for (boost::tie(i, end) = vertices(g); i != end; ++i)
{
cout << setw(12) << setiosflags(ios::left) << g[*i].name << get(em, *i)
<< endl;
}
cout << "radius: " << r << endl;
cout << "diamter: " << d << endl;

View File

@ -9,8 +9,8 @@
/*
IMPORTANT!!!
~~~~~~~~~~~~
This example uses interfaces that have been deprecated and removed from Boost.Grpah.
Someone needs to update it, as it does NOT compile.
This example uses interfaces that have been deprecated and removed from
Boost.Grpah. Someone needs to update it, as it does NOT compile.
*/
#include <boost/config.hpp>
@ -43,17 +43,14 @@ namespace boost
template < typename Graph, typename OutputIterator >
void neighbors(const Graph& g,
typename graph_traits < Graph >::vertex_descriptor u,
OutputIterator result)
typename graph_traits< Graph >::vertex_descriptor u, OutputIterator result)
{
typename graph_traits< Graph >::adjacency_iterator ai, aend;
for (boost::tie(ai, aend) = adjacent_vertices(u, g); ai != aend; ++ai)
*result++ = *ai;
}
template < typename Graph, typename VertexIterator,
typename OutputIterator > void neighbors(const Graph & g,
VertexIterator first,
VertexIterator last,
template < typename Graph, typename VertexIterator, typename OutputIterator >
void neighbors(const Graph& g, VertexIterator first, VertexIterator last,
OutputIterator result)
{
for (; first != last; ++first)
@ -61,20 +58,22 @@ namespace boost
}
template < typename VertexListGraph, typename OutputIterator >
typename graph_traits < VertexListGraph >::degree_size_type
edge_connectivity(VertexListGraph & g, OutputIterator disconnecting_set)
typename graph_traits< VertexListGraph >::degree_size_type edge_connectivity(
VertexListGraph& g, OutputIterator disconnecting_set)
{
typedef typename graph_traits <
VertexListGraph >::vertex_descriptor vertex_descriptor;
typedef typename graph_traits <
VertexListGraph >::degree_size_type degree_size_type;
typedef typename graph_traits< VertexListGraph >::vertex_descriptor
vertex_descriptor;
typedef typename graph_traits< VertexListGraph >::degree_size_type
degree_size_type;
typedef color_traits< default_color_type > Color;
typedef typename adjacency_list_traits < vecS, vecS,
directedS >::edge_descriptor edge_descriptor;
typedef
typename adjacency_list_traits< vecS, vecS, directedS >::edge_descriptor
edge_descriptor;
typedef adjacency_list< vecS, vecS, directedS, no_property,
property< edge_capacity_t, degree_size_type,
property< edge_residual_capacity_t, degree_size_type,
property < edge_reverse_t, edge_descriptor > > > > FlowGraph;
property< edge_reverse_t, edge_descriptor > > > >
FlowGraph;
vertex_descriptor u, v, p, k;
edge_descriptor e1, e2;
@ -87,15 +86,16 @@ namespace boost
std::vector< edge_descriptor > pred(num_vertices(g));
FlowGraph flow_g(num_vertices(g));
typename property_map < FlowGraph, edge_capacity_t >::type
cap = get(edge_capacity, flow_g);
typename property_map < FlowGraph, edge_residual_capacity_t >::type
res_cap = get(edge_residual_capacity, flow_g);
typename property_map < FlowGraph, edge_reverse_t >::type
rev_edge = get(edge_reverse, flow_g);
typename property_map< FlowGraph, edge_capacity_t >::type cap
= get(edge_capacity, flow_g);
typename property_map< FlowGraph, edge_residual_capacity_t >::type res_cap
= get(edge_residual_capacity, flow_g);
typename property_map< FlowGraph, edge_reverse_t >::type rev_edge
= get(edge_reverse, flow_g);
typename graph_traits< VertexListGraph >::edge_iterator ei, ei_end;
for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) {
for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)
{
u = source(*ei, g), v = target(*ei, g);
boost::tie(e1, inserted) = add_edge(u, v, flow_g);
cap[e1] = 1;
@ -110,17 +110,19 @@ namespace boost
alpha_star = delta;
S.insert(p);
neighbor_S.insert(p);
neighbors(g, S.begin(), S.end(),
std::inserter(neighbor_S, neighbor_S.begin()));
neighbors(
g, S.begin(), S.end(), std::inserter(neighbor_S, neighbor_S.begin()));
std::set_difference(vertices(g).first, vertices(g).second,
neighbor_S.begin(), neighbor_S.end(),
std::back_inserter(nonneighbor_S));
while (!nonneighbor_S.empty()) {
while (!nonneighbor_S.empty())
{
k = nonneighbor_S.front();
alpha_S_k = edmonds_karp_max_flow
(flow_g, p, k, cap, res_cap, rev_edge, &color[0], &pred[0]);
if (alpha_S_k < alpha_star) {
alpha_S_k = edmonds_karp_max_flow(
flow_g, p, k, cap, res_cap, rev_edge, &color[0], &pred[0]);
if (alpha_S_k < alpha_star)
{
alpha_star = alpha_S_k;
S_star.clear();
for (boost::tie(vi, vi_end) = vertices(flow_g); vi != vi_end; ++vi)
@ -141,10 +143,12 @@ namespace boost
for (si = S_star.begin(); si != S_star.end(); ++si)
in_S_star[*si] = true;
degree_size_type c = 0;
for (si = S_star.begin(); si != S_star.end(); ++si) {
for (si = S_star.begin(); si != S_star.end(); ++si)
{
typename graph_traits< VertexListGraph >::out_edge_iterator ei, ei_end;
for (boost::tie(ei, ei_end) = out_edges(*si, g); ei != ei_end; ++ei)
if (!in_S_star[target(*ei, g)]) {
if (!in_S_star[target(*ei, g)])
{
*disconnecting_set++ = *ei;
++c;
}
@ -155,8 +159,7 @@ namespace boost
}
int
main()
int main()
{
using namespace boost;
GraphvizGraph g;
@ -165,20 +168,19 @@ main()
typedef graph_traits< GraphvizGraph >::edge_descriptor edge_descriptor;
typedef graph_traits< GraphvizGraph >::degree_size_type degree_size_type;
std::vector< edge_descriptor > disconnecting_set;
degree_size_type c =
edge_connectivity(g, std::back_inserter(disconnecting_set));
degree_size_type c
= edge_connectivity(g, std::back_inserter(disconnecting_set));
std::cout << "The edge connectivity is " << c << "." << std::endl;
property_map < GraphvizGraph, vertex_attribute_t >::type
attr_map = get(vertex_attribute, g);
property_map< GraphvizGraph, vertex_attribute_t >::type attr_map
= get(vertex_attribute, g);
std::cout << "The disconnecting set is {";
for (std::vector < edge_descriptor >::iterator i =
disconnecting_set.begin(); i != disconnecting_set.end(); ++i)
std::
cout << "(" << attr_map[source(*i, g)]["label"] << "," <<
attr_map[target(*i, g)]["label"] << ") ";
for (std::vector< edge_descriptor >::iterator i = disconnecting_set.begin();
i != disconnecting_set.end(); ++i)
std::cout << "(" << attr_map[source(*i, g)]["label"] << ","
<< attr_map[target(*i, g)]["label"] << ") ";
std::cout << "}." << std::endl;
return EXIT_SUCCESS;
}

View File

@ -13,9 +13,9 @@
using namespace boost;
template < typename Graph, typename VertexNamePropertyMap > void
read_graph_file(std::istream & graph_in, std::istream & name_in,
Graph & g, VertexNamePropertyMap name_map)
template < typename Graph, typename VertexNamePropertyMap >
void read_graph_file(std::istream& graph_in, std::istream& name_in, Graph& g,
VertexNamePropertyMap name_map)
{
typedef typename graph_traits< Graph >::vertices_size_type size_type;
size_type n_vertices;
@ -23,7 +23,8 @@ read_graph_file(std::istream & graph_in, std::istream & name_in,
typename property_traits< VertexNamePropertyMap >::value_type name;
graph_in >> n_vertices; // read in number of vertices
for (size_type i = 0; i < n_vertices; ++i) { // Add n vertices to the graph
for (size_type i = 0; i < n_vertices; ++i)
{ // Add n vertices to the graph
u = add_vertex(g);
name_in >> name;
put(name_map, u, name); // ** Attach name property to vertex u **
@ -36,10 +37,10 @@ read_graph_file(std::istream & graph_in, std::istream & name_in,
break;
}
template < typename Graph, typename VertexNameMap > void
output_adjacent_vertices(std::ostream & out,
typename graph_traits < Graph >::vertex_descriptor u,
const Graph & g, VertexNameMap name_map)
template < typename Graph, typename VertexNameMap >
void output_adjacent_vertices(std::ostream& out,
typename graph_traits< Graph >::vertex_descriptor u, const Graph& g,
VertexNameMap name_map)
{
typename graph_traits< Graph >::adjacency_iterator vi, vi_end;
out << get(name_map, u) << " -> { ";
@ -48,7 +49,8 @@ output_adjacent_vertices(std::ostream & out,
out << "}" << std::endl;
}
template < typename NameMap > class name_equals_t {
template < typename NameMap > class name_equals_t
{
public:
name_equals_t(const std::string& n, NameMap map)
: m_name(n), m_name_map(map)
@ -58,6 +60,7 @@ public:
{
return get(m_name_map, u) == m_name;
}
private:
std::string m_name;
NameMap m_name_map;
@ -65,41 +68,45 @@ private:
// object generator function
template < typename NameMap >
inline name_equals_t < NameMap >
name_equals(const std::string & str, NameMap name)
inline name_equals_t< NameMap > name_equals(
const std::string& str, NameMap name)
{
return name_equals_t< NameMap >(str, name);
}
int
main(int argc, const char** argv)
int main(int argc, const char** argv)
{
typedef adjacency_list<listS,// Store out-edges of each vertex in a std::list
typedef adjacency_list< listS, // Store out-edges of each vertex in a
// std::list
vecS, // Store vertex set in a std::vector
directedS, // The graph is directed
property< vertex_name_t, std::string > // Add a vertex property
>graph_type;
>
graph_type;
graph_type g; // use default constructor to create empty graph
const char* dep_file_name = argc >= 2 ? argv[1] : "makefile-dependencies.dat";
const char* target_file_name = argc >= 3 ? argv[2] : "makefile-target-names.dat";
const char* dep_file_name
= argc >= 2 ? argv[1] : "makefile-dependencies.dat";
const char* target_file_name
= argc >= 3 ? argv[2] : "makefile-target-names.dat";
std::ifstream file_in(dep_file_name), name_in(target_file_name);
if (!file_in) {
if (!file_in)
{
std::cerr << "** Error: could not open file " << dep_file_name
<< std::endl;
return -1;
}
if (!name_in) {
if (!name_in)
{
std::cerr << "** Error: could not open file " << target_file_name
<< std::endl;
return -1;
}
// Obtain internal property map from the graph
property_map < graph_type, vertex_name_t >::type name_map =
get(vertex_name, g);
property_map< graph_type, vertex_name_t >::type name_map
= get(vertex_name, g);
read_graph_file(file_in, name_in, g, name_map);
graph_traits< graph_type >::vertex_descriptor yow, zag, bar;

View File

@ -13,28 +13,27 @@
using namespace boost;
template < typename T >
std::istream & operator >> (std::istream & in, std::pair < T, T > &p) {
std::istream& operator>>(std::istream& in, std::pair< T, T >& p)
{
in >> p.first >> p.second;
return in;
}
int
main(int argc, const char** argv)
int main(int argc, const char** argv)
{
typedef adjacency_list <
listS, // Store out-edges of each vertex in a std::list
typedef adjacency_list< listS, // Store out-edges of each vertex in a
// std::list
vecS, // Store vertex set in a std::vector
directedS // The graph is directed
> graph_type;
>
graph_type;
std::ifstream file_in(argc >= 2 ? argv[1] : "makefile-dependencies.dat");
typedef graph_traits< graph_type >::vertices_size_type size_type;
size_type n_vertices;
file_in >> n_vertices; // read in number of vertices
graph_type
g(n_vertices); // create graph with n vertices
graph_type g(n_vertices); // create graph with n vertices
// Read in edges
graph_traits< graph_type >::vertices_size_type u, v;

View File

@ -15,7 +15,6 @@
using namespace std;
using namespace boost;
/*
Edge Basics
@ -37,10 +36,8 @@ using namespace boost;
*/
template <class Graph>
struct exercise_edge {
template < class Graph > struct exercise_edge
{
exercise_edge(Graph& g) : G(g) {}
typedef typename boost::graph_traits< Graph >::edge_descriptor Edge;
@ -63,9 +60,7 @@ struct exercise_edge {
Graph& G;
};
int
main()
int main()
{
typedef adjacency_list<> MyGraph;

View File

@ -36,7 +36,9 @@ int main(int, char *[])
{
using namespace boost;
using namespace std;
typedef adjacency_list<vecS, vecS, undirectedS, no_property, size_t, no_property> Graph;
typedef adjacency_list< vecS, vecS, undirectedS, no_property, size_t,
no_property >
Graph;
typedef std::pair< std::size_t, std::size_t > Pair;
Pair edges[14] = { Pair(0, 3), // a-d
@ -62,10 +64,12 @@ int main(int, char *[])
size_t colors = edge_coloring(G, get(edge_bundle, G));
cout << "Colored using " << colors << " colors" << endl;
for (size_t i = 0; i < sizeof(edges)/sizeof(edges[0]); i++) {
cout << " " << (char)('a' + edges[i].first) << "-" << (char)('a' + edges[i].second) << ": " << G[edge(edges[i].first, edges[i].second, G).first] << endl;
for (size_t i = 0; i < sizeof(edges) / sizeof(edges[0]); i++)
{
cout << " " << (char)('a' + edges[i].first) << "-"
<< (char)('a' + edges[i].second) << ": "
<< G[edge(edges[i].first, edges[i].second, G).first] << endl;
}
return 0;
}

View File

@ -7,12 +7,11 @@
// http://www.boost.org/LICENSE_1_0.txt)
//=======================================================================
/*
IMPORTANT!!!
~~~~~~~~~~~~
This example uses interfaces that have been deprecated and removed from Boost.Grpah.
Someone needs to update it, as it does NOT compile.
This example uses interfaces that have been deprecated and removed from
Boost.Grpah. Someone needs to update it, as it does NOT compile.
*/
#include <boost/config.hpp>
@ -25,8 +24,7 @@
using namespace boost;
int
main()
int main()
{
const int N = 8;
typedef adjacency_list< vecS, vecS, undirectedS > UndirectedGraph;
@ -51,7 +49,8 @@ main()
typedef graph_traits< UndirectedGraph >::degree_size_type degree_size_type;
std::vector< edge_descriptor > disconnecting_set;
degree_size_type c = edge_connectivity(g, std::back_inserter(disconnecting_set));
degree_size_type c
= edge_connectivity(g, std::back_inserter(disconnecting_set));
std::cout << "The edge connectivity is " << c << "." << std::endl;
std::cout << "The disconnecting set is {";

View File

@ -41,7 +41,8 @@
#include <boost/graph/graph_utility.hpp>
#include <boost/graph/adjacency_list.hpp>
class edge_stream_iterator {
class edge_stream_iterator
{
public:
typedef std::input_iterator_tag iterator_category;
typedef std::pair< int, int > value_type;
@ -52,46 +53,45 @@ public:
edge_stream_iterator() : m_stream(0), m_end_marker(false) {}
edge_stream_iterator(std::istream& s) : m_stream(&s) { m_read(); }
reference operator*() const { return m_edge; }
edge_stream_iterator& operator++() {
edge_stream_iterator& operator++()
{
m_read();
return *this;
}
edge_stream_iterator operator++(int) {
edge_stream_iterator operator++(int)
{
edge_stream_iterator tmp = *this;
m_read();
return tmp;
}
protected:
std::istream* m_stream;
value_type m_edge;
bool m_end_marker;
void m_read() {
void m_read()
{
m_end_marker = (*m_stream) ? true : false;
if (m_end_marker) {
if (m_end_marker)
{
*m_stream >> m_edge.first >> m_edge.second;
}
m_end_marker = (*m_stream) ? true : false;
}
friend bool operator==(const edge_stream_iterator& x,
const edge_stream_iterator& y);
friend bool operator==(
const edge_stream_iterator& x, const edge_stream_iterator& y);
};
bool operator==(const edge_stream_iterator& x,
const edge_stream_iterator& y)
bool operator==(const edge_stream_iterator& x, const edge_stream_iterator& y)
{
return (x.m_stream == y.m_stream && x.m_end_marker == y.m_end_marker)
|| (x.m_end_marker == false && y.m_end_marker == false);
}
bool operator!=(const edge_stream_iterator& x,
const edge_stream_iterator& y)
bool operator!=(const edge_stream_iterator& x, const edge_stream_iterator& y)
{
return !(x == y);
}
int
main(int argc, const char** argv)
int main(int argc, const char** argv)
{
typedef boost::adjacency_list<> IteratorConstructibleGraph;
typedef boost::graph_traits< IteratorConstructibleGraph > Traits;
@ -105,7 +105,8 @@ main(int argc, const char** argv)
#if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
// VC++ can't handle the iterator constructor
IteratorConstructibleGraph G(size_V);
while (edge_iter != end) {
while (edge_iter != end)
{
int i, j;
boost::tie(i, j) = *edge_iter++;
boost::add_edge(i, j, G);

View File

@ -55,36 +55,41 @@
#include <boost/property_map/property_map.hpp>
#include <boost/graph/adjacency_list.hpp>
using namespace boost;
using namespace std;
enum edge_myflow_t
{
edge_myflow
};
enum edge_mycapacity_t
{
edge_mycapacity
};
enum edge_myflow_t { edge_myflow };
enum edge_mycapacity_t { edge_mycapacity };
namespace boost {
namespace boost
{
BOOST_INSTALL_PROPERTY(edge, myflow);
BOOST_INSTALL_PROPERTY(edge, mycapacity);
}
template <class Graph>
void print_network(const Graph& G)
template < class Graph > void print_network(const Graph& G)
{
typedef typename boost::graph_traits< Graph >::vertex_iterator Viter;
typedef typename boost::graph_traits<Graph>::out_edge_iterator OutEdgeIter;
typedef
typename boost::graph_traits< Graph >::out_edge_iterator OutEdgeIter;
typedef typename boost::graph_traits< Graph >::in_edge_iterator InEdgeIter;
typename property_map<Graph, edge_mycapacity_t>::const_type
capacity = get(edge_mycapacity, G);
typename property_map<Graph, edge_myflow_t>::const_type
flow = get(edge_myflow, G);
typename property_map< Graph, edge_mycapacity_t >::const_type capacity
= get(edge_mycapacity, G);
typename property_map< Graph, edge_myflow_t >::const_type flow
= get(edge_myflow, G);
Viter ui, uiend;
boost::tie(ui, uiend) = vertices(G);
for (; ui != uiend; ++ui) {
for (; ui != uiend; ++ui)
{
OutEdgeIter out, out_end;
cout << *ui << "\t";
@ -104,13 +109,12 @@ void print_network(const Graph& G)
}
}
int main(int, char*[])
{
typedef property< edge_mycapacity_t, int > Cap;
typedef property< edge_myflow_t, int, Cap > Flow;
typedef adjacency_list<vecS, vecS, bidirectionalS,
no_property, Flow> Graph;
typedef adjacency_list< vecS, vecS, bidirectionalS, no_property, Flow >
Graph;
const int num_vertices = 9;
Graph G(num_vertices);
@ -143,8 +147,7 @@ int main(int , char* [])
print_network(G);
property_map<Graph, edge_myflow_t>::type
flow = get(edge_myflow, G);
property_map< Graph, edge_myflow_t >::type flow = get(edge_myflow, G);
boost::graph_traits< Graph >::vertex_iterator v, v_end;
boost::graph_traits< Graph >::out_edge_iterator e, e_end;
@ -158,6 +161,5 @@ int main(int , char* [])
print_network(G);
return 0;
}

View File

@ -47,8 +47,7 @@
// f 7 6 0
// f 7 5 0
int
main()
int main()
{
using namespace boost;
@ -57,15 +56,16 @@ main()
property< vertex_name_t, std::string >,
property< edge_capacity_t, long,
property< edge_residual_capacity_t, long,
property < edge_reverse_t, Traits::edge_descriptor > > > > Graph;
property< edge_reverse_t, Traits::edge_descriptor > > > >
Graph;
Graph g;
property_map < Graph, edge_capacity_t >::type
capacity = get(edge_capacity, g);
property_map< Graph, edge_capacity_t >::type capacity
= get(edge_capacity, g);
property_map< Graph, edge_reverse_t >::type rev = get(edge_reverse, g);
property_map < Graph, edge_residual_capacity_t >::type
residual_capacity = get(edge_residual_capacity, g);
property_map< Graph, edge_residual_capacity_t >::type residual_capacity
= get(edge_residual_capacity, g);
Traits::vertex_descriptor s, t;
read_dimacs_max_flow(g, capacity, rev, s, t);
@ -73,8 +73,8 @@ main()
#if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
std::vector< default_color_type > color(num_vertices(g));
std::vector< Traits::edge_descriptor > pred(num_vertices(g));
long flow = edmonds_karp_max_flow
(g, s, t, capacity, residual_capacity, rev, &color[0], &pred[0]);
long flow = edmonds_karp_max_flow(
g, s, t, capacity, residual_capacity, rev, &color[0], &pred[0]);
#else
long flow = edmonds_karp_max_flow(g, s, t);
#endif
@ -89,7 +89,8 @@ main()
for (boost::tie(ei, e_end) = out_edges(*u_iter, g); ei != e_end; ++ei)
if (capacity[*ei] > 0)
std::cout << "f " << *u_iter << " " << target(*ei, g) << " "
<< (capacity[*ei] - residual_capacity[*ei]) << std::endl;
<< (capacity[*ei] - residual_capacity[*ei])
<< std::endl;
return EXIT_SUCCESS;
}

View File

@ -32,7 +32,6 @@
// 8
// <--(10,8)-- 6
#include <boost/config.hpp>
#include <iostream>
#include <boost/graph/adjacency_list.hpp>
@ -42,33 +41,40 @@ template <class Graph, class Capacity, class Flow>
void print_network(Graph& G, Capacity capacity, Flow flow)
{
typedef typename boost::graph_traits< Graph >::vertex_iterator Viter;
typedef typename boost::graph_traits<Graph>::out_edge_iterator OutEdgeIter;
typedef
typename boost::graph_traits< Graph >::out_edge_iterator OutEdgeIter;
typedef typename boost::graph_traits< Graph >::in_edge_iterator InEdgeIter;
Viter ui, uiend;
for (boost::tie(ui, uiend) = boost::vertices(G); ui != uiend; ++ui) {
for (boost::tie(ui, uiend) = boost::vertices(G); ui != uiend; ++ui)
{
OutEdgeIter out, out_end;
std::cout << *ui << "\t";
for(boost::tie(out, out_end) = boost::out_edges(*ui, G); out != out_end; ++out)
for (boost::tie(out, out_end) = boost::out_edges(*ui, G);
out != out_end; ++out)
std::cout << "--(" << boost::get(capacity, *out) << ", "
<< boost::get(flow, *out) << ")--> " << boost::target(*out,G) << "\t";
<< boost::get(flow, *out) << ")--> "
<< boost::target(*out, G) << "\t";
std::cout << std::endl << "\t";
InEdgeIter in, in_end;
for(boost::tie(in, in_end) = boost::in_edges(*ui, G); in != in_end; ++in)
std::cout << "<--(" << boost::get(capacity, *in) << "," << boost::get(flow, *in) << ")-- "
for (boost::tie(in, in_end) = boost::in_edges(*ui, G); in != in_end;
++in)
std::cout << "<--(" << boost::get(capacity, *in) << ","
<< boost::get(flow, *in) << ")-- "
<< boost::source(*in, G) << "\t";
std::cout << std::endl;
}
}
int main(int , char* []) {
int main(int, char*[])
{
typedef boost::adjacency_list< boost::vecS, boost::vecS,
boost::bidirectionalS, boost::no_property,
boost::property<boost::edge_index_t, std::size_t> > Graph;
boost::property< boost::edge_index_t, std::size_t > >
Graph;
const int num_vertices = 9;
Graph G(num_vertices);
@ -104,10 +110,12 @@ int main(int , char* []) {
boost::add_edge(6, 8, 9, G);
typedef boost::property_map<Graph, boost::edge_index_t>::type EdgeIndexMap;
typedef boost::property_map< Graph, boost::edge_index_t >::type
EdgeIndexMap;
EdgeIndexMap edge_id = boost::get(boost::edge_index, G);
typedef boost::iterator_property_map<int*, EdgeIndexMap, int, int&> IterMap;
typedef boost::iterator_property_map< int*, EdgeIndexMap, int, int& >
IterMap;
print_network(G, IterMap(capacity, edge_id), IterMap(flow, edge_id));

View File

@ -54,19 +54,18 @@ using namespace boost;
*/
template < class EdgeIter, class Graph, class Name >
void who_owes_who(EdgeIter first, EdgeIter last, const Graph& G,
Name name)
void who_owes_who(EdgeIter first, EdgeIter last, const Graph& G, Name name)
{
while (first != last)
{
while (first != last) {
cout << name[source(*first,G)] << " owes "
<< name[target(*first,G)] << " some money" << endl;
cout << name[source(*first, G)] << " owes " << name[target(*first, G)]
<< " some money" << endl;
++first;
}
}
int
main(int, char*[])
int main(int, char*[])
{
/* The property will be "names" attached to the vertices */
@ -81,8 +80,8 @@ main(int, char*[])
typedef pair< int, int > Pair;
Pair edge_array[11] = { Pair(0, 1), Pair(0, 2), Pair(0, 3), Pair(0, 4),
Pair(2,0), Pair(3,0), Pair(2,4), Pair(3,1),
Pair(3,4), Pair(4,0), Pair(4,1) };
Pair(2, 0), Pair(3, 0), Pair(2, 4), Pair(3, 1), Pair(3, 4), Pair(4, 0),
Pair(4, 1) };
MyGraphType G(5);
for (int i = 0; i < 11; ++i)

View File

@ -12,14 +12,21 @@
#include <boost/graph/adjacency_list.hpp>
#include <boost/tuple/tuple.hpp>
enum family
{ Jeanie, Debbie, Rick, John, Amanda, Margaret, Benjamin, N };
int
main()
{
Jeanie,
Debbie,
Rick,
John,
Amanda,
Margaret,
Benjamin,
N
};
int main()
{
using namespace boost;
const char* name[] = { "Jeanie", "Debbie", "Rick", "John", "Amanda",
"Margaret", "Benjamin"
};
"Margaret", "Benjamin" };
adjacency_list<> g(N);
add_edge(Jeanie, Debbie, g);
@ -31,17 +38,19 @@ main()
graph_traits< adjacency_list<> >::vertex_iterator i, end;
graph_traits< adjacency_list<> >::adjacency_iterator ai, a_end;
property_map < adjacency_list <>, vertex_index_t >::type
index_map = get(vertex_index, g);
property_map< adjacency_list<>, vertex_index_t >::type index_map
= get(vertex_index, g);
for (boost::tie(i, end) = vertices(g); i != end; ++i) {
for (boost::tie(i, end) = vertices(g); i != end; ++i)
{
std::cout << name[get(index_map, *i)];
boost::tie(ai, a_end) = adjacent_vertices(*i, g);
if (ai == a_end)
std::cout << " has no children";
else
std::cout << " is the parent of ";
for (; ai != a_end; ++ai) {
for (; ai != a_end; ++ai)
{
std::cout << name[get(index_map, *ai)];
if (boost::next(ai) != a_end)
std::cout << ", ";

View File

@ -24,16 +24,18 @@ namespace random_ns = boost;
using namespace boost;
int
main()
int main()
{
typedef indirect_cmp< float*, std::less< float > > ICmp;
int i;
random_ns::mt19937 gen;
for (int N = 2; N < 200; ++N) {
for (int N = 2; N < 200; ++N)
{
uniform_int<> distrib(0, N - 1);
boost::variate_generator<random_ns::mt19937&, uniform_int<> > rand_gen(gen, distrib);
for (int t = 0; t < 10; ++t) {
boost::variate_generator< random_ns::mt19937&, uniform_int<> > rand_gen(
gen, distrib);
for (int t = 0; t < 10; ++t)
{
std::vector< float > v, w(N);
ICmp cmp(&w[0], std::less< float >());
@ -50,20 +52,24 @@ main()
for (i = 0; i < N; ++i)
Q.push(i);
for (i = 0; i < N; ++i) {
for (i = 0; i < N; ++i)
{
int u = rand_gen();
float r = rand_gen(); r /= 2.0;
float r = rand_gen();
r /= 2.0;
w[u] = w[u] - r;
Q.update(u);
}
for (i = 0; i < N; ++i) {
for (i = 0; i < N; ++i)
{
v.push_back(w[Q.top()]);
Q.pop();
}
std::sort(w.begin(), w.end());
if (! std::equal(v.begin(), v.end(), w.begin())) {
if (!std::equal(v.begin(), v.end(), w.begin()))
{
std::cout << std::endl << "heap sorted: ";
std::copy(v.begin(), v.end(),
std::ostream_iterator< float >(std::cout, " "));

View File

@ -44,67 +44,75 @@
using namespace std;
using namespace boost;
enum files_e { dax_h, yow_h, boz_h, zow_h, foo_cpp,
foo_o, bar_cpp, bar_o, libfoobar_a,
zig_cpp, zig_o, zag_cpp, zag_o,
libzigzag_a, killerapp, N };
const char* name[] = { "dax.h", "yow.h", "boz.h", "zow.h", "foo.cpp",
"foo.o", "bar.cpp", "bar.o", "libfoobar.a",
"zig.cpp", "zig.o", "zag.cpp", "zag.o",
enum files_e
{
dax_h,
yow_h,
boz_h,
zow_h,
foo_cpp,
foo_o,
bar_cpp,
bar_o,
libfoobar_a,
zig_cpp,
zig_o,
zag_cpp,
zag_o,
libzigzag_a,
killerapp,
N
};
const char* name[] = { "dax.h", "yow.h", "boz.h", "zow.h", "foo.cpp", "foo.o",
"bar.cpp", "bar.o", "libfoobar.a", "zig.cpp", "zig.o", "zag.cpp", "zag.o",
"libzigzag.a", "killerapp" };
struct print_visitor : public bfs_visitor<> {
struct print_visitor : public bfs_visitor<>
{
template < class Vertex, class Graph >
void discover_vertex(Vertex v, Graph&) {
void discover_vertex(Vertex v, Graph&)
{
cout << name[v] << " ";
}
};
struct cycle_detector : public dfs_visitor<>
{
cycle_detector(bool& has_cycle)
: m_has_cycle(has_cycle) { }
cycle_detector(bool& has_cycle) : m_has_cycle(has_cycle) {}
template < class Edge, class Graph > void back_edge(Edge, Graph&)
{
m_has_cycle = true;
}
template <class Edge, class Graph>
void back_edge(Edge, Graph&) { m_has_cycle = true; }
protected:
bool& m_has_cycle;
};
int main(int, char*[])
{
typedef pair< int, int > Edge;
Edge used_by[] = {
Edge(dax_h, foo_cpp), Edge(dax_h, bar_cpp), Edge(dax_h, yow_h),
Edge(yow_h, bar_cpp), Edge(yow_h, zag_cpp),
Edge used_by[] = { Edge(dax_h, foo_cpp), Edge(dax_h, bar_cpp),
Edge(dax_h, yow_h), Edge(yow_h, bar_cpp), Edge(yow_h, zag_cpp),
Edge(boz_h, bar_cpp), Edge(boz_h, zig_cpp), Edge(boz_h, zag_cpp),
Edge(zow_h, foo_cpp),
Edge(foo_cpp, foo_o),
Edge(foo_o, libfoobar_a),
Edge(bar_cpp, bar_o),
Edge(bar_o, libfoobar_a),
Edge(libfoobar_a, libzigzag_a),
Edge(zig_cpp, zig_o),
Edge(zig_o, libzigzag_a),
Edge(zag_cpp, zag_o),
Edge(zag_o, libzigzag_a),
Edge(libzigzag_a, killerapp)
};
Edge(zow_h, foo_cpp), Edge(foo_cpp, foo_o), Edge(foo_o, libfoobar_a),
Edge(bar_cpp, bar_o), Edge(bar_o, libfoobar_a),
Edge(libfoobar_a, libzigzag_a), Edge(zig_cpp, zig_o),
Edge(zig_o, libzigzag_a), Edge(zag_cpp, zag_o),
Edge(zag_o, libzigzag_a), Edge(libzigzag_a, killerapp) };
const std::size_t nedges = sizeof(used_by) / sizeof(Edge);
typedef adjacency_list< vecS, vecS, bidirectionalS > Graph;
#if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
// VC++ can't handle the iterator constructor
Graph g(N);
for (std::size_t j = 0; j < nedges; ++j) {
graph_traits<Graph>::edge_descriptor e; bool inserted;
boost::tie(e, inserted) = add_edge(used_by[j].first, used_by[j].second, g);
for (std::size_t j = 0; j < nedges; ++j)
{
graph_traits< Graph >::edge_descriptor e;
bool inserted;
boost::tie(e, inserted)
= add_edge(used_by[j].first, used_by[j].second, g);
}
#else
Graph g(used_by, used_by + nedges, N);
@ -120,21 +128,22 @@ int main(int,char*[])
topological_sort(g, std::front_inserter(make_order));
cout << "make ordering: ";
for (i = make_order.begin();
i != make_order.end(); ++i)
for (i = make_order.begin(); i != make_order.end(); ++i)
cout << name[*i] << " ";
cout << endl << endl;
// Parallel compilation ordering
std::vector< int > time(N, 0);
for (i = make_order.begin(); i != make_order.end(); ++i) {
for (i = make_order.begin(); i != make_order.end(); ++i)
{
// Walk through the in_edges an calculate the maximum time.
if (in_degree (*i, g) > 0) {
if (in_degree(*i, g) > 0)
{
Graph::in_edge_iterator j, j_end;
int maxdist = 0;
// Through the order from topological sort, we are sure that every
// time we are using here is already initialized.
// Through the order from topological sort, we are sure that
// every time we are using here is already initialized.
for (boost::tie(j, j_end) = in_edges(*i, g); j != j_end; ++j)
maxdist = (std::max)(time[source(*j, g)], maxdist);
time[*i] = maxdist + 1;
@ -142,13 +151,13 @@ int main(int,char*[])
}
cout << "parallel make ordering, " << endl
<< "vertices with same group number can be made in parallel" << endl;
<< "vertices with same group number can be made in parallel"
<< endl;
{
graph_traits< Graph >::vertex_iterator i, iend;
for (boost::tie(i, iend) = vertices(g); i != iend; ++i)
cout << "time_slot[" << name[*i] << "] = " << time[*i] << endl;
}
}
cout << endl;

View File

@ -12,39 +12,48 @@
#include <boost/graph/filtered_graph.hpp>
#include <boost/graph/graph_utility.hpp>
template <typename Graph>
struct non_zero_degree {
template < typename Graph > struct non_zero_degree
{
non_zero_degree() {} // has to have a default constructor!
non_zero_degree(const Graph& g) : g(&g) {}
bool operator()(typename boost::graph_traits<Graph>::vertex_descriptor v) const
bool operator()(
typename boost::graph_traits< Graph >::vertex_descriptor v) const
{
return degree(v, *g) != 0;
}
const Graph* g;
};
int
main()
int main()
{
using namespace boost;
typedef adjacency_list< vecS, vecS, bidirectionalS,
property < vertex_name_t, char > > graph_t;
property< vertex_name_t, char > >
graph_t;
enum { a, b, c, d, e, f, g, N };
enum
{
a,
b,
c,
d,
e,
f,
g,
N
};
graph_t G(N);
property_map < graph_t, vertex_name_t >::type
name_map = get(vertex_name, G);
property_map< graph_t, vertex_name_t >::type name_map = get(vertex_name, G);
char name = 'a';
graph_traits< graph_t >::vertex_iterator v, v_end;
for (boost::tie(v, v_end) = vertices(G); v != v_end; ++v, ++name)
name_map[*v] = name;
typedef std::pair< int, int > E;
E edges[] = { E(a, c), E(a, d), E(b, a), E(b, d), E(c, f),
E(d, c), E(d, e), E(d, f), E(e, b), E(e, g), E(f, e), E(f, g)
};
E edges[] = { E(a, c), E(a, d), E(b, a), E(b, d), E(c, f), E(d, c), E(d, e),
E(d, f), E(e, b), E(e, g), E(f, e), E(f, g) };
for (int i = 0; i < 12; ++i)
add_edge(edges[i].first, edges[i].second, G);
@ -55,7 +64,9 @@ main()
clear_vertex(d, G);
graph_t G_copy;
copy_graph(make_filtered_graph(G, keep_all(), non_zero_degree<graph_t>(G)), G_copy);
copy_graph(
make_filtered_graph(G, keep_all(), non_zero_degree< graph_t >(G)),
G_copy);
print_graph(G_copy, get(vertex_name, G_copy));

View File

@ -25,27 +25,35 @@
#include <boost/graph/filtered_graph.hpp>
#include <boost/graph/graph_utility.hpp>
template <typename EdgeWeightMap>
struct positive_edge_weight {
template < typename EdgeWeightMap > struct positive_edge_weight
{
positive_edge_weight() {}
positive_edge_weight(EdgeWeightMap weight) : m_weight(weight) {}
template <typename Edge>
bool operator()(const Edge& e) const {
template < typename Edge > bool operator()(const Edge& e) const
{
return 0 < boost::get(m_weight, e);
}
EdgeWeightMap m_weight;
};
int main()
{
using namespace boost;
typedef adjacency_list<vecS, vecS, directedS,
no_property, property<edge_weight_t, int> > Graph;
typedef adjacency_list< vecS, vecS, directedS, no_property,
property< edge_weight_t, int > >
Graph;
typedef property_map< Graph, edge_weight_t >::type EdgeWeightMap;
enum { A, B, C, D, E, N };
enum
{
A,
B,
C,
D,
E,
N
};
const char* name = "ABCDE";
Graph g(N);
add_edge(A, B, 2, g);
@ -56,8 +64,8 @@ int main()
add_edge(E, C, 0, g);
positive_edge_weight< EdgeWeightMap > filter(get(edge_weight, g));
filtered_graph<Graph, positive_edge_weight<EdgeWeightMap> >
fg(g, filter);
filtered_graph< Graph, positive_edge_weight< EdgeWeightMap > > fg(
g, filter);
std::cout << "filtered edge set: ";
print_edges(fg, name);

View File

@ -25,27 +25,35 @@
#include <boost/graph/filtered_graph.hpp>
#include <boost/graph/graph_utility.hpp>
template <typename EdgeWeightMap>
struct positive_edge_weight {
template < typename EdgeWeightMap > struct positive_edge_weight
{
positive_edge_weight() {}
positive_edge_weight(EdgeWeightMap weight) : m_weight(weight) {}
template <typename Edge>
bool operator()(const Edge& e) const {
template < typename Edge > bool operator()(const Edge& e) const
{
return 0 < boost::get(m_weight, e);
}
EdgeWeightMap m_weight;
};
int main()
{
using namespace boost;
typedef adjacency_list<multisetS, vecS, directedS,
no_property, property<edge_weight_t, int> > Graph;
typedef adjacency_list< multisetS, vecS, directedS, no_property,
property< edge_weight_t, int > >
Graph;
typedef property_map< Graph, edge_weight_t >::type EdgeWeightMap;
enum { A, B, C, D, E, N };
enum
{
A,
B,
C,
D,
E,
N
};
const char* name = "ABCDE";
Graph g(N);
add_edge(A, B, 2, g);
@ -62,11 +70,12 @@ int main()
std::cout << "unfiltered edge_range(C,D)\n";
graph_traits< Graph >::out_edge_iterator f, l;
for (boost::tie(f, l) = edge_range(C, D, g); f != l; ++f)
std::cout << name[source(*f, g)] << " --" << weight[*f]
<< "-> " << name[target(*f, g)] << "\n";
std::cout << name[source(*f, g)] << " --" << weight[*f] << "-> "
<< name[target(*f, g)] << "\n";
positive_edge_weight< EdgeWeightMap > filter(weight);
typedef filtered_graph<Graph, positive_edge_weight<EdgeWeightMap> > FGraph;
typedef filtered_graph< Graph, positive_edge_weight< EdgeWeightMap > >
FGraph;
FGraph fg(g, filter);
std::cout << "filtered edge_range(C,D)\n";

View File

@ -24,21 +24,30 @@
#include <boost/graph/filtered_graph.hpp>
#include <boost/graph/graph_utility.hpp>
struct constant_target {
struct constant_target
{
constant_target() {}
constant_target(int t) : target(t) {}
bool operator()(const std::pair<int,int>& e) const {
bool operator()(const std::pair< int, int >& e) const
{
return e.second == target;
}
int target;
};
int main()
{
using namespace boost;
enum { A, B, C, D, E, N };
enum
{
A,
B,
C,
D,
E,
N
};
const char* name = "ABCDE";
typedef std::vector< std::list< int > > Graph;
Graph g(N);

View File

@ -31,17 +31,22 @@ void usage()
<< "\t--iterations n\tNumber of iterations to execute.\n"
<< "\t\t\tThe default value is 100.\n"
<< "Input:\n"
<< " Input is read from standard input as a list of edges, one per line.\n"
<< " Each edge contains two string labels (the endpoints) separated by a space.\n\n"
<< " Input is read from standard input as a list of edges, one "
"per line.\n"
<< " Each edge contains two string labels (the endpoints) "
"separated by a space.\n\n"
<< "Output:\n"
<< " Vertices and their positions are written to standard output with the label,\n x-position, and y-position of a vertex on each line, separated by spaces.\n";
<< " Vertices and their positions are written to standard "
"output with the label,\n x-position, and y-position of a "
"vertex on each line, separated by spaces.\n";
}
typedef boost::rectangle_topology<> topology_type;
typedef topology_type::point_type point_type;
typedef adjacency_list< listS, vecS, undirectedS,
property<vertex_name_t, std::string> > Graph;
property< vertex_name_t, std::string > >
Graph;
typedef graph_traits< Graph >::vertex_descriptor Vertex;
@ -79,28 +84,44 @@ int main(int argc, char* argv[])
{
int iterations = 100;
if (argc < 3) { usage(); return -1; }
if (argc < 3)
{
usage();
return -1;
}
double width = 0;
double height = 0;
for (int arg_idx = 1; arg_idx < argc; ++arg_idx) {
for (int arg_idx = 1; arg_idx < argc; ++arg_idx)
{
std::string arg = argv[arg_idx];
if (arg == "--iterations") {
if (arg == "--iterations")
{
++arg_idx;
if (arg_idx >= argc) { usage(); return -1; }
if (arg_idx >= argc)
{
usage();
return -1;
}
iterations = lexical_cast< int >(argv[arg_idx]);
} else {
if (width == 0.0) width = lexical_cast<double>(arg);
else if (height == 0.0) height = lexical_cast<double>(arg);
else {
}
else
{
if (width == 0.0)
width = lexical_cast< double >(arg);
else if (height == 0.0)
height = lexical_cast< double >(arg);
else
{
usage();
return -1;
}
}
}
if (width == 0.0 || height == 0.0) {
if (width == 0.0 || height == 0.0)
{
usage();
return -1;
}
@ -109,7 +130,8 @@ int main(int argc, char* argv[])
NameToVertex names;
std::string source, target;
while (std::cin >> source >> target) {
while (std::cin >> source >> target)
{
add_edge(get_vertex(source, g, names), get_vertex(target, g, names), g);
}
@ -123,14 +145,14 @@ int main(int argc, char* argv[])
minstd_rand gen;
topology_type topo(gen, -width / 2, -height / 2, width / 2, height / 2);
random_graph_layout(g, position, topo);
fruchterman_reingold_force_directed_layout
(g, position, topo,
cooling(progress_cooling(iterations)));
fruchterman_reingold_force_directed_layout(
g, position, topo, cooling(progress_cooling(iterations)));
graph_traits< Graph >::vertex_iterator vi, vi_end;
for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) {
std::cout << get(vertex_name, g, *vi) << '\t'
<< position[*vi][0] << '\t' << position[*vi][1] << std::endl;
for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
{
std::cout << get(vertex_name, g, *vi) << '\t' << position[*vi][0]
<< '\t' << position[*vi][1] << std::endl;
}
return 0;
}

View File

@ -38,21 +38,23 @@
// incoming/outgoing edges for v become incoming/outgoing edges for u
// v is deleted
template < class Graph, class GetEdgeProperties >
void merge_vertex
(typename boost::graph_traits<Graph>::vertex_descriptor u,
typename boost::graph_traits<Graph>::vertex_descriptor v,
Graph& g, GetEdgeProperties getp)
void merge_vertex(typename boost::graph_traits< Graph >::vertex_descriptor u,
typename boost::graph_traits< Graph >::vertex_descriptor v, Graph& g,
GetEdgeProperties getp)
{
typedef boost::graph_traits< Graph > Traits;
typename Traits::edge_descriptor e;
typename Traits::out_edge_iterator out_i, out_end;
for (boost::tie(out_i, out_end) = out_edges(v, g); out_i != out_end; ++out_i) {
for (boost::tie(out_i, out_end) = out_edges(v, g); out_i != out_end;
++out_i)
{
e = *out_i;
typename Traits::vertex_descriptor targ = target(e, g);
add_edge(u, targ, getp(e), g);
}
typename Traits::in_edge_iterator in_i, in_end;
for (boost::tie(in_i, in_end) = in_edges(v, g); in_i != in_end; ++in_i) {
for (boost::tie(in_i, in_end) = in_edges(v, g); in_i != in_end; ++in_i)
{
e = *in_i;
typename Traits::vertex_descriptor src = source(e, g);
add_edge(src, u, getp(e), g);
@ -61,47 +63,52 @@ void merge_vertex
remove_vertex(v, g);
}
template <class StoredEdge>
struct order_by_name
template < class StoredEdge > struct order_by_name
{
typedef StoredEdge first_argument_type;
typedef StoredEdge second_argument_type;
typedef bool result_type;
bool operator()(const StoredEdge& e1, const StoredEdge& e2) const {
bool operator()(const StoredEdge& e1, const StoredEdge& e2) const
{
// Using std::pair operator< as an easy way to get lexicographical
// compare over tuples.
return std::make_pair(e1.get_target(), boost::get(boost::edge_name, e1))
< std::make_pair(e2.get_target(), boost::get(boost::edge_name, e2));
}
};
struct ordered_set_by_nameS { };
struct ordered_set_by_nameS
{
};
#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
namespace boost {
namespace boost
{
template < class ValueType >
struct container_gen<ordered_set_by_nameS, ValueType> {
struct container_gen< ordered_set_by_nameS, ValueType >
{
typedef std::set< ValueType, order_by_name< ValueType > > type;
};
template <>
struct parallel_edge_traits<ordered_set_by_nameS> {
template <> struct parallel_edge_traits< ordered_set_by_nameS >
{
typedef allow_parallel_edge_tag type;
};
}
#endif
template <class Graph>
struct get_edge_name {
template < class Graph > struct get_edge_name
{
get_edge_name(const Graph& g_) : g(g_) {}
template < class Edge >
boost::property<boost::edge_name_t, char> operator()(Edge e) const {
return boost::property<boost::edge_name_t, char>(boost::get(boost::edge_name, g, e));
boost::property< boost::edge_name_t, char > operator()(Edge e) const
{
return boost::property< boost::edge_name_t, char >(
boost::get(boost::edge_name, g, e));
}
const Graph& g;
};
int
main()
int main()
{
#ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
std::cout << "This program requires partial specialization." << std::endl;
@ -109,7 +116,8 @@ main()
using namespace boost;
typedef property< edge_name_t, char > EdgeProperty;
typedef adjacency_list< ordered_set_by_nameS, vecS, bidirectionalS,
no_property, EdgeProperty> graph_type;
no_property, EdgeProperty >
graph_type;
graph_type g;
@ -129,10 +137,12 @@ main()
graph_traits< graph_type >::vertex_iterator i, end;
graph_traits< graph_type >::out_edge_iterator ei, edge_end;
for (boost::tie(i, end) = vertices(g); i != end; ++i) {
for (boost::tie(i, end) = vertices(g); i != end; ++i)
{
std::cout << id[*i] << " ";
for (boost::tie(ei, edge_end) = out_edges(*i, g); ei != edge_end; ++ei)
std::cout << " --" << name[*ei] << "--> " << id[target(*ei, g)] << " ";
std::cout << " --" << name[*ei] << "--> " << id[target(*ei, g)]
<< " ";
std::cout << std::endl;
}
std::cout << std::endl;
@ -140,10 +150,12 @@ main()
std::cout << "merging vertex 1 into vertex 0" << std::endl << std::endl;
merge_vertex(0, 1, g, get_edge_name< graph_type >(g));
for (boost::tie(i, end) = vertices(g); i != end; ++i) {
for (boost::tie(i, end) = vertices(g); i != end; ++i)
{
std::cout << id[*i] << " ";
for (boost::tie(ei, edge_end) = out_edges(*i, g); ei != edge_end; ++ei)
std::cout << " --" << name[*ei] << "--> " << id[target(*ei, g)] << " ";
std::cout << " --" << name[*ei] << "--> " << id[target(*ei, g)]
<< " ";
std::cout << std::endl;
}
std::cout << std::endl;

View File

@ -69,46 +69,48 @@ class diameter_and_girth_visitor : public boost::bfs_visitor<>
{
public:
diameter_and_girth_visitor(std::size_t& k_, std::size_t& girth_)
: k(k_), girth(girth_) { }
: k(k_), girth(girth_)
{
}
void tree_edge(edge_descriptor e, Graph* g) {
void tree_edge(edge_descriptor e, Graph* g)
{
vertex_descriptor u = source(e, g), v = target(e, g);
k = d_map[u] + 1;
d_map[v] = k;
++distance_list[k];
p_map[v] = u;
}
void non_tree_edge(edge_descriptor e, Graph* g) {
void non_tree_edge(edge_descriptor e, Graph* g)
{
vertex_descriptor u = source(e, g), v = target(e, g);
k = d_map[u] + 1;
if (d_map[v] + k < girth && v != p_map[u])
girth = d_map[v] + k;
}
private:
std::size_t& k;
std::size_t& girth;
};
int
main()
int main()
{
std::cout <<
"This program explores the girth and diameter of Ramanujan graphs."
std::cout
<< "This program explores the girth and diameter of Ramanujan graphs."
<< std::endl;
std::cout <<
"The bipartite graphs have q^3-q vertices, and the non-bipartite"
std::cout
<< "The bipartite graphs have q^3-q vertices, and the non-bipartite"
<< std::endl;
std::cout <<
"graphs have half that number. Each vertex has degree p+1."
std::cout << "graphs have half that number. Each vertex has degree p+1."
<< std::endl;
std::cout << "Both p and q should be odd prime numbers;" << std::endl;
std::cout << " or you can try p = 2 with q = 17 or 43." << std::endl;
while (1) {
while (1)
{
std::cout << std::endl
<< "Choose a branching factor, p: ";
std::cout << std::endl << "Choose a branching factor, p: ";
long p = 0, q = 0;
std::cin >> p;
if (p == 0)
@ -120,7 +122,8 @@ main()
Graph* g;
g = raman(p, q, 0L, 0L);
if (g == 0) {
if (g == 0)
{
std::cerr << " Sorry, I couldn't make that graph (error code "
<< panic_code << ")" << std::endl;
continue;
@ -151,9 +154,8 @@ main()
std::cout << distance_list[d] << " vertices at distance " << d
<< (distance_list[d + 1] != 0 ? "," : ".") << std::endl;
std::cout << "So the diameter is " << k - 1
<< ", and the girth is " << girth
<< "." << std::endl;
std::cout << "So the diameter is " << k - 1 << ", and the girth is "
<< girth << "." << std::endl;
} // end while
return 0;

View File

@ -10,8 +10,7 @@
using namespace boost;
template < typename Graph > void
generic_foo(Graph & g)
template < typename Graph > void generic_foo(Graph& g)
{
// Access descriptor types
typedef typename graph_traits< Graph >::vertex_descriptor Vertex;
@ -24,11 +23,11 @@ generic_foo(Graph & g)
// Now do something useful...
}
template < typename Graph > void
generic_bar(Graph & g)
template < typename Graph > void generic_bar(Graph& g)
{
// Declare some vertex and edge descriptor variables
typename graph_traits < Graph >::vertex_descriptor u = vertex(0,g), v = vertex(1,g);
typename graph_traits< Graph >::vertex_descriptor u = vertex(0, g),
v = vertex(1, g);
typename graph_traits< Graph >::edge_descriptor e1, e2;
// Set u and e1 to valid descriptors...
v = u; // Make v a handle to the same vertex as u.
@ -40,28 +39,25 @@ generic_bar(Graph & g)
}
// This version of foo gets called when g is directed
template < typename Graph > void
foo_dispatch(Graph & g, boost::directed_tag)
template < typename Graph > void foo_dispatch(Graph& g, boost::directed_tag)
{
//...
}
// This version of foo gets called when g is undirected
template < typename Graph > void
foo_dispatch(Graph & g, boost::undirected_tag)
template < typename Graph > void foo_dispatch(Graph& g, boost::undirected_tag)
{
//...
}
template < typename Graph > void
foo(Graph & g)
template < typename Graph > void foo(Graph& g)
{
typedef typename boost::graph_traits< Graph >::directed_category Cat;
foo_dispatch(g, Cat());
}
template < typename Digraph > void
foo(Digraph & digraph,
template < typename Digraph >
void foo(Digraph& digraph,
typename graph_traits< Digraph >::vertex_descriptor u,
typename graph_traits< Digraph >::vertex_descriptor v)
{
@ -71,8 +67,8 @@ foo(Digraph & digraph,
e2 = edge(v, u, digraph);
assert(e1.first != e2.first);
}
template < typename Undigraph > void
bar(Undigraph & undigraph,
template < typename Undigraph >
void bar(Undigraph& undigraph,
typename graph_traits< Undigraph >::vertex_descriptor u,
typename graph_traits< Undigraph >::vertex_descriptor v)
{
@ -83,9 +79,7 @@ bar(Undigraph & undigraph,
assert(e1.first == e2.first);
}
int
main()
int main()
{
boost::adjacency_list< vecS, vecS, directedS > g(2);

View File

@ -12,12 +12,12 @@
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/property_iter_range.hpp>
int
main()
int main()
{
using namespace boost;
typedef adjacency_list< listS, vecS, directedS,
property < vertex_name_t, std::string > >graph_t;
property< vertex_name_t, std::string > >
graph_t;
graph_t g(3);
const char* vertex_names[] = { "Kubrick", "Clark", "Hal" };

View File

@ -9,8 +9,6 @@
// Author: Ronald Garcia
#include <boost/graph/graphviz.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/foreach.hpp>
@ -20,61 +18,64 @@
using namespace boost;
using namespace std;
//
// Create a custom graph properties
// (see the documentation for adjacency list)
struct graph_identifier_t { typedef graph_property_tag kind; };
struct vertex_label_t { typedef vertex_property_tag kind; };
struct graph_identifier_t
{
typedef graph_property_tag kind;
};
struct vertex_label_t
{
typedef vertex_property_tag kind;
};
int main() {
int main()
{
// Vertex properties
typedef property< vertex_name_t, string,
property < vertex_label_t, string,
property < vertex_root_t, int > > > vertex_p;
property< vertex_label_t, string, property< vertex_root_t, int > > >
vertex_p;
// Edge properties
typedef property< edge_name_t, string > edge_p;
// Graph properties
typedef property< graph_name_t, string,
property < graph_identifier_t, string > > graph_p;
property< graph_identifier_t, string > >
graph_p;
// adjacency_list-based type
typedef adjacency_list < vecS, vecS, directedS,
vertex_p, edge_p, graph_p > graph_t;
typedef adjacency_list< vecS, vecS, directedS, vertex_p, edge_p, graph_p >
graph_t;
// Construct an empty graph and prepare the dynamic_property_maps.
graph_t graph(0);
dynamic_properties dp;
property_map<graph_t, vertex_name_t>::type vname =
get(vertex_name, graph);
property_map< graph_t, vertex_name_t >::type vname
= get(vertex_name, graph);
dp.property("node_id", vname);
property_map<graph_t, vertex_label_t>::type vlabel =
get(vertex_label_t(), graph);
property_map< graph_t, vertex_label_t >::type vlabel
= get(vertex_label_t(), graph);
dp.property("label", vlabel);
property_map<graph_t, vertex_root_t>::type root =
get(vertex_root, graph);
property_map< graph_t, vertex_root_t >::type root = get(vertex_root, graph);
dp.property("root", root);
property_map<graph_t, edge_name_t>::type elabel =
get(edge_name, graph);
property_map< graph_t, edge_name_t >::type elabel = get(edge_name, graph);
dp.property("label", elabel);
// Use ref_property_map to turn a graph property into a property map
ref_property_map<graph_t*,string>
gname(get_property(graph,graph_name));
ref_property_map< graph_t*, string > gname(get_property(graph, graph_name));
dp.property("name", gname);
// Use ref_property_map to turn a graph property into a property map
ref_property_map<graph_t*,string>
gid(get_property(graph,graph_identifier_t()));
ref_property_map< graph_t*, string > gid(
get_property(graph, graph_identifier_t()));
dp.property("identifier", gid);
// Sample graph as an istream;
const char* dot =
"digraph \
const char* dot = "digraph \
{ \
graph [name=\"GRAPH\", identifier=\"CX2A1Z\"] \
\
@ -85,21 +86,22 @@ const char* dot =
b -> c [label=\"EDGE_2\"] \
}";
istringstream gvgraph(dot);
bool status = read_graphviz(gvgraph, graph, dp, "node_id");
if (!status) {
if (!status)
{
cerr << "read_graphviz() failed." << endl;
return -1;
}
cout << "graph " << get("name",dp,&graph) <<
" (" << get("identifier",dp,&graph) << ")\n\n";
cout << "graph " << get("name", dp, &graph) << " ("
<< get("identifier", dp, &graph) << ")\n\n";
BOOST_FOREACH( graph_t::vertex_descriptor v, vertices(graph) ) {
cout << "vertex " << get("node_id",dp,v) <<
" (" << get("label",dp,v) << ")\n";
BOOST_FOREACH (graph_t::vertex_descriptor v, vertices(graph))
{
cout << "vertex " << get("node_id", dp, v) << " ("
<< get("label", dp, v) << ")\n";
}
return 0;

View File

@ -21,16 +21,19 @@ typedef property<vertex_color_t, default_color_type,
property< vertex_distance_t, int,
property< vertex_degree_t, int,
property< vertex_in_degree_t, int,
property<vertex_out_degree_t,int> > > > > VertexProperty;
property< vertex_out_degree_t, int > > > > >
VertexProperty;
typedef property< edge_weight_t, int > EdgeProperty;
typedef adjacency_list<vecS, vecS, bidirectionalS,
VertexProperty, EdgeProperty> Graph;
typedef adjacency_list< vecS, vecS, bidirectionalS, VertexProperty,
EdgeProperty >
Graph;
template <class Graph>
void print(Graph& g) {
template < class Graph > void print(Graph& g)
{
typename Graph::vertex_iterator i, end;
typename Graph::out_edge_iterator ei, edge_end;
for(boost::tie(i,end) = vertices(g); i != end; ++i) {
for (boost::tie(i, end) = vertices(g); i != end; ++i)
{
cout << *i << " --> ";
for (boost::tie(ei, edge_end) = out_edges(*i, g); ei != edge_end; ++ei)
cout << target(*ei, g) << " ";
@ -38,14 +41,15 @@ void print(Graph& g) {
}
}
std::size_t myrand(std::size_t N) {
std::size_t myrand(std::size_t N)
{
std::size_t ret = rand() % N;
// cout << "N = " << N << " rand = " << ret << endl;
return ret;
}
template <class Graph>
bool check_edge(Graph& g, std::size_t a, std::size_t b) {
template < class Graph > bool check_edge(Graph& g, std::size_t a, std::size_t b)
{
typename Graph::adjacency_iterator vi, viend, found;
boost::tie(vi, viend) = adjacent_vertices(vertex(a, g), g);
@ -65,9 +69,11 @@ int main(int, char*[])
bool is_failed = false;
for (i=0; i<6; ++i) {
for (i = 0; i < 6; ++i)
{
std::size_t a = myrand(N), b = myrand(N);
while ( a == b ) b = myrand(N);
while (a == b)
b = myrand(N);
cout << "edge edge (" << a << "," << b << ")" << endl;
// add edges
add_edge(a, b, g);
@ -82,9 +88,11 @@ int main(int, char*[])
print(g);
// remove_edge
for (i = 0; i<2; ++i) {
for (i = 0; i < 2; ++i)
{
std::size_t a = myrand(N), b = myrand(N);
while ( a == b ) b = myrand(N);
while (a == b)
b = myrand(N);
cout << "remove edge (" << a << "," << b << ")" << endl;
remove_edge(a, b, g);
is_failed = is_failed || check_edge(g, a, b);
@ -109,10 +117,13 @@ int main(int, char*[])
cerr << " Passed." << endl;
is_failed = false;
for (i=0; i<2; ++i) {
for (i = 0; i < 2; ++i)
{
std::size_t a = myrand(N), b = myrand(N);
while ( a == vid ) a = myrand(N);
while ( b == vidp1 ) b = myrand(N);
while (a == vid)
a = myrand(N);
while (b == vidp1)
b = myrand(N);
cout << "add edge (" << vid << "," << a << ")" << endl;
cout << "add edge (" << vid << "," << vidp1 << ")" << endl;
add_edge(vid, a, g);

View File

@ -12,36 +12,34 @@
#include <boost/cstdlib.hpp>
#include <iostream>
class tree_printer {
class tree_printer
{
public:
template <typename Node, typename Tree>
void preorder(Node, Tree&) {
template < typename Node, typename Tree > void preorder(Node, Tree&)
{
std::cout << "(";
}
template <typename Node, typename Tree>
void inorder(Node n, Tree& t)
template < typename Node, typename Tree > void inorder(Node n, Tree& t)
{
std::cout << get(boost::vertex_name, t)[n];
}
template <typename Node, typename Tree>
void postorder(Node, Tree&) {
template < typename Node, typename Tree > void postorder(Node, Tree&)
{
std::cout << ")";
}
};
int main()
{
using namespace boost;
typedef adjacency_list< vecS, vecS, directedS,
property<vertex_name_t, std::string> > graph_t;
property< vertex_name_t, std::string > >
graph_t;
typedef graph_traits< graph_t >::vertex_descriptor vertex_t;
graph_t g;
vertex_t a = add_vertex(g),
b = add_vertex(g),
c = add_vertex(g);
vertex_t a = add_vertex(g), b = add_vertex(g), c = add_vertex(g);
add_edge(a, b, g);
add_edge(a, c, g);
@ -53,11 +51,12 @@ int main()
name[c] = "C";
typedef iterator_property_map< std::vector< vertex_t >::iterator,
property_map<graph_t, vertex_index_t>::type> parent_map_t;
property_map< graph_t, vertex_index_t >::type >
parent_map_t;
std::vector< vertex_t > parent(num_vertices(g));
typedef graph_as_tree< graph_t, parent_map_t > tree_t;
tree_t t(g, a, make_iterator_property_map(parent.begin(),
get(vertex_index, g)));
tree_t t(
g, a, make_iterator_property_map(parent.begin(), get(vertex_index, g)));
tree_printer vis;
traverse_tree(a, t, vis);

View File

@ -9,15 +9,14 @@
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/subgraph.hpp>
int
main()
int main()
{
using namespace boost;
using std::string;
typedef adjacency_list< vecS, vecS, directedS, no_property,
property<edge_index_t, int>,
property<graph_name_t, string> > graph_t;
property< edge_index_t, int >, property< graph_name_t, string > >
graph_t;
graph_t g;
get_property(g, graph_name) = "graph";

View File

@ -15,12 +15,12 @@
using namespace boost;
typedef boost::adjacency_list< vecS, vecS, directedS,
property<vertex_name_t, std::string>,
property<edge_weight_t, double> > Digraph;
property< vertex_name_t, std::string >, property< edge_weight_t, double > >
Digraph;
typedef boost::adjacency_list< vecS, vecS, undirectedS,
property<vertex_name_t, std::string>,
property<edge_weight_t, double> > Graph;
property< vertex_name_t, std::string >, property< edge_weight_t, double > >
Graph;
void test_graph_read_write(const std::string& filename)
{

View File

@ -18,12 +18,14 @@ typedef grid_graph<DIMENSIONS> Graph;
typedef graph_traits< Graph > Traits;
// Define a simple function to print vertices
void print_vertex(Traits::vertex_descriptor vertex_to_print) {
std::cout << "(" << vertex_to_print[0] << ", " << vertex_to_print[1] <<
", " << vertex_to_print[2] << ")" << std::endl;
void print_vertex(Traits::vertex_descriptor vertex_to_print)
{
std::cout << "(" << vertex_to_print[0] << ", " << vertex_to_print[1] << ", "
<< vertex_to_print[2] << ")" << std::endl;
}
int main(int argc, char* argv[]) {
int main(int argc, char* argv[])
{
// Define a 3x5x7 grid_graph where the second dimension doesn't wrap
boost::array< std::size_t, 3 > lengths = { { 3, 5, 7 } };
@ -31,36 +33,39 @@ int main(int argc, char* argv[]) {
Graph graph(lengths, wrapped);
// Do a round-trip test of the vertex index functions
for (Traits::vertices_size_type v_index = 0;
v_index < num_vertices(graph); ++v_index) {
for (Traits::vertices_size_type v_index = 0; v_index < num_vertices(graph);
++v_index)
{
// The two indicies should always be equal
std::cout << "Index of vertex " << v_index << " is " <<
get(boost::vertex_index, graph, vertex(v_index, graph)) << std::endl;
std::cout << "Index of vertex " << v_index << " is "
<< get(boost::vertex_index, graph, vertex(v_index, graph))
<< std::endl;
}
// Do a round-trip test of the edge index functions
for (Traits::edges_size_type e_index = 0;
e_index < num_edges(graph); ++e_index) {
for (Traits::edges_size_type e_index = 0; e_index < num_edges(graph);
++e_index)
{
// The two indicies should always be equal
std::cout << "Index of edge " << e_index << " is " <<
get(boost::edge_index, graph, edge_at(e_index, graph)) << std::endl;
std::cout << "Index of edge " << e_index << " is "
<< get(boost::edge_index, graph, edge_at(e_index, graph))
<< std::endl;
}
// Print number of dimensions
std::cout << graph.dimensions() << std::endl; // prints "3"
// Print dimension lengths (same order as in the lengths array)
std::cout << graph.length(0) << "x" << graph.length(1) <<
"x" << graph.length(2) << std::endl; // prints "3x5x7"
std::cout << graph.length(0) << "x" << graph.length(1) << "x"
<< graph.length(2) << std::endl; // prints "3x5x7"
// Print dimension wrapping (W = wrapped, U = unwrapped)
std::cout << (graph.wrapped(0) ? "W" : "U") << ", " <<
(graph.wrapped(1) ? "W" : "U") << ", " <<
(graph.wrapped(2) ? "W" : "U") << std::endl; // prints "W, U, W"
std::cout << (graph.wrapped(0) ? "W" : "U") << ", "
<< (graph.wrapped(1) ? "W" : "U") << ", "
<< (graph.wrapped(2) ? "W" : "U")
<< std::endl; // prints "W, U, W"
// Start with the first vertex in the graph
Traits::vertex_descriptor first_vertex = vertex(0, graph);

View File

@ -22,11 +22,13 @@ int main(int argc, char* argv[])
GraphType graph(lengths);
// Get the index map of the grid graph
typedef boost::property_map<GraphType, boost::vertex_index_t>::const_type indexMapType;
typedef boost::property_map< GraphType, boost::vertex_index_t >::const_type
indexMapType;
indexMapType indexMap(get(boost::vertex_index, graph));
// Create a float for every node in the graph
boost::vector_property_map<float, indexMapType> dataMap(num_vertices(graph), indexMap);
boost::vector_property_map< float, indexMapType > dataMap(
num_vertices(graph), indexMap);
// Associate the value 2.0 with the node at position (0,1) in the grid
boost::graph_traits< GraphType >::vertex_descriptor v = { { 0, 1 } };

View File

@ -16,13 +16,9 @@
#include <iterator>
#include <map>
template <typename OutputStream>
struct cycle_printer
template < typename OutputStream > struct cycle_printer
{
cycle_printer(OutputStream& stream)
: os(stream)
{ }
cycle_printer(OutputStream& stream) : os(stream) {}
template < typename Path, typename Graph >
void cycle(Path const& p, Graph const& g)
@ -32,15 +28,15 @@ struct cycle_printer
// Get the property map containing the vertex indices
// so we can print them.
typedef typename boost::property_map<
Graph, boost::vertex_index_t
>::const_type IndexMap;
typedef typename boost::property_map< Graph,
boost::vertex_index_t >::const_type IndexMap;
IndexMap indices = get(boost::vertex_index, g);
// Iterate over path printing each vertex that forms the cycle.
typename Path::const_iterator i, before_end = boost::prior(p.end());
for (i = p.begin(); i != before_end; ++i) {
for (i = p.begin(); i != before_end; ++i)
{
os << get(indices, *i) << " ";
}
os << get(indices, *i) << '\n';
@ -48,12 +44,12 @@ struct cycle_printer
OutputStream& os;
};
// VertexPairIterator is an iterator over pairs of whitespace separated
// vertices `u` and `v` representing a directed edge from `u` to `v`.
template < typename Graph, typename VertexPairIterator >
void build_graph(Graph& graph, unsigned int const nvertices,
VertexPairIterator first, VertexPairIterator last) {
VertexPairIterator first, VertexPairIterator last)
{
typedef boost::graph_traits< Graph > Traits;
typedef typename Traits::vertex_descriptor vertex_descriptor;
std::map< unsigned int, vertex_descriptor > vertices;
@ -61,7 +57,8 @@ void build_graph(Graph& graph, unsigned int const nvertices,
for (unsigned int i = 0; i < nvertices; ++i)
vertices[i] = add_vertex(graph);
for (; first != last; ++first) {
for (; first != last; ++first)
{
unsigned int u = *first++;
BOOST_ASSERT_MSG(first != last,
@ -77,9 +74,10 @@ void build_graph(Graph& graph, unsigned int const nvertices,
BOOST_ASSERT(num_vertices(graph) == nvertices);
}
int main(int argc, char const* argv[]) {
if (argc < 2) {
int main(int argc, char const* argv[])
{
if (argc < 2)
{
std::cout << "usage: " << argv[0] << " num_vertices < input\n";
return EXIT_FAILURE;
}

View File

@ -15,8 +15,8 @@
#include <boost/graph/properties.hpp>
template < typename Graph, typename NameMap, typename VertexMap >
typename boost::graph_traits<Graph>::vertex_descriptor
add_named_vertex(Graph& g, NameMap nm, const std::string& name, VertexMap& vm)
typename boost::graph_traits< Graph >::vertex_descriptor add_named_vertex(
Graph& g, NameMap nm, const std::string& name, VertexMap& vm)
{
typedef typename boost::graph_traits< Graph >::vertex_descriptor Vertex;
typedef typename VertexMap::iterator Iterator;
@ -25,13 +25,15 @@ add_named_vertex(Graph& g, NameMap nm, const std::string& name, VertexMap& vm)
Iterator iter;
bool inserted;
boost::tie(iter, inserted) = vm.insert(make_pair(name, Vertex()));
if(inserted) {
if (inserted)
{
// The name was unique so we need to add a vertex to the graph
v = add_vertex(g);
iter->second = v;
put(nm, v, name); // store the name in the name map
}
else {
else
{
// We had alread inserted this name so we can return the
// associated vertex.
v = iter->second;
@ -40,13 +42,16 @@ add_named_vertex(Graph& g, NameMap nm, const std::string& name, VertexMap& vm)
}
template < typename Graph, typename NameMap, typename InputStream >
inline std::map<std::string, typename boost::graph_traits<Graph>::vertex_descriptor>
inline std::map< std::string,
typename boost::graph_traits< Graph >::vertex_descriptor >
read_graph(Graph& g, NameMap nm, InputStream& is)
{
typedef typename boost::graph_traits< Graph >::vertex_descriptor Vertex;
std::map< std::string, Vertex > verts;
for(std::string line; std::getline(is, line); ) {
if(line.empty()) continue;
for (std::string line; std::getline(is, line);)
{
if (line.empty())
continue;
std::size_t index = line.find_first_of(',');
std::string first(line, 0, index);
std::string second(line, index + 1);
@ -59,7 +64,8 @@ read_graph(Graph& g, NameMap nm, InputStream& is)
}
template < typename Graph, typename InputStream >
inline std::map<std::string, typename boost::graph_traits<Graph>::vertex_descriptor>
inline std::map< std::string,
typename boost::graph_traits< Graph >::vertex_descriptor >
read_graph(Graph& g, InputStream& is)
{
typedef typename boost::graph_traits< Graph >::vertex_descriptor Vertex;
@ -67,15 +73,19 @@ read_graph(Graph& g, InputStream& is)
return read_graph(g, NameMap(), is);
}
template <typename Graph, typename NameMap, typename WeightMap, typename InputStream>
inline std::map<std::string, typename boost::graph_traits<Graph>::vertex_descriptor>
template < typename Graph, typename NameMap, typename WeightMap,
typename InputStream >
inline std::map< std::string,
typename boost::graph_traits< Graph >::vertex_descriptor >
read_weighted_graph(Graph& g, NameMap nm, WeightMap wm, InputStream& is)
{
typedef typename boost::graph_traits< Graph >::vertex_descriptor Vertex;
typedef typename boost::graph_traits< Graph >::edge_descriptor Edge;
std::map< std::string, Vertex > verts;
for(std::string line; std::getline(is, line); ) {
if(line.empty()) continue;
for (std::string line; std::getline(is, line);)
{
if (line.empty())
continue;
std::size_t i = line.find_first_of(',');
std::size_t j = line.find_first_of(',', i + 1);
std::string first(line, 0, i);
@ -98,9 +108,9 @@ read_weighted_graph(Graph& g, NameMap nm, WeightMap wm, InputStream& is)
return verts;
}
template < typename Graph, typename WeightMap, typename InputStream >
inline std::map<std::string, typename boost::graph_traits<Graph>::vertex_descriptor>
inline std::map< std::string,
typename boost::graph_traits< Graph >::vertex_descriptor >
read_weighted_graph(Graph& g, WeightMap wm, InputStream& is)
{
typedef typename boost::graph_traits< Graph >::vertex_descriptor Vertex;
@ -109,5 +119,4 @@ read_weighted_graph(Graph& g, WeightMap wm, InputStream& is)
return read_weighted_graph(g, NameMap(), wm, is);
}
#endif

View File

@ -15,7 +15,6 @@
#include <iostream>
#include <utility>
/*
This file defines a simple example of a read-only implicit weighted graph
built using the Boost Graph Library. It can be used as a starting point for
@ -89,28 +88,28 @@ class ring_edge_iterator;
struct edge_weight_map;
// ReadablePropertyGraph associated types
namespace boost {
template<>
struct property_map< ring_graph, edge_weight_t > {
namespace boost
{
template <> struct property_map< ring_graph, edge_weight_t >
{
typedef edge_weight_map type;
typedef edge_weight_map const_type;
};
template<>
struct property_map< const ring_graph, edge_weight_t > {
template <> struct property_map< const ring_graph, edge_weight_t >
{
typedef edge_weight_map type;
typedef edge_weight_map const_type;
};
}
// Tag values that specify the traversal type in graph::traversal_category.
struct ring_traversal_catetory:
virtual public boost::bidirectional_graph_tag,
struct ring_traversal_catetory : virtual public boost::bidirectional_graph_tag,
virtual public boost::adjacency_graph_tag,
virtual public boost::vertex_list_graph_tag,
virtual public boost::edge_list_graph_tag
{};
{
};
/*
Undirected graph of vertices arranged in a ring shape.
@ -118,7 +117,8 @@ Undirected graph of vertices arranged in a ring shape.
Vertices are indexed by integer, and edges connect vertices with consecutive
indices. Vertex 0 is also adjacent to the vertex n-1.
*/
class ring_graph {
class ring_graph
{
public:
// Graph associated types
typedef std::size_t vertex_descriptor;
@ -153,6 +153,7 @@ public:
ring_graph(std::size_t n) : m_n(n) {};
std::size_t n() const { return m_n; }
private:
// The number of vertices in the graph.
std::size_t m_n;
@ -164,19 +165,26 @@ typedef boost::graph_traits<ring_graph>::vertex_descriptor vertex_descriptor;
typedef boost::graph_traits< ring_graph >::edge_descriptor edge_descriptor;
typedef boost::graph_traits< ring_graph >::out_edge_iterator out_edge_iterator;
typedef boost::graph_traits< ring_graph >::in_edge_iterator in_edge_iterator;
typedef boost::graph_traits<ring_graph>::adjacency_iterator adjacency_iterator;
typedef boost::graph_traits< ring_graph >::adjacency_iterator
adjacency_iterator;
typedef boost::graph_traits< ring_graph >::degree_size_type degree_size_type;
typedef boost::graph_traits< ring_graph >::vertex_iterator vertex_iterator;
typedef boost::graph_traits<ring_graph>::vertices_size_type vertices_size_type;
typedef boost::graph_traits< ring_graph >::vertices_size_type
vertices_size_type;
typedef boost::graph_traits< ring_graph >::edge_iterator edge_iterator;
typedef boost::graph_traits< ring_graph >::edges_size_type edges_size_type;
// Tag values passed to an iterator constructor to specify whether it should
// be created at the start or the end of its range.
struct iterator_position {};
struct iterator_start:virtual public iterator_position {};
struct iterator_end:virtual public iterator_position {};
struct iterator_position
{
};
struct iterator_start : virtual public iterator_position
{
};
struct iterator_end : virtual public iterator_position
{
};
/*
Iterator over edges incident on a vertex in a ring graph.
@ -187,33 +195,31 @@ around the end of the ring as needed.
It is implemented with the boost::iterator_adaptor class, adapting an
offset into the dereference::ring_offset array.
*/
class ring_incident_edge_iterator:public boost::iterator_adaptor <
ring_incident_edge_iterator,
boost::counting_iterator<std::size_t>,
edge_descriptor,
boost::use_default,
edge_descriptor > {
class ring_incident_edge_iterator
: public boost::iterator_adaptor< ring_incident_edge_iterator,
boost::counting_iterator< std::size_t >, edge_descriptor,
boost::use_default, edge_descriptor >
{
public:
ring_incident_edge_iterator():
ring_incident_edge_iterator::iterator_adaptor_(0),m_n(0),m_u(0) {};
explicit ring_incident_edge_iterator(const ring_graph& g,
vertex_descriptor u,
iterator_start):
ring_incident_edge_iterator::iterator_adaptor_(0),
m_n(g.n()),m_u(u) {};
explicit ring_incident_edge_iterator(const ring_graph& g,
vertex_descriptor u,
iterator_end):
// A graph with one vertex only has a single self-loop. A graph with
ring_incident_edge_iterator()
: ring_incident_edge_iterator::iterator_adaptor_(0), m_n(0), m_u(0) {};
explicit ring_incident_edge_iterator(
const ring_graph& g, vertex_descriptor u, iterator_start)
: ring_incident_edge_iterator::iterator_adaptor_(0), m_n(g.n()), m_u(u) {};
explicit ring_incident_edge_iterator(
const ring_graph& g, vertex_descriptor u, iterator_end)
: // A graph with one vertex only has a single self-loop. A graph with
// two vertices has a single edge between them. All other graphs have
// two edges per vertex.
ring_incident_edge_iterator::iterator_adaptor_(g.n() > 2 ? 2:1),
m_n(g.n()),m_u(u) {};
ring_incident_edge_iterator::iterator_adaptor_(g.n() > 2 ? 2 : 1)
, m_n(g.n())
, m_u(u) {};
private:
friend class boost::iterator_core_access;
edge_descriptor dereference() const {
edge_descriptor dereference() const
{
static const int ring_offset[] = { 1, -1 };
vertex_descriptor v;
@ -229,102 +235,98 @@ private:
vertex_descriptor m_u; // Vertex whose out edges are iterated
};
// IncidenceGraph valid expressions
vertex_descriptor source(edge_descriptor e, const ring_graph&) {
vertex_descriptor source(edge_descriptor e, const ring_graph&)
{
// The first vertex in the edge pair is the source.
return e.first;
}
vertex_descriptor target(edge_descriptor e, const ring_graph&) {
vertex_descriptor target(edge_descriptor e, const ring_graph&)
{
// The second vertex in the edge pair is the target.
return e.second;
}
std::pair<out_edge_iterator, out_edge_iterator>
out_edges(vertex_descriptor u, const ring_graph& g) {
std::pair< out_edge_iterator, out_edge_iterator > out_edges(
vertex_descriptor u, const ring_graph& g)
{
return std::pair< out_edge_iterator, out_edge_iterator >(
out_edge_iterator(g, u, iterator_start()),
out_edge_iterator(g, u, iterator_end()));
}
degree_size_type out_degree(vertex_descriptor, const ring_graph&) {
degree_size_type out_degree(vertex_descriptor, const ring_graph&)
{
// All vertices in a ring graph have two neighbors.
return 2;
}
// BidirectionalGraph valid expressions
std::pair<in_edge_iterator, in_edge_iterator>
in_edges(vertex_descriptor u, const ring_graph& g) {
std::pair< in_edge_iterator, in_edge_iterator > in_edges(
vertex_descriptor u, const ring_graph& g)
{
// The in-edges and out-edges are the same in an undirected graph.
return out_edges(u, g);
}
degree_size_type in_degree(vertex_descriptor u, const ring_graph& g) {
degree_size_type in_degree(vertex_descriptor u, const ring_graph& g)
{
// The in-degree and out-degree are both equal to the number of incident
// edges in an undirected graph.
return out_degree(u, g);
}
degree_size_type degree(vertex_descriptor u, const ring_graph& g) {
degree_size_type degree(vertex_descriptor u, const ring_graph& g)
{
// The in-degree and out-degree are both equal to the number of incident
// edges in an undirected graph.
return out_degree(u, g);
}
/*
Iterator over vertices adjacent to a given vertex.
This iterates over the target vertices of all the incident edges.
*/
class ring_adjacency_iterator:public boost::adjacency_iterator_generator<
ring_graph,
vertex_descriptor,
out_edge_iterator>::type {
class ring_adjacency_iterator
: public boost::adjacency_iterator_generator< ring_graph, vertex_descriptor,
out_edge_iterator >::type
{
// The parent class is an iterator_adpator that turns an iterator over
// out edges into an iterator over adjacent vertices.
typedef boost::adjacency_iterator_generator<
ring_graph,
vertex_descriptor,
typedef boost::adjacency_iterator_generator< ring_graph, vertex_descriptor,
out_edge_iterator >::type parent_class;
public:
ring_adjacency_iterator() {};
ring_adjacency_iterator(vertex_descriptor u,
const ring_graph& g,
iterator_start):
parent_class(out_edge_iterator(g, u, iterator_start()), &g) {};
ring_adjacency_iterator(vertex_descriptor u,
const ring_graph& g,
iterator_end):
parent_class(out_edge_iterator(g, u, iterator_end()), &g) {};
ring_adjacency_iterator(
vertex_descriptor u, const ring_graph& g, iterator_start)
: parent_class(out_edge_iterator(g, u, iterator_start()), &g) {};
ring_adjacency_iterator(
vertex_descriptor u, const ring_graph& g, iterator_end)
: parent_class(out_edge_iterator(g, u, iterator_end()), &g) {};
};
// AdjacencyGraph valid expressions
std::pair<adjacency_iterator, adjacency_iterator>
adjacent_vertices(vertex_descriptor u, const ring_graph& g) {
std::pair< adjacency_iterator, adjacency_iterator > adjacent_vertices(
vertex_descriptor u, const ring_graph& g)
{
return std::pair< adjacency_iterator, adjacency_iterator >(
adjacency_iterator(u, g, iterator_start()),
adjacency_iterator(u, g, iterator_end()));
}
// VertexListGraph valid expressions
vertices_size_type num_vertices(const ring_graph& g) {
return g.n();
};
vertices_size_type num_vertices(const ring_graph& g) { return g.n(); };
std::pair<vertex_iterator, vertex_iterator> vertices(const ring_graph& g) {
std::pair< vertex_iterator, vertex_iterator > vertices(const ring_graph& g)
{
return std::pair< vertex_iterator, vertex_iterator >(
vertex_iterator(0), // The first iterator position
vertex_iterator(num_vertices(g))); // The last iterator position
}
/*
Iterator over edges in a ring graph.
@ -334,27 +336,26 @@ vertex returns its first outgoing edge.
It is implemented with the boost::iterator_adaptor class, because it is
essentially a vertex_iterator with a customized deference operation.
*/
class ring_edge_iterator:public boost::iterator_adaptor<
ring_edge_iterator,
vertex_iterator,
edge_descriptor,
boost::use_default,
edge_descriptor > {
class ring_edge_iterator
: public boost::iterator_adaptor< ring_edge_iterator, vertex_iterator,
edge_descriptor, boost::use_default, edge_descriptor >
{
public:
ring_edge_iterator():
ring_edge_iterator::iterator_adaptor_(0),m_g(NULL) {};
explicit ring_edge_iterator(const ring_graph& g, iterator_start):
ring_edge_iterator::iterator_adaptor_(vertices(g).first),m_g(&g) {};
explicit ring_edge_iterator(const ring_graph& g, iterator_end):
ring_edge_iterator::iterator_adaptor_(
ring_edge_iterator()
: ring_edge_iterator::iterator_adaptor_(0), m_g(NULL) {};
explicit ring_edge_iterator(const ring_graph& g, iterator_start)
: ring_edge_iterator::iterator_adaptor_(vertices(g).first), m_g(&g) {};
explicit ring_edge_iterator(const ring_graph& g, iterator_end)
: ring_edge_iterator::iterator_adaptor_(
// Size 2 graphs have a single edge connecting the two vertices.
g.n() == 2 ? ++(vertices(g).first) : vertices(g).second ),
m_g(&g) {};
g.n() == 2 ? ++(vertices(g).first) : vertices(g).second)
, m_g(&g) {};
private:
friend class boost::iterator_core_access;
edge_descriptor dereference() const {
edge_descriptor dereference() const
{
// The first element in the incident edge list of the current vertex.
return *(out_edges(*this->base_reference(), *m_g).first);
}
@ -364,49 +365,51 @@ private:
};
// EdgeListGraph valid expressions
std::pair<edge_iterator, edge_iterator> edges(const ring_graph& g) {
std::pair< edge_iterator, edge_iterator > edges(const ring_graph& g)
{
return std::pair< edge_iterator, edge_iterator >(
ring_edge_iterator(g, iterator_start()),
ring_edge_iterator(g, iterator_end()));
}
edges_size_type num_edges(const ring_graph& g) {
edges_size_type num_edges(const ring_graph& g)
{
// There are as many edges as there are vertices, except for size 2
// graphs, which have a single edge connecting the two vertices.
return g.n() == 2 ? 1 : g.n();
}
// AdjacencyMatrix valid expressions
std::pair<edge_descriptor, bool>
edge(vertex_descriptor u, vertex_descriptor v, const ring_graph& g) {
if ((u == v + 1 || v == u + 1) &&
u > 0 && u < num_vertices(g) && v > 0 && v < num_vertices(g))
std::pair< edge_descriptor, bool > edge(
vertex_descriptor u, vertex_descriptor v, const ring_graph& g)
{
if ((u == v + 1 || v == u + 1) && u > 0 && u < num_vertices(g) && v > 0
&& v < num_vertices(g))
return std::pair< edge_descriptor, bool >(edge_descriptor(u, v), true);
else
return std::pair< edge_descriptor, bool >(edge_descriptor(), false);
}
/*
Map from edges to weight values
*/
struct edge_weight_map {
struct edge_weight_map
{
typedef double value_type;
typedef value_type reference;
typedef edge_descriptor key_type;
typedef boost::readable_property_map_tag category;
// Edges have a weight equal to the average of their endpoint indexes.
reference operator[](key_type e) const {
reference operator[](key_type e) const
{
return (e.first + e.second) / 2.0;
}
};
// Use these propety_map and property_traits parameterizations to refer to
// the associated property map types.
typedef boost::property_map<ring_graph,
boost::edge_weight_t>::const_type
typedef boost::property_map< ring_graph, boost::edge_weight_t >::const_type
const_edge_weight_map;
typedef boost::property_traits< const_edge_weight_map >::reference
edge_weight_map_value_type;
@ -414,38 +417,41 @@ typedef boost::property_traits<const_edge_weight_map>::key_type
edge_weight_map_key;
// PropertyMap valid expressions
edge_weight_map_value_type
get(const_edge_weight_map pmap, edge_weight_map_key e) {
edge_weight_map_value_type get(
const_edge_weight_map pmap, edge_weight_map_key e)
{
return pmap[e];
}
// ReadablePropertyGraph valid expressions
const_edge_weight_map get(boost::edge_weight_t, const ring_graph&) {
const_edge_weight_map get(boost::edge_weight_t, const ring_graph&)
{
return const_edge_weight_map();
}
edge_weight_map_value_type get(boost::edge_weight_t tag,
const ring_graph& g,
edge_weight_map_key e) {
edge_weight_map_value_type get(
boost::edge_weight_t tag, const ring_graph& g, edge_weight_map_key e)
{
return get(tag, g)[e];
}
// This expression is not part of a graph concept, but is used to return the
// default vertex index map used by the Dijkstra search algorithm.
boost::identity_property_map get(boost::vertex_index_t, const ring_graph&) {
boost::identity_property_map get(boost::vertex_index_t, const ring_graph&)
{
// The vertex descriptors are already unsigned integer indices, so just
// return an identity map.
return boost::identity_property_map();
}
// Print edges as (x, y)
std::ostream& operator<<(std::ostream& output, const edge_descriptor& e) {
std::ostream& operator<<(std::ostream& output, const edge_descriptor& e)
{
return output << "(" << e.first << ", " << e.second << ")";
}
int main (int argc, char const *argv[]) {
int main(int argc, char const* argv[])
{
using namespace boost;
// Check the concepts that graph models. This is included to demonstrate
// how concept checking works, but is not required for a working program
@ -455,10 +461,10 @@ int main (int argc, char const *argv[]) {
BOOST_CONCEPT_ASSERT((VertexListGraphConcept< ring_graph >));
BOOST_CONCEPT_ASSERT((EdgeListGraphConcept< ring_graph >));
BOOST_CONCEPT_ASSERT((AdjacencyMatrixConcept< ring_graph >));
BOOST_CONCEPT_ASSERT((
ReadablePropertyMapConcept<const_edge_weight_map, edge_descriptor> ));
BOOST_CONCEPT_ASSERT((
ReadablePropertyGraphConcept<ring_graph, edge_descriptor, edge_weight_t> ));
BOOST_CONCEPT_ASSERT(
(ReadablePropertyMapConcept< const_edge_weight_map, edge_descriptor >));
BOOST_CONCEPT_ASSERT((ReadablePropertyGraphConcept< ring_graph,
edge_descriptor, edge_weight_t >));
// Specify the size of the graph on the command line, or use a default size
// of 5.
@ -478,7 +484,8 @@ int main (int argc, char const *argv[]) {
// 5 vertices
std::cout << "Vertices, outgoing edges, and adjacent vertices" << std::endl;
vertex_iterator vi, vi_end;
for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; vi++) {
for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; vi++)
{
vertex_descriptor u = *vi;
std::cout << "Vertex " << u << ": ";
// Adjacenct edges
@ -487,9 +494,12 @@ int main (int argc, char const *argv[]) {
std::cout << *ei << " ";
std::cout << " Adjacent vertices ";
// Adjacent vertices
// Here we want our adjacency_iterator and not boost::adjacency_iterator.
// Here we want our adjacency_iterator and not
// boost::adjacency_iterator.
::adjacency_iterator ai, ai_end;
for (boost::tie(ai, ai_end) = adjacent_vertices(u, g); ai != ai_end; ai++) {
for (boost::tie(ai, ai_end) = adjacent_vertices(u, g); ai != ai_end;
ai++)
{
std::cout << *ai << " ";
}
std::cout << std::endl;
@ -508,13 +518,15 @@ int main (int argc, char const *argv[]) {
// 5 edges
std::cout << "Edges and weights" << std::endl;
edge_iterator ei, ei_end;
for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ei++) {
for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ei++)
{
edge_descriptor e = *ei;
std::cout << e << " weight " << get(edge_weight, g, e) << std::endl;
}
std::cout << num_edges(g) << " edges" << std::endl;
if (n>0) {
if (n > 0)
{
std::cout << std::endl;
// Do a Dijkstra search from vertex 0. For n=5 this will print:
//
@ -530,21 +542,21 @@ int main (int argc, char const *argv[]) {
iterator_property_map< std::vector< vertex_descriptor >::iterator,
property_map< ring_graph, vertex_index_t >::const_type >
pred_pm(pred.begin(), get(vertex_index, g));
iterator_property_map<std::vector<edge_weight_map_value_type>::iterator,
iterator_property_map<
std::vector< edge_weight_map_value_type >::iterator,
property_map< ring_graph, vertex_index_t >::const_type >
dist_pm(dist.begin(), get(vertex_index, g));
dijkstra_shortest_paths(g, source,
predecessor_map(pred_pm).
distance_map(dist_pm) );
dijkstra_shortest_paths(
g, source, predecessor_map(pred_pm).distance_map(dist_pm));
std::cout << "Dijkstra search from vertex " << source << std::endl;
for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) {
for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
{
vertex_descriptor u = *vi;
std::cout << "Vertex " << u << ": "
<< "parent " << pred[*vi] << ", "
<< "distance " << dist[u]
<< std::endl;
<< "distance " << dist[u] << std::endl;
}
}

View File

@ -43,7 +43,8 @@ int main(int , char* [])
boost::graph_traits< Graph >::vertex_iterator i, end;
boost::graph_traits< Graph >::in_edge_iterator ei, edge_end;
for(boost::tie(i,end) = vertices(g); i != end; ++i) {
for (boost::tie(i, end) = vertices(g); i != end; ++i)
{
cout << *i << " <-- ";
for (boost::tie(ei, edge_end) = in_edges(*i, g); ei != edge_end; ++ei)
cout << source(*ei, g) << " ";

View File

@ -20,9 +20,7 @@ using namespace boost;
// This template structure defines the function that we will apply
// to compute both the per-vertex mean geodesic distances and the
// graph's mean geodesic distance.
template <typename Graph,
typename DistanceType,
typename ResultType,
template < typename Graph, typename DistanceType, typename ResultType,
typename Divides = divides< ResultType > >
struct inclusive_average
{
@ -31,10 +29,12 @@ struct inclusive_average
result_type operator()(distance_type d, const Graph& g)
{
if(d == numeric_values<distance_type>::infinity()) {
if (d == numeric_values< distance_type >::infinity())
{
return numeric_values< result_type >::infinity();
}
else {
else
{
return div(result_type(d), result_type(num_vertices(g)));
}
}
@ -84,8 +84,7 @@ typedef GeodesicProperty::map_type GeodesicMap;
static float exclusive_geodesics(const Graph&, DistanceMatrixMap, GeodesicMap);
static float inclusive_geodesics(const Graph&, DistanceMatrixMap, GeodesicMap);
int
main(int argc, char *argv[])
int main(int argc, char* argv[])
{
// Create the graph, a name map that providse abstract access
// to the web page names, and the weight map as an accessor to
@ -121,11 +120,10 @@ main(int argc, char *argv[])
cout << setw(12) << setiosflags(ios::left) << "excluding";
cout << setw(12) << setiosflags(ios::left) << "including" << endl;
graph_traits< Graph >::vertex_iterator i, end;
for(boost::tie(i, end) = vertices(g); i != end; ++i) {
cout << setw(12) << setiosflags(ios::left)
<< g[*i].name
<< setw(12) << get(exmap, *i)
<< setw(12) << get(inmap, *i) << endl;
for (boost::tie(i, end) = vertices(g); i != end; ++i)
{
cout << setw(12) << setiosflags(ios::left) << g[*i].name << setw(12)
<< get(exmap, *i) << setw(12) << get(inmap, *i) << endl;
}
cout << "small world (excluding self-loops): " << ex << endl;
cout << "small world (including self-loops): " << in << endl;
@ -133,17 +131,14 @@ main(int argc, char *argv[])
return 0;
}
float
exclusive_geodesics(const Graph& g, DistanceMatrixMap dm, GeodesicMap gm)
float exclusive_geodesics(const Graph& g, DistanceMatrixMap dm, GeodesicMap gm)
{
// Compute the mean geodesic distances, which excludes distances
// of self-loops by default. Return the measure for the entire graph.
return all_mean_geodesics(g, dm, gm);
}
float
inclusive_geodesics(const Graph &g, DistanceMatrixMap dm, GeodesicMap gm)
float inclusive_geodesics(const Graph& g, DistanceMatrixMap dm, GeodesicMap gm)
{
// Create a new measure object for computing the mean geodesic
// distance of all vertices. This measure will actually be used

View File

@ -53,9 +53,10 @@ int main(int argc, char* argv[])
ds.union_set(4, 0);
ds.union_set(2, 5);
BOOST_FOREACH(Vertex current_vertex, vertices(graph)) {
std::cout << "representative[" << current_vertex << "] = " <<
ds.find_set(current_vertex) << std::endl;
BOOST_FOREACH (Vertex current_vertex, vertices(graph))
{
std::cout << "representative[" << current_vertex
<< "] = " << ds.find_set(current_vertex) << std::endl;
}
std::cout << std::endl;
@ -68,12 +69,13 @@ int main(int argc, char* argv[])
Components components(parent.begin(), parent.end());
// Iterate through the component indices
BOOST_FOREACH(VertexIndex component_index, components) {
BOOST_FOREACH (VertexIndex component_index, components)
{
std::cout << "component " << component_index << " contains: ";
// Iterate through the child vertex indices for [component_index]
BOOST_FOREACH(VertexIndex child_index,
components[component_index]) {
BOOST_FOREACH (VertexIndex child_index, components[component_index])
{
std::cout << child_index << " ";
}

View File

@ -86,9 +86,10 @@ int main(int argc, char* argv[])
print_graph(graph, get(boost::vertex_index, graph));
std::cout << std::endl;
BOOST_FOREACH(Vertex current_vertex, vertices(graph)) {
std::cout << "representative[" << current_vertex << "] = " <<
ds.find_set(current_vertex) << std::endl;
BOOST_FOREACH (Vertex current_vertex, vertices(graph))
{
std::cout << "representative[" << current_vertex
<< "] = " << ds.find_set(current_vertex) << std::endl;
}
std::cout << std::endl;
@ -102,12 +103,13 @@ int main(int argc, char* argv[])
Components components(parent.begin(), parent.end());
// Iterate through the component indices
BOOST_FOREACH(VertexIndex current_index, components) {
BOOST_FOREACH (VertexIndex current_index, components)
{
std::cout << "component " << current_index << " contains: ";
// Iterate through the child vertex indices for [current_index]
BOOST_FOREACH(VertexIndex child_index,
components[current_index]) {
BOOST_FOREACH (VertexIndex child_index, components[current_index])
{
std::cout << child_index << " ";
}
@ -116,4 +118,3 @@ int main(int argc, char* argv[])
return (0);
}

View File

@ -39,8 +39,7 @@ typedef exterior_vertex_property<Graph, unsigned> CentralityProperty;
typedef CentralityProperty::container_type CentralityContainer;
typedef CentralityProperty::map_type CentralityMap;
int
main(int argc, char *argv[])
int main(int argc, char* argv[])
{
// Create the graph and a property map that provides
// access to the actor names.
@ -62,12 +61,11 @@ main(int argc, char *argv[])
// Print the degree centrality of each vertex
graph_traits< Graph >::vertex_iterator i, end;
for(boost::tie(i, end) = vertices(g); i != end; ++i) {
for (boost::tie(i, end) = vertices(g); i != end; ++i)
{
Vertex v = *i;
cout << setiosflags(ios::left) << setw(12)
<< g[v].name << "\t"
<< im[v] << "\t"
<< pm[v] << endl;
cout << setiosflags(ios::left) << setw(12) << g[v].name << "\t" << im[v]
<< "\t" << pm[v] << endl;
}
return 0;

View File

@ -48,7 +48,8 @@ using namespace boost;
template < class EdgeIter, class Graph >
void who_owes_who(EdgeIter first, EdgeIter last, const Graph& G)
{
while (first != last) {
while (first != last)
{
cout << G[source(*first, G)].first_name << " owes "
<< G[target(*first, G)].first_name << " some money" << endl;
++first;
@ -60,8 +61,7 @@ struct VertexData
string first_name;
};
int
main()
int main()
{
{
// Create the graph, and specify that we will use std::string to
@ -70,8 +70,8 @@ main()
typedef pair< int, int > Pair;
Pair edge_array[11] = { Pair(0, 1), Pair(0, 2), Pair(0, 3), Pair(0, 4),
Pair(2,0), Pair(3,0), Pair(2,4), Pair(3,1),
Pair(3,4), Pair(4,0), Pair(4,1) };
Pair(2, 0), Pair(3, 0), Pair(2, 4), Pair(3, 1), Pair(3, 4),
Pair(4, 0), Pair(4, 1) };
MyGraphType G(5);
for (int i = 0; i < 11; ++i)

View File

@ -46,8 +46,12 @@ using namespace boost;
// create a tag for our new property
enum vertex_first_name_t { vertex_first_name };
namespace boost {
enum vertex_first_name_t
{
vertex_first_name
};
namespace boost
{
BOOST_INSTALL_PROPERTY(vertex, first_name);
}
@ -55,36 +59,36 @@ template <class EdgeIter, class Graph>
void who_owes_who(EdgeIter first, EdgeIter last, const Graph& G)
{
// Access the propety acessor type for this graph
typedef typename property_map<Graph, vertex_first_name_t>
::const_type NamePA;
typedef
typename property_map< Graph, vertex_first_name_t >::const_type NamePA;
NamePA name = get(vertex_first_name, G);
typedef typename boost::property_traits< NamePA >::value_type NameType;
NameType src_name, targ_name;
while (first != last) {
while (first != last)
{
src_name = boost::get(name, source(*first, G));
targ_name = boost::get(name, target(*first, G));
cout << src_name << " owes "
<< targ_name << " some money" << endl;
cout << src_name << " owes " << targ_name << " some money" << endl;
++first;
}
}
int
main()
int main()
{
{
// Create the graph, and specify that we will use std::string to
// store the first name's.
typedef adjacency_list< vecS, vecS, directedS,
property<vertex_first_name_t, std::string> > MyGraphType;
property< vertex_first_name_t, std::string > >
MyGraphType;
typedef pair< int, int > Pair;
Pair edge_array[11] = { Pair(0, 1), Pair(0, 2), Pair(0, 3), Pair(0, 4),
Pair(2,0), Pair(3,0), Pair(2,4), Pair(3,1),
Pair(3,4), Pair(4,0), Pair(4,1) };
Pair(2, 0), Pair(3, 0), Pair(2, 4), Pair(3, 1), Pair(3, 4),
Pair(4, 0), Pair(4, 1) };
MyGraphType G(5);
for (int i = 0; i < 11; ++i)

View File

@ -16,60 +16,73 @@
f: 9 10 11 0 1 3 2 4 6 8 7 5
*/
int
main()
int main()
{
using namespace boost;
const int n = 12;
typedef adjacency_list< vecS, listS, undirectedS,
property<vertex_index_t, int> > graph_t;
property< vertex_index_t, int > >
graph_t;
graph_t g1(n), g2(n);
std::vector< graph_traits< graph_t >::vertex_descriptor > v1(n), v2(n);
property_map<graph_t, vertex_index_t>::type
v1_index_map = get(vertex_index, g1),
property_map< graph_t, vertex_index_t >::type v1_index_map
= get(vertex_index, g1),
v2_index_map = get(vertex_index, g2);
graph_traits< graph_t >::vertex_iterator i, end;
int id = 0;
for (boost::tie(i, end) = vertices(g1); i != end; ++i, ++id) {
for (boost::tie(i, end) = vertices(g1); i != end; ++i, ++id)
{
put(v1_index_map, *i, id);
v1[id] = *i;
}
id = 0;
for (boost::tie(i, end) = vertices(g2); i != end; ++i, ++id) {
for (boost::tie(i, end) = vertices(g2); i != end; ++i, ++id)
{
put(v2_index_map, *i, id);
v2[id] = *i;
}
add_edge(v1[0], v1[1], g1); add_edge(v1[1], v1[2], g1);
add_edge(v1[0], v1[1], g1);
add_edge(v1[1], v1[2], g1);
add_edge(v1[0], v1[2], g1);
add_edge(v1[3], v1[4], g1); add_edge(v1[4], v1[5], g1);
add_edge(v1[5], v1[6], g1); add_edge(v1[6], v1[3], g1);
add_edge(v1[7], v1[8], g1); add_edge(v1[8], v1[9], g1);
add_edge(v1[3], v1[4], g1);
add_edge(v1[4], v1[5], g1);
add_edge(v1[5], v1[6], g1);
add_edge(v1[6], v1[3], g1);
add_edge(v1[7], v1[8], g1);
add_edge(v1[8], v1[9], g1);
add_edge(v1[9], v1[10], g1);
add_edge(v1[10], v1[11], g1); add_edge(v1[11], v1[7], g1);
add_edge(v1[10], v1[11], g1);
add_edge(v1[11], v1[7], g1);
add_edge(v2[9], v2[10], g2); add_edge(v2[10], v2[11], g2);
add_edge(v2[9], v2[10], g2);
add_edge(v2[10], v2[11], g2);
add_edge(v2[11], v2[9], g2);
add_edge(v2[0], v2[1], g2); add_edge(v2[1], v2[3], g2);
add_edge(v2[3], v2[2], g2); add_edge(v2[2], v2[0], g2);
add_edge(v2[4], v2[5], g2); add_edge(v2[5], v2[7], g2);
add_edge(v2[0], v2[1], g2);
add_edge(v2[1], v2[3], g2);
add_edge(v2[3], v2[2], g2);
add_edge(v2[2], v2[0], g2);
add_edge(v2[4], v2[5], g2);
add_edge(v2[5], v2[7], g2);
add_edge(v2[7], v2[8], g2);
add_edge(v2[8], v2[6], g2); add_edge(v2[6], v2[4], g2);
add_edge(v2[8], v2[6], g2);
add_edge(v2[6], v2[4], g2);
std::vector< graph_traits< graph_t >::vertex_descriptor > f(n);
#if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
bool ret = isomorphism
(g1, g2, make_iterator_property_map(f.begin(), v1_index_map, f[0]),
degree_vertex_invariant(), get(vertex_index, g1), get(vertex_index, g2));
bool ret = isomorphism(g1, g2,
make_iterator_property_map(f.begin(), v1_index_map, f[0]),
degree_vertex_invariant(), get(vertex_index, g1),
get(vertex_index, g2));
#else
bool ret = isomorphism
(g1, g2, isomorphism_map
(make_iterator_property_map(f.begin(), v1_index_map, f[0])));
bool ret = isomorphism(g1, g2,
isomorphism_map(
make_iterator_property_map(f.begin(), v1_index_map, f[0])));
#endif
std::cout << "isomorphic? " << ret << std::endl;

View File

@ -17,14 +17,23 @@
#include <boost/graph/iteration_macros.hpp>
#include <iostream>
enum family { Jeanie, Debbie, Rick, John, Amanda, Margaret, Benjamin, N };
enum family
{
Jeanie,
Debbie,
Rick,
John,
Amanda,
Margaret,
Benjamin,
N
};
int main()
{
using namespace boost;
const char* name[] = { "Jeanie", "Debbie", "Rick", "John", "Amanda",
"Margaret", "Benjamin"
};
"Margaret", "Benjamin" };
adjacency_list<> g(N);
add_edge(Jeanie, Debbie, g);
@ -36,10 +45,11 @@ int main()
graph_traits< adjacency_list<> >::vertex_iterator i, end;
graph_traits< adjacency_list<> >::adjacency_iterator ai, a_end;
property_map<adjacency_list <>, vertex_index_t>::type
index_map = get(vertex_index, g);
property_map< adjacency_list<>, vertex_index_t >::type index_map
= get(vertex_index, g);
BGL_FORALL_VERTICES(i, g, adjacency_list<>) {
BGL_FORALL_VERTICES(i, g, adjacency_list<>)
{
std::cout << name[get(index_map, i)];
if (out_degree(i, g) == 0)

View File

@ -8,12 +8,12 @@
#include <iostream>
#include <boost/property_map/property_map.hpp>
int
main()
int main()
{
using namespace boost;
double x[] = { 0.2, 4.5, 3.2 };
iterator_property_map < double *, identity_property_map, double, double& > pmap(x);
iterator_property_map< double*, identity_property_map, double, double& >
pmap(x);
std::cout << "x[1] = " << get(pmap, 1) << std::endl;
put(pmap, 0, 1.7);
std::cout << "x[0] = " << pmap[0] << std::endl;

View File

@ -15,19 +15,17 @@
#include <boost/graph/graphviz.hpp>
#include <boost/graph/johnson_all_pairs_shortest.hpp>
int
main()
int main()
{
using namespace boost;
typedef adjacency_list< vecS, vecS, directedS, no_property,
property< edge_weight_t, int, property< edge_weight2_t, int > > > Graph;
property< edge_weight_t, int, property< edge_weight2_t, int > > >
Graph;
const int V = 6;
typedef std::pair< int, int > Edge;
Edge edge_array[] =
{ Edge(0, 1), Edge(0, 2), Edge(0, 3), Edge(0, 4), Edge(0, 5),
Edge(1, 2), Edge(1, 5), Edge(1, 3), Edge(2, 4), Edge(2, 5),
Edge(3, 2), Edge(4, 3), Edge(4, 1), Edge(5, 4)
};
Edge edge_array[] = { Edge(0, 1), Edge(0, 2), Edge(0, 3), Edge(0, 4),
Edge(0, 5), Edge(1, 2), Edge(1, 5), Edge(1, 3), Edge(2, 4), Edge(2, 5),
Edge(3, 2), Edge(4, 3), Edge(4, 1), Edge(5, 4) };
const std::size_t E = sizeof(edge_array) / sizeof(Edge);
#if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
// VC++ can't handle the iterator constructor
@ -54,9 +52,11 @@ main()
for (int k = 0; k < V; ++k)
std::cout << std::setw(5) << k;
std::cout << std::endl;
for (int i = 0; i < V; ++i) {
for (int i = 0; i < V; ++i)
{
std::cout << std::setw(3) << i << " -> ";
for (int j = 0; j < V; ++j) {
for (int j = 0; j < V; ++j)
{
if (D[i][j] == (std::numeric_limits< int >::max)())
std::cout << std::setw(5) << "inf";
else
@ -70,7 +70,8 @@ main()
<< " rankdir=LR\n"
<< "size=\"5,3\"\n"
<< "ratio=\"fill\"\n"
<< "edge[style=\"bold\"]\n" << "node[shape=\"circle\"]\n";
<< "edge[style=\"bold\"]\n"
<< "node[shape=\"circle\"]\n";
graph_traits< Graph >::edge_iterator ei, ei_end;
for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)

View File

@ -27,34 +27,35 @@ public:
template < typename Edge, typename Graph >
void tree_edge(Edge e, const Graph& g) const
{
typename graph_traits<Graph>::vertex_descriptor
u = source(e, g), v = target(e, g);
typename graph_traits< Graph >::vertex_descriptor u = source(e, g),
v = target(e, g);
d[v] = d[u] + 1;
}
private:
DistanceMap d;
};
// Convenience function
template < typename DistanceMap >
bacon_number_recorder<DistanceMap>
record_bacon_number(DistanceMap d)
bacon_number_recorder< DistanceMap > record_bacon_number(DistanceMap d)
{
return bacon_number_recorder< DistanceMap >(d);
}
int
main(int argc, const char** argv)
int main(int argc, const char** argv)
{
std::ifstream datafile(argc >= 2 ? argv[1] : "./kevin-bacon.dat");
if (!datafile) {
if (!datafile)
{
std::cerr << "No ./kevin-bacon.dat file" << std::endl;
return EXIT_FAILURE;
}
typedef adjacency_list < vecS, vecS, undirectedS, property < vertex_name_t,
std::string >, property < edge_name_t, std::string > > Graph;
typedef adjacency_list< vecS, vecS, undirectedS,
property< vertex_name_t, std::string >,
property< edge_name_t, std::string > >
Graph;
Graph g;
typedef property_map< Graph, vertex_name_t >::type actor_name_map_t;
@ -66,7 +67,8 @@ main(int argc, const char** argv)
typedef std::map< std::string, Vertex > NameVertexMap;
NameVertexMap actors;
for (std::string line; std::getline(datafile, line);) {
for (std::string line; std::getline(datafile, line);)
{
char_delimiters_separator< char > sep(false, "", ";");
tokenizer<> line_toks(line, sep);
tokenizer<>::iterator i = line_toks.begin();
@ -74,29 +76,33 @@ main(int argc, const char** argv)
NameVertexMap::iterator pos;
bool inserted;
Vertex u, v;
boost::tie(pos, inserted) = actors.insert(std::make_pair(actors_name, Vertex()));
if (inserted) {
boost::tie(pos, inserted)
= actors.insert(std::make_pair(actors_name, Vertex()));
if (inserted)
{
u = add_vertex(g);
actor_name[u] = actors_name;
pos->second = u;
} else
}
else
u = pos->second;
std::string movie_name = *i++;
boost::tie(pos, inserted) = actors.insert(std::make_pair(*i, Vertex()));
if (inserted) {
if (inserted)
{
v = add_vertex(g);
actor_name[v] = *i;
pos->second = v;
} else
}
else
v = pos->second;
graph_traits< Graph >::edge_descriptor e;
boost::tie(e, inserted) = add_edge(u, v, g);
if (inserted)
connecting_movie[e] = movie_name;
}
std::vector< int > bacon_number(num_vertices(g));
@ -104,11 +110,12 @@ main(int argc, const char** argv)
Vertex src = actors["Kevin Bacon"];
bacon_number[src] = 0;
breadth_first_search(g, src,
visitor(record_bacon_number(&bacon_number[0])));
breadth_first_search(
g, src, visitor(record_bacon_number(&bacon_number[0])));
graph_traits< Graph >::vertex_iterator i, end;
for (boost::tie(i, end) = vertices(g); i != end; ++i) {
for (boost::tie(i, end) = vertices(g); i != end; ++i)
{
std::cout << actor_name[*i] << " has a Bacon number of "
<< bacon_number[*i] << std::endl;
}

View File

@ -6,12 +6,12 @@
// http://www.boost.org/LICENSE_1_0.txt)
//=======================================================================
/*
IMPORTANT:
~~~~~~~~~~
This example appears to be broken and crashes at runtime, see https://github.com/boostorg/graph/issues/148
This example appears to be broken and crashes at runtime, see
https://github.com/boostorg/graph/issues/148
*/
@ -28,28 +28,33 @@
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/string.hpp>
struct vertex_properties {
struct vertex_properties
{
std::string name;
template < class Archive >
void serialize(Archive & ar, const unsigned int version) {
void serialize(Archive& ar, const unsigned int version)
{
ar& name;
}
};
struct edge_properties {
struct edge_properties
{
std::string name;
template < class Archive >
void serialize(Archive & ar, const unsigned int version) {
void serialize(Archive& ar, const unsigned int version)
{
ar& name;
}
};
using namespace boost;
typedef adjacency_list<vecS, vecS, undirectedS,
vertex_properties, edge_properties> Graph;
typedef adjacency_list< vecS, vecS, undirectedS, vertex_properties,
edge_properties >
Graph;
typedef graph_traits< Graph >::vertex_descriptor Vertex;
typedef graph_traits< Graph >::edge_descriptor Edge;
@ -58,10 +63,12 @@ class bacon_number_recorder : public default_bfs_visitor
public:
bacon_number_recorder(int* dist) : d(dist) {}
void tree_edge(Edge e, const Graph& g) const {
void tree_edge(Edge e, const Graph& g) const
{
Vertex u = source(e, g), v = target(e, g);
d[v] = d[u] + 1;
}
private:
int* d;
};
@ -69,7 +76,8 @@ private:
int main(int argc, const char** argv)
{
std::ifstream ifs(argc >= 2 ? argv[1] : "./kevin-bacon2.dat");
if (!ifs) {
if (!ifs)
{
std::cerr << "No ./kevin-bacon2.dat file" << std::endl;
return EXIT_FAILURE;
}
@ -90,12 +98,12 @@ int main(int argc, const char** argv)
bacon_number[src] = 0;
// Perform a breadth first search to compute everyone' Bacon number.
breadth_first_search(g, src,
visitor(bacon_number_recorder(&bacon_number[0])));
breadth_first_search(
g, src, visitor(bacon_number_recorder(&bacon_number[0])));
for (boost::tie(i, end) = vertices(g); i != end; ++i)
std::cout << g[*i].name << " has a Bacon number of "
<< bacon_number[*i] << std::endl;
std::cout << g[*i].name << " has a Bacon number of " << bacon_number[*i]
<< std::endl;
return 0;
}

Some files were not shown because too many files have changed in this diff Show More