DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> AJAX入門 >> AJAX詳解 >> C++ Boost graph 深度(廣度)優先算法示例
C++ Boost graph 深度(廣度)優先算法示例
編輯:AJAX詳解     
//整理 by  RobinKin 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 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. Whether 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;

//到達結點時 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;
};

//顯示鄰接結點時 。調用 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;
};

//某結點的所有鄰接結點都已經被訪問過時  調用 Operator()

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;

//第 1 2 3 個參數分別是 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;
}


//輸出:
*** 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

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved