public GraphColoringAlgorithm(Graph.IGraphInterface graph)
 {
     this.graph            = graph;
     countInterchangeCalls = 0;
     coloredGraph          = graph.GetColoredGraph();
     countVertices         = graph.GetRealCountVertices();
 }
Пример #2
0
        /// <summary>
        /// Write a graph to a file
        /// If the graph is not initialized throws GraphInitializationException
        /// </summary>
        /// <param name="graph">graph</param>
        /// <returns>Is everything OK</returns>
        public bool WriteFile(Graph.IGraphInterface graph)
        {
            if (!graph.GetIsInitialized())
            {
                throw new MyException.GraphException.GraphInitializationException();
            }

            if (ExistsFile())
            {
                DeleteFile();
            }

            CreateFile();

            using (StreamWriter streamWriter = new StreamWriter(GetPath()))
            {
                // Header
                streamWriter.WriteLine(READERWRITERHEADER + Graph.Graph.GraphRepresentationEnum.adjacencyList);
                streamWriter.WriteLine(READERWRITERBALLAST);
                streamWriter.WriteLine(READERWRITERNAME + graph.GetName());
                streamWriter.WriteLine(READERWRITERCOUNTVERTICES + graph.GetRealCountVertices());
                streamWriter.WriteLine(READERWRITERBALLAST);

                // Graph
                streamWriter.WriteLine(READERWRITERGRAPH);

                foreach (Graph.Vertex vertex in graph.AllVertices())
                {
                    streamWriter.WriteLine(vertex.GetUserName());

                    foreach (Graph.Vertex neighbour in graph.Neighbours(vertex))
                    {
                        streamWriter.WriteLine(LEFTSEPARATORADJACENCYLIST + neighbour.GetUserName() + RIGHTSEPARATORADJACENCYLIST);
                    }
                }

                // Colored graph
                streamWriter.WriteLine(READERWRITERBALLAST);
                streamWriter.Write(READERWRITERCOLOREDGRAPH);
            }

            return(true);
        }
Пример #3
0
        public void Color()
        {
            // Variable
            Graph.IVertexInterface        vertex;
            List <Graph.IVertexInterface> neighboursVertexList;

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

            coloredGraph.ResetColors();

            while (!graph.GetColoredGraph().AreAllVerticesColored())
            {
                copyGraph = Graph.GraphOperation.GraphOperation.SubGraph(graph, graph.GetColoredGraph().GetUnColoredVertexList());

                while (copyGraph.GetRealCountVertices() != 0)
                {
                    vertex = copyGraph.GetGraphProperty().GetVertexWithDegree(copyGraph.GetGraphProperty().GetMinimumVertexDegree());
                    neighboursVertexList = copyGraph.Neighbours(vertex);

                    copyGraph.VertexDelete(vertex);
                    foreach (Graph.IVertexInterface neighbour in neighboursVertexList)
                    {
                        copyGraph.VertexDelete(neighbour);
                    }

                    graph.GetColoredGraph().ColorVertex(graph.GetVertexByUserName(vertex.GetUserName()), color);
                }

                color++;
            }

            bool isColored = coloredGraph.InitializeColoredGraph();

            if (!isColored)
            {
                throw new MyException.GraphColoringAlgorithmException.AlgorithmGraphIsNotColored();
            }
        }
Пример #4
0
        private void Testing(ErdosRenyiModelEnum erdosRenyiModelEnum)
        {
            try
            {
                switch (erdosRenyiModelEnum)
                {
                case ErdosRenyiModelEnum.erdosRenyiModelCDividedByNLessThanOne:
                    stringBuilder.AppendLine(erdosRenyiModelEnum.ToString());
                    erdosRenyiModel = new ErdosRenyiModel(COUNTVERTICES, ErdosRenyiModel.ErdosRenyiModelProbabilityEnum.cDividedByNLessThanOne);
                    graph           = erdosRenyiModel.GenerateGraph();

                    if (graph.GetRealCountVertices() != COUNTVERTICES)
                    {
                        throw new MyException.GraphException.GraphInvalidCountVerticesException("The number of vertices of generated graph is wrong!");
                    }

                    stringBuilder.AppendLine("OK");
                    break;

                case ErdosRenyiModelEnum.erdosRenyiModelCDividedByNMoreThanOne:
                    stringBuilder.AppendLine(erdosRenyiModelEnum.ToString());
                    erdosRenyiModel = new ErdosRenyiModel(COUNTVERTICES, ErdosRenyiModel.ErdosRenyiModelProbabilityEnum.cDividedByNMoreThanOne);
                    graph           = erdosRenyiModel.GenerateGraph();

                    if (graph.GetRealCountVertices() != COUNTVERTICES)
                    {
                        throw new MyException.GraphException.GraphInvalidCountVerticesException("The number of vertices of generated graph is wrong!");
                    }

                    stringBuilder.AppendLine("OK");
                    break;

                case ErdosRenyiModelEnum.erdosRenyiModelCLogNDividedByN:
                    stringBuilder.AppendLine(erdosRenyiModelEnum.ToString());
                    erdosRenyiModel = new ErdosRenyiModel(COUNTVERTICES, ErdosRenyiModel.ErdosRenyiModelProbabilityEnum.cLogNDividedByN);
                    graph           = erdosRenyiModel.GenerateGraph();

                    if (graph.GetRealCountVertices() != COUNTVERTICES)
                    {
                        throw new MyException.GraphException.GraphInvalidCountVerticesException("The number of vertices of generated graph is wrong!");
                    }

                    stringBuilder.AppendLine("OK");
                    break;

                case ErdosRenyiModelEnum.erdosRenyiModelNotAssigned:
                    stringBuilder.AppendLine(erdosRenyiModelEnum.ToString());
                    erdosRenyiModel = new ErdosRenyiModel(COUNTVERTICES, ErdosRenyiModel.ErdosRenyiModelProbabilityEnum.notAssigned);
                    graph           = erdosRenyiModel.GenerateGraph();

                    if (erdosRenyiModel.GetErdosRenyiModelProbabilityEnum() == ErdosRenyiModel.ErdosRenyiModelProbabilityEnum.notAssigned)
                    {
                        throw new MyException.GenerateGraphException.ErdosReneiModelChoosePNotAssigned();
                    }

                    if (graph.GetRealCountVertices() != COUNTVERTICES)
                    {
                        throw new MyException.GraphException.GraphInvalidCountVerticesException("The number of vertices of generated graph is wrong!");
                    }

                    stringBuilder.AppendLine("OK");
                    break;

                case ErdosRenyiModelEnum.invalidVerticesCount:
                    stringBuilder.AppendLine(erdosRenyiModelEnum.ToString());
                    erdosRenyiModel = new ErdosRenyiModel(0, ErdosRenyiModel.ErdosRenyiModelProbabilityEnum.notAssigned);
                    graph           = erdosRenyiModel.GenerateGraph();
                    break;

                default:
                    throw new MyException.TestsException.TestsMissingTestException();
                }
            }
            catch (Exception e)
            {
                stringBuilder.AppendLine(e.Message);
            }
        }