无向图上顶点和边的变化

changes in vertices and edges on undirected graph

本文关键字:变化 顶点      更新时间:2023-10-16

Im using visual studio c++, boost.

假设我有无向图。 每 t 秒我需要检查它是否发生了变化(顶点\边删除\添加)。

时间复杂度非常重要。

这是构建图形的示例:

typedef adjacency_matrix<undirectedS> UGraph;
    UGraph G(9);
    add_edge(1, 2, G);
    add_edge(2, 5, G);
    add_edge(5, 1, G);
    add_edge(3, 6, G);
    add_edge(3, 4, G);
    add_edge(7, 8, G);

因为我使用adjacency_matrixadd_edge()remove_edge()add_vertex()remove_vertex()的时间复杂度是O(1)。

我想使用 add_edge() 通过其返回值来检查该边缘是否已经存在:

retVal = add_edge(1, 2, G);
if(already exist)
 //do something
else
 //do something else
retVal = add_vertex(1,G);
if(already exist)
 //do something
else
 //do something else

但据我了解,如果我将边缘添加到现有边缘(在无向图中),它将并行添加到现有边缘。

那么,有没有一种胖方法(我的方式或其他方式)来检查两个无向图之间的变化(顶点和边)?

谢谢。

> ok.k

经过一些阅读,这是我发现的:

检查边缘是否存在-

graph_traits<UGraph>::edge_descriptor e;   //UGraph is the undirected graph
bool found = false;
boost::tie(e, found) = edge(1, 2, G); //found = true
boost::tie(e, found) = edge(1, 3, G); //found = false
boost::tie(e, found) = edge(2, 1, G); //found = true

另一种方式:

found = edge(1, 2, G).second; //found = true
found = edge(1, 3, G).second; //found = false

检查顶点是否存在-

我没有找到任何内置函数,但可以在adjacency_matrix上找到特定的顶点