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(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);
            }
        }
        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);
            }
        }
Ejemplo n.º 4
0
        public LargestFirstSequence(Graph.IGraphInterface graph, GraphColoringAlgorithInterchangeEnum interchangeEnum = GraphColoringAlgorithInterchangeEnum.none) : base(graph)
        {
            // Interchange
            this.interchangeEnum = interchangeEnum;

            switch (interchangeEnum)
            {
            case GraphColoringAlgorithInterchangeEnum.none:
                name           = "Largest first sequence algorithm";
                timeComplexity = TimeComplexityEnum.quadratic;
                break;

            case GraphColoringAlgorithInterchangeEnum.interchange:
                name           = "Largest first sequence interchange algorithm";
                timeComplexity = TimeComplexityEnum.cubic;
                break;

            case GraphColoringAlgorithInterchangeEnum.interchangeExtended:
                name           = "Largest first sequence interchange extended algorithm";
                timeComplexity = TimeComplexityEnum.quartic;
                break;

            case GraphColoringAlgorithInterchangeEnum.interchangeExtendedK3:
                name           = "Largest first sequence interchange extended with K3 algorithm";
                timeComplexity = TimeComplexityEnum.quintic;
                break;
            }
        }
Ejemplo n.º 5
0
 public GraphColoringAlgorithm(Graph.IGraphInterface graph)
 {
     this.graph            = graph;
     countInterchangeCalls = 0;
     coloredGraph          = graph.GetColoredGraph();
     countVertices         = graph.GetRealCountVertices();
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Constructor
        /// Default value for fileNameExtensionEnum is .jpg
        /// </summary>
        /// <param name="graph">The graph</param>
        public GraphVisualization(Graph.IGraphInterface graph,
                                  bool isSchedule, bool showSpanningTree, bool showSimplicialVertex, bool showCutVerticesAndBridges, bool showMaximumAndMinimumDegreeVertices,
                                  FileNameExtensionEnum fileNameExtensionEnum = FileNameExtensionEnum.jpg)
        {
            graphList = new List <Graph.IGraphInterface>();
            graphList.Add(graph);
            this.fileNameExtensionEnum = fileNameExtensionEnum;

            this.isSchedule                          = isSchedule;
            this.showSpanningTree                    = showSpanningTree;
            this.showSimplicialVertex                = showSimplicialVertex;
            this.showCutVerticesAndBridges           = showCutVerticesAndBridges;
            this.showMaximumAndMinimumDegreeVertices = showMaximumAndMinimumDegreeVertices;
        }
Ejemplo n.º 7
0
        /// <summary>
        /// If count of vertices is less than 1 throws GraphInvalidCountVerticesException
        /// </summary>
        /// <param name="countVertices">count of vertices</param>
        /// <param name="erdosRenyiModelProbabilityEnum">probability attribute - deafult = notAssigned</param>
        public ErdosRenyiModel(int countVertices, ErdosRenyiModelProbabilityEnum erdosRenyiModelProbabilityEnum = ErdosRenyiModelProbabilityEnum.notAssigned)
        {
            SetCountVertices(countVertices);
            this.erdosRenyiModelProbabilityEnum = erdosRenyiModelProbabilityEnum;
            usedVerticesHashSet = new HashSet <Graph.IVertexInterface>();

            // Create graph
            graph = new Graph.GraphEdgeList(countVertices);
            graph.SetName(GRAPHNAME);
            graph.FullGenerateVertices();
            graph.InitializeGraph();

            // Set random
            SetRandom();
        }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Read a file with a graph and construct it
        /// </summary>
        private void Testing()
        {
            try
            {
                stringBuilder.AppendLine("Reading file: " + reader.GetPath());

                graph = reader.ReadFile();

                stringBuilder.AppendLine("Graph created.");
                stringBuilder.AppendLine(graph.ToString());
            }
            catch (Exception e)
            {
                stringBuilder.AppendLine(e.Message);
            }
        }
Ejemplo n.º 10
0
        private void Testing(ConverterGraphToDotEnum converterGraphToDotEnum)
        {
            try
            {
                // Variable
                ConvertGraphToDot convertGraphToDot;

                testPath = ReaderWriter.ReaderWriter.CreateTestFile(testsDictionary[converterGraphToDotEnum]);

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

                List <Graph.IGraphInterface> graphList = graph.GetGraphProperty().GetComponents();

                stringBuilder.AppendLine(converterGraphToDotEnum.ToString());
                stringBuilder.AppendLine(graph.ToString());

                stringBuilder.AppendLine("Standard graph");
                convertGraphToDot = new ConvertGraphToDot(graphList, false, true, true, true, true);
                stringBuilder.AppendLine(convertGraphToDot.Convert());

                stringBuilder.AppendLine("Uncolored schedule");
                convertGraphToDot = new ConvertGraphToDot(graphList, true, true, true, true, true);
                stringBuilder.AppendLine(convertGraphToDot.Convert());

                GraphColoringAlgorithm.SequenceAlgorithm.LargestFirstSequence.LargestFirstSequence largestFirstSequence;
                foreach (Graph.IGraphInterface graph in graphList)
                {
                    largestFirstSequence = new GraphColoringAlgorithm.SequenceAlgorithm.LargestFirstSequence.LargestFirstSequence(graph, GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithInterchangeEnum.none);
                    largestFirstSequence.Color();
                }

                stringBuilder.AppendLine("Colored schedule");
                convertGraphToDot = new ConvertGraphToDot(graphList, true, true, true, true, true);
                stringBuilder.AppendLine(convertGraphToDot.Convert());
            }
            catch (KeyNotFoundException)
            {
                throw new MyException.TestsException.TestsMissingTestException(converterGraphToDotEnum.ToString());
            }
            catch (MyException.ReaderWriterException.ReaderWriterException e)
            {
                stringBuilder.AppendLine(e.Message);
            }
        }
Ejemplo n.º 11
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);
        }
Ejemplo n.º 12
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();
            }
        }
Ejemplo n.º 13
0
        /// <summary>
        /// If the populationSize is less than 1, throw exception AlgorithmGraphGeneticAlgorithmInvalidPopulationCount
        /// If the exponentCountOfIteration is less than 1 or greater than 10 throws AlgorithmGraphGeneticAlgorithmInvalidExponentCountOfIteration
        /// </summary>
        /// <param name="graph">Graph</param>
        /// <param name="populationSize">Size of population</param>
        /// <param name="exponentCountOfIteration">Exponent of count of iteration = (populationSize)^(exponentCountOfIteration)</param>
        public GeneticAlgorithm(Graph.IGraphInterface graph, int exponentCountOfIteration, int populationSize = 10) : base(graph)
        {
            if (populationSize < 1)
            {
                throw new MyException.GraphColoringAlgorithmException.AlgorithmGraphGeneticAlgorithmInvalidPopulationCount();
            }

            if (exponentCountOfIteration < 1 || exponentCountOfIteration > 10)
            {
                throw new MyException.GraphColoringAlgorithmException.AlgorithmGraphGeneticAlgorithmInvalidExponentCountOfIteration(exponentCountOfIteration.ToString());
            }

            random = new Random();

            // Even property
            if (populationSize % 2 == 1)
            {
                populationSize++;
            }

            this.populationSize           = populationSize;
            this.exponentCountOfIteration = exponentCountOfIteration;
            name = "Genetic algorithm (exponent: " + exponentCountOfIteration + ")";

            timeComplexity = TimeComplexityEnum.undefined;
            if (populationSize == 10)
            {
                switch (exponentCountOfIteration)
                {
                case 1:
                    timeComplexity = TimeComplexityEnum.quadraticPlusMultiply;
                    break;

                case 2:
                    timeComplexity = TimeComplexityEnum.cubicPlusQuadratic;
                    break;
                }
            }
        }
Ejemplo n.º 14
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);
            }
        }
Ejemplo n.º 15
0
 public SaturationLargestFirstSequence(Graph.IGraphInterface graph) : base(graph)
 {
     name           = "Saturation largest first sequence algorithm";
     timeComplexity = TimeComplexityEnum.cubic;
 }
Ejemplo n.º 16
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);
        }
Ejemplo n.º 17
0
 public Optimal(Graph.IGraphInterface graph) : base(graph)
 {
     name           = "Optimal algorithm";
     timeComplexity = TimeComplexityEnum.factorial;
 }
Ejemplo n.º 18
0
 public CombinationAlgorithm(Graph.IGraphInterface graph) : base(graph)
 {
     algorithmList  = new List <IGraphColoringAlgorithmStepInterface>();
     name           = "Combination algorithm";
     timeComplexity = TimeComplexityEnum.cubic;
 }
Ejemplo n.º 19
0
 public GreedyIndependentSet(Graph.IGraphInterface graph) : base(graph)
 {
     name           = "Greedy independent set algorithm";
     timeComplexity = TimeComplexityEnum.cubic;
 }
 public GraphColoringSequenceAlgorithm(Graph.IGraphInterface graph) : base(graph)
 {
 }
Ejemplo n.º 21
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);
            }
        }
Ejemplo n.º 22
0
 public AI(Graph.IGraphInterface graph) : base(graph)
 {
     name           = "AI";
     timeComplexity = TimeComplexityEnum.undefined;
 }
Ejemplo n.º 23
0
        /// <summary>
        /// Read a file and create a graph
        /// </summary>
        /// <returns>the graph from the file</returns>
        public Graph.IGraphInterface ReadFile()
        {
            // Variable
            Graph.IGraphInterface                graph = null;
            Graph.IGraphEdgeListInterface        graphEdgeList;
            Graph.IGraphAdjacencyMatrixInterface graphAdjacencyMatrix;

            string header = "";
            int    countVertices, numberColors;
            string line, graphName, numberColorsString;
            ReaderWriterHeaderEnum graphRepresentationEnum;

            using (FileStream fileStream = File.OpenRead(GetPath()))
                using (StreamReader streamReader = new StreamReader(fileStream, Encoding.UTF8))
                {
                    try
                    {
                        // Header
                        header = streamReader.ReadLine();

                        /// Invalid header
                        if (!header.StartsWith(READERWRITERHEADER))
                        {
                            throw new MyException.ReaderWriterException.ReaderWriterInvalidHeaderException("Invalid header");
                        }

                        graphRepresentationEnum = (ReaderWriterHeaderEnum)Enum.Parse(typeof(ReaderWriterHeaderEnum), header.Split(SEPARATOR).Last());

                        // Ballast
                        line = streamReader.ReadLine();
                        if (line != READERWRITERBALLAST)
                        {
                            throw new MyException.ReaderWriterException.ReaderWriterInvalidFormatException("Missing empty line");
                        }

                        // Graph name
                        line = streamReader.ReadLine();
                        if (!line.StartsWith(READERWRITERNAME))
                        {
                            throw new MyException.ReaderWriterException.ReaderWriterInvalidFormatException("Invalid name of graph (header)");
                        }
                        graphName = line.Substring(READERWRITERNAME.Length);

                        // Count of vertices
                        line = streamReader.ReadLine();
                        if (!line.StartsWith(READERWRITERCOUNTVERTICES))
                        {
                            throw new MyException.ReaderWriterException.ReaderWriterInvalidFormatException("Invalid vertex count (header)");
                        }
                        countVertices = Int32.Parse(line.Substring(READERWRITERCOUNTVERTICES.Length));
                        if (countVertices < 0)
                        {
                            throw new MyException.ReaderWriterException.ReaderWriterInvalidFormatException("Invalid vertex count (not a number)");
                        }
                        if (countVertices == 0)
                        {
                            throw new MyException.ReaderWriterException.ReaderWriterInvalidFormatException("Invalid vertex count (0)");
                        }

                        // Ballast
                        line = streamReader.ReadLine();
                        if (line != READERWRITERBALLAST)
                        {
                            throw new MyException.ReaderWriterException.ReaderWriterInvalidFormatException("Missing empty line");
                        }

                        // Graph
                        line = streamReader.ReadLine();
                        if (line != READERWRITERGRAPH)
                        {
                            throw new MyException.ReaderWriterException.ReaderWriterInvalidFormatException("Invalid graph (header)");
                        }

                        switch (graphRepresentationEnum)
                        {
                        case ReaderWriterHeaderEnum.adjacencyList:
                            graphEdgeList = new Graph.GraphEdgeList(countVertices);
                            graphEdgeList.SetName(graphName);

                            ReadFileAdjacencyList(graphEdgeList, streamReader);
                            graph = graphEdgeList;
                            break;

                        case ReaderWriterHeaderEnum.adjacencyMatrix:
                            graphAdjacencyMatrix = new Graph.GraphAdjacencyMatrix(countVertices);
                            graphAdjacencyMatrix.SetName(graphName);

                            ReadFileAdjacencyMatrix(graphAdjacencyMatrix, streamReader, countVertices);
                            graph = graphAdjacencyMatrix;
                            break;

                        case ReaderWriterHeaderEnum.edgeList:
                            graphEdgeList = new Graph.GraphEdgeList(countVertices);
                            graphEdgeList.SetName(graphName);

                            ReadFileEdgeList(graphEdgeList, streamReader);
                            graph = graphEdgeList;
                            break;

                        default:
                            throw new MyException.ReaderWriterException.ReaderWriterInvalidHeaderException("Unknown header");
                        }

                        // Colored graph
                        line = streamReader.ReadLine();
                        if (line != READERWRITERCOLOREDGRAPH)
                        {
                            throw new MyException.ReaderWriterException.ReaderWriterInvalidFormatException("Invalid colored graph (header)");
                        }

                        while (!streamReader.EndOfStream)
                        {
                            // Number of colors
                            line = streamReader.ReadLine();
                            if (!line.StartsWith(READERWRITERNUMBEROFCOLORS) && !line.StartsWith(READERWRITERCHROMATICNUMBER))
                            {
                                throw new MyException.ReaderWriterException.ReaderWriterInvalidFormatException("Invalid number of colors / chromatic number (header)");
                            }
                            if (line.StartsWith(READERWRITERNUMBEROFCOLORS))
                            {
                                numberColorsString = line.Substring(READERWRITERNUMBEROFCOLORS.Length);
                            }
                            else
                            {
                                numberColorsString = line.Substring(READERWRITERCHROMATICNUMBER.Length);
                            }
                            numberColors = Int32.Parse(numberColorsString);

                            // Used algorithm
                            line = streamReader.ReadLine();
                            if (!line.StartsWith(READERWRITERUSEDALGORITHM))
                            {
                                throw new MyException.ReaderWriterException.ReaderWriterInvalidHeaderException("Invalid used algorithm (header)");
                            }

                            line = line.Substring(READERWRITERUSEDALGORITHM.Length);

                            if (!Enum.GetNames(typeof(GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum)).Contains(line))
                            {
                                throw new MyException.ReaderWriterException.ReaderWriterInvalidFormatException("Unknown algorithm");
                            }

                            // Colored graph
                            line = streamReader.ReadLine();
                            while (line != null && line != "")
                            {
                                line = streamReader.ReadLine();
                            }
                        }
                    }
                    catch (ArgumentException)
                    {
                        throw new MyException.ReaderWriterException.ReaderWriterInvalidHeaderException("Invalid header");
                    }
                    catch (FormatException)
                    {
                        throw new MyException.ReaderWriterException.ReaderWriterInvalidFormatException("Invalid vertex count (not a number)");
                        // throw new MyException.ReaderWriterInvalidFormatException("Invalid chromatic number (not a number)");
                    }
                    catch (IOException)
                    {
                        throw new MyException.ReaderWriterException.ReaderWriterInvalidDataException("Something went wrong with a file");
                    }
                }

            return(graph);
        }
Ejemplo n.º 24
0
 public ConnectedSequential(Graph.IGraphInterface graph) : base(graph)
 {
     name            = "Connected sequenctial algorithm";
     timeComplexity  = TimeComplexityEnum.linear;
     interchangeEnum = GraphColoringAlgorithInterchangeEnum.none;
 }