diff --git a/src/Advanced.Algorithms/Geometry/BentleyOttmann.cs b/src/Advanced.Algorithms/Geometry/BentleyOttmann.cs index ed6b2cfd..6e68152e 100644 --- a/src/Advanced.Algorithms/Geometry/BentleyOttmann.cs +++ b/src/Advanced.Algorithms/Geometry/BentleyOttmann.cs @@ -12,7 +12,7 @@ public class BentleyOttmann { private readonly PointComparer pointComparer; - private HashSet verticalHorizontalLines; + private HashSet verticalAndHorizontalLines; private HashSet otherLines; private BHeap eventQueue; @@ -41,7 +41,7 @@ private void initialize(IEnumerable lineSegments) currentlyTrackedLines = new RedBlackTree(true, pointComparer); intersectionEvents = new Dictionary>>(pointComparer); - verticalHorizontalLines = new HashSet(); + verticalAndHorizontalLines = new HashSet(); otherLines = new HashSet(); rightLeftEventLookUp = lineSegments @@ -82,9 +82,9 @@ public Dictionary> FindIntersections(IEnumerable lineSeg case EventType.Start: //special case - if (verticalHorizontalLines.Count > 0) + if (verticalAndHorizontalLines.Count > 0) { - foreach (var line in verticalHorizontalLines) + foreach (var line in verticalAndHorizontalLines) { var intersection = findIntersection(currentEvent, line); recordIntersection(currentEvent, line, intersection); @@ -94,7 +94,7 @@ public Dictionary> FindIntersections(IEnumerable lineSeg //special case if (currentEvent.Segment.IsVertical || currentEvent.Segment.IsHorizontal) { - verticalHorizontalLines.Add(currentEvent); + verticalAndHorizontalLines.Add(currentEvent); foreach (var line in otherLines) { @@ -129,7 +129,7 @@ public Dictionary> FindIntersections(IEnumerable lineSeg //special case if (currentEvent.Segment.IsVertical || currentEvent.Segment.IsHorizontal) { - verticalHorizontalLines.Remove(currentEvent); + verticalAndHorizontalLines.Remove(currentEvent); break; } diff --git a/src/Advanced.Algorithms/Graph/Coloring/MColorer.cs b/src/Advanced.Algorithms/Graph/Coloring/MColorer.cs index fb50a299..975a6ddb 100644 --- a/src/Advanced.Algorithms/Graph/Coloring/MColorer.cs +++ b/src/Advanced.Algorithms/Graph/Coloring/MColorer.cs @@ -14,21 +14,32 @@ public class MColorer /// public MColorResult Color(IGraph graph, C[] colors) { + var totalProgress = new Dictionary, C>(); - var first = graph.ReferenceVertex; - - var progress = canColor(first, colors, - new Dictionary, C>(), - new HashSet>()); + foreach (var vertex in graph.VerticesAsEnumberable) + { + var progress = canColor(vertex, colors, + new Dictionary, C>(), + new HashSet>()); + + 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(false, null); } var result = new Dictionary>(); - foreach (var vertex in progress) + foreach (var vertex in totalProgress) { if (!result.ContainsKey(vertex.Value)) { diff --git a/tests/Advanced.Algorithms.Tests/Graph/Matching/BiPartiteMatching_Tests.cs b/tests/Advanced.Algorithms.Tests/Graph/Matching/BiPartiteMatching_Tests.cs index acdcc493..c126ad33 100644 --- a/tests/Advanced.Algorithms.Tests/Graph/Matching/BiPartiteMatching_Tests.cs +++ b/tests/Advanced.Algorithms.Tests/Graph/Matching/BiPartiteMatching_Tests.cs @@ -42,6 +42,31 @@ public void MaxBiPartiteMatch_AdjacencyListGraph_Smoke_Test() Assert.AreEqual(result.Count, 4); } + /// + /// Test Max BiParitite Edges using Ford-Fukerson algorithm + /// + [TestMethod] + public void MaxBiPartiteMatch_AdjacencyListGraph_Accuracy_Test_1() + { + var graph = new Advanced.Algorithms.DataStructures.Graph.AdjacencyList.Graph(); + + graph.AddVertex('0'); + graph.AddVertex('1'); + graph.AddVertex('2'); + graph.AddVertex('3'); + + + graph.AddEdge('0', '2'); + graph.AddEdge('1', '3'); + + + var algorithm = new BiPartiteMatching(new BiPartiteMatchOperators()); + + var result = algorithm.GetMaxBiPartiteMatching(graph); + + Assert.AreEqual(result.Count, 2); + } + [TestMethod] public void MaxBiPartiteMatch_AdjacencyMatrixGraph_Smoke_Test() { @@ -74,6 +99,31 @@ public void MaxBiPartiteMatch_AdjacencyMatrixGraph_Smoke_Test() Assert.AreEqual(result.Count, 4); } + + /// + /// Test Max BiParitite Edges using Ford-Fukerson algorithm + /// + [TestMethod] + public void MaxBiPartiteMatch_AdjacencyMatrixGraph_Accuracy_Test_1() + { + var graph = new Advanced.Algorithms.DataStructures.Graph.AdjacencyMatrix.Graph(); + + graph.AddVertex('0'); + graph.AddVertex('1'); + graph.AddVertex('2'); + graph.AddVertex('3'); + + + graph.AddEdge('0', '2'); + graph.AddEdge('1', '3'); + + + var algorithm = new BiPartiteMatching(new BiPartiteMatchOperators()); + + var result = algorithm.GetMaxBiPartiteMatching(graph); + + Assert.AreEqual(result.Count, 2); + } /// /// operators for generics /// implemented for int type for edge weights