当前位置: 首页 > 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;返回…...

突破不可导策略的训练难题:零阶优化与强化学习的深度嵌合

强化学习&#xff08;Reinforcement Learning, RL&#xff09;是工业领域智能控制的重要方法。它的基本原理是将最优控制问题建模为马尔可夫决策过程&#xff0c;然后使用强化学习的Actor-Critic机制&#xff08;中文译作“知行互动”机制&#xff09;&#xff0c;逐步迭代求解…...

屋顶变身“发电站” ,中天合创屋面分布式光伏发电项目顺利并网!

5月28日&#xff0c;中天合创屋面分布式光伏发电项目顺利并网发电&#xff0c;该项目位于内蒙古自治区鄂尔多斯市乌审旗&#xff0c;项目利用中天合创聚乙烯、聚丙烯仓库屋面作为场地建设光伏电站&#xff0c;总装机容量为9.96MWp。 项目投运后&#xff0c;每年可节约标煤3670…...

【Oracle】分区表

个人主页&#xff1a;Guiat 归属专栏&#xff1a;Oracle 文章目录 1. 分区表基础概述1.1 分区表的概念与优势1.2 分区类型概览1.3 分区表的工作原理 2. 范围分区 (RANGE Partitioning)2.1 基础范围分区2.1.1 按日期范围分区2.1.2 按数值范围分区 2.2 间隔分区 (INTERVAL Partit…...

精益数据分析(97/126):邮件营销与用户参与度的关键指标优化指南

精益数据分析&#xff08;97/126&#xff09;&#xff1a;邮件营销与用户参与度的关键指标优化指南 在数字化营销时代&#xff0c;邮件列表效度、用户参与度和网站性能等指标往往决定着创业公司的增长成败。今天&#xff0c;我们将深入解析邮件打开率、网站可用性、页面参与时…...

Java线上CPU飙高问题排查全指南

一、引言 在Java应用的线上运行环境中&#xff0c;CPU飙高是一个常见且棘手的性能问题。当系统出现CPU飙高时&#xff0c;通常会导致应用响应缓慢&#xff0c;甚至服务不可用&#xff0c;严重影响用户体验和业务运行。因此&#xff0c;掌握一套科学有效的CPU飙高问题排查方法&…...

HDFS分布式存储 zookeeper

hadoop介绍 狭义上hadoop是指apache的一款开源软件 用java语言实现开源框架&#xff0c;允许使用简单的变成模型跨计算机对大型集群进行分布式处理&#xff08;1.海量的数据存储 2.海量数据的计算&#xff09;Hadoop核心组件 hdfs&#xff08;分布式文件存储系统&#xff09;&a…...

【网络安全】开源系统getshell漏洞挖掘

审计过程&#xff1a; 在入口文件admin/index.php中&#xff1a; 用户可以通过m,c,a等参数控制加载的文件和方法&#xff0c;在app/system/entrance.php中存在重点代码&#xff1a; 当M_TYPE system并且M_MODULE include时&#xff0c;会设置常量PATH_OWN_FILE为PATH_APP.M_T…...

git: early EOF

macOS报错&#xff1a; Initialized empty Git repository in /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/.git/ remote: Enumerating objects: 2691797, done. remote: Counting objects: 100% (1760/1760), done. remote: Compressing objects: 100% (636/636…...

HTML前端开发:JavaScript 获取元素方法详解

作为前端开发者&#xff0c;高效获取 DOM 元素是必备技能。以下是 JS 中核心的获取元素方法&#xff0c;分为两大系列&#xff1a; 一、getElementBy... 系列 传统方法&#xff0c;直接通过 DOM 接口访问&#xff0c;返回动态集合&#xff08;元素变化会实时更新&#xff09;。…...

rknn toolkit2搭建和推理

安装Miniconda Miniconda - Anaconda Miniconda 选择一个 新的 版本 &#xff0c;不用和RKNN的python版本保持一致 使用 ./xxx.sh进行安装 下面配置一下载源 # 清华大学源&#xff08;最常用&#xff09; conda config --add channels https://mirrors.tuna.tsinghua.edu.cn…...