问题
Suppose you are given an undirected graph
G
withn
vertices andm
edges represented by ann x n
adjacency matrixA
, and you are also given a subset of verticesS
(represented by an array of sizem
). How can you check whetherS
is a vertex cover ofG
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
回答1:
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
来源:https://stackoverflow.com/questions/59851921/quadratic-time-vertex-cover-verification