繁体   English   中英

我收集的最大匹配边返回为空(在C#中使用QuickGraph)

[英]My collection of maximal matched edges comes back empty (using QuickGraph in C#)

我正在尝试使用QuickGraph在我的二部图中找到最大匹配项,但是它们返回给我的MatchEdges集合为空。 我知道有匹配项,因为我用K7,7(完全二部图)测试了它。 因此,我对自己做错了感到困惑。

这是我的代码(出于可读性的考虑,我写了Vertex和Edge代替了实际的类):

    public void findMatchings(List<Friend> setA, List<Friend> setB, List<Edge> candidateEdges) {
        // we need a graph and two sets of vertices
        IMutableVertexAndEdgeListGraph<Vertex, Edge> graph = new AdjacencyGraph<Vertex, Edge>();

        foreach (Vertex vertex in setA) {
            graph.AddVertex(vertex);
        }

        foreach (Vertex vertex in setB) {
            graph.AddVertex(vertex);
        }

        foreach (Edge candidate in candidatesEdges) {
            graph.AddEdge(candidate);
        }

        // sets a and b must be distinct, and their union must be equal to the set of all vertices in the graph
        // (though they're conceptually the same set, to you or me, I created new instances of everything so they should be practically different.)
        IEnumerable<Vertex> vertexSetA = setA;
        IEnumerable<Vertex> vertexSetB = setB;

        // These functions are used to create new vertices and edges during the execution of the algorithm.  
        // All added objects are removed before the computation returns
        VertexFactory<Vertex> vertexFactory = newVertex; //newVertex() is defined below
        EdgeFactory<Vertex, Edge> edgeFactory = (source, target) => new Edge(source, target);

        // computing the maximum bipartite match
        var maxMatch = new MaximumBipartiteMatchingAlgorithm<Vertex, Edge>(
                graph, vertexSetA, vertexSetB, vertexFactory, edgeFactory);

        Console.WriteLine(maxMatch.MatchedEdges.Count);

    }

    //This is in the same class as the above function:
    static Vertex newVertex() {
        return new Vertex("");
    }

    //This is the constructor in the Edge class:
    public Edge(Vertex source, Vertex target) {
        this.source = source;
        this.target = target;
    }

maxMatch.MatchedEdges.Count始终返回为0。这就是问题所在。

我希望对此有一个简单的解决方案,例如我不应该使用新的AdjacencyGraph()之类的东西,但是我也乐于接受其他建议在二部图中找到最大匹配项的建议。

谢谢!

顺便说一句,这个链接是我用来写东西的: QuickGraph中的最大二分匹配

创建MaximumBipartiteMatchingAlgorithm实例后,您需要调用其Compute方法,以便计算匹配。 就您的示例而言,这意味着添加:

maxMatch.Compute();

同样,对newVertex()方法的每次调用都应返回一个唯一的字符串,该字符串不同于标识MaximumBipartiteMatchingAlgorithm的输入中的顶点的字符串。

仅供参考:我个人发现,有时maxMatch.MatchedEdges包含太多的边:有些顶点被覆盖两次。 另外,我还看到maxMatch.MatchedEdges包含的边不在MaximumBipartiteMatchingAlgorithm的输入中,而是由算法在内部创建。

暂无
暂无

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

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