Boost Graph provides tools to work with graphs. Graphas are two-dimensional point clouds with any number of lines between ponts.

Vertices and Edges

1 adjacency_list

#include <boost/graph/adjacency_list.hpp>
#include <iostream> int main() {
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 << std::endl;
return ;
}

输出  0, 1, 2, 3

boost::adjacency_list is a template that is instantiated with default parameters. boost::add_vertex() adds a point to a graph. boost::add_vertex() returns an object of type boost::adjacency_list::vertex_descriptor. This object represents a newly added point in the graph.

std::vector is the container boost::adjacency_list uses by default to store points. In this case, boost::adjacency_list::vertex_descriptor is a type definition for std::size_t. Because other containers can be used to store points, boost::adjacency_list::vertex_descriptor isn't necessarily always std::size_t.

2. vertices()

#include <boost/graph/adjacency_list.hpp>
#include <utility>
#include <algorithm>
#include <iterator>
#include <iostream> int main() {
boost::adjacency_list<> g; boost::add_vertex(g);
boost::add_vertex(g);
boost::add_vertex(g);
boost::add_vertex(g); std::pair<boost::adjacency_list<>::vertex_iterator, boost::adjacency_list<>::vertex_iterator> vs = boost::vertices(g); std::copy(vs.first, vs.second, std::ostream_iterator<boost::adjacency_list<>::vertex_descriptor>{std::cout, "\n"}); return ;
}

To get all points from a graph, call boost::vertices(). This function returns two iterators of type boost::adjacency_list::vertex_iterator, which refer to the beginning and ending points.

3. edges()

#include <boost/graph/adjacency_list.hpp>
#include <utility>
#include <algorithm>
#include <iterator>
#include <iostream> int main() {
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 << std::endl; p = boost::add_edge(v1, v2, g);
std::cout << p.second << std::endl; p = boost::add_edge(v2, v2, g);
std::cout << p.second << std::endl; 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"}); return ;
}

输出:

true

true

true

(0,1)

(0,1)

(1,0)

You call boost::add_edge() to connect two points in a graph. You have to pass the points and the graph as parameters. boost::add_edge() returns a std::pair. first provides access to the line. second is a bool variable that indicates whether the line was successfully added.

boost::edges() provides access to all lines in a graph. boost::edges() returns two iterators that refer to the beginning and ending lines. lines start at the first point, one at the second. The direction of the lines depends on the order of the parameters passed to boost::add_edge().

As you see, you can have multiple lines between the same two points.

4. boost::adjacency_list with selectors

#include <boost/graph/adjacency_list.hpp>
#include <utility>
#include <algorithm>
#include <iterator>
#include <iostream> int main() {
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<boost::adjacency_list<>::edge_descriptor, bool> p = boost::add_edge(v1, v2, g);
std::cout.setf(std::ios::boolalpha);
std::cout << p.second << std::endl; p = boost::add_edge(v1, v2, g);
std::cout << p.second << std::endl; p = boost::add_edge(v2, v2, g);
std::cout << p.second << std::endl; 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"}); return ;
}

By default, boost::adjacency_list uses std::vector for points and lines. By passing boost::setS as the first template parameter, std::set is selected as the container for lines.

The second template parameter tells boost::adjacency_list which class should be used for points.

The third template parameter determines whether lines are directed or undirected. The default is boost::directedS, which means all lines are directed and can be drawn as arrows. Lines can only be crossed in one direction.

Boost.Graph offers more selectors, including boost::listS, boost::mapS, and boost::hash_setS. boost::bidirectionalS can be used to make lines bidirectional.

5. creating indexes automatically with boost::add_edge()

#include <boost/graph/adjacency_list.hpp>
#include <tuple>
#include <algorithm>
#include <iterator>
#include <iostream> int main() {
typedef boost::adjacency_list<boost::setS, boost::vecS, boost::undirectedS> graph;
graph g; enum { topLeft, topRight, bottomRight, bottomLeft }; boost::add_edge(topLeft, topRight, g);
boost::add_edge(topRight, bottomRight, g);
boost::add_edge(bottomRight, bottomLeft, g);
boost::add_edge(bottomLeft, topLeft, g); graph::edge_iterator it, end;
std::tie(it, end) = boost::edges(g);
std::copy(it, end, std::ostream_iterator<graph::edge_descriptor>{std::cout, "\n"}); return ;
}

It is possible to define a graph without calling boost::add_vertex(). Boost.Graph adds missing points to a graph automatically if the points passed to boost::add_edge() don't exist.

Containers

except boost::adjacency_list, there are two other graph containers provided by Boost.Graph: boost::adjacency_matrix, boost::compressed_sparse_row_graph.

1. boost::adjacency_matrix

#include <boost/graph/adjacency_matrix.hpp>
#include <array>
#include <utility> int main() {
enum { topLeft, topRight, bottomRight, bottomLeft }; std::array<std::pair<int, int>, > edges{{
std::make_pair(topLeft, topRight);
std::make_pair(topRight, bottomRight);
std::make_pair(bottomRight, bottomLeft);
std::make_pair(bottomLeft, topLeft);
}}; typedef boost::adjacency_matrix<boost::undirectedS> graph;
graph g{edges.beign(), edges.end(), }; return ;
}

The two template parameters that pass selectors don't exist with boost::adjacency_matrix. With boost::adjacency_matrix, no selectors, such as boost::vecS and boost::setS, are used. boost::adjacency_matrix stores the graph in a matrix, and the internal structure is hardcoded. You can think of the matrix as a two-dimensional table: the table is a square with as many rows and columuns as the graph has points. The internal structure of boost::adjacency_matrix makes it possible to add and remove lines quickly. However, memory consumption is highter. The rule of thumb is to use boost::adjacency_list when there are relatively few lines compared to points. The more lines there are, the more it makes sense to use boost::adjacency_matrix.

2. boost::compressed_sparse_row_graph

#include <boost/graph/adjacency_matrix.hpp>
#include <array>
#include <utility> int main() {
enum { topLeft, topRight, bottomRight, bottomLeft }; std::array<std::pair<int, int>, > edges{{
std::make_pair(topLeft, topRight);
std::make_pair(topRight, bottomRight);
std::make_pair(bottomRight, bottomLeft);
std::make_pair(bottomLeft, topLeft);
}}; typedef boost::compressed_sparse_row_graph<boost::bidirectionalS> graph;
graph g{boost::edges_are_unsorted_multi_pass, edges.beign(), edges.end(), }; return ;
}

boost::compressed_sparse_row_graph can't be changed with. Once the graph has been created, points and lines can't be added or removed. Thus, boost::compressed_sparse_rwo_graph makes only sense when using an immutable graph. only supports directed lines and is low memory consumption.

boost graph的更多相关文章

  1. 【转】使用Boost Graph library(二)

    原文转自:http://shanzhizi.blog.51cto.com/5066308/942972 让我们从一个新的图的开始,定义一些属性,然后加入一些带属性的顶点和边.我们将给出所有的代码,这样 ...

  2. 【转】使用Boost Graph library(一)

    转自:http://shanzhizi.blog.51cto.com/5066308/942970 本文是一篇译文,来自:http://blog.csdn.net/jjqtony/article/de ...

  3. Boost Graph Library使用学习

    Boost Graph Library,BGL 使用学习 探索 Boost Graph Library https://www.ibm.com/developerworks/cn/aix/librar ...

  4. Boost Graph Library materials

    Needed to compute max flow in a project and found the official document of BGL to be rather obscure, ...

  5. boost库之graph入门

    #include <boost/graph/undirected_graph.hpp> #include <boost/graph/adjacency_list.hpp> us ...

  6. 转债---Pregel: A System for Large-Scale Graph Processing(译)

    转载:http://duanple.blog.163.com/blog/static/70971767201281610126277/   作者:Grzegorz Malewicz, Matthew ...

  7. 译:Boost Property Maps

    传送门:Boost Graph Library 快速入门 原文:Boost Property Map 图的抽象数学性质与它们被用来解决具体问题之间的主要联系就是被附加在图的顶点和边上的属性(prope ...

  8. Pregel: A System for Large-Scale Graph Processing(译)

    [说明:Pregel这篇是发表在2010年的SIGMOD上,Pregel这个名称是为了纪念欧拉,在他提出的格尼斯堡七桥问题中,那些桥所在的河就叫Pregel.最初是为了解决PageRank计算问题,由 ...

  9. Linux上安装使用boost入门指导

    Data Mining Linux上安装使用boost入门指导 获得boost boost分布 只需要头文件的库 使用boost建立一个简单的程序 准备使用boost二进制文件库 把你的程序链接到bo ...

随机推荐

  1. MatrixTraceTransform主要逻辑在transform方法中

    @Override public void transform(TransformInvocation transformInvocation) throws TransformException, ...

  2. (转)GIS理论知识(三)之ArcGIS平台、SuperMap超图平台和开源平台

    3.1.ArcGIS平台 ArcGIS为美国ESRI公司研发的产品,为用户提供一个可伸缩的,全面的GIS平台.ArcObjects包含了许多的可编程组件,从细粒度的对象(例如单个的几何对象)到粗粒度的 ...

  3. Charles抓取https

    步骤一:将Charles的根证书(Charles Root Certificates)安装到Mac上. Help -> SSL Proxying -> Install Charles Ro ...

  4. Uva 12563 Jin Ge Jin Qu hao(01背包)

    题意: 假定你在唱KTV,还剩下t秒时间.你决定接下来唱你最喜爱的n首歌(不包含劲歌金曲)中的一些歌曲.在时间结束之前再唱一个劲歌金曲.使得唱的歌的总曲目尽量多以及时间总长度. 输入保证所有n+1曲子 ...

  5. MySQL定义数据库对象之指定definer

    mysql创建view.trigger.function.procedure.event时都会定义一个Definer: SQL SECURITY 有两个选项,一个为DEFINER,一个为INVOKER ...

  6. Jsoup代码示例、解析网页+提取文本

    使用Jsoup解析HTML 那么我们就必须用到HttpClient先获取到html 同样我们引入HttpClient相关jar包 以及commonIO的jar包 我们把httpClient的基本代码写 ...

  7. 纯文本编辑语言markdown

    Markdown的主要目的是生成可以复制到网页或写入平台的HTML代码.但你不必那样使用它.Markdown也可以作为强大记笔记的基础,许多Markdown编辑可以将您的写作导出为其他格式,如Word ...

  8. vs2017或vs2019添加引用时报错

    我先安装的是vs2019,进入VS命令提示符里后一直说:gacutil 不是有效的命令,一直没能解决,然后直接装了vs2017后,该命令可以使用了, 还是用VS2017吧,2019的版本感觉还有点问题 ...

  9. 136、TensorFlow的Embedding lookup

    import tensorflow as tf; import numpy as np; c = np.random.random([10, 1]) b = tf.nn.embedding_looku ...

  10. mariadb(三)查

    -查询基本使用(条件,排序,聚合函数,分组,分页) 1)创建一个表结构然后添加数据 create table baba (id int unsigned not null auto_increment ...