[英]Given an undirected graph G = (V, E), determine whether G is a complete graph
我很确定这个问题是P而不是NP,但是我很难提出多项式绑定算法来解决它。
You can : 您可以 :
n(n-1)/2
. n(n-1)/2
。 n-1
distinct vertices. n-1
不同的顶点。 This will run in O(V²)
, which is polynomial. 这将在多项式
O(V²)
运行。
Hope it helped. 希望能有所帮助。
Here's an O(|E|) algorithm that also has a small constant. 这是一个O(| E |)算法,该算法的常数也较小。
It's trivial to enumerate every edge in a complete graph. 枚举完整图形中的每个边都是微不足道的。 So all you need to do is scan your edge list and verify that every such edge exists.
因此,您要做的就是扫描边缘列表,并验证每个这样的边缘是否存在。
For each edge (i, j), let f(i, j) = i*|V| 对于每个边(i,j),令f(i,j)= i * | V | + j.
+ j。 Assuming vertices are numbered 0 to |V|-1.
假设顶点编号为0到| V | -1。
Let bitvec
be a bit vector of length |V| 设
bitvec
为长度| V |的位向量 2 , initialized to 0. 2 ,初始化为0。
For each edge (i, j), set bitvec[f(i, j)]
= 1. 对于每个边(i,j),设置
bitvec[f(i, j)]
= 1。
G is a complete graph if and only if every element of bitvec
== 1. 当且仅当
bitvec
每个元素== 1时,G才是完整的图。
This algorithm not only touches E once, but it's also completely vectorizable if you have a scatter instruction. 该算法不仅触及E一次,而且如果您有分散指令,它也可以完全向量化。 That also means it's trivial to parallelize.
这也意味着并行化很简单。
Here is an O(E) algorithm: 这是一个O(E)算法:
Total is O(E) 总计为O(E)
For a given graph G = (V,E), check for each pair u, v in the V, and see if edge (u,v) is in E. The total number of u, v pairs are |V|*(|V|-1)/2. 对于给定的图G =(V,E),检查V中的每对u,v,并查看边(u,v)是否在E中。u,v对的总数为| V | *( | V | -1)/ 2。 As a result, with a time complexity of O(|V|^2), you can check and see if a graph is complete or not.
结果,时间复杂度为O(| V | ^ 2),您可以检查并查看图形是否完整。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.