All problems
Vertex Cover 2-Approx Size
Problem
Return the size of the vertex cover produced by the maximal-matching 2-approximation.
Scan edges in order and, whenever both endpoints are still unmatched, add both endpoints to the cover and mark them used. The result is twice a maximal matching, hence at most twice the optimal cover; return 0 for non-positive n.
Hint
Any vertex cover must contain at least one endpoint of every matching edge.
Test cases
case 1
in: [3,[[0,1],[1,2],[2,0]]]
expected: 2
case 2
in: [4,[[0,1],[0,2],[0,3]]]
expected: 2
case 3
in: [4,[[0,1],[1,2],[2,3]]]
expected: 4
case 4
in: [3,[]]
expected: 0
case 5
in: [2,[[0,1]]]
expected: 2
Starter codePython
def vertex_cover_approx_size(n, edges):
# Your code here
pass5 test cases · run your code against them in the editor