Beispiel #1
0
        public void HopcroftKarp_AdjacencyListGraph_Smoke_Test()
        {
            var graph = new Advanced.Algorithms.DataStructures.Graph.AdjacencyList.Graph <char>();

            graph.AddVertex('A');
            graph.AddVertex('B');
            graph.AddVertex('C');
            graph.AddVertex('D');
            graph.AddVertex('E');

            graph.AddVertex('F');
            graph.AddVertex('G');
            graph.AddVertex('H');
            graph.AddVertex('I');

            graph.AddEdge('A', 'F');
            graph.AddEdge('B', 'F');
            graph.AddEdge('B', 'G');
            graph.AddEdge('C', 'H');
            graph.AddEdge('C', 'I');
            graph.AddEdge('D', 'G');
            graph.AddEdge('D', 'H');
            graph.AddEdge('E', 'F');
            graph.AddEdge('E', 'I');

            var algorithm = new HopcroftKarpMatching <char>();

            var result = algorithm.GetMaxBiPartiteMatching(graph);

            Assert.AreEqual(result.Count, 4);
        }
        public void HopcroftKarp_AdjacencyListGraph_Accurancy_Test_Fully_Connected_Bipartite_Graph()
        {
            var graph = new Advanced.Algorithms.DataStructures.Graph.AdjacencyList.Graph <char>();

            graph.AddVertex('A');
            graph.AddVertex('B');
            graph.AddVertex('C');
            graph.AddVertex('D');
            graph.AddVertex('E');
            graph.AddVertex('F');

            graph.AddEdge('A', 'D');
            graph.AddEdge('A', 'E');
            graph.AddEdge('A', 'F');
            graph.AddEdge('B', 'D');
            graph.AddEdge('B', 'E');
            graph.AddEdge('B', 'F');
            graph.AddEdge('C', 'D');
            graph.AddEdge('C', 'E');
            graph.AddEdge('C', 'F');

            var algorithm = new HopcroftKarpMatching <char>();

            var result = algorithm.GetMaxBiPartiteMatching(graph);

            Assert.AreEqual(result.Count, 3);
        }
Beispiel #3
0
        public void DepthFirst_AdjacencyListGraph_Smoke_Test()
        {
            var graph = new Advanced.Algorithms.DataStructures.Graph.AdjacencyList.Graph <char>();

            graph.AddVertex('A');
            graph.AddVertex('B');
            graph.AddVertex('C');
            graph.AddVertex('D');
            graph.AddVertex('E');

            graph.AddVertex('F');
            graph.AddVertex('G');
            graph.AddVertex('H');
            graph.AddVertex('I');

            graph.AddEdge('A', 'F');
            graph.AddEdge('B', 'F');
            graph.AddEdge('B', 'G');
            graph.AddEdge('C', 'H');
            graph.AddEdge('C', 'I');
            graph.AddEdge('D', 'G');
            graph.AddEdge('D', 'H');
            graph.AddEdge('E', 'F');
            graph.AddEdge('E', 'I');

            var algorithm = new DepthFirst <char>();

            Assert.IsTrue(algorithm.Find(graph, 'D'));

            Assert.IsFalse(algorithm.Find(graph, 'M'));
        }
        public void TarjanBridge_AdjacencyListGraph_Smoke_Test()
        {
            var graph = new Advanced.Algorithms.DataStructures.Graph.AdjacencyList.Graph <char>();

            graph.AddVertex('A');
            graph.AddVertex('B');
            graph.AddVertex('C');
            graph.AddVertex('D');
            graph.AddVertex('E');
            graph.AddVertex('F');
            graph.AddVertex('G');
            graph.AddVertex('H');


            graph.AddEdge('A', 'B');
            graph.AddEdge('A', 'C');
            graph.AddEdge('B', 'C');

            graph.AddEdge('C', 'D');
            graph.AddEdge('D', 'E');

            graph.AddEdge('E', 'F');
            graph.AddEdge('F', 'G');
            graph.AddEdge('G', 'E');

            graph.AddEdge('F', 'H');

            var algorithm = new TarjansBridgeFinder <char>();

            var result = algorithm.FindBridges(graph);

            Assert.AreEqual(3, result.Count);

            var expected = new List <char[]>()
            {
                new char[] { 'C', 'D' },
                new char[] { 'D', 'E' },
                new char[] { 'F', 'H' }
            };

            foreach (var bridge in result)
            {
                Assert.IsTrue(expected.Any(x => bridge.vertexA == x[0] &&
                                           bridge.vertexB == x[1]));
            }
        }
Beispiel #5
0
        public static Dictionary <int, int> _GraphColorHalfEdgesDict(this Mesh M, int numberOfColors = 2)
        {
            var graph = new Advanced.Algorithms.DataStructures.Graph.AdjacencyList.Graph <int>();


            var V = M._FEFlattenV();

            foreach (var v in V)
            {
                graph.AddVertex(v);
            }

            var E = M._eehalf();

            foreach (var edges in E)
            {
                foreach (var e in edges)
                {
                    if (!graph.HasEdge(e[0], e[1]))
                    {
                        graph.AddEdge(e[0], e[1]);
                    }
                }
            }

            MColorer <int, string>     algorithm = new MColorer <int, string>();
            MColorResult <int, string> result    = algorithm.Color(graph, Letters(numberOfColors));
            Dictionary <int, int>      dtResult  = new Dictionary <int, int>();


            if (result.Partitions != null)
            {
                int counter = 0;
                foreach (KeyValuePair <string, List <int> > p in result.Partitions)
                {
                    foreach (int id in p.Value)
                    {
                        dtResult.Add(id, counter);
                    }
                    counter++;
                }
            }

            return(dtResult);
        }
        public void MaxBiPartiteMatch_AdjacencyListGraph_Accurancy_Test()
        {
            var graph = new Advanced.Algorithms.DataStructures.Graph.AdjacencyList.Graph <char>();

            graph.AddVertex('E');
            graph.AddVertex('N');
            graph.AddVertex('J');
            graph.AddVertex('O');
            graph.AddVertex('Y');
            graph.AddVertex('Z');

            graph.AddVertex('1');
            graph.AddVertex('2');
            graph.AddVertex('3');
            graph.AddVertex('4');
            graph.AddVertex('5');
            graph.AddVertex('6');

            graph.AddEdge('E', '1');

            graph.AddEdge('N', '4');

            graph.AddEdge('J', '1');
            graph.AddEdge('J', '2');
            graph.AddEdge('J', '4');

            graph.AddEdge('O', '2');
            graph.AddEdge('O', '3');

            graph.AddEdge('Y', '3');
            graph.AddEdge('Y', '5');

            graph.AddEdge('Z', '4');
            graph.AddEdge('Z', '5');
            graph.AddEdge('Z', '6');

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

            var result = algorithm.GetMaxBiPartiteMatching(graph);

            Assert.AreEqual(result.Count, 6);
        }
        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);
        }
        public void MColoring_AdjacencyListGraph_Smoke_Test()
        {
            var graph = new Advanced.Algorithms.DataStructures.Graph.AdjacencyList.Graph <int>();

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

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

            var algorithm = new MColorer <int, string>();

            var result = algorithm.Color(graph, new string[] { "red", "green", "blue" });

            Assert.IsTrue(result.CanColor);
        }
        public void TarjanArticulation_AdjacencyListGraph_Smoke_Test()
        {
            var graph = new Advanced.Algorithms.DataStructures.Graph.AdjacencyList.Graph <char>();

            graph.AddVertex('A');
            graph.AddVertex('B');
            graph.AddVertex('C');
            graph.AddVertex('D');
            graph.AddVertex('E');
            graph.AddVertex('F');
            graph.AddVertex('G');
            graph.AddVertex('H');


            graph.AddEdge('A', 'B');
            graph.AddEdge('A', 'C');
            graph.AddEdge('B', 'C');

            graph.AddEdge('C', 'D');
            graph.AddEdge('D', 'E');

            graph.AddEdge('E', 'F');
            graph.AddEdge('F', 'G');
            graph.AddEdge('G', 'E');

            graph.AddEdge('F', 'H');

            var algorithm = new TarjansArticulationFinder <char>();

            var result = algorithm.FindArticulationPoints(graph);

            Assert.AreEqual(4, result.Count);

            var expectedResult = new char[] { 'C', 'D', 'E', 'F' };

            foreach (var v in result)
            {
                Assert.IsTrue(expectedResult.Contains(v));
            }
        }
Beispiel #10
0
        public static DataTree <int> _GraphColorFaces(this Mesh M, int numberOfColors = 2)
        {
            var graph = new Advanced.Algorithms.DataStructures.Graph.AdjacencyList.Graph <int>();



            List <int>[] adj = M.GetNgonFaceAdjacencyOrdered();

            for (int i = 0; i < adj.Length; i++)
            {
                graph.AddVertex(i);
            }

            for (int i = 0; i < adj.Length; i++)
            {
                foreach (var e in adj[i])
                {
                    if (!graph.HasEdge(i, e))
                    {
                        graph.AddEdge(i, e);
                    }
                }
            }

            MColorer <int, string>     algorithm = new MColorer <int, string>();
            MColorResult <int, string> result    = algorithm.Color(graph, Letters(numberOfColors));
            DataTree <int>             dtResult  = new DataTree <int>();


            if (result.Partitions != null)
            {
                int counter = 0;
                foreach (KeyValuePair <string, List <int> > p in result.Partitions)
                {
                    dtResult.AddRange(p.Value, new GH_Path(counter++));
                }
            }

            return(dtResult);
        }
Beispiel #11
0
        public void TarjanIsBiConnected_AdjacencyListGraph_Smoke_Test()
        {
            var graph = new Advanced.Algorithms.DataStructures.Graph.AdjacencyList.Graph <char>();

            graph.AddVertex('A');
            graph.AddVertex('B');
            graph.AddVertex('C');

            graph.AddEdge('A', 'B');
            graph.AddEdge('A', 'C');
            graph.AddEdge('B', 'C');

            var algorithm = new TarjansBiConnected <char>();

            var result = algorithm.IsBiConnected(graph);

            Assert.IsTrue(result);

            graph.AddVertex('D');
            graph.AddVertex('E');
            graph.AddVertex('F');
            graph.AddVertex('G');
            graph.AddVertex('H');

            graph.AddEdge('C', 'D');
            graph.AddEdge('D', 'E');

            graph.AddEdge('E', 'F');
            graph.AddEdge('F', 'G');
            graph.AddEdge('G', 'E');

            graph.AddEdge('F', 'H');

            result = algorithm.IsBiConnected(graph);

            Assert.IsFalse(result);
        }