boost free peer-reviwed portable C++ source libraries
example: transitive closure of a graph
#ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
#error The transitive closure algorithm uses partial specialization.
#endif
#include <boost/graph/transitive_closure.hpp>
#include <boost/graph/graphviz.hpp>
#include <boost/graph/graph_utility.hpp>
#include <string>
using namespace std;
using namespace boost;
int main(int, char *[])
{
typedef property < vertex_name_t, string >Name;
typedef property < vertex_index_t, std::size_t, Name > Index;
typedef adjacency_list < listS, listS, directedS, Index > graph_t;
typedef graph_traits < graph_t >::vertex_descriptor vertex_t;
graph_t G;
std::vector < vertex_t > verts;
vector < string > idx2string;
typedef map < string, int > MAP_STRING;
MAP_STRING ms; // path -> index into verts
MAP_STRING::iterator it1, it2;
string path1, path2;
int i = 0;
fstream in;
in.open("d");
//while (!cin.eof())
while (!in.eof())
{
path1 = "";
in >> path1;
in >> path2; // read ->
in >> path2;
if (path1.length() == 0)
break;
it1 = ms.find(path1);
if (it1 == ms.end())
{
ms[path1] = i;
cout.flush();
verts.push_back(add_vertex(Index(i, Name(path1)),G));
idx2string.push_back(path1);
i++;
}
it2 = ms.find(path2);
if (it2 == ms.end())
{
ms[path2] = i;
cout.flush();
verts.push_back(add_vertex(Index(i, Name(path2)),G));
idx2string.push_back(path2);
i++;
}
int s = ms[path1];
int d = ms[path2];
add_edge(verts[s], verts[d], G);
}
std::cout << "Graph G:" << std::endl;
print_graph(G, get(vertex_name, G));
typedef adjacency_list <> ADJ_LIST;
ADJ_LIST TC;
graph_traits::vertex_iterator vi, vi_end, next;
graph_traits::adjacency_iterator ai, a_end;
property_map::type
index_map = get(vertex_index, TC);
transitive_closure(G, TC);
std::cout << std::endl << "Graph G+:" << std::endl;
tie(vi, vi_end) = vertices(TC);
for (next = vi; vi != vi_end; vi = next)
{
cout << idx2string[get(index_map, *next)] << " -> ";
tie(ai, a_end) = adjacent_vertices(*next, TC);
for (; ai != a_end; ++ai)
{
cout << idx2string[get(index_map, *ai)] << " ";
}
cout << endl;
++next;
}
std::cout << std::endl;
return 0;
}
#include <boost/config.hpp>
#include <iostream>
#include <fstream>
#include <string>
#include <boost/graph/adjacency_list.hpp>
using namespace boost;
int main()
{
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 g; // use default constructor to create empty graph
property_map < graph_type, vertex_name_t >::type name_map =
get(vertex_name, g);
graph_traits < graph_type >::vertex_descriptor u1, u2;
u1 = add_vertex(g);
put(name_map, u1, "hello");
u2 = add_vertex(g);
put(name_map, u2, "hello2");
add_edge(u1, u2, g);
graph_traits < graph_type >::vertex_iterator vi, vi_end;
for (tie(vi,vi_end)=vertices(g);vi!=vi_end;++vi)
{
std::cout << get(name_map, *vi) << " ";
}
return 0;
}