예제 #1
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);
        }
예제 #2
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();
            }
        }