C ++ Boost graph library breadth-first traversal algorithm example

Source: Internet
Author: User
// (Sort by mongokin from devonit)
# Include <boost/graph/adjacency_list.hpp>
# Include <boost/graph/breadth_first_search.hpp>
# Include <boost/pending/indirect_cmp.hpp>
# Include <boost/pending/integer_range.hpp>

# Include <iostream>

Using namespace boost;
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>
Void discover_vertex (vertex u, const graph & G) const
{
Put (m_timemap, U, m_time ++); // Add 1 for each access to m_time
}
Timemap m_timemap;
T & m_time;
};

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 };
Const char * name = "rstuvwxy ";
// Specify the edges in the graph
Typedef STD: pair <int, int> E; // edge pair
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
// VC ++ has trouble with the edge iterator constructor
Graph_t g (N );
For (std: size_t j = 0; j <n_edges; ++ j)
Add_edge (edge_array [j]. first, edge_array [j]. second, g );
# Else
Typedef graph_traits <graph_t>: vertices_size_type v_size_t;
Graph_t g (edge_array, edge_array + n_edges, v_size_t (N); // structure chart
# Endif

// Typedefs
Typedef graph_traits <graph_t>: vertex_descriptor Vertex;
Typedef graph_traits <graph_t>: vertices_size_type Size;
Typedef Size * Iiter;

// A vector to hold the discover time property for each vertex
Std: vector <Size> dtime (num_vertices (g); // access time

Size time = 0;
Bfs_time_visitor <Size *> vis (& dtime [0], time );
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 );
Integer_range <int> range (0, N );
Std: copy (range. begin (), range. end (), discover_order.begin ());
Std: sort (discover_order.begin (), discover_order.end (),
Indirect_cmp <Iiter, std: less <Size> (& dtime [0]);

Std: cout <"order of discovery :";
For (int I = 0; I <N; ++ I)
Std: cout <name [discover_order [I] <"";
Std: cout <std: endl;

Return EXIT_SUCCESS;
}

// Output:
Order of discovery: s r w v t x u y

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.