public GraphColoringAlgorithm(Graph.IGraphInterface graph)
 {
     this.graph            = graph;
     countInterchangeCalls = 0;
     coloredGraph          = graph.GetColoredGraph();
     countVertices         = graph.GetRealCountVertices();
 }
        private void Testing(SmallestLastSequenceEnum smallestLastSequence)
        {
            try
            {
                testPath = ReaderWriter.ReaderWriter.CreateTestFile(testsDictionary[smallestLastSequence]);

                reader = new ReaderWriter.ReaderGraph(testPath, false);
                graph  = reader.ReadFile();

                SmallestLastSequence smallestLastSequenceAlgorithm = new SmallestLastSequence(graph);
                smallestLastSequenceAlgorithm.Color();

                stringBuilder.AppendLine(smallestLastSequence.ToString());
                stringBuilder.AppendLine("Graph colored.");
                stringBuilder.AppendLine(graph.GetColoredGraph().ToString());
            }
            catch (KeyNotFoundException)
            {
                throw new MyException.TestsException.TestsMissingTestException(smallestLastSequence.ToString());
            }
            catch (MyException.ReaderWriterException.ReaderWriterException e)
            {
                stringBuilder.AppendLine(e.Message);
            }
        }
        private void Testing(ConnectedSequentialEnum connectedSequential)
        {
            try
            {
                testPath = ReaderWriter.ReaderWriter.CreateTestFile(testsDictionary[connectedSequential]);

                reader = new ReaderWriter.ReaderGraph(testPath, false);
                graph  = reader.ReadFile();

                ConnectedSequential optimalAlgorithm = new ConnectedSequential(graph);
                optimalAlgorithm.Color();

                stringBuilder.AppendLine(connectedSequential.ToString());
                stringBuilder.AppendLine("Graph colored.");
                stringBuilder.AppendLine(graph.GetColoredGraph().ToString());
            }
            catch (KeyNotFoundException)
            {
                throw new MyException.TestsException.TestsMissingTestException(connectedSequential.ToString());
            }
            catch (MyException.ReaderWriterException.ReaderWriterException e)
            {
                stringBuilder.AppendLine(e.Message);
            }
        }
        private void Testing(GreedyIndependentSetEnum greedyIndependentSet)
        {
            try
            {
                testPath = ReaderWriter.ReaderWriter.CreateTestFile(testsDictionary[greedyIndependentSet]);

                reader = new ReaderWriter.ReaderGraph(testPath, false);
                graph  = reader.ReadFile();

                GreedyIndependentSet greedyIndependentSetAlgorithm = new GreedyIndependentSet(graph);
                greedyIndependentSetAlgorithm.Color();

                stringBuilder.AppendLine(greedyIndependentSetAlgorithm.ToString());
                stringBuilder.AppendLine("Graph colored.");
                stringBuilder.AppendLine(graph.GetColoredGraph().ToString());
            }
            catch (KeyNotFoundException)
            {
                throw new MyException.TestsException.TestsMissingTestException(greedyIndependentSet.ToString());
            }
            catch (MyException.ReaderWriterException.ReaderWriterException e)
            {
                stringBuilder.AppendLine(e.Message);
            }
        }
Пример #5
0
        /// <summary>
        /// Write a colored graph to a file
        /// If the colored graph is not initialized throws ColoredGraphNotInitializationException
        /// </summary>
        /// <param name="graph">Colored graph</param>
        /// <param name="graphColoringAlgorithm">Algorithm which was used for coloring the graph</param>
        /// <param name="isOptimal">Is the algorithm optimal for the graph</param>
        public bool WriteFileColor(Graph.IGraphInterface graph, GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum graphColoringAlgorithm, bool isOptimal)
        {
            // Variable
            Graph.IColoredGraphInterface coloredGraph;

            coloredGraph = graph.GetColoredGraph();

            if (!coloredGraph.GetIsInitializedColoredGraph())
            {
                throw new MyException.GraphException.ColoredGraphNotInitializationException();
            }

            if (CheckIfRecordExists(graphColoringAlgorithm))
            {
                return(false);
            }

            using (StreamWriter streamWriter = File.AppendText(GetPath()))
            {
                streamWriter.WriteLine();

                // Number of colors
                if (isOptimal)
                {
                    streamWriter.WriteLine(READERWRITERCHROMATICNUMBER + coloredGraph.GetCountUsedColors());
                }
                else
                {
                    streamWriter.WriteLine(READERWRITERNUMBEROFCOLORS + coloredGraph.GetCountUsedColors());
                }

                // Used algorithm
                streamWriter.WriteLine(READERWRITERUSEDALGORITHM + graphColoringAlgorithm);

                // Colored graph
                List <int> colorList = coloredGraph.UsedColors();

                foreach (int color in colorList)
                {
                    List <Graph.IVertexInterface> vertexList = coloredGraph.ColoredVertices(color);

                    streamWriter.WriteLine("- " + color);
                    vertexList.ForEach(vertex => { streamWriter.WriteLine("-- " + vertex.GetUserName()); });
                }
            }

            return(true);
        }
Пример #6
0
        private void Testing(GeneticAlgorithmEnum geneticAlgorithm)
        {
            try
            {
                erdosRenyiModel = new GenerateGraph.ErdosRenyiModel.ErdosRenyiModel(random.Next(15, 25));
                graph           = erdosRenyiModel.GenerateGraph();

                GeneticAlgorithm algorithm = new GeneticAlgorithm(graph, 2, random.Next(250, 300));
                algorithm.Color();

                stringBuilder.AppendLine(algorithm.ToString());
                stringBuilder.AppendLine("Graph colored.");
                stringBuilder.AppendLine("Is valid colored: " + graph.GetColoredGraph().IsValidColored().ToString());
            }
            catch (KeyNotFoundException)
            {
                throw new MyException.TestsException.TestsMissingTestException(geneticAlgorithm.ToString());
            }
            catch (MyException.ReaderWriterException.ReaderWriterException e)
            {
                stringBuilder.AppendLine(e.Message);
            }
        }
Пример #7
0
        /// <summary>
        /// Return true if a graph has been colored in polynomal time, otherwise false
        /// </summary>
        /// <param name="graph">graph</param>
        /// <returns>true if graph has been colored in polynomal time, otherwise false</returns>
        public static bool TryOptimalColorInPolynomalTime(Graph.IGraphInterface graph)
        {
            bool isColored;
            IGraphColoringAlgorithmInterface algorithm;
            List <Graph.IVertexInterface>    optimalVertexList;

            Graph.IColoredGraphInterface coloredGraph = graph.GetColoredGraph();

            coloredGraph.ResetColors();

            // If the graph is chordal => use PEO for coloring
            if (graph.GetGraphProperty().GetIsChordal())
            {
                optimalVertexList = graph.GetGraphProperty().GetPerfectEliminationOrdering();

                coloredGraph.GreedyColoring(optimalVertexList);
                isColored = coloredGraph.InitializeColoredGraph();

                if (!isColored)
                {
                    throw new MyException.GraphColoringAlgorithmException.AlgorithmGraphIsNotColored();
                }

                return(true);
            }

            switch (graph.GetGraphProperty().GetGraphClass())
            {
            case Graph.GraphClass.GraphClass.GraphClassEnum.bipartiteGraph:
                Tuple <List <Graph.IVertexInterface>, List <Graph.IVertexInterface> > partites = graph.GetGraphProperty().GetPartites();

                coloredGraph.GreedyColoring(partites.Item1.Concat(partites.Item2).ToList());
                isColored = coloredGraph.InitializeColoredGraph();

                if (!isColored)
                {
                    throw new MyException.GraphColoringAlgorithmException.AlgorithmGraphIsNotColored();
                }

                return(true);

            case Graph.GraphClass.GraphClass.GraphClassEnum.completeBipartiteGraph:
                algorithm = new SequenceAlgorithm.SmallestLastSequence.SmallestLastSequence(graph);

                algorithm.Color();

                return(true);

            case Graph.GraphClass.GraphClass.GraphClassEnum.completeGraph:
                coloredGraph.GreedyColoring(graph.AllVertices());
                isColored = coloredGraph.InitializeColoredGraph();

                if (!isColored)
                {
                    throw new MyException.GraphColoringAlgorithmException.AlgorithmGraphIsNotColored();
                }

                return(true);

            case Graph.GraphClass.GraphClass.GraphClassEnum.cycleGraph:
            case Graph.GraphClass.GraphClass.GraphClassEnum.treeGraph:
                algorithm = new SequenceAlgorithm.ConnectedSequential.ConnectedSequential(graph);

                algorithm.Color();

                return(true);
            }

            return(false);
        }