Skip to content

Commit

Permalink
#35 Bipartitie Matching Bug Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
justcoding121 committed Oct 10, 2020
1 parent 0e1b90a commit e4b9fa2
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/Advanced.Algorithms/Graph/Coloring/MColorer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,32 @@ public class MColorer<T, C>
/// </summary>
public MColorResult<T, C> Color(IGraph<T> graph, C[] colors)
{
var totalProgress = new Dictionary<IGraphVertex<T>, C>();

var first = graph.ReferenceVertex;

var progress = canColor(first, colors,
new Dictionary<IGraphVertex<T>, C>(),
new HashSet<IGraphVertex<T>>());
foreach (var vertex in graph.VerticesAsEnumberable)
{
var progress = canColor(vertex, colors,
new Dictionary<IGraphVertex<T>, C>(),
new HashSet<IGraphVertex<T>>());

foreach(var item in progress)
{
if (!totalProgress.ContainsKey(item.Key))
{
totalProgress.Add(item.Key, item.Value);
}
}

}

if (progress.Count != graph.VerticesCount)
if (totalProgress.Count != graph.VerticesCount)
{
return new MColorResult<T, C>(false, null);
}

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

foreach (var vertex in progress)
foreach (var vertex in totalProgress)
{
if (!result.ContainsKey(vertex.Value))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,31 @@ public void MaxBiPartiteMatch_AdjacencyListGraph_Smoke_Test()
Assert.AreEqual(result.Count, 4);
}

/// <summary>
/// Test Max BiParitite Edges using Ford-Fukerson algorithm
/// </summary>
[TestMethod]
public void MaxBiPartiteMatch_AdjacencyListGraph_Accuracy_Test_1()
{
var graph = new Advanced.Algorithms.DataStructures.Graph.AdjacencyList.Graph<char>();

graph.AddVertex('0');
graph.AddVertex('1');
graph.AddVertex('2');
graph.AddVertex('3');


graph.AddEdge('0', '2');
graph.AddEdge('1', '3');


var algorithm = new BiPartiteMatching<char>(new BiPartiteMatchOperators());

var result = algorithm.GetMaxBiPartiteMatching(graph);

Assert.AreEqual(result.Count, 2);
}

[TestMethod]
public void MaxBiPartiteMatch_AdjacencyMatrixGraph_Smoke_Test()
{
Expand Down Expand Up @@ -74,6 +99,31 @@ public void MaxBiPartiteMatch_AdjacencyMatrixGraph_Smoke_Test()

Assert.AreEqual(result.Count, 4);
}

/// <summary>
/// Test Max BiParitite Edges using Ford-Fukerson algorithm
/// </summary>
[TestMethod]
public void MaxBiPartiteMatch_AdjacencyMatrixGraph_Accuracy_Test_1()
{
var graph = new Advanced.Algorithms.DataStructures.Graph.AdjacencyMatrix.Graph<char>();

graph.AddVertex('0');
graph.AddVertex('1');
graph.AddVertex('2');
graph.AddVertex('3');


graph.AddEdge('0', '2');
graph.AddEdge('1', '3');


var algorithm = new BiPartiteMatching<char>(new BiPartiteMatchOperators());

var result = algorithm.GetMaxBiPartiteMatching(graph);

Assert.AreEqual(result.Count, 2);
}
/// <summary>
/// operators for generics
/// implemented for int type for edge weights
Expand Down

0 comments on commit e4b9fa2

Please sign in to comment.