Example #1
0
        public void GraphToCSP(GraphLib.Definitions.Graph graph)
        {
            int verticesCount = graph.VerticesCount;
            int edgesCount    = 0;

            for (int i = 0; i < verticesCount; i++)
            {
                edgesCount += graph.GetNeighbors(i).Count;
            }
            edgesCount /= 2;

            CSP.CspInstance cspInstance3 = Converter.GraphToCSP(graph, 3);
            CSP.CspInstance cspInstance4 = Converter.GraphToCSP(graph, 4);

            Assert.Equal(verticesCount, cspInstance3.Variables.Count);
            Assert.Equal(3 * edgesCount, cspInstance3.Restrictions.Count);
            Assert.Equal(verticesCount, cspInstance4.Variables.Count);
            Assert.Equal(4 * edgesCount, cspInstance4.Restrictions.Count);
        }
Example #2
0
        public static CspInstance GraphToCSP(GraphLib.Definitions.Graph graph, int numberOfColors = 3)
        {
            List <Variable> addedVariables = new();

            CspInstance cspInstance = new();

            for (int i = 0; i < graph.VerticesCount; i++)
            {
                addedVariables.Add(new Variable(numberOfColors));
                addedVariables[i].Id = i;
                cspInstance.AddVariable(addedVariables[i]);
                foreach (var neighbour in graph.GetNeighbors(i))
                {
                    if (neighbour < i)
                    {
                        for (int j = 0; j < numberOfColors; j++)
                        {
                            cspInstance.AddRestriction(new Pair(addedVariables[i], addedVariables[i].AvalibleColors[j]), new Pair(addedVariables[neighbour], addedVariables[neighbour].AvalibleColors[j]));
                        }
                    }
                }
            }
            return(cspInstance);
        }