Example #1
0
        public static DataTree <int> Color(List <int> V, List <int> E0, List <int> E1, int numberOfColors = 2)
        {
            var graph = new Advanced.Algorithms.DataStructures.Graph.AdjacencyList.Graph <int>();

            for (int i = 0; i < V.Count; i++)
            {
                graph.AddVertex(V[i]);
            }

            for (int i = 0; i < E0.Count; i++)
            {
                if (!graph.HasEdge(E0[i], E1[i]))
                {
                    graph.AddEdge(E0[i], E1[i]);
                }
            }


            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);
        }
        /// <summary>
        /// Returns a list of Max BiPartite Match Edges
        /// </summary>
        /// <param name="graph"></param>
        /// <returns></returns>
        public List <MatchEdge <T> > GetMaxBiPartiteMatching(Graph <T> graph)
        {
            //check if the graph is BiPartite by coloring 2 colors
            var mColorer    = new MColorer <T, int>();
            var colorResult = mColorer.Color(graph, new int[] { 1, 2 });

            if (colorResult.CanColor == false)
            {
                throw new Exception("Graph is not BiPartite.");
            }

            return(GetMaxBiPartiteMatching(graph, colorResult.Partitions));
        }
Example #3
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 MColoring_Smoke_Test()
        {
            var graph = new 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 algo = new MColorer <int, string>();

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

            Assert.IsTrue(result.CanColor);
        }
        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);
        }
Example #6
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);
        }