public void TestHasAnotherMatching() { var graph = new BipartiteGraph(2, 2); graph.AddEdge(0, 0); graph.AddEdge(0, 1); graph.AddEdge(1, 1); var matching = new Matching(2); matching.AddPair(0, 0); matching.AddPair(1, 1); Assert.IsFalse(builder.HasAnotherMatching(graph, matching)); }
public bool HasAnotherMatching(BipartiteGraph graph, Matching matching) { for (var i = 0; i < graph.SecondPartSize; i++) { if (matching.HasPair(i)) { var pair = matching.GetPair(i); matching.RemovePair(i); graph.RemoveEdge(pair, i); var used = new bool[graph.FirstPartSize]; if (TryKuhn(pair, used, graph, matching)) { matching.AddPair(i, pair); graph.AddEdge(pair, i); return(true); } matching.AddPair(i, pair); graph.AddEdge(pair, i); } } return(false); }
private bool TryKuhn(int v, bool[] used, BipartiteGraph bipartiteGraph, Matching matching) { if (used[v]) { return(false); } used[v] = true; for (var i = 0; i < bipartiteGraph.SecondPartSize; ++i) { if (bipartiteGraph.IsConnected(v, i)) { var to = i; if (!matching.HasPair(to) || TryKuhn(matching.GetPair(to), used, bipartiteGraph, matching)) { matching.AddPair(to, v); return(true); } } } return(false); }