【图论实战】Boost学习 01:基本操作

文章目录

头文件

cpp 复制代码
#include <boost/graph/adjacency_list.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<boost/graph/dijkstra_shortest_paths.hpp>
#include <iostream>
#include <fstream>
using namespace boost;
using namespace std;

图的构建

参考网址

cpp 复制代码
// 无属性图的定义
typedef adjacency_list<vecS, //表示顶点是用vector来存储的
					   vecS, // 表示边是用vector来存储的
					   directedS, //表示有向图
					   no_property, //表示不带属性
					   no_property> //表示不带属性
					   MyGraphType;


// 有属性图的定义
struct VertexProperty{
	int id;
	string name;
};
struct EdgeProperty{
	int id;
	int weight;
};
typedef boost::adjacency_list<vecS, 			//使用数组来存储vertex vecS,
							  bidirectionalS, 	//声明为有向图,可以访问其out-edge,若要都能访问
							  VertexProperty, 	//定义顶点属性
							  EdgeProperty> 	//定义边的属性 > Graph;在此之前,要定义顶点和边的属性,可以用结构体自定义
							  MyGraphType;
					   
// 创建图	
MyGraphType G;
auto v0=add_vertex(G);
auto v1=add_vertex(G);
auto v2=add_vertex(G);
auto v3=add_vertex(G);
auto e01=add_edge(v0,v1,G);
auto e12=add_edge(v1,v2,G);
auto e23=add_edge(v2,v3,G);
auto e30=add_edge(v3,v0,G);
// 设置权重
property_map<MyGraphType,edge_weight_t>::type weightmap= get(boost::edge_weight, G);
weightmap[e]=10.1; 		// 方式1
put(weightmap,e,20); 	// 方式2
// 获取顶点ID

图的可视化

方式一: 利用graphviz

1、将图保存为gv文件

cpp 复制代码
// g is the graph
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);

2、将gv文件转化为png文件
工具下载 graphviz 2.38


在线转换工具

方式二: 打印结果

cpp 复制代码
auto vpair =vertices(G);
for(auto iter=vpair.first;iter!=vpair.second;iter++){
	cout<<"vertex "<<*iter<<endl;
}

auto epair=edges(G);
for(auto iter=epair.first;iter!=epair.second;iter++){
	cout<<"edge "<<source(*iter,G)<<" - "<<target(*iter,G)<<endl;
}	

基本操作

cpp 复制代码
// 获取顶点个数
boost::num_vertices(g)
相关推荐
郜太素2 分钟前
PyTorch 中神经网络相关要点(损失函数,学习率)及优化方法总结
人工智能·pytorch·python·深度学习·神经网络·学习
为自己_带盐23 分钟前
浅聊一下数据库的索引优化
开发语言·数据库·php
threelab29 分钟前
05.three官方示例+编辑器+AI快速学习three.js webgl - animation - skinning - ik
人工智能·学习·编辑器
threelab29 分钟前
04.three官方示例+编辑器+AI快速学习webgl_animation_skinning_additive_blending
人工智能·学习·编辑器
深度学习机器学习33 分钟前
计算机视觉最不卷的方向:三维重建学习路线梳理
人工智能·深度学习·学习·yolo·目标检测·机器学习·计算机视觉
gb42152871 小时前
mysql数据库中某个数据表的碎片率自己降低了,mysql数据表对碎片率有自动优化机制吗?
数据库·mysql
AI大模型顾潇1 小时前
[特殊字符] 本地大模型编程实战(29):用大语言模型LLM查询图数据库NEO4J(2)
前端·数据库·人工智能·语言模型·自然语言处理·prompt·neo4j
有时间要学习1 小时前
MySQL——数据类型&&表的约束
数据库·mysql
jimsten1 小时前
苍穹外卖 - Day02 学习笔记
java·笔记·学习
人类恶.1 小时前
C 语言学习笔记(6)
c语言·笔记·学习