Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <boost/config.hpp>
- #include <iostream> // for std::cout
- #include <utility> // for std::pair
- #include <algorithm> // for std::for_each
- #include <boost/utility.hpp> // for boost::tie
- #include <boost/graph/adjacency_list.hpp>
- #include <boost/graph/graphviz.hpp>
- #include <boost/range/begin.hpp>
- #include <boost/range/end.hpp>
- using namespace boost;
- #include <boost/range/iterator_range.hpp>
- #include <type_traits>
- template<class T> using Invoke = typename T::type
- template<class T> using RemoveRef = Invoke<std::remove_reference>;
- template<class G> using OutEdgeIterator = typename boost::graph_traits<G>::out_edge_iterator;
- template<class V, class G>
- auto out_edges_range(V&& v, G&& g)
- -> boost::iterator_range<OutEdgeIterator<RemoveRef<G>>>
- {
- auto edge_pair = out_edges(std::forward<V>(v), std::forward<G>(g));
- return boost::make_iterator_range(edge_pair.first, edge_pair.second);
- }
- template <class Graph> struct exercise_vertex {
- exercise_vertex(Graph& g_, const char name_[]) : g(g_),name(name_) { }
- typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
- void operator()(const Vertex& v) const
- {
- using namespace boost;
- typename property_map<Graph, vertex_index_t>::type
- vertex_id = get(vertex_index, g);
- std::cout << "vertex: " << name[get(vertex_id, v)] << std::endl;
- std::cout << "\tout-edges: ";
- /*for (auto &e : out_edges(v, g)){
- std::cout<<"!";
- }
- //*/
- typename boost::graph_traits<Graph>::out_edge_iterator out_i, out_end;
- typename boost::graph_traits<Graph>::edge_descriptor e;
- for (std::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 << "(" << name[get(vertex_id, src)]
- << "," << name[get(vertex_id, targ)] << ") ";
- }
- std::cout << std::endl;
- }
- Graph& g;
- const char *name;
- };
- int main(int,char*[])
- {
- typedef adjacency_list<vecS, vecS, bidirectionalS,
- no_property, property<edge_weight_t, float> > Graph;
- enum { A, B, C, D, E, N };
- const int num_vertices = N;
- const char name[] = "ABCDE";
- 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), };
- const int num_edges = sizeof(edge_array)/sizeof(edge_array[0]);
- float transmission_delay[] = { 1.2, 4.5, 2.6, 0.4, 5.2, 1.8, 3.3, 9.1 };
- Graph g(edge_array, edge_array + num_edges,
- transmission_delay, num_vertices);
- std::for_each(vertices(g).first, vertices(g).second,
- exercise_vertex<Graph>(g, name));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement