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

boost::graph学习

boost::graph API简单小结

boost::graph是boost为图算法提供的API,简单易用。

API说明

  1. boost::add_vertex
    创建一个顶点。

  2. boost::add_edge
    创建一条边。

  3. boost::edges
    获取所有的边。

  4. boost::vertices
    获取所有的顶点。

  5. graph.operator[vertex_descriptor]
    获取顶点的属性Property。

  6. graph.operator[edge_descriptor]
    获取边的属性Property。

  7. boost::out_edges
    获取顶点的“出边”(out edges),即以当前顶点为出发点的边。

参考文档:
https://theboostcpplibraries.com/boost.graph-algorithms
https://zhuanlan.zhihu.com/p/338279100
https://www.boost.org/doc/libs/1_83_0/libs/graph/doc/quick_tour.html
https://www.boost.org/doc/libs/1_83_0/libs/graph/doc/adjacency_list.html
https://www.technical-recipes.com/2015/getting-started-with-the-boost-graph-library/
https://blog.csdn.net/Augusdi/article/details/105757441

代码示例:

#include "PSParametricModelingEngine.h"#include <QDebug>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <utility>using namespace std;
using namespace boost;namespace
{struct VertexProperty{int id;string name;};struct EdgeProperty{int id;int weight;};typedef boost::adjacency_list<vecS,           // 使用数组来存储vertex vecS,vecS,           // 使用数组来存储vertex vecS,directedS,      // 申明为有向图,可以访问其out-edge,若要都能访问VertexProperty, // 定义顶点属性EdgeProperty    // 定义边的属性>PSGraph;typedef graph_traits<PSGraph>::vertex_descriptor vertex_descriptor_t;void printVertices(){boost::adjacency_list<> g;boost::adjacency_list<>::vertex_descriptor v1 = boost::add_vertex(g);boost::adjacency_list<>::vertex_descriptor v2 = boost::add_vertex(g);boost::adjacency_list<>::vertex_descriptor v3 = boost::add_vertex(g);boost::adjacency_list<>::vertex_descriptor v4 = boost::add_vertex(g);std::cout << v1 << ", " << v2 << ", " << v3 << ", " << v4 << '\n';}void printEdgeVector(){boost::adjacency_list<> g;boost::adjacency_list<>::vertex_descriptor v1 = boost::add_vertex(g);boost::adjacency_list<>::vertex_descriptor v2 = boost::add_vertex(g);boost::add_vertex(g);boost::add_vertex(g);std::pair<boost::adjacency_list<>::edge_descriptor, bool> p = boost::add_edge(v1, v2, g);std::cout.setf(std::ios::boolalpha);std::cout << p.second << '\n';p = boost::add_edge(v1, v2, g);std::cout << p.second << '\n';p = boost::add_edge(v2, v1, g);std::cout << p.second << '\n';std::pair<boost::adjacency_list<>::edge_iterator, boost::adjacency_list<>::edge_iterator> es = boost::edges(g);std::copy(es.first, es.second, std::ostream_iterator<boost::adjacency_list<>::edge_descriptor>{std::cout, "\n"});for (boost::adjacency_list<>::edge_iterator iterator = es.first; iterator != es.second; iterator++){std::stringstream ss;ss << (*iterator);qDebug() << QString::fromUtf8(ss.str().c_str());}}void printEdgeSet(){typedef boost::adjacency_list<boost::setS, boost::vecS, boost::undirectedS> graph;graph g;boost::adjacency_list<>::vertex_descriptor v1 = boost::add_vertex(g);boost::adjacency_list<>::vertex_descriptor v2 = boost::add_vertex(g);boost::add_vertex(g);boost::add_vertex(g);std::pair<graph::edge_descriptor, bool> p = boost::add_edge(v1, v2, g);std::cout.setf(std::ios::boolalpha);std::cout << p.second << '\n';p = boost::add_edge(v1, v2, g);std::cout << p.second << '\n';p = boost::add_edge(v2, v1, g);std::cout << p.second << '\n';std::pair<graph::edge_iterator, graph::edge_iterator> es = boost::edges(g);std::copy(es.first, es.second, std::ostream_iterator<graph::edge_descriptor>{std::cout, "\n"});for (graph::edge_iterator iterator = es.first; iterator != es.second; iterator++){std::stringstream ss;ss << (*iterator);qDebug() << QString::fromUtf8(ss.str().c_str());}}int printDirectedGraph(){// 定义图类型,使用vector存放顶点和边,有向图typedef adjacency_list<vecS, vecS, directedS> graph_t;// 产生图对象,有6个顶点graph_t g(6);// 加入边add_edge(1, 2, g);add_edge(1, 5, g);add_edge(2, 2, g);add_edge(2, 0, g);add_edge(3, 4, g);add_edge(4, 3, g);add_edge(5, 0, g);// 显示所有的顶点// 顶点迭代器类型typedef graph_traits<graph_t>::vertex_iterator vertex_iter;// 得到所有顶点,vrange中的一对迭代器分别指向第一个顶点和最后的一个顶点之后。std::pair<vertex_iter, vertex_iter> vrange = vertices(g);for (vertex_iter itr = vrange.first; itr != vrange.second; ++itr)qDebug() << *itr;// 显示所有的边// 边迭代器类型typedef graph_traits<graph_t>::edge_iterator edge_iter;// 得到所有边,erange中的一对迭代器分别指向第一条边和最后的一条边之后std::pair<edge_iter, edge_iter> erange = edges(g);for (edge_iter itr = erange.first; itr != erange.second; ++itr)qDebug() << source(*itr, g) << "-->" << target(*itr, g);return 0;};void printPSGraph(){PSGraph g; // 声明一个图VertexProperty vertex1{101, "Vertex 1"};VertexProperty vertex2{102, "Vertex 2"};VertexProperty vertex3{103, "Vertex 3"};VertexProperty vertex4{104, "Vertex 4"};EdgeProperty edge1{101, 1};EdgeProperty edge2{102, 2};EdgeProperty edge3{103, 3};EdgeProperty edge4{104, 4};EdgeProperty edge5{105, 5};EdgeProperty edge6{106, 6};vertex_descriptor_t vert1 = boost::add_vertex(vertex1, g);vertex_descriptor_t vert2 = boost::add_vertex(vertex2, g);vertex_descriptor_t vert3 = boost::add_vertex(vertex3, g);vertex_descriptor_t vert4 = boost::add_vertex(vertex4, g);auto e1 = boost::add_edge(vert1, vert2, edge1, g);auto e2 = boost::add_edge(vert2, vert3, edge2, g);auto e3 = boost::add_edge(vert3, vert4, edge3, g);auto e4 = boost::add_edge(vert4, vert1, edge4, g);auto e5 = boost::add_edge(vert4, vert2, edge5, g);auto e6 = boost::add_edge(vert4, vert3, edge6, g);typedef graph_traits<PSGraph>::vertex_iterator vertex_iter;std::pair<vertex_iter, vertex_iter> vrange = vertices(g);for (vertex_iter itr = vrange.first; itr != vrange.second; ++itr){auto prop = g[*itr];qDebug() << *itr << ": {" << prop.id << ", " << QString::fromUtf8(prop.name.c_str()) << "}";typedef graph_traits<PSGraph> GraphTraits;typename GraphTraits::out_edge_iterator out_i, out_end;typename GraphTraits::edge_descriptor e;for (boost::tie(out_i, out_end) = out_edges(*itr, g); out_i != out_end; ++out_i){e = *out_i;auto prop = g[e];qDebug() << source(e, g) << "-->" << target(e, g) << ": {" << prop.id << ", " << prop.weight << "}";}}typedef graph_traits<PSGraph>::edge_iterator edge_iter;std::pair<edge_iter, edge_iter> erange = edges(g);for (edge_iter itr = erange.first; itr != erange.second; ++itr){auto prop = g[*itr];qDebug() << source(*itr, g) << "-->" << target(*itr, g) << ": {" << prop.id << ", " << prop.weight << "}";}}
} // namespacevoid main()
{// printEdgeSet();// printDirectedGraph();printPSGraph();
}

相关文章:

boost::graph学习

boost::graph API简单小结 boost::graph是boost为图算法提供的API&#xff0c;简单易用。 API说明 boost::add_vertex 创建一个顶点。 boost::add_edge 创建一条边。 boost::edges 获取所有的边。 boost::vertices 获取所有的顶点。 graph.operator[vertex_descriptor] 获…...

【C语言:动态内存管理】

文章目录 前言1.malloc2.free3.calloc4.realloc5.动态内存常见错误6.动态内存经典笔试题分析7.柔性数组8.C/C中的内存区域划分 前言 文章的标题是动态内存管理&#xff0c;那什么是动态内存管理&#xff1f;为什么有动态内存管理呢&#xff1f; 回顾一下以前学的知识&#xff…...

【Python基础】迭代器

文章目录 [toc]什么是迭代可迭代对象判断数据类型是否是可迭代类型 迭代器对可迭代对象进行迭代的本质获取可迭代对象的迭代器通过迭代器获取数据StopIteration异常 自定义迭代器__iter__()方法__next__()方法判断数据类型是否是可迭代类型自定义迭代器案例分离模式整合模式 fo…...

QVTK 可视化

#ifndef MAINWINDOW_H #define MAINWINDOW_H#include <QMainWindow>#include <vtkNew.h> // 智能指针 #include <QVTKOpenGLNativeWidget.h> #include <vtkCylinderSource.h> // 圆柱#include <vtkPolyDataMapper.h&g…...

【初阶C++】入门(超详解)

C入门 前言1. C关键字(C98)2. 命名空间2.1 命名空间定义2.2 命名空间使用2.3嵌套命名空间 3. C输入&输出4. 缺省参数4.1 缺省参数概念4.2 缺省参数分类 5. 函数重载5.1 函数重载概念5.2 C支持函数重载的原理--名字修饰(name Mangling) 6. 引用6.1 引用概念6.2 引用特性6.3 …...

Java正则表达式的使用

标题&#xff1a;Java正则表达式的使用 介绍&#xff1a; 正则表达式是一种强大的文本匹配模式和搜索工具。在Java中&#xff0c;通过使用正则表达式&#xff0c;我们可以快速有效地进行字符串的匹配、查找和替换。本文将介绍Java正则表达式的基本使用方法&#xff0c;并提供相…...

Collecting Application Engine Performance Data 收集应用程序引擎性能数据

You can collect performance data of any specific SQL action of an Application Engine program to address any performance issue. 您可以收集应用程序引擎程序的任何特定SQL操作的性能数据&#xff0c;以解决任何性能问题。 You can collect performance data of the S…...

C Primer Plus阅读--章节16

C Primer Plus阅读–章节16 翻译程序的第一步 预处理之前&#xff0c;编译器必须对该程序进行一些翻译处理。 首先&#xff0c;编译器将源代码中出现的字符映射到源字符集。第二&#xff0c;编译器定位每个反斜杠后面跟着换行符的实力&#xff0c;并删除他们。物理行的合并。…...

直接插入排序与希尔排序

目录 前言 插入排序 直接插入排序 时空复杂度 直接插入排序的特性 希尔排序&#xff08;缩小增量排序&#xff09; 预排序 顺序排序 多组并排 小总结 直接插入排序 时空复杂度 希尔排序的特性 前言 字可能有点多&#xff0c;但是真的理解起来真的没那么难&#…...

敏捷:应对软件定义汽车时代的开发模式变革

随着软件定义汽车典型应用场景的落地&#xff0c;汽车从交通工具转向智能移动终端的趋势愈发明显。几十年前&#xff0c;一台好车的定义主要取决于高性能的底盘操稳与动力系统&#xff1b;几年前&#xff0c;一台好车的定义主要取决于智能化系统与智能交互能否满足终端用户的用…...

做题笔记:SQL Sever 方式做牛客SQL的题目--查询每天刷题通过数最多的前二名用户

----查询每天刷题通过数最多的前二名用户id和刷题数 现有牛客刷题表questions_pass_record&#xff0c;请查询每天刷题通过数最多的前二名用户id和刷题数&#xff0c;输出按照日期升序排序&#xff0c;查询返回结果名称和顺序为&#xff1a; date|user_id|pass_count 表单创建…...

Vue3 用 Proxy API 替代 defineProperty API 的那些事

一、Object.defineProperty 定义&#xff1a;Object.defineProperty() 方法会直接在一个对象上定义一个新属性&#xff0c;或者修改一个对象的现有属性&#xff0c;并返回此对象 1.1 为什么能实现响应式 通过defineProperty 两个属性&#xff0c;get及set get 属性的 gett…...

成都工业学院Web技术基础(WEB)实验五:CSS3动画制作

写在前面 1、基于2022级计算机大类实验指导书 2、代码仅提供参考&#xff0c;前端变化比较大&#xff0c;按照要求&#xff0c;只能做到像&#xff0c;不能做到一模一样 3、图片和文字仅为示例&#xff0c;需要自行替换 4、如果代码不满足你的要求&#xff0c;请寻求其他的…...

【Docker】学习笔记(三)三剑客之 docker-compose文件书写项目多服务容器运行

简介 引言&#xff08;需求&#xff09; 为了完成一个完整项目势必用到N多个容器配合完成项目中的业务开发&#xff0c;一旦引入N多个容器&#xff0c;N个容器之间就会形成某种依赖&#xff0c;也就意味着某个容器的运行需要其他容器优先启动之后才能正常运行&#xff1b; 容…...

node.js基础

node.js基础 &#x1f353;什么是node.js&#x1f353;node.js模块&#x1f352;&#x1f352; 内置模块&#x1f345;&#x1f345;&#x1f345;fs模块&#x1f345;&#x1f345;&#x1f345;path模块&#x1f345;&#x1f345;&#x1f345;http模块 &#x1f352;&#…...

fastapi实现websocket在线聊天

最近要实现一个在线聊天功能&#xff0c;基于fastapi的websocket实现了这个功能。下面介绍一下遇到的技术问题 1.问题难点 在线上环境部署时&#xff0c;一般是多进程的方式进行部署启动fastapi服务&#xff0c;而每个启动的进程都有自己的独立存储空间。导致存储的连接对象分…...

LLM推理部署(六):TogetherAI推出世界上LLM最快推理引擎,性能超过vLLM和TGI三倍

LLM能有多快&#xff1f;答案在于LLM推理的最新突破。 TogetherAI声称&#xff0c;他们在CUDA上构建了世界上最快的LLM推理引擎&#xff0c;该引擎运行在NVIDIA Tensor Core GPU上。Together推理引擎可以支持100多个开源大模型&#xff0c;比如Llama-2&#xff0c;并在Llama-2–…...

Unity | 渡鸦避难所-2 | 搭建场景并添加碰撞器

1 规范项目结构 上期中在导入一系列的商店资源包后&#xff0c;Assets 目录已经变的混乱不堪 开发过程中&#xff0c;随着资源不断更新&#xff0c;遵循一定的项目结构和设计规范是非常必要的。这可以增加项目的可读性、维护性、扩展性以及提高团队协作效率 这里先做下简单的…...

展望2024年供应链安全

2023年是开展供应链安全&#xff0c;尤其是开源治理如火如荼的一年&#xff0c;开源治理是供应链安全最重要的一个方面&#xff0c;所以我们从开源治理谈起。我们先回顾一下2023的开源治理情况。我们从信通院《2023年中国企业开源治理全景观察》发布的信息。信通院调研了来自七…...

React 列表页实现

一、介绍 列表页是常用的功能&#xff0c;从后端获取列表数据&#xff0c;刷新到页面上。开发列表页需要考虑以下技术要点:1.如何翻页&#xff1b;2.如何进行内容搜索&#xff1b;3.何时进行页面刷新。 二、使用教程 1.user-service 根据用户id获取用户列表&#xff0c;返回…...

华为云AI开发平台ModelArts

华为云ModelArts&#xff1a;重塑AI开发流程的“智能引擎”与“创新加速器”&#xff01; 在人工智能浪潮席卷全球的2025年&#xff0c;企业拥抱AI的意愿空前高涨&#xff0c;但技术门槛高、流程复杂、资源投入巨大的现实&#xff0c;却让许多创新构想止步于实验室。数据科学家…...

XCTF-web-easyupload

试了试php&#xff0c;php7&#xff0c;pht&#xff0c;phtml等&#xff0c;都没有用 尝试.user.ini 抓包修改将.user.ini修改为jpg图片 在上传一个123.jpg 用蚁剑连接&#xff0c;得到flag...

7.4.分块查找

一.分块查找的算法思想&#xff1a; 1.实例&#xff1a; 以上述图片的顺序表为例&#xff0c; 该顺序表的数据元素从整体来看是乱序的&#xff0c;但如果把这些数据元素分成一块一块的小区间&#xff0c; 第一个区间[0,1]索引上的数据元素都是小于等于10的&#xff0c; 第二…...

利用ngx_stream_return_module构建简易 TCP/UDP 响应网关

一、模块概述 ngx_stream_return_module 提供了一个极简的指令&#xff1a; return <value>;在收到客户端连接后&#xff0c;立即将 <value> 写回并关闭连接。<value> 支持内嵌文本和内置变量&#xff08;如 $time_iso8601、$remote_addr 等&#xff09;&a…...

Qt/C++开发监控GB28181系统/取流协议/同时支持udp/tcp被动/tcp主动

一、前言说明 在2011版本的gb28181协议中&#xff0c;拉取视频流只要求udp方式&#xff0c;从2016开始要求新增支持tcp被动和tcp主动两种方式&#xff0c;udp理论上会丢包的&#xff0c;所以实际使用过程可能会出现画面花屏的情况&#xff0c;而tcp肯定不丢包&#xff0c;起码…...

2.Vue编写一个app

1.src中重要的组成 1.1main.ts // 引入createApp用于创建应用 import { createApp } from "vue"; // 引用App根组件 import App from ./App.vue;createApp(App).mount(#app)1.2 App.vue 其中要写三种标签 <template> <!--html--> </template>…...

将对透视变换后的图像使用Otsu进行阈值化,来分离黑色和白色像素。这句话中的Otsu是什么意思?

Otsu 是一种自动阈值化方法&#xff0c;用于将图像分割为前景和背景。它通过最小化图像的类内方差或等价地最大化类间方差来选择最佳阈值。这种方法特别适用于图像的二值化处理&#xff0c;能够自动确定一个阈值&#xff0c;将图像中的像素分为黑色和白色两类。 Otsu 方法的原…...

基于TurtleBot3在Gazebo地图实现机器人远程控制

1. TurtleBot3环境配置 # 下载TurtleBot3核心包 mkdir -p ~/catkin_ws/src cd ~/catkin_ws/src git clone -b noetic-devel https://github.com/ROBOTIS-GIT/turtlebot3.git git clone -b noetic https://github.com/ROBOTIS-GIT/turtlebot3_msgs.git git clone -b noetic-dev…...

【VLNs篇】07:NavRL—在动态环境中学习安全飞行

项目内容论文标题NavRL: 在动态环境中学习安全飞行 (NavRL: Learning Safe Flight in Dynamic Environments)核心问题解决无人机在包含静态和动态障碍物的复杂环境中进行安全、高效自主导航的挑战&#xff0c;克服传统方法和现有强化学习方法的局限性。核心算法基于近端策略优化…...

虚拟电厂发展三大趋势:市场化、技术主导、车网互联

市场化&#xff1a;从政策驱动到多元盈利 政策全面赋能 2025年4月&#xff0c;国家发改委、能源局发布《关于加快推进虚拟电厂发展的指导意见》&#xff0c;首次明确虚拟电厂为“独立市场主体”&#xff0c;提出硬性目标&#xff1a;2027年全国调节能力≥2000万千瓦&#xff0…...