boost bgl dijkstra算法

 #include <iostream> #include <fstream> #include <boost/config.hpp> #include <boost/graph/graph_traits.hpp> #include <boost/graph/adjacency_list.hpp> #include <boost/graph/dijkstra_shortest_paths.hpp> #include <stdlib.h> using namespace boost; //链接表 typedef adjacency_list <listS, vecS, directedS, no_property, property < edge_weight_t, int > > graph_t; //点 typedef graph_traits < graph_t >::vertex_descriptor vertex_descriptor; typedef graph_traits < graph_t >::edge_descriptor edge_descriptor; //边 typedef std::pair<int, int> Edge; #define num_nodes 9 int main(int, char *[]) { //点标记 enum nodes {A,B,C,D,E,F,G,H,L}; //点面子 意思下 char name[] = “ABCDEFGHL”; //边数组 一边2点 Edge edge_array[] = { Edge(A, C), Edge(B, G), Edge(B, D), Edge(B, E), Edge(C, B), Edge(C, D), Edge(D, E), Edge(E, A), Edge(E, B), Edge(D, H), Edge(C, G), Edge(F, H), Edge(C, L), Edge(L, D)}; //点之间的距离 int weights[] = { 1, 2, 5, 9, 11,23,17,14, 29,99,33,7, 37,2,11,49, 3,77}; //边的个数 int num_arcs = sizeof(edge_array) / sizeof(Edge); //其实上面的那些什么点名字,边标记,点距离都应该合并掉,分开写容易出错 //接下来就要加边生成图了 graph_t g(edge_array, edge_array + num_arcs, weights, num_nodes); property_map<graph_t, edge_weight_t>::type weightmap = get(edge_weight, g); std::vector<vertex_descriptor> p(num_vertices(g)); std::vector<int> d(num_vertices(g)); vertex_descriptor s = vertex(A, g); //求取最短路径 dijkstra_shortest_paths(g, s, predecessor_map(&p[0]).distance_map(&d[0])); std::cout << “distances and parents:” << std::endl; graph_traits < graph_t >::vertex_iterator vi, vend; for (tie(vi, vend) = vertices(g); vi != vend; ++vi) { std::cout << “distance(” << name[*vi] << “) = ” << d[*vi] << “, “; std::cout << “parent(” << name[*vi] << “) = ” << name[p[*vi]] << std::endl; } std::cout << std::endl; std::ofstream dot_file(“dijkstra-eg.dot”); if(dot_file.fail()==true) { std::cout<<“create file failed.”<<std::endl; } dot_file << ” digraph D {/n” << ” rankdir=LR/n” << ” size=/”4,3/”/n” << ” ratio=/”fill/”/n” << ” edge[style=/”bold/”]/n” << ” node[shape=/”circle/”]/n”; graph_traits < graph_t >::edge_iterator ei, ei_end; for (tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) { graph_traits < graph_t >::edge_descriptor e = *ei; graph_traits < graph_t >::vertex_descriptor u = source(e, g), v = target(e, g); dot_file << name[u] << ” -> ” << name[v] << “[label=/”” << get(weightmap, e) << “/””; if (p[v] == u) dot_file << “, color=/”black/””; else dot_file << “, color=/”grey/””; dot_file << “]”; } dot_file << “}”; dot_file.close(); _sleep(111101); return EXIT_SUCCESS; }

 

修改过的

#include <iostream> #include <vector> #include <string> #include <algorithm> #include <boost/config.hpp> #include <boost/tuple/tuple.hpp> #include <boost/graph/graph_traits.hpp> #include <boost/graph/adjacency_list.hpp> #include <boost/graph/dijkstra_shortest_paths.hpp> #include <stdlib.h> #include “gtrinode.hpp” using namespace std; using namespace boost; using namespace g::gstl; //链接表 typedef adjacency_list <listS, vecS, directedS, no_property, property < edge_weight_t, int > > graph_t; //点 typedef graph_traits < graph_t >::vertex_descriptor vertex_descriptor; typedef graph_traits < graph_t >::edge_descriptor edge_descriptor; //边 typedef std::pair<int, int> Edge; #define num_nodes 7 vector<trinode<string,int,string> > vect_path; int get_dijkstra_path(string from, string to, vector<string> &paht_list); template<typename LeftType, typename MidType, typename RightType> class trinode_find_left { public: trinode_find_left(LeftType _t1):t1(_t1){} bool operator()(const trinode<LeftType,MidType,RightType> &node) { return(t1 == node.lvalue); } private: LeftType t1; }; template<typename LeftType, typename MidType, typename RightType> class trinode_find_mid { public: trinode_find_mid(MidType _t1):t1(_t1){} bool operator()(const trinode<LeftType,MidType,RightType> &node) { return (t1 == node.mvalue); } private: MidType t1; }; template<typename LeftType, typename MidType, typename RightType> class trinode_find_right { public: trinode_find_right(RightType _t1):t1(_t1){} bool operator()(const trinode<LeftType,MidType,RightType> &node) { return(t1 == node.rvalue); } private: RightType t1; }; int main(int, char *[]) { //点标记 enum nodes {A,B,C,D,E,F,G,H}; //点面子 意思下 string name[] = { “西安”,”北京”,”成都”,”广东”, “天津”,”汉中”,”上海”,”深圳”}; //边数组 一边2点 Edge edge_array[] = { Edge(A, C), Edge(B, G), Edge(B, D), Edge(B, E), Edge(C, B), Edge(C, D), Edge(D, E), Edge(E, A), Edge(E, B), Edge(C, G), Edge(D, H), Edge(H, A)}; //点之间的距离 int weights[] = { 1, 2, 5, 9, 11,23,17, 14, 29,99, 6, 12}; //边的个数 int num_arcs = sizeof(edge_array) / sizeof(Edge); //接下来就要加边生成图了 graph_t g(edge_array, edge_array + num_arcs, weights, num_nodes); property_map<graph_t, edge_weight_t>::type weightmap = get(edge_weight, g); std::vector<vertex_descriptor> p(num_vertices(g)); std::vector<int> d(num_vertices(g)); vertex_descriptor s = vertex(A, g); //求取最短路径 dijkstra_shortest_paths(g, s, predecessor_map(&p[0]).distance_map(&d[0])); //std::cout << “distances and parents:” << std::endl; graph_traits < graph_t >::vertex_iterator vi, vend; for (tie(vi, vend) = vertices(g); vi != vend; ++vi) { std::cout<<“distance(“<<name[*vi]<<“)=”<<d[*vi]<<“,”<<name[p[*vi]]<<std::endl; trinode<string,int,string> trin(name[*vi].c_str(), d[*vi], name[p[*vi]].c_str()); vect_path.push_back(trin); } //system(“pause”); vector<string> path_list; cout<<endl; cout<<“aimed: “<<get_dijkstra_path(“西安”, “天津”,path_list)<<endl; copy(path_list.begin(),path_list.end(),ostream_iterator<string>(cout,”/n”)); system(“pause”); return EXIT_SUCCESS; } int get_dijkstra_path(string from, string to, vector<string> &path_list) { int value = -1; path_list.clear(); vector<trinode<string,int,string> >::iterator itr = vect_path.begin(); while(itr != vect_path.end()) { if((*itr).lvalue.compare(to)==0) value = (*itr).mvalue; itr ++; } string curnode(to); path_list.push_back(to); while(1) { if(curnode == from) break; else { vector<trinode<string,int,string> >::iterator itr = vect_path.begin(); while(itr != vect_path.end()) { if((*itr).lvalue == curnode) { curnode = (*itr).rvalue; path_list.push_back(curnode); break; } itr ++; } } } reverse(path_list.begin(),path_list.end()); return value; }

    原文作者:Dijkstra算法
    原文地址: https://blog.csdn.net/ccsdu2004/article/details/3868648
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞