0

I'm pretty sure this problem is P and not NP, but I'm having difficulty coming up with a polynomially bound algorithm to solve it.

user3316874
  • 223
  • 2
  • 6
  • 10
  • 4
    All problems in P are also in NP. You mean "is P and not NP-complete" (or "not NP-hard"). – chepner May 08 '15 at 04:07
  • Exactly where are you stuck? Do you have an algorithm that's not P? Or one that you can't prove is P? Or stuck on the definitions? – Paul Hankin May 08 '15 at 05:44

4 Answers4

1

You can :

  1. check that number of edges in the graph is n(n-1)/2.
  2. check that each vertice is connected to exaclty n-1 distinct vertices.

This will run in O(V²), which is polynomial.

Hope it helped.

yahya el fakir
  • 562
  • 2
  • 12
  • 3
    You gotta be more pedantic with your symbols. It will not run in O(V^2) because you haven't even described how `V` relates to `n`. – Adam May 08 '15 at 04:24
1

Here's an O(|E|) algorithm that also has a small constant.

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| + j. Assuming vertices are numbered 0 to |V|-1.

Let bitvec be a bit vector of length |V|2, initialized to 0.

For each edge (i, j), set bitvec[f(i, j)] = 1.

G is a complete graph if and only if every element of bitvec == 1.

This algorithm not only touches E once, but it's also completely vectorizable if you have a scatter instruction. That also means it's trivial to parallelize.

Adam
  • 16,808
  • 7
  • 52
  • 98
0

Here is an O(E) algorithm:

  1. Use O(E) as it is input time, to scan the graph
  2. Meanwhile, record each vertex p's degree, increase degree only if the neighbor is not p itself (self-connecting edge) and is not a vertex q where p and q has another edge counted already (multiple edge), these checking can be done in O(1)
  3. Check if all vertex's degree is |V|-1, this step is O(V), if Yes then it is a complete graph

Total is O(E)

shole
  • 4,046
  • 2
  • 29
  • 69
0

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. As a result, with a time complexity of O(|V|^2), you can check and see if a graph is complete or not.

Ebram
  • 161
  • 1
  • 6