>我正在尝试执行将顶点添加到现有图形的简单任务,但代码崩溃:
typedef adjacency_matrix<undirectedS> UGraph;
graph_traits<UGraph>::vertex_descriptor u;
UGraph G(2);
int numOfComponents = 0;
int numOfVertices = 0;
numOfVertices = num_vertices(G); //numOfVertices = 2
vector<int> component(numOfVertices);
numOfComponents = connected_components(G, &component[0]); //numOfComponents = 2
到目前为止,这似乎是合乎逻辑的 - 两个顶点,没有连接,所以有两个连接的组件。
现在,当我尝试添加顶点时:
u = vertex(3, G); //u=3
add_vertex(u,G); // <--- crashes here
我错过了什么吗? 如何将顶点添加到现有图形? 删除呢? 命令remove_vertex(id)
后我会看到少一个顶点吗?
另外,是否可以添加 id 为 100 而不是 id 3 的顶点(不是延续的 id 号..)?
谢谢。
矩阵。它有一个固定的大小(nxn,其中n是顶点数)。
您无法"寻址"第三个顶点,因为它在 2x2 矩阵中不存在。
add_edge
仅针对MutableGraph
、MutablePropertyGraph
(带属性)定义。 adjacency_matrix
没有对这些概念进行建模
http://www.boost.org/doc/libs/1_58_0/libs/graph/doc/graph_concepts.html