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(); } }
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); } }
/// <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); }