3

如何计算 C# 中的最小二分顶点覆盖?有这样做的代码片段吗?

编辑:虽然问题对于一般图来说是 NP 完全的,但对于二部图来说,它可以在多项式时间内解决。我知道它在某种程度上与二分图中的最大匹配有关(通过 Konig 定理),但我无法正确理解该定理以便能够将最大二分匹配的结果转换为顶点覆盖。

4

3 回答 3

5

我可以弄清楚:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class VertexCover
{
    static void Main(string[] args)
    {
        var v = new VertexCover();
        v.ParseInput();
        v.FindVertexCover();
        v.PrintResults();
    }

    private void PrintResults()
    {
        Console.WriteLine(String.Join(" ", VertexCoverResult.Select(x => x.ToString()).ToArray()));
    }

    private void FindVertexCover()
    {
        FindBipartiteMatching();

        var TreeSet = new HashSet<int>();
        foreach (var v in LeftVertices)
            if (Matching[v] < 0)
                DepthFirstSearch(TreeSet, v, false);

        VertexCoverResult = new HashSet<int>(LeftVertices.Except(TreeSet).Union(RightVertices.Intersect(TreeSet)));
    }

    private void DepthFirstSearch(HashSet<int> TreeSet, int v, bool left)
    {
        if (TreeSet.Contains(v))
            return;
        TreeSet.Add(v);
        if (left) {
            foreach (var u in Edges[v])
                if (u != Matching[v])
                    DepthFirstSearch(TreeSet, u, true);
        } else if (Matching[v] >= 0)
            DepthFirstSearch(TreeSet, Matching[v], false);

    }

    private void FindBipartiteMatching()
    {
        Bicolorate();
        Matching = Enumerable.Repeat(-1, VertexCount).ToArray();
        var cnt = 0;
        foreach (var i in LeftVertices) {
            var seen = new bool[VertexCount];
            if (BipartiteMatchingInternal(seen, i)) cnt++;
        }
    }

    private bool BipartiteMatchingInternal(bool[] seen, int u)
    {
        foreach (var v in Edges[u]) {
            if (seen[v]) continue;
            seen[v] = true;
            if (Matching[v] < 0 || BipartiteMatchingInternal(seen, Matching[v])) {
                Matching[u] = v;
                Matching[v] = u;
                return true;
            }
        }
        return false;
    }

    private void Bicolorate()
    {
        LeftVertices = new HashSet<int>();
        RightVertices = new HashSet<int>();

        var colors = new int[VertexCount];
        for (int i = 0; i < VertexCount; ++i)
            if (colors[i] == 0 && !BicolorateInternal(colors, i, 1))
                throw new InvalidOperationException("Graph is NOT bipartite.");
    }

    private bool BicolorateInternal(int[] colors, int i, int color)
    {
        if (colors[i] == 0) {
            if (color == 1) LeftVertices.Add(i);
            else RightVertices.Add(i);
            colors[i] = color;
        } else if (colors[i] != color)
            return false;
        else
            return true;
        foreach (var j in Edges[i])
            if (!BicolorateInternal(colors, j, 3 - color))
                return false;
        return true;
    }

    private int VertexCount;
    private HashSet<int>[] Edges;
    private HashSet<int> LeftVertices;
    private HashSet<int> RightVertices;
    private HashSet<int> VertexCoverResult;
    private int[] Matching;

    private void ReadIntegerPair(out int x, out int y)
    {
        var input = Console.ReadLine();
        var splitted = input.Split(new char[] { ' ' }, 2);
        x = int.Parse(splitted[0]);
        y = int.Parse(splitted[1]);
    }

    private void ParseInput()
    {
        int EdgeCount;
        ReadIntegerPair(out VertexCount, out EdgeCount);
        Edges = new HashSet<int>[VertexCount];
        for (int i = 0; i < Edges.Length; ++i)
            Edges[i] = new HashSet<int>();

        for (int i = 0; i < EdgeCount; i++) {
            int x, y;
            ReadIntegerPair(out x, out y);
            Edges[x].Add(y);
            Edges[y].Add(x);
        }
    }
}

如您所见,这段代码在多项式时间内解决了这个问题。

于 2009-02-06T20:59:36.930 回答
1

最好只是继续随机选择一个节点。对于每个节点,要么它进入顶点覆盖,要么它的所有邻居都这样做(因为你需要包括那个边)。整个事情的最终结果将是一组顶点覆盖,你选择最小的一个。不过,我不会坐在这里编写代码,因为如果我没记错的话,它的 NP 是完整的。

于 2009-02-06T19:08:19.930 回答
0

 private void DepthFirstSearch(HashSet TreeSet, int v, bool left)
    {
        if (TreeSet.Contains(v))
            return;
        TreeSet.Add(v);
        if (left) {
            foreach (var u in Edges[v])
                if (u != Matching[v])
                    DepthFirstSearch(TreeSet, u, true);
        } else if (Matching[v] >= 0)
            DepthFirstSearch(TreeSet, Matching[v], false);

    }

if(left)将执行时,我认为它的值始终为false

于 2010-04-20T06:36:47.803 回答