简体   繁体   English

以下测试用例的最小顶点覆盖集的大小应该是多少?

[英]What should be the size of the minimum vertex cover set for the below test case?

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?这些是由 5 个顶点和 8 个边组成的无向图的边根据我对最小顶点覆盖的理解,输出应该是:{1,2,4} 或 {1,4,5} 但是当我执行这个https://www.codingninjas.com/codestudio/problems/vertex-cover-problem_1081481?leftPanelTab=0上的测试用例给出最小顶点覆盖集的大小为 5,当我在https://www上执行时.geeksforgeeks.org/vertex-cover-problem-set-1-introduction-approximate-algorithm-2/它告诉我应该是 4。谁能确认我是否正确解释了这个问题?

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;我可以确认这个测试用例的答案确实应该是 3。 {1, 2, 4} 和 {1, 4, 5} 是图的顶点覆盖; all edges are covered.所有边缘都被覆盖。 It is also minimum because there are no solutions with 2 vertexes (checked by hand and by code).它也是最小的,因为没有具有 2 个顶点的解决方案(手动和代码检查)。


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表示有 4 个节点和 8 个边):

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.这应该是正确的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM