The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.
// boost_demo_v01.cpp : Definiert den Einstiegspunkt fÃƒÂ¼r die Konsolenanwendung.
//
#include "stdafx.h"
#include <iostream> // for std::cout
#include <utility> // for std::pair
#include <algorithm> // for std::for_each
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
using namespace boost;
template <class Graph> struct exercise_vertex {
//...
void operator()(const Vertex& v) const
{
typedef graph_traits<Graph> GraphTraits;
typename property_map<Graph, vertex_index_t>::type
index = get(vertex_index, g);
std::cout << "out-edges: ";
typename GraphTraits::out_edge_iterator out_i, out_end;
typename GraphTraits::edge_descriptor e;
for (tie(out_i, out_end) = out_edges(v, g);
out_i != out_end; ++out_i) {
e = *out_i;
Vertex src = source(e, g), targ = target(e, g);
std::cout << "(" << index[src] << ","
<< index[targ] << ") ";
}
std::cout << std::endl;
//...
}
//...
};
int _tmain(int argc, _TCHAR* argv[])
{
/*Constructing a Graph
In this example we will use the BGL adjacency_list class to demonstrate the main ideas in the
BGL interface. The adjacency_list class provides a generalized version of the classic "adjacency list"
data structure. The adjacency_list is a template class with six template parameters, though here we only fill
in the first three parameters and use the defaults for the remaining three. The first two template arguments (
vecS, vecS) determine the data structure used to represent the out-edges for each vertex in the graph and
the data structure used to represent the graph's vertex set (see section Choosing the Edgelist and VertexList
for information about the tradeoffs of the different data structures). The third argument, bidirectionalS,
selects a directed graph that provides access to both out and in-edges. The other options for the third
argument are directedS which selects a directed graph with only out-edges, and undirectedS which selects
an undirected graph.
Once we have the graph type selected, we can create the graph in Figure 2 by declaring a graph object
and filling in edges using the add_edge() function of the MutableGraph interface (which adjacency_list implements).
We use the array of pairs edge_array merely as a convenient way to explicitly create the edges for this example.*/
// create a typedef for the Graph type
typedef adjacency_list<vecS, vecS, bidirectionalS> Graph;
// Make convenient labels for the vertices
enum { A, B, C, D, E, N };
const int num_vertices = N;
const char* name = "ABCDE";
// writing out the edges in the graph
typedef std::pair<int, int> Edge;
Edge edge_array[] =
{ Edge(A,B), Edge(A,D), Edge(C,A), Edge(D,C),
Edge(C,E), Edge(B,D), Edge(D,E),Edge(5,5),Edge(A,5), Edge(C,7), Edge(B,7), Edge(D,8),Edge(5,8),Edge(9,5) ,Edge(9,8),Edge(9,8)};
const int num_edges = sizeof(edge_array)/sizeof(edge_array[0]);
//
std::cout << "Version 01.02.03 \n\n";
// declare a graph object
Graph g(num_vertices);
// add the edges to the graph object
for (int i = 0; i < num_edges; ++i)
add_edge(edge_array[i].first, edge_array[i].second, g);
//Accessing the Vertex Set
//Now that we have created a graph, we can use the graph interface to access the graph data in different ways.
//First we can access all of the vertices in the graph using the vertices() function of the VertexListGraph
//interface. This function returns a std::pair of vertex iterators (the first iterator points to the "beginning"
//of the vertices and the second iterator points "past the end"). Dereferencing a vertex iterator gives a
//vertex object. The type of the vertex iterator is given by the graph_traits class. Note that different
//graph classes can have different associated vertex iterator types, which is why we need the graph_traits class.
//Given some graph type, the graph_traits class will provide access to the vertex_iterator type.
//
//The following example prints out the index for each of the vertices in the graph. All vertex and edge properties,
//including index, are accessed via property map objects. The property_map class is used to obtain the property map
//type for a specific property (specified by vertex_index_t, one of the BGL predefined properties) and function
//call get(vertex_index, g) returns the actual property map object.
typedef property_map<Graph, vertex_index_t>::type IndexMap;
IndexMap index = get(vertex_index, g);
std::cout << "vertices(g) = ";
typedef graph_traits<Graph>::vertex_iterator vertex_iter;
std::pair<vertex_iter, vertex_iter> vp;
for (vp = vertices(g); vp.first != vp.second; ++vp.first)
std::cout << index[*vp.first] << " ";
std::cout << std::endl;
//Accessing the Edge Set
//The set of edges for a graph can be accessed with the edges() function of the EdgeListGraph interface.
//Similar to the vertices() function, this returns a pair of iterators, but in this case the iterators
//are edge iterators. Dereferencing an edge iterator gives an edge object. The source() and target()
//functions return the two vertices that are connected by the edge. Instead of explicitly creating a std::pair
//for the iterators, this time we will use the tie() helper function. This handy function can be used to assign
//the parts of a std::pair into two separate variables, in this case ei and ei_end. This is usually more convenient
//than creating a std::pair and is our method of choice for the BGL.
std::cout << "edges(g) = ";
graph_traits<Graph>::edge_iterator ei, ei_end;
for (tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)
std::cout << "(" << index[source(*ei, g)]
<< "," << index[target(*ei, g)] << ") ";
std::cout << std::endl;
// -----------------
// The Adjacency Structure
// will encapsulate this in an "exercise vertex" function, and apply it to each vertex in the graph.
// std::for_each(vertices(g).first, vertices(g).second, exercise_vertex<Graph>(g));
std::for_each(vertices(g).first, vertices(g).second,
exercise_vertex<Graph>(g));
system ("pause");
return 0;
}
//
// Header File for the Template
// filename : Template_Header.h
// date : 22-11-2007
//
template <class Graph> struct exercise_vertex { void operator()(const Vertex& v) const };
http://www.boost.org/libs/graph/example/quick_tour.cpp
If you are experiencing a similar issue, please ask a related question
Title | # Comments | Views | Activity |
---|---|---|---|
Getting IP address | 8 | 79 | |
Grammars for C C++ and java | 1 | 122 | |
Printing the elements of a set declared inside a map in C++. | 3 | 47 | |
c++ syntax question | 9 | 48 |
Join the community of 500,000 technology professionals and ask your questions.