Exemple #1
0
        public void GetSymmetricalDifference_ShouldReturn_Count_0()
        {
            var list1 = new List <Edge <int> >
            {
                new Edge <int>(2, 3),
                new Edge <int>(0, 1),
                new Edge <int>(4, 5),
                new Edge <int>(1, 2),
                new Edge <int>(2, 5),
                new Edge <int>(7, 8),
                new Edge <int>(2, 5),
                new Edge <int>(3, 88),
                new Edge <int>(8, 5),
            };
            var list2 = new List <Edge <int> >()
            {
                new Edge <int>(2, 3),
                new Edge <int>(0, 1),
                new Edge <int>(4, 5),
                new Edge <int>(1, 2),
                new Edge <int>(2, 5),
                new Edge <int>(7, 8),
                new Edge <int>(2, 5),
                new Edge <int>(3, 88),
                new Edge <int>(8, 5),
            };

            var difference = EdmondsAlgorithm.GetSymmetricalDifference(list1, list2);

            Assert.AreEqual(0, difference.Count);

            Assert.IsTrue(IsDifferenceCorrect(difference, list1, list2));
        }
Exemple #2
0
        private static bool CheckIfPathAlwaysExists(UndirectedGraph <int, Edge <int> > g)
        {
            bool pathAlwaysExists = true;

            bool shouldContinue = true;

            foreach (var v1 in g.Vertices)
            {
                if (!shouldContinue)
                {
                    break;
                }
                foreach (var v2 in g.Vertices)
                {
                    bool pathFound = EdmondsAlgorithm.DFSSearch(v1, v2, g, out List <Edge <int> > path);
                    if (!pathFound)
                    {
                        pathAlwaysExists = pathFound;
                        shouldContinue   = false;
                        break;
                    }
                }
            }

            return(pathAlwaysExists);
        }
Exemple #3
0
        private AdvancedAlgorithms.TestResult VerifyMatching(List <Edge <int> > matching, UndirectedGraph <int, Edge <int> > g, int correctEdgesCount)
        {
            if (correctEdgesCount != matching.Count)
            {
                return(new AdvancedAlgorithms.TestResult(false, $"Incorrect matching edges count. " +
                                                         $"Expected <{correctEdgesCount}>. Acutal <{matching.Count}>"));
            }
            var matchedVertices = EdmondsAlgorithm.GetAllVerticesForPath(matching);

            if (correctEdgesCount * 2 != matchedVertices.Count)
            {
                return(new AdvancedAlgorithms.TestResult(false, $"Some vertices in more than one edge." +
                                                         $" Expected <{correctEdgesCount * 2}>. Acutal <{matchedVertices.Count}>"));
            }
            bool allEdgesInGraph = AreAllEdgesInGraph(g, matching);

            if (!allEdgesInGraph)
            {
                return(new AdvancedAlgorithms.TestResult(false, "Not all edges are a part of the original graph"));
            }
            else
            {
                return(new AdvancedAlgorithms.TestResult(true, ""));
            }
        }
Exemple #4
0
        public void TryGetRootForVertex_minus1_ShouldNotFindPath()
        {
            var forest       = GetSampleForest(out int[] levels);
            int sourceVertex = -1;

            bool rootFound = EdmondsAlgorithm.TryGetRootForVertex(forest, sourceVertex, levels, out int foundRootVertex, out List <Edge <int> > pathToRoot);

            Assert.IsFalse(rootFound);
        }
Exemple #5
0
        public void MaximumMatchingCount_ShouldReturn_4()
        {
            var g = GetGraph2();
            int correctEdgesCount = 4;

            var maximumMatching = EdmondsAlgorithm.CalculateMaximumMatching(g);
            var testResult      = VerifyMatching(maximumMatching, g, correctEdgesCount);

            Assert.IsTrue(testResult.IsCorrect, testResult.ErrorMessage);
        }
Exemple #6
0
        public void TryGetRootForVertex_13_ShouldFind_12()
        {
            var forest       = GetSampleForest(out int[] levels);
            int sourceVertex = 13;
            int expectedRoot = 12;

            bool rootFound = EdmondsAlgorithm.TryGetRootForVertex(forest, sourceVertex, levels, out int foundRootVertex, out List <Edge <int> > pathToRoot);

            Assert.IsTrue(rootFound);
            Assert.AreEqual(expectedRoot, foundRootVertex);
        }
Exemple #7
0
        public void MaximumMatchingCount_FullGraph_6Vertices()
        {
            var generator         = new GraphGenerator();
            int verticesCount     = 6;
            var g                 = generator.GetFullGraph(verticesCount);
            int correctEdgesCount = 3;

            var maximumMatching = EdmondsAlgorithm.CalculateMaximumMatching(g);
            var testResult      = VerifyMatching(maximumMatching, g, correctEdgesCount);

            Assert.IsTrue(testResult.IsCorrect, testResult.ErrorMessage);
        }
        public void GetCycleGraph_CountTest_ShouldRetun_10()
        {
            int verticesCount = 10;
            var generator     = new GraphGenerator();
            var g             = generator.GetCycleGraph(verticesCount);

            Assert.AreEqual(verticesCount, g.EdgeCount);

            foreach (var source in g.Vertices)
            {
                foreach (var target in g.Vertices)
                {
                    Assert.IsTrue(EdmondsAlgorithm.DFSSearch(source, target, g, out List <Edge <int> > path));
                }
            }
        }
Exemple #9
0
        public void GetPathEnds_ShouldReturn_1_0()
        {
            var path = new List <Edge <int> >
            {
                new Edge <int>(2, 1),
                new Edge <int>(4, 2),
                new Edge <int>(9, 4),
                new Edge <int>(9, 8),
                new Edge <int>(3, 8),
                new Edge <int>(3, 5),
                new Edge <int>(5, 0),
            };

            EdmondsAlgorithm.GetPathEnds(path, out int beginning, out int end);

            Assert.AreEqual(1, beginning);
            Assert.AreEqual(0, end);
        }
Exemple #10
0
        public void RemoveEdgeFromGraph_ShouldRemove()
        {
            var g = new UndirectedGraph <int, Edge <int> >();

            var edges = GetGraph1().Edges;

            g.AddVerticesAndEdgeRange(edges);

            foreach (var edge in edges)
            {
                int  originalGraphCount = g.EdgeCount;
                bool edgeRemoved        = EdmondsAlgorithm.RemoveEdgeFromGraph(g, edge, out UndirectedGraph <int, Edge <int> > smallerGraph);
                g = smallerGraph;

                Assert.IsTrue(edgeRemoved, "Edge was not removed");
                int edgesCountDifference = originalGraphCount - smallerGraph.EdgeCount;
                Assert.AreEqual(1, edgesCountDifference, $"Edges numbers are incorrect. " +
                                $"Expected <{originalGraphCount}>. Actual <{smallerGraph.EdgeCount}>");
            }
        }
Exemple #11
0
        public void ContractGraph_Graph2_test1()
        {
            var g       = GetGraph2();
            var blossom = new List <Edge <int> >
            {
                new Edge <int>(1, 2),
                new Edge <int>(2, 4),
                new Edge <int>(1, 4),
            };
            var matching = new List <Edge <int> >
            {
                new Edge <int>(4, 1),
                new Edge <int>(0, 3),
            };

            var contractedGraph = EdmondsAlgorithm.ContractGraph(g, blossom, out int superVertex, matching);

            Assert.IsFalse(IsSuperVertexMatched(superVertex, matching));

            Assert.IsTrue(IsContractedGraphCorrect(contractedGraph, blossom, superVertex));
        }
Exemple #12
0
        public void ContractGraph_Graph1()
        {
            var g       = GetGraph1();
            var blossom = new List <Edge <int> >
            {
                new Edge <int>(0, 4),
                new Edge <int>(0, 1),
                new Edge <int>(1, 4),
            };
            var matching = new List <Edge <int> >
            {
                new Edge <int>(4, 1),
                new Edge <int>(2, 3),
            };

            var contractedGraph = EdmondsAlgorithm.ContractGraph(g, blossom, out int superVertex, matching);

            Assert.IsFalse(IsSuperVertexMatched(superVertex, matching));

            Assert.AreEqual(contractedGraph.EdgeCount, g.EdgeCount - blossom.Count);
        }
Exemple #13
0
        private bool IsPathCorrect(List <Edge <int> > path, UndirectedGraph <int, Edge <int> > g, int source, int target)
        {
            foreach (var edge in path)
            {
                if (!g.ContainsEdge(edge))
                {
                    throw new ArgumentException("Path is not part of the graph");
                }
            }

            if (path.Count == 0 && source != target)
            {
                return(false);
            }

            int currentSource = source;

            foreach (var edge in path)
            {
                try
                {
                    currentSource = EdmondsAlgorithm.GetTargetVertex(edge, currentSource);
                }
                catch (ArgumentException)
                {
                    return(false);
                }
            }

            if (currentSource == target)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemple #14
0
        public void ContractMatching_Count_ReverseOrder_ShouldReturn_2()
        {
            var matching = new List <Edge <int> >()
            {
                new Edge <int>(1, 2),
                new Edge <int>(3, 4),
                new Edge <int>(5, 6),
                new Edge <int>(8, 7),
            };
            var blossom = new List <Edge <int> >()
            {
                new Edge <int>(8, 4),
                new Edge <int>(7, 6),
                new Edge <int>(4, 5),
                new Edge <int>(6, 5),
                new Edge <int>(8, 7),
            };

            var newMatching           = EdmondsAlgorithm.ContractMatching(matching, blossom);
            int expectedMatchingCount = matching.Count - blossom.Count / 2;

            Assert.AreEqual(expectedMatchingCount, newMatching.Count);
        }
Exemple #15
0
 public void GetTargetVertex_ShouldReturn_0()
 {
     Assert.AreEqual(0, EdmondsAlgorithm.GetTargetVertex(new Edge <int>(0, 1), 1));
     Assert.AreEqual(0, EdmondsAlgorithm.GetTargetVertex(new Edge <int>(1, 0), 1));
 }