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

【Axure高保真原型】引导弹窗

今天和大家中分享引导弹窗的原型模板&#xff0c;载入页面后&#xff0c;会显示引导弹窗&#xff0c;适用于引导用户使用页面&#xff0c;点击完成后&#xff0c;会显示下一个引导弹窗&#xff0c;直至最后一个引导弹窗完成后进入首页。具体效果可以点击下方视频观看或打开下方…...

谷歌浏览器插件

项目中有时候会用到插件 sync-cookie-extension1.0.0&#xff1a;开发环境同步测试 cookie 至 localhost&#xff0c;便于本地请求服务携带 cookie 参考地址&#xff1a;https://juejin.cn/post/7139354571712757767 里面有源码下载下来&#xff0c;加在到扩展即可使用FeHelp…...

设计模式和设计原则回顾

设计模式和设计原则回顾 23种设计模式是设计原则的完美体现,设计原则设计原则是设计模式的理论基石, 设计模式 在经典的设计模式分类中(如《设计模式:可复用面向对象软件的基础》一书中),总共有23种设计模式,分为三大类: 一、创建型模式(5种) 1. 单例模式(Sing…...

高频面试之3Zookeeper

高频面试之3Zookeeper 文章目录 高频面试之3Zookeeper3.1 常用命令3.2 选举机制3.3 Zookeeper符合法则中哪两个&#xff1f;3.4 Zookeeper脑裂3.5 Zookeeper用来干嘛了 3.1 常用命令 ls、get、create、delete、deleteall3.2 选举机制 半数机制&#xff08;过半机制&#xff0…...

Auto-Coder使用GPT-4o完成:在用TabPFN这个模型构建一个预测未来3天涨跌的分类任务

通过akshare库&#xff0c;获取股票数据&#xff0c;并生成TabPFN这个模型 可以识别、处理的格式&#xff0c;写一个完整的预处理示例&#xff0c;并构建一个预测未来 3 天股价涨跌的分类任务 用TabPFN这个模型构建一个预测未来 3 天股价涨跌的分类任务&#xff0c;进行预测并输…...

渲染学进阶内容——模型

最近在写模组的时候发现渲染器里面离不开模型的定义,在渲染的第二篇文章中简单的讲解了一下关于模型部分的内容,其实不管是方块还是方块实体,都离不开模型的内容 🧱 一、CubeListBuilder 功能解析 CubeListBuilder 是 Minecraft Java 版模型系统的核心构建器,用于动态创…...

剑指offer20_链表中环的入口节点

链表中环的入口节点 给定一个链表&#xff0c;若其中包含环&#xff0c;则输出环的入口节点。 若其中不包含环&#xff0c;则输出null。 数据范围 节点 val 值取值范围 [ 1 , 1000 ] [1,1000] [1,1000]。 节点 val 值各不相同。 链表长度 [ 0 , 500 ] [0,500] [0,500]。 …...

CMake控制VS2022项目文件分组

我们可以通过 CMake 控制源文件的组织结构,使它们在 VS 解决方案资源管理器中以“组”(Filter)的形式进行分类展示。 🎯 目标 通过 CMake 脚本将 .cpp、.h 等源文件分组显示在 Visual Studio 2022 的解决方案资源管理器中。 ✅ 支持的方法汇总(共4种) 方法描述是否推荐…...

Go 并发编程基础:通道(Channel)的使用

在 Go 中&#xff0c;Channel 是 Goroutine 之间通信的核心机制。它提供了一个线程安全的通信方式&#xff0c;用于在多个 Goroutine 之间传递数据&#xff0c;从而实现高效的并发编程。 本章将介绍 Channel 的基本概念、用法、缓冲、关闭机制以及 select 的使用。 一、Channel…...

MySQL 部分重点知识篇

一、数据库对象 1. 主键 定义 &#xff1a;主键是用于唯一标识表中每一行记录的字段或字段组合。它具有唯一性和非空性特点。 作用 &#xff1a;确保数据的完整性&#xff0c;便于数据的查询和管理。 示例 &#xff1a;在学生信息表中&#xff0c;学号可以作为主键&#xff…...