Example of C ++ Boost graph depth (breadth) Priority Algorithm

Source: Internet
Author: User

// Sort by mongokin from DevonIT. inc
# Include <boost/config. hpp>
# Include <iostream>
# Include <vector>
# Include <string>
# Include <boost/graph/adjacency_list.hpp>
# Include <boost/graph/depth_first_search.hpp>
# Include <boost/graph/breadth_first_search.hpp>
# Include <boost/property_map.hpp>
# Include <boost/graph/graph_utility.hpp> // for boost: make_list

/*
Example of using a visitor with the depth first search
And breadth first search algorithm

Sacramento ---- Reno ---- Salt Lake City
|
San Francisco
|
San Jose ---- Fresno
|
Los Angeles ---- Los Vegas ---- Pheonix
|
San Diego

The visitor has three main functions:

Discover_vertex (u, g) is invoked when the algorithm first arrives at
Vertex u. This will happen in the depth first or breadth first
Order depending on which algorithm you use.

Examine_edge (e, g) is invoked when the algorithm first checks an edge to see
Whether it has already been there. Whether using BFS or DFS, all
The edges of vertex u are examined immediately after the call
Visit (u ).

Finish_vertex (u, g) is called when after all the vertices reachable from vertex
U have already been visited.

*/

Using namespace std;
Using namespace boost;

// Operator ()

Struct city_arrival: public base_visitor <city_arrival>
{
City_arrival (string * n): names (n ){}
Typedef on_discover_vertex event_filter;
Template <class Vertex, class Graph>
Inline void operator () (Vertex u, Graph &){
Cout <endl <"arriving at" <names [u] <endl
<"Neighboring cities are :";
}
String * names;
};

// Display the adjacent points. Call operator ()
Struct neighbor_cities: public base_visitor <neighbor_cities>
{
Neighbor_cities (string * n): names (n ){}
Typedef on_examine_edge event_filter;
Template <class Edge, class Graph>
Inline void operator () (Edge e, Graph & g ){
Cout <names [target (e, g)] <",";
}
String * names;
};

// When all the adjacent nodes of a node have been accessed, operator () is called ()

Struct finish_city: public base_visitor <finish_city>
{
Finish_city (string * n): names (n ){}
Typedef on_finish_vertex event_filter;
Template <class Vertex, class Graph>
Inline void operator () (Vertex u, Graph &){
Cout <endl <"finished with" <names [u] <endl;
}
String * names;
};

Int main (int, char * [])
{

Enum {SanJose, SanFran, LA, SanDiego, Fresno, LosVegas, Reno,
Sacramento, SaltLake, Pheonix, N };

String names [] = {"San Jose", "San Francisco", "San Jose ",
"San Francisco", "Los Angeles", "San Diego ",
"Fresno", "Los Vegas", "Reno", "Sacramento ",
"Salt Lake City", "Pheonix "};

Typedef std: pair <int, int> E;
E edge_array [] = {E (Sacramento, Reno), E (Sacramento, SanFran ),
E (Reno, SaltLake ),
E (SanFran, SanJose ),
E (SanJose, Fresno), E (SanJose, LA ),
E (LA, LosVegas), E (LA, SanDiego ),
E (LosVegas, Pheonix )};

/* Create the graph type we want .*/
Typedef adjacency_list <vecS, vecS, undirectedS> Graph;

Graph G (edge_array, edge_array + sizeof (edge_array)/sizeof (E), N );

Cout <"*** Depth First ***" <endl;

// 1st 2. The three parameters are arrival neighbor finish.
Depth_first_search
(G,
Visitor (make_dfs_visitor (boost: make_list (city_arrival (names ),
Neighbor_cities (names ),
Finish_city (names )))));
Cout <endl;

/* Get the source vertex */
Boost: graph_traits <Graph >:: vertex_descriptor
S = vertex (SanJose, G );

Cout <"*** Breadth First ***" <endl;
Breadth_first_search
(G, s, visitor (make_bfs_visitor (boost: make_list (city_arrival (names ),
Neighbor_cities (names ),
Finish_city (names )))));

Return 0;
}

// Output:
* ** Depth First ***

Arriving at San Jose
Neighboring cities are: San Francisco,
Arriving at San Francisco
Neighboring cities are: Los Vegas,
Arriving at Los Vegas
Neighboring cities are: Fresno,
Arriving at Fresno
Neighboring cities are: Los Vegas, Reno,
Arriving at Reno
Neighboring cities are: Fresno,
Finished with Reno

Finished with Fresno
San Francisco,
Finished with Los Vegas
San Jose,
Finished with San Francisco
Los Angeles,
Arriving at Los Angeles
Neighboring cities are: San Jose,
Finished with Los Angeles

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.