oompa
oompa

Reputation: 295

Quadratic-time vertex cover verification

Suppose you are given an undirected graph G with n vertices and m edges represented by an n x n adjacency matrix A, and you are also given a subset of vertices S (represented by an array of size m). How can you check whether S is a vertex cover of G with quadratic time and space complexity?

By the definition of a vertex cover, I know that we require every edge must be incident to a vertex that's contained in S.

I can easily come up with a cubic algorithm: iterate over the adjacency matrix; each 1 represents an edge (u, v). Check whether u or v are in S. If not, the answer is no. If we get to the end of the adjacency matrix, the answer is yes.

But how can I do this in O(n^2) time? I guess the only real "observation" I've made so far is that we can possibly skip intermediate rows while iterating over the adjacency matrix if we've already found the vertex corresponding to that row in S. However, this has not helped me very much.

Can someone please help me (or point me in the correct direction)?

Thanks

Upvotes: 0

Views: 416

Answers (1)

btilly
btilly

Reputation: 46445

Construct an array T which is the positions of all of the elements NOT in S.

And then:

for i in T:
    for j in T:
        if A[i][j] == 1:
            return False
return True

Upvotes: 2

Related Questions