简体   繁体   English

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

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

I'm trying to use QuickGraph to, well, find a maximal matching in my bipartite graph, but the MatchedEdges collection they return to me is empty. 我正在尝试使用QuickGraph在我的二部图中找到最大匹配项,但是它们返回给我的MatchEdges集合为空。 I know there are matchings because I tested it with a K7,7 (complete bipartite) graph. 我知道有匹配项,因为我用K7,7(完全二部图)测试了它。 So, I'm confused on what I did wrong. 因此,我对自己做错了感到困惑。

Here's my code (I wrote Vertex and Edge in place of my actual classes for readability): 这是我的代码(出于可读性的考虑,我写了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 always comes back as 0. That's the problem. maxMatch.MatchedEdges.Count始终返回为0。这就是问题所在。

I'm hopeful that there will be an easy solution to this, like I shouldn't be using new AdjacencyGraph() or something, but I'm also open to suggestions to other ways to find maximal matchings in bipartite graphs. 我希望对此有一个简单的解决方案,例如我不应该使用新的AdjacencyGraph()之类的东西,但是我也乐于接受其他建议在二部图中找到最大匹配项的建议。

Thanks! 谢谢!

BTW, this link is what I used to write my stuff: Maximum Bipartite Matching in QuickGraph 顺便说一句,这个链接是我用来写东西的: QuickGraph中的最大二分匹配

After creating an instance of MaximumBipartiteMatchingAlgorithm , you need to call the its Compute method so a matching is computed. 创建MaximumBipartiteMatchingAlgorithm实例后,您需要调用其Compute方法,以便计算匹配。 In terms of your example, this means adding: 就您的示例而言,这意味着添加:

maxMatch.Compute();

Also each call to the newVertex() method should return a unique string which differs from the strings identifying the vertices in the input to MaximumBipartiteMatchingAlgorithm . 同样,对newVertex()方法的每次调用都应返回一个唯一的字符串,该字符串不同于标识MaximumBipartiteMatchingAlgorithm的输入中的顶点的字符串。

FYI: I personally found that sometimes maxMatch.MatchedEdges contains too many edges: some vertices are covered twice. 仅供参考:我个人发现,有时maxMatch.MatchedEdges包含太多的边:有些顶点被覆盖两次。 Also I've seen maxMatch.MatchedEdges contain edges not in the input to MaximumBipartiteMatchingAlgorithm but created internally by the algorithm. 另外,我还看到maxMatch.MatchedEdges包含的边不在MaximumBipartiteMatchingAlgorithm的输入中,而是由算法在内部创建。

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

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