[英]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.