boost graph libaray: my own note(1)

进来看就正常了。。小BS下CSDN博客。。。

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/depth_first_search.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/topological_sort.hpp>
#include <boost/graph/breadth_first_search.hpp>
#include <string>
#include <utility>
#include <iostream>
#include <iterator>
#include <deque>

using namespace std;
using namespace boost;

typedef property<vertex_name_t, string> VertexNameProperty;
typedef adjacency_list<vecS, vecS, directedS, VertexNameProperty>  MyGraph;
typedef graph_traits<MyGraph>::vertex_iterator VertexIterator;
typedef pair<VertexIterator, VertexIterator> VerIterPair;
typedef graph_traits<MyGraph>::edge_descriptor Edge;
typedef graph_traits<MyGraph>::edge_iterator EdgeIter;
typedef property_map<MyGraph, vertex_index_t>::type MapIndex;
typedef property_map<MyGraph, vertex_name_t>::type NameArray;
typedef graph_traits<MyGraph>::vertex_descriptor Vertex;
typedef deque<Vertex> VertexArray;
typedef back_insert_iterator<VertexArray> BackIter;

template<class Name>
struct GetName
{
 const Name& name;
 GetName(const NameArray& name):name(name){}
 string operator()(int i) const
 {
  return name[i];
 }
};

template <typename OutputIterator>
class MyBFSVisitor : public default_bfs_visitor
{
public:
 MyBFSVisitor(OutputIterator iter) : iter(iter){}
 OutputIterator iter;
 template < typename Vertex, typename Graph >
  void discover_vertex(Vertex u, const Graph & g)
 {
  *iter++=u;
 }
};

template<class Graph, class Name>
void VisitAllVertex(const Graph& graph, const Name& name)
{
 cout<<"--------Visit all vertex--------"<<endl;
 MapIndex index = get(vertex_index, graph);
 for(VerIterPair vp=vertices(graph); vp.first!=vp.second; ++vp.first){
  cout<<"Vertex "<<index[*vp.first]<<":"<<name[*vp.first]<<endl;
 }
}

template<class Graph>
void VisitAllEdge(const Graph& graph)
{
 cout<<"--------Visit all edge--------"<<endl;
 EdgeIter first, last;
 for(tie(first, last)=edges(graph); first!=last; ++first){
  cout<<"Edge: "<<*first<<endl;
 }
}

template<class Graph, class Name>
void TopologicalSort(const Graph& graph, const Name& name)
{
 cout<<"--------Topological sort--------"<<endl;
 VertexArray vertexs;
 topological_sort(graph, front_insert_iterator<VertexArray>(vertexs) );
 transform(vertexs.begin(), vertexs.end(),
  ostream_iterator<string>(cout, "/n"),
  GetName<Name>(name) );
}

template<class Graph, class Vertex, class Name>
void BFS(const Graph& graph, const Vertex& v0, const Name& name)
{
 cout<<"--------BFS--------"<<endl;
 VertexArray vertexs;
 MyBFSVisitor<BackIter> vis=BackIter(vertexs);
 breadth_first_search(graph, v0, visitor(vis) );
 transform(vertexs.begin(), vertexs.end(),
  ostream_iterator<string>(cout, "/n"),
  GetName<Name>(name) );
}

int main(int argc,char* argv[])
{
 //Graph Construction
 MyGraph graph;
 NameArray name=get(vertex_name, graph);

 Vertex v0, v1, v2, v3, v4, v5;
 v0=add_vertex(graph);
 v1=add_vertex(graph);
 v2=add_vertex(graph);
 v3=add_vertex(graph);
 v4=add_vertex(graph);
 v5=add_vertex(graph);
 name[v0]="起床";
 name[v1]="看书";
 name[v2]="吃饭";
 name[v3]="午睡";
 name[v4]="游泳";
 name[v5]="上网";

 Edge e1=(add_edge(v0, v1, graph)).first;
 Edge e2=(add_edge(v0, v2, graph)).first;
 Edge e3=(add_edge(v0, v5, graph)).first;
 Edge e4=(add_edge(v1, v3, graph)).first;
 Edge e5=(add_edge(v2, v4, graph)).first;
 Edge e6=(add_edge(v3, v4, graph)).first;
 Edge e7=(add_edge(v4, v5, graph)).first;

 VisitAllVertex(graph, name);
 VisitAllEdge(graph);
 TopologicalSort(graph, name);
 BFS(graph, v0, name);

 system("pause");
 return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值