我有一个无向无权图G = (V, E),和一个随机选择的节点子集S。我想检查S中的节点是否相互邻接(即形成一个完全子图/团)。
我有以下算法(伪代码):
foreach vertex in S {
// Check that the vertex has enough edges
if (vertex.edges.count < S.size - 1)
return false;
// Check that there is an edge to each vertex in S
foreach vertex2 in S {
if (!vertex.hasEdgeTo(vertex2))
return false;
}
}
return true;
问题在于这个算法的最坏情况下性能为O(|V|2)(当子集S包含完全图的所有顶点时)。
我的问题是:是否有更快的算法,其最坏情况下的复杂度更好?