当前位置: 首页 > news >正文

【图论实战】 Boost学习 03:dijkstra_shortest_paths

文章目录

  • 示例
  • 代码

示例

最短路径: A -> C -> D -> F -> E -> G 长度 16

代码

#include <iostream> 
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include <boost/graph/graphviz.hpp>
#include <boost/graph/properties.hpp>
#include <boost/property_map/property_map.hpp>
#include <boost/graph/named_function_params.hpp>
#include <vector>
#include <string>
using namespace std;
using namespace boost;struct Node{string id_;
};
struct EdgeProperty{int id_;int weight_;EdgeProperty(int id,int weight){id_=id;weight_=weight;}
};
typedef boost::adjacency_list < boost::listS, boost::vecS, boost::undirectedS, Node, boost::property <boost::edge_weight_t, unsigned long>> graph_t;
typedef boost::graph_traits <graph_t>::vertex_descriptor vertex_descriptor;
typedef boost::graph_traits <graph_t>::edge_descriptor edge_descriptor;
enum {A, B, C, D, E, F,G };
string vtx[]={"A","B","C","D","E","F","G"};/* 获取路径经过结点的信息 */
void GetPath(int fromId,int toId,vector<vertex_descriptor>& vPredecessor,std::string& strPath){vector<int> vecPath;while(fromId!=toId){vecPath.push_back(toId);//因为本例子的特殊性和自己很懒,所以可以直接取值toId = vPredecessor[toId];}vecPath.push_back(toId);	vector<int>::reverse_iterator pIter = vecPath.rbegin();strPath="路径:";std::string strOperator="->";string c[20]={};for(;pIter!=vecPath.rend();pIter++){// sprintf(c,"%c",vtx[*pIter]);if(*pIter!=fromId){strPath+=(strOperator+vtx[*pIter]);}else{strPath+=vtx[*pIter];}}
}int main()
{/* modify vertex */graph_t g(7);for(int i=0;i< boost::num_vertices(g); i++){g[i].id_=vtx[i];}/* modify edge and weight */typedef std::pair<int, int> Edge;	boost::property_map<graph_t,edge_weight_t>::type weightmap= get(boost::edge_weight_t(), g);Edge edge_array[] = { Edge(A,B), Edge(A,C), Edge(B,C), Edge(B,D),Edge(B,E),Edge(C,D), Edge(D,F), Edge(E,F),Edge(E,G),Edge(F,G)};int weight[]={2,5,4,6,10,2,1,3,5,9};						  int num_edges = sizeof(edge_array)/sizeof(edge_array[0]);					for (int i = 0; i < num_edges; ++i){auto ed=add_edge(edge_array[i].first, edge_array[i].second, g);boost::put(boost::edge_weight_t(), g, ed.first,weight[i]);}/* 路径计算结果定义*///存储从起始结点到其他结点的路径上经过的最后一个中间结点序号vector<vertex_descriptor> vPredecessor(boost::num_vertices(g));//存储起始结点到其他结点的路径的距离vector<unsigned long> vDistance(boost::num_vertices(g)); /*路径探索起始点定义*/vertex_descriptor s = boost::vertex(0, g); boost::property_map<graph_t, boost::vertex_index_t>::type pmpIndexmap = boost::get(boost::vertex_index, g);boost::dijkstra_shortest_paths(g, // IN: 图s, // IN: 起始点&vPredecessor[0], // OUT:存储从起始结点到其他结点的路径上经过的最后一个中间结点序号&vDistance[0], 	 // UTIL/OUT:存储起始结点到其他结点的路径的距离weightmap, 	 	 // IN: 权重矩阵pmpIndexmap,		 // IN:std::less<unsigned long>(), // IN: 对比函数boost::closed_plus<unsigned long>(), // IN: 用来计算路径距离的混合函数std::numeric_limits<unsigned long>::max(), // IN: 距离最大值 0, boost::default_dijkstra_visitor()); // OUT:指定每个点所运行的动作std::string strPath;GetPath(0,6,vPredecessor,strPath);cout<<strPath<<endl;cout<<"路径长度:"<<vDistance[6]<<endl;boost::dynamic_properties dp;dp.property("node_id", get(boost::vertex_index, g));dp.property("label",  get(boost::edge_weight,  g));ofstream outf("min.gv");write_graphviz_dp(outf, g,dp);return 0;
}
// named parameter version
template <typename Graph, typename P, typename T, typename R>
void dijkstra_shortest_paths(Graph& g,typename graph_traits<Graph>::vertex_descriptor s,const bgl_named_params<P, T, R>& params);// non-named parameter version
template <typename Graph, typename DijkstraVisitor, typename PredecessorMap, typename DistanceMap,typename WeightMap, typename VertexIndexMap, typename CompareFunction, typename CombineFunction, typename DistInf, typename DistZero, typename ColorMap = default>
void dijkstra_shortest_paths(const Graph& g,typename graph_traits<Graph>::vertex_descriptor s, PredecessorMap predecessor, DistanceMap distance, WeightMap weight, VertexIndexMap index_map,CompareFunction compare, CombineFunction combine, DistInf inf, DistZero zero,DijkstraVisitor vis, ColorMap color = default)// version that does not initialize the property maps (except for the default color map)
template <class Graph, class DijkstraVisitor,class PredecessorMap, class DistanceMap,class WeightMap, class IndexMap, class Compare, class Combine,class DistZero, class ColorMap>
void dijkstra_shortest_paths_no_init(const Graph& g,typename graph_traits<Graph>::vertex_descriptor s,PredecessorMap predecessor, DistanceMap distance, WeightMap weight,IndexMap index_map,Compare compare, Combine combine, DistZero zero,DijkstraVisitor vis, ColorMap color = default);

相关文章:

  • 数据库MHA高可用
  • 浪潮服务器安装操作系统
  • 【iOS】JSONModel的基本使用
  • PyCharm鼠标控制字体缩放
  • 阿里云centos7.9乱码问题
  • 【C++】——运算符重载
  • mysql主从复制-使用心得
  • 图片批量编辑器,高效拼接多张图片,释放无限创意!
  • promise多请求并发
  • 【01】Istio-1.17 部署
  • 活动通知邀请函H5页面制作源码系统+动感的背景音乐 自定义你想要的页面 源码完全开源可二开 带完整搭建教程
  • Banana Pi BPI-M5 Boot Log 导出说明
  • 适合孩子写作业的台灯?精选专业的读写台灯
  • 2023nacos源码解读第2集——nacos-server的启动
  • 腾讯待办下架,待办事项提醒怎么设置?
  • CSS 三角实现
  • Java|序列化异常StreamCorruptedException的解决方法
  • Node项目之评分系统(二)- 数据库设计
  • python学习笔记-类对象的信息
  • Vue ES6 Jade Scss Webpack Gulp
  • 浮动相关
  • 关于 Linux 进程的 UID、EUID、GID 和 EGID
  • 关于字符编码你应该知道的事情
  • 后端_ThinkPHP5
  • 开年巨制!千人千面回放技术让你“看到”Flutter用户侧问题
  • 巧用 TypeScript (一)
  • 使用Envoy 作Sidecar Proxy的微服务模式-4.Prometheus的指标收集
  • 问题之ssh中Host key verification failed的解决
  • 学习笔记DL002:AI、机器学习、表示学习、深度学习,第一次大衰退
  • 以太坊客户端Geth命令参数详解
  • ​iOS实时查看App运行日志
  • ​决定德拉瓦州地区版图的关键历史事件
  • "无招胜有招"nbsp;史上最全的互…
  • $HTTP_POST_VARS['']和$_POST['']的区别
  • (07)Hive——窗口函数详解
  • (八)Spring源码解析:Spring MVC
  • (八十八)VFL语言初步 - 实现布局
  • (补)B+树一些思想
  • (附源码)计算机毕业设计SSM在线影视购票系统
  • (官网安装) 基于CentOS 7安装MangoDB和MangoDB Shell
  • (九)c52学习之旅-定时器
  • (删)Java线程同步实现一:synchronzied和wait()/notify()
  • (原創) 如何動態建立二維陣列(多維陣列)? (.NET) (C#)
  • (转) SpringBoot:使用spring-boot-devtools进行热部署以及不生效的问题解决
  • (转载)虚幻引擎3--【UnrealScript教程】章节一:20.location和rotation
  • .babyk勒索病毒解析:恶意更新如何威胁您的数据安全
  • .bat批处理(五):遍历指定目录下资源文件并更新
  • .bat批处理出现中文乱码的情况
  • .NET Core MongoDB数据仓储和工作单元模式封装
  • .NET/C# 中设置当发生某个特定异常时进入断点(不借助 Visual Studio 的纯代码实现)
  • .NET开发者必备的11款免费工具
  • .NET开源项目介绍及资源推荐:数据持久层
  • @Repository 注解
  • @RequestMapping处理请求异常
  • @serverendpoint注解_SpringBoot 使用WebSocket打造在线聊天室(基于注解)