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

文章目录

示例


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

代码

cpp 复制代码
#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;
}
cpp 复制代码
// 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);
相关推荐
量子-Alex1 小时前
【多模态聚类】用于无标记视频自监督学习的多模态聚类网络
学习·音视频·聚类
吉大一菜鸡1 小时前
FPGA学习(基于小梅哥Xilinx FPGA)学习笔记
笔记·学习·fpga开发
爱吃西瓜的小菜鸡3 小时前
【C语言】判断回文
c语言·学习·算法
小A1594 小时前
STM32完全学习——SPI接口的FLASH(DMA模式)
stm32·嵌入式硬件·学习
岁岁岁平安4 小时前
spring学习(spring-DI(字符串或对象引用注入、集合注入)(XML配置))
java·学习·spring·依赖注入·集合注入·基本数据类型注入·引用数据类型注入
武昌库里写JAVA4 小时前
Java成长之路(一)--SpringBoot基础学习--SpringBoot代码测试
java·开发语言·spring boot·学习·课程设计
qq_589568104 小时前
数据可视化echarts学习笔记
学习·信息可视化·echarts
兔C5 小时前
微信小程序的轮播图学习报告
学习·微信小程序·小程序
海海不掉头发5 小时前
苍穹外卖-day05redis 缓存的学习
学习·缓存
小木_.6 小时前
【Python 图片下载器】一款专门为爬虫制作的图片下载器,多线程下载,速度快,支持续传/图片缩放/图片压缩/图片转换
爬虫·python·学习·分享·批量下载·图片下载器