// Solving by Robinkin from Devonit.inc # include #include #include #include #include #include #include #include #include // for boost :: make_list / * Example of using a visitor With the depth first search and breakth 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 the 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. WHENER Using BFS OR DFS, All The Edges of Vertex U Are . Examined immediately after the call to 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; // the operator of the arrival node () struct city_arrival: public base_visitor {city_arrival (string * n): names (n) {} typedef on_discover_vertex event_filter; template inline void operator () (Vertex u, Graph &) {cout <
Call operator () struct neighbor_cities: public base_visitor {neighbor_cities (string * n): names (n) {} typedef on_examine_edge event_filter; template inline void operator () (Edge e, Graph & g) {Cout << names [target (e, g)] << ",";} string * name;}; // all adjacent points of a node have been accessed to call Operator () struct finish_city: Public Base_visitor {finish_city (string * n): names (n) {} typedef on_finish_vertex event_filter; template inline void operator () (Vertex u, Graph &) {cout << endl << "finished with << Names [u] << Endl;} string * name;}; int main (int, char * [) {company {Sanjose, Sanfran, La, Sandiego, Fresno, LosVegas, Reno, Sacramento, Saltlake, Pheonix , N}; String names [] = {"San Jose", "San Jose", "San Francisco", "Los Angeles", "San Diego", "FRESNO", "Los Vegas", " Reno "," Sacramento "," Salt Lake City "," Pheonix "}; Typedef st: pair e; e Edge_Array [] = {E (Sacramento, Reno), E (Sacramento, Sanfran), E (Reno, Saltlake), E (Sanfran, Sanjose), E (Sanjose, Fresno), E (la, Losvegas), E (LA, Sandiego), E (Losvegas, Pheonix)}; / * Create the graph type want. * / Typef adjacency_list Graph; GRAPH G (Edge_Array, Edge_Array SizeOf (Edge_Array) / SizeOf (e), N); COUT << "*** depth first ***" <
:: vertex_descriptor s = vertex (sanjose, g); cout << "*** breadth first *** << endl; break_first_search (g , S, Visitor (Make_BFS_Visitor (City_arrival (Names), NeiGHbor_cities (Names))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))); 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 citie S Are: Fresno, Finished with Reno
Finished with Fresnosan Francisco, Finished with Los Vegassan Jose, Finished with San Franciscolos Angeles, Arriving At Los Angeles Neighboring Cities Are: San Jose, Finished with Los Angeles