Skip to content

Commit e4b9fa2

Browse files
committed
#35 Bipartitie Matching Bug Fix
1 parent 0e1b90a commit e4b9fa2

File tree

2 files changed

+68
-7
lines changed

2 files changed

+68
-7
lines changed

src/Advanced.Algorithms/Graph/Coloring/MColorer.cs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,32 @@ public class MColorer<T, C>
1414
/// </summary>
1515
public MColorResult<T, C> Color(IGraph<T> graph, C[] colors)
1616
{
17+
var totalProgress = new Dictionary<IGraphVertex<T>, C>();
1718

18-
var first = graph.ReferenceVertex;
19-
20-
var progress = canColor(first, colors,
21-
new Dictionary<IGraphVertex<T>, C>(),
22-
new HashSet<IGraphVertex<T>>());
19+
foreach (var vertex in graph.VerticesAsEnumberable)
20+
{
21+
var progress = canColor(vertex, colors,
22+
new Dictionary<IGraphVertex<T>, C>(),
23+
new HashSet<IGraphVertex<T>>());
24+
25+
foreach(var item in progress)
26+
{
27+
if (!totalProgress.ContainsKey(item.Key))
28+
{
29+
totalProgress.Add(item.Key, item.Value);
30+
}
31+
}
32+
33+
}
2334

24-
if (progress.Count != graph.VerticesCount)
35+
if (totalProgress.Count != graph.VerticesCount)
2536
{
2637
return new MColorResult<T, C>(false, null);
2738
}
2839

2940
var result = new Dictionary<C, List<T>>();
3041

31-
foreach (var vertex in progress)
42+
foreach (var vertex in totalProgress)
3243
{
3344
if (!result.ContainsKey(vertex.Value))
3445
{

tests/Advanced.Algorithms.Tests/Graph/Matching/BiPartiteMatching_Tests.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,31 @@ public void MaxBiPartiteMatch_AdjacencyListGraph_Smoke_Test()
4242
Assert.AreEqual(result.Count, 4);
4343
}
4444

45+
/// <summary>
46+
/// Test Max BiParitite Edges using Ford-Fukerson algorithm
47+
/// </summary>
48+
[TestMethod]
49+
public void MaxBiPartiteMatch_AdjacencyListGraph_Accuracy_Test_1()
50+
{
51+
var graph = new Advanced.Algorithms.DataStructures.Graph.AdjacencyList.Graph<char>();
52+
53+
graph.AddVertex('0');
54+
graph.AddVertex('1');
55+
graph.AddVertex('2');
56+
graph.AddVertex('3');
57+
58+
59+
graph.AddEdge('0', '2');
60+
graph.AddEdge('1', '3');
61+
62+
63+
var algorithm = new BiPartiteMatching<char>(new BiPartiteMatchOperators());
64+
65+
var result = algorithm.GetMaxBiPartiteMatching(graph);
66+
67+
Assert.AreEqual(result.Count, 2);
68+
}
69+
4570
[TestMethod]
4671
public void MaxBiPartiteMatch_AdjacencyMatrixGraph_Smoke_Test()
4772
{
@@ -74,6 +99,31 @@ public void MaxBiPartiteMatch_AdjacencyMatrixGraph_Smoke_Test()
7499

75100
Assert.AreEqual(result.Count, 4);
76101
}
102+
103+
/// <summary>
104+
/// Test Max BiParitite Edges using Ford-Fukerson algorithm
105+
/// </summary>
106+
[TestMethod]
107+
public void MaxBiPartiteMatch_AdjacencyMatrixGraph_Accuracy_Test_1()
108+
{
109+
var graph = new Advanced.Algorithms.DataStructures.Graph.AdjacencyMatrix.Graph<char>();
110+
111+
graph.AddVertex('0');
112+
graph.AddVertex('1');
113+
graph.AddVertex('2');
114+
graph.AddVertex('3');
115+
116+
117+
graph.AddEdge('0', '2');
118+
graph.AddEdge('1', '3');
119+
120+
121+
var algorithm = new BiPartiteMatching<char>(new BiPartiteMatchOperators());
122+
123+
var result = algorithm.GetMaxBiPartiteMatching(graph);
124+
125+
Assert.AreEqual(result.Count, 2);
126+
}
77127
/// <summary>
78128
/// operators for generics
79129
/// implemented for int type for edge weights

0 commit comments

Comments
 (0)