进来看就正常了。。小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;
}