如何添加带有属性包的boost :: adjacency_matrix边缘? [英] How do you add edges to boost::adjacency_matrix with property bundles?

查看:91
本文介绍了如何添加带有属性包的boost :: adjacency_matrix边缘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在带有捆绑的顶点和边缘属性的boost :: adjacency_matrix图中添加边缘.

I was trying to add edges in a boost::adjacency_matrix graph w/ bundled vertex and edge properties.

代码如下:

// VD and ED are just trivial structs
using Graph = boost::adjacency_matrix<boost::directedS, VD, ED>;
using Vertex = boost::graph_traits<Graph>::vertex_descriptor;
using Edge = boost::graph_traits<Graph>::edge_descriptor;

const unsigned kNodesCount = 4;
Graph g { kNodesCount };

// create two nodes
auto one = boost::add_vertex(g);
auto two = boost::add_vertex(g);

// add edge between nodes (vertices)
boost::add_edge(one, two, g);

由于boost::adjacency_matrixboost::add_edge重载内的断言失败,我得到了SIGABRT.事实证明它是 UNDER CONSTRUCTION (以及邻接矩阵的其他一些功能).

I got a SIGABRT due to a failed assertion within the boost::add_edge overload for boost::adjacency_matrix. It turns out it's UNDER CONSTRUCTION (as are a few other functions for adjacency matrices).

   template <typename D, typename VP, typename EP, typename GP, typename A>
  inline typename adjacency_matrix<D,VP,EP,GP,A>::vertex_descriptor
  add_vertex(adjacency_matrix<D,VP,EP,GP,A>& g) {
    // UNDER CONSTRUCTION
    BOOST_ASSERT(false);
    return *vertices(g).first;
  }

  template <typename D, typename VP, typename EP, typename GP, typename A,
            typename VP2>
  inline typename adjacency_matrix<D,VP,EP,GP,A>::vertex_descriptor
  add_vertex(const VP2& /*vp*/, adjacency_matrix<D,VP,EP,GP,A>& g) {
    // UNDER CONSTRUCTION
    BOOST_ASSERT(false);
    return *vertices(g).first;
  }

  template <typename D, typename VP, typename EP, typename GP, typename A>
  inline void
  remove_vertex(typename adjacency_matrix<D,VP,EP,GP,A>::vertex_descriptor /*u*/,
                adjacency_matrix<D,VP,EP,GP,A>& /*g*/)
  {
    // UNDER CONSTRUCTION
    BOOST_ASSERT(false);
  }

所以,我的问题是:我们如何解决这个问题?我在网上找到的所有示例均使用boost::adjacency_list.目前有没有办法实现同一目标?

So, my question is: how do we get around this? All the examples I find online utilize boost::adjacency_list. Is there a way of achieving the same thing currently?

推荐答案

您没有.只是没有必要添加顶点,因为已经使用所需的所有kNodesCount对其进行了初始化.相反,获取现有顶点的顶点描述符:

You don't. It's just not necessary to add vertices since you already initialized it with all the kNodesCount you require. Instead, get the vertex descriptor for the existing vertices:

在Coliru上直播

#include <boost/graph/adjacency_matrix.hpp>
#include <boost/graph/graph_utility.hpp>
struct VD { };
struct ED { };

// VD and ED are just trivial structs
using Graph  = boost::adjacency_matrix<boost::directedS, VD, ED>;
using Vertex = Graph::vertex_descriptor;
using Edge   = Graph::edge_descriptor;

int main() {
    const unsigned kNodesCount = 4;
    Graph g { kNodesCount };

    // create two nodes
    Vertex one = vertex(0, g);
    Vertex two = vertex(1, g);

    // add edge between nodes (vertices)
    add_edge(one, two, g);
    print_graph(g);
}

打印

0 --> 1 
1 --> 
2 --> 
3 --> 

实际上,由于adjacency_matrix<>模型中顶点描述符的琐碎性质,您可以对顶点描述符进行硬编码,例如:

In fact, due to the trivial nature of vertex descriptors in the adjacency_matrix<> model, you can hardcode the vertex descriptors like:

在Coliru上直播

add_edge(0, 1, g);


可以将顶点添加到矩阵中吗?

否,不是动态的.支持的操作列在


Can you add vertices to a matrix at all?

No, not dynamically. The operations supported are listed under

  • AdjacencyMatrix concept which refines
  • Graph concept

这篇关于如何添加带有属性包的boost :: adjacency_matrix边缘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆