Quadratic-time vertex cover verification

China☆狼群 提交于 2020-06-18 01:28:52

问题


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


回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!