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

news/2025/3/12 9:27:12/

文章目录

  • 示例
  • 代码

示例

最短路径: 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);

http://www.ppmy.cn/news/1210161.html

相关文章

【Python】11 Conda常用命令

Conda简介 Conda是一个开源的软件包管理系统和环境管理器&#xff0c;用于安装和管理不同语言的软件包&#xff0c;如Python、R等。它可以创建独立的环境&#xff0c;每个环境都可以安装特定版本的软件包和依赖项&#xff0c;而不必担心与其他环境冲突。Conda还可以轻松地在不…

Java架构师分布式搜索词库解决方案

目录 1 IK分词器字典热加载实现思路2 分析IK分词器的配置3 基于MySQL更新字典的实现4 常见报错4.1 java.lang.ExceptionInInitializerError: null …access denied (“java.lang.RuntimePermission” “setContextClassLoader”)4.2 java.sql.SQLNonTransientConnectionExcepti…

StartUML的基本使用

文章目录 简介和安装创建包创建类视图时序图 简介和安装 最近在学习一个项目的时候用到了StartUML来构造项目的类图和时序图 虽然vs2019有类视图&#xff0c;但是也不是很清晰&#xff0c;并没有生成uml图&#xff0c;但是宇宙最智能的IDE IDEA有生成uml图的功能 下面就简单介…

【CASS精品教程】cass3d基于DOM和DEM生成倾斜三维模型

和EPS一样&#xff0c;cass3d也可以生成三维模型。本文讲解 cass3d基于pix4d生成的正射影像DOM和DSM生成倾斜三维模型&#xff0c;并进行三维测图。 一、三维倾斜模型打开 打开cass11.0软件&#xff0c;打开三维窗口&#xff0c;点击打开模型&#xff0c;选择基于dom和dsm生成…

Linux的基本指令(1)

目录 快速认识的几个指令 pwd指令 mkdir指令 touch指令 cd指令 clear指令 whoami指令 ls指令 ls -l ls -la ls 目录名 ls -ld 目录名 文件 路径 路径是什么&#xff1f; 路径的形成 ​ 怎么保证路径必须有唯一性&#xff1f; ls -la隐藏文件 隐藏文件的是什…

C复习-函数指针+字符串常量

参考&#xff1a; 里科《C和指针》 指针热身 int *f(); // ()优先级高于*&#xff0c;所以f是一个函数&#xff0c;返回int指针 int (*f)(); // f是一个函数指针&#xff0c;它指向的函数返回一个int值 int *(*f)(); // f是一个函数指针&#xff0c;它指向的函数返回一个int指…

ubuntu上如何移植thttpd

thttpd的特点 thttpd 是一个简单、小巧、便携、快速且安全的 HTTP 服务器。 简单&#xff1a; 它只处理实现 HTTP/1.1 所需的最低限度。好吧&#xff0c;也许比最低限度多一点。 小&#xff1a; 请参阅比较图表。它还具有非常小的运行时大小&#xff0c;因为它不会分叉并且非…

ES6 部分新特性使用

箭头函数 // 箭头函数定义 const add (a, b) > a b; console.log(add(1, 2)); // 输出3// 箭头函数表达式 const nums [1, 2, 3]; const sum nums.reduce((total, num) > total num, 0); console.log(sum); // 输出6 模板字符串 // 使用模板字符串拼接字符串 con…