0

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. I know there are matchings because I tested it with a K7,7 (complete bipartite) graph. 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):

    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.

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.

Thanks!

BTW, this link is what I used to write my stuff: Maximum Bipartite Matching in QuickGraph

1 Answers1

1

After creating an instance of MaximumBipartiteMatchingAlgorithm, you need to call the its Compute method so a matching is computed. 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.

FYI: I personally found that sometimes maxMatch.MatchedEdges contains too many edges: some vertices are covered twice. Also I've seen maxMatch.MatchedEdges contain edges not in the input to MaximumBipartiteMatchingAlgorithm but created internally by the algorithm.

snow_abstraction
  • 408
  • 6
  • 13