1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| #include <boost/graph/adjacency_list.hpp>
// Les structures qui contiennent les informations sur les vertices, edges et graph
struct EdgeProperties { std::string name; unsigned id; };
struct VertexProperties { ... };
struct GraphProperties { ... };
// un graph générique
typedef boost::adjacency_list
<boost::vecS, boost::vecS, boost::bidirectionalS,
boost::property<boost::vertex_bundle_t, VertexProperties>, // vertex properties
boost::property<boost::edge_bundle_t, EdgeProperties>, // edges properties
boost::no_property // graph properties (pas de propriétés pour le graph ici mais on pourrait utiliser GraphProperties aussi)
> Graph;
Graph g;
// Ajout vertex
const Graph::vertex_descriptor v1 = boost::add_vertex(VertexProperties(...), g);
// Accès à un vertex
VertexProperties& properties = get(boost::vertex_bundle, g)[v1];
// Création d'un edge
const std::pair<Graph::edge_descriptor, bool> e1 = boost::add_edge(v1, v2, EdgeProperties{...}, g);
// Accès à un edge
if (e1.second)
get(boost::edge_bundle, g)[e1.first] = EdgaData{"Edge 1", 5};
// Récupérer tous les vertices
std::pair<vertex_iterator_t, vertex_iterator_t> iterators = boost::vertices(g);
// Nombre de vertices
boost::graph_traits<Graph>::vertices_size_type s = num_vertices(g);
// Récupérer tous les edges
std::pair<edge_iterator_t, edge_iterator_t> iterators = boost::edges(g);
// Récupérer les edges partant d'un vertex
std::pair<out_edge_iterator_t, out_edge_iterator_t> iterators = boost::out_edges(v1, g);
// Récupérer les edges arrivant sur un vertex
std::pair<in_edge_iterator_t, in_edge_iterator_t> iterators = boost::in_edges(v1, g);
// récupérer les vertices adjacent à un vertex
std::pair<adjacency_iterator_t, adjacency_iterator_t> iterators = = adjacent_vertices(v1, g);
// récupérer un vertex a partir d'un edge
Graph::vertex_descriptor v1 = boost::source(e1.first, g);
Graph::vertex_descriptor v2 = boost::target(e1.first, g); |
Partager