-1

I am solving a problem for finding the size of the vertex cover for a given graph. Below is the test case for which I am not able to interpret what should be the output:

1 4
1 5
1 2
4 1
2 4
3 4
5 2
1 3

These are the edges of an undirected graph consisting of 5 vertices and 8 edges According to my understanding of minimum vertex cover the output should be either: {1,2,4} or {1,4,5} but when I am executing this test case on https://www.codingninjas.com/codestudio/problems/vertex-cover-problem_1081481?leftPanelTab=0 It is giving out that size of the minimum vertex cover set is 5 and when I execute on https://www.geeksforgeeks.org/vertex-cover-problem-set-1-introduction-approximate-algorithm-2/ It is telling me that it should be 4. Can anyone confirm me that whether I am interpreting this question correctly or not?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Noob_Coder
  • 29
  • 3

1 Answers1

0

I can confirm that the answer for this test case should indeed be 3. {1, 2, 4} and {1, 4, 5} are vertex covers of the graph; all edges are covered. It is also minimum because there are no solutions with 2 vertexes (checked by hand and by code).


See this code snippet for the minimum vertex cover:

#include <bits/stdc++.h>

using namespace std;

struct edge {
    int x, y; // two endpoint of edges
};

int n, m, s = -1; // n = number of nodes, m = number of edges, s = answer
vector<edge> g; // graph
vector<bool> nodeColored; // whether node is colored

void dfs(int x, int c);
int main() {
    
    cin >> n >> m; // read number of nodes, number of edges
    nodeColored = vector<bool>(n+1); // initialize
    for(int i = 1; i <= m; ++i) {
        edge newEdge;
        cin >> newEdge.x >> newEdge.y;
        g.push_back(newEdge);
    }
    dfs(1, 0); // consider first node, no nodes have been colored yet
    cout << s << endl;
    
    return 0;
}

void dfs(int x, int c) { // x = considering node x, c = c nodes already colored
    if(x > n) {
        // all nodes have been considered, simply run check
        bool ok = true; // assume this coloring is ok
        for(edge i : g) {
            if(!nodeColored[i.x] && !nodeColored[i.y]) { // not a vertex cover?
                ok = false;
            }
        }
        if(ok) { // we should update answer
            if(s == -1) s = c; // s = -1 entails answer has never been set
            else s = min(s, c); // min vertex cover
        }
        return;
    }
    dfs(x+1, c); // ignore this node
    nodeColored[x] = true;
    dfs(x+1, c+1); // color this node
    nodeColored[x] = false;
    return;
}

I have tried to make the code as intuitive as possible, as well as add comments in order to make the meaning behind each line of code clearer.

Texting against this input, which is the one you provided (note that for the first line, 4, 8 means there are 4 nodes and 8 edges):

4 8
1 4
1 5
1 2
4 1
2 4
3 4
5 2
1 3

The output is:

3

This should be a correct output.

Ryan Zhang
  • 1,856
  • 9
  • 19
  • Can you share me the code for the same through which you have checked the answer? – Noob_Coder Jun 06 '22 at 09:47
  • I have left my computer now - I will post it as an edit to my answer in a few hours. However, the two solutions you constructed are correct and therefore already prove that answers 4 and 5 are not minimum. What language do you want me to implement it in - Python, C, C++, or Java? If you want another language just ask to see if I know it. – Ryan Zhang Jun 06 '22 at 09:50
  • C++ would be fine... Also can you let me know what is the time complexity of your code? – Noob_Coder Jun 06 '22 at 09:54
  • As I understand it, the problem is NP-Hard. On my computer at home I have an O(2^N*M) solution, for a graph with N nodes and M edges. – Ryan Zhang Jun 06 '22 at 09:55
  • @Noob_Coder ok, I am back. I will add the code as a snippet. – Ryan Zhang Jun 06 '22 at 12:41