boost graph 有向图 无向图

复制源:https://www.cnblogs.com/sssblog/p/11189402.html(纯英文)

 

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

 Boost Graph提供了处理图形的工具。图形是二维点云,点之间有任意数量的线。

 

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;
}
 

输出  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.

Boost::adjacency_list是使用默认参数实例化的模板。Boost::add_vertex()在图中添加一个点。Boost::add_vertex()返回一个Boost::adjacency_list::vertex_descriptor类型的对象。该对象表示图形中新添加的点。

 

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.

vector是boost::adjacency_list默认用来存储点的容器。在这种情况下,boost::adjacency_list::vertex_descriptor是std::size_t的类型定义。因为其他容器可以用来存储点,boost::adjacency_list::vertex_descriptor不一定总是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 0;
}
 

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.

要从图中获取所有点,请调用boost::vertices()。此函数返回两个boost::adjacency_list::vertex_iterator类型的迭代器,它们指向起始点和结束点。

 

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 0;
}
 

输出:

true

true

true

(0,1)

(0,1)

(1,1)

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::add_edge()来连接图中的两点。你必须传递点和图形作为参数。Boost::add_edge()返回一个std::pair。首先提供对线路的访问。第二个是bool变量,表示该行是否成功添加。

 

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().

Boost::edges()提供了对图中所有线的访问。edge()返回两个迭代器,分别指向起始行和结束行。线从第一点开始,一条从第二点开始。线条的方向取决于传递给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 0;
}
 

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.

缺省情况下,boost::adjacency_list使用std::vector表示点和线。通过传递boost::setS作为第一个模板参数,std::set被选为行容器。

 

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

第二个模板参数告诉boost::adjacency_list应该使用哪个类作为点。

 

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:: directedds,这意味着所有的线都是有方向的,可以画成箭头。线只能在一个方向上交叉。

 

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

Boost.Graph提供了更多的选择器,包括boost::listS、boost::mapS和boost::hash_setS。boost::bidirectionalS可以用来使线路双向。

 

5. creating indexes automatically with boost::add_edge()  使用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 0;
}
 

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.

不调用boost::add_vertex()也可以定义一个图。如果传递给boost::add_edge()的点不存在,Boost.Graph会自动将缺失点添加到图形中。

 

Containers

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

除了boost::adjacency_list之外,boost还提供了另外两个图容器。图: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>, 4> 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(), 4};

  return 0;
}
 

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.

在boost::adjacency_matrix中不存在传递选择器的两个模板参数。对于boost::adjacency_matrix,不使用boost::vecS和boost::setS等选择器。Boost::adjacency_matrix将图存储在矩阵中,内部结构是硬编码的。

 

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.

boost::adjacency_matrix的内部结构使得快速添加和删除行成为可能。但是,内存消耗更高。经验法则是使用boost::adjacency_list当有相对较少的线与点相比。行越多,使用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>, 4> 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(), 4};

  return 0;
}
 

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::compressed_sparse_row_graph不能修改。一旦创建了图形,点和线就不能添加或删除。因此,boost::compressed_sparse_rwo_graph仅在使用不可变图时才有意义。只支持有向线,内存消耗低。

posted @ 2023-07-28 10:40  冥府骑士格斯  阅读(215)  评论(0编辑  收藏  举报