예제 #1
0
        /// <summary>
        /// Set potrentional Perfect elimination ordering.
        /// It is correct only if a graph is chordal!
        /// You can check it via IsPerfectEliminationOrderingParallel
        /// Change: perfectEliminationOrderingList
        /// Use lex-BFS algorithm
        /// Time complexity: O(V * log(V) + E)
        /// Space complexity: O(V)
        /// </summary>
        private void PerfectEliminationOrdering()
        {
            // Variable
            int countVertices = graph.GetRealCountVertices();

            perfectEliminationOrderingList = new List <IVertexInterface>(countVertices);
            MyDataStructure.FibonacciHeap      priorityQueue           = new MyDataStructure.FibonacciHeap(countVertices);
            Dictionary <IVertexInterface, int> mappingVertexDictionary = new Dictionary <IVertexInterface, int>();

            IVertexInterface[] mappingVertexArray = new IVertexInterface[countVertices];

            // Initilize perfectEliminationOrderingList + mapping
            int help = 0;

            foreach (IVertexInterface vertex in graph.AllVertices())
            {
                // Mapping
                mappingVertexDictionary.Add(vertex, help);
                mappingVertexArray[help] = vertex;
                help++;
            }

            // Add vertices to fibonacci heap
            foreach (IVertexInterface vertex in graph.AllVertices())
            {
                priorityQueue.Insert(mappingVertexDictionary[vertex], countVertices);
            }

            for (int i = 0; i < countVertices; i++)
            {
                IVertexInterface selectedVertex = mappingVertexArray[priorityQueue.ExtractMin().GetIdentifier()];

                foreach (IVertexInterface neighbour in graph.Neighbours(selectedVertex))
                {
                    int neighbourFibonacciIdentifier = mappingVertexDictionary[neighbour];
                    if (priorityQueue.ElementExists(neighbourFibonacciIdentifier))
                    {
                        priorityQueue.Decrease(neighbourFibonacciIdentifier, priorityQueue.GetValue(neighbourFibonacciIdentifier) - 1);
                    }
                }

                perfectEliminationOrderingList.Add(selectedVertex);
            }
        }
예제 #2
0
        public void Color()
        {
            // Variable
            int countVertices   = graph.GetRealCountVertices();
            int maxDegreeVertex = graph.GetGraphProperty().GetDegreeSequenceInt(false).Max();
            int maxCountOfNeighborsOfNeighbors = int.MinValue;

            Graph.IVertexInterface startingVertex = null;
            int sumOfNeighborsOfMyNeighbors;

            Graph.IVertexInterface[]                 mappingVertexArray      = new Graph.IVertexInterface[countVertices];
            MyDataStructure.FibonacciHeap            fibonacciHeap           = new MyDataStructure.FibonacciHeap(countVertices);
            Dictionary <Graph.IVertexInterface, int> mappingVertexDictionary = new Dictionary <Graph.IVertexInterface, int>();

            // Reset
            countInterchangeCalls = 0;

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

            // Graph is not connected
            if (!graph.GetGraphProperty().GetIsConnected())
            {
                throw new MyException.GraphException.GraphIsNotConnected(graph.ToString());
            }

            coloredGraph.ResetColors();

            // Initilize mapping
            int help = 0;

            foreach (Graph.IVertexInterface vertex in graph.AllVertices())
            {
                // Mapping
                mappingVertexDictionary.Add(vertex, help);
                mappingVertexArray[help] = vertex;
                help++;
            }

            // Find starting vertex
            foreach (var record in graph.GetGraphProperty().GetDegreeSequence(false))
            {
                if (record.Value == maxDegreeVertex)
                {
                    sumOfNeighborsOfMyNeighbors = SumOfNeighborsOfMyNeighbors(record.Key);
                    if (maxCountOfNeighborsOfNeighbors < sumOfNeighborsOfMyNeighbors)
                    {
                        maxCountOfNeighborsOfNeighbors = sumOfNeighborsOfMyNeighbors;
                        startingVertex = record.Key;
                    }
                }
            }

            Graph.IVertexInterface actualVertex = startingVertex;
            for (int i = 0; i < countVertices; i++)
            {
                foreach (Graph.IVertexInterface neighbor in graph.Neighbours(actualVertex))
                {
                    if (neighbor.GetColor() == Graph.VertexExtended.GetDefaultColor())
                    {
                        if (!fibonacciHeap.ElementExists(mappingVertexDictionary[neighbor]))
                        {
                            fibonacciHeap.Insert(mappingVertexDictionary[neighbor], countVertices - graph.CountNeighbours(neighbor));
                        }
                    }
                }

                List <int> availableColorList = coloredGraph.UsedColors();
                availableColorList = availableColorList.Except(coloredGraph.ColorsNeighbours(actualVertex)).ToList();

                int color = coloredGraph.GetMostUsedColorNeighborsNeighbor(actualVertex, availableColorList);

                if (color != Graph.VertexExtended.GetDefaultColor())
                {
                    coloredGraph.ColorVertex(actualVertex, color);
                }
                else
                {
                    switch (interchangeEnum)
                    {
                    case GraphColoringAlgorithInterchangeEnum.none:
                        coloredGraph.ColorVertex(actualVertex, coloredGraph.GreedyColoring(actualVertex));
                        break;

                    case GraphColoringAlgorithInterchangeEnum.interchange:
                        coloredGraph.TryChangeColoring(actualVertex, coloredGraph.GreedyColoring(actualVertex));
                        break;

                    case GraphColoringAlgorithInterchangeEnum.interchangeExtended:
                        coloredGraph.TryChangeColoringExtended(actualVertex, coloredGraph.GreedyColoring(actualVertex), false);
                        break;

                    case GraphColoringAlgorithInterchangeEnum.interchangeExtendedK3:
                        coloredGraph.TryChangeColoringExtended(actualVertex, coloredGraph.GreedyColoring(actualVertex), true);
                        break;
                    }

                    if (interchangeEnum != GraphColoringAlgorithInterchangeEnum.none)
                    {
                        countInterchangeCalls++;
                    }
                }

                if (fibonacciHeap.GetCountNodes() != 0)
                {
                    actualVertex = mappingVertexArray[fibonacciHeap.ExtractMin().GetIdentifier()];
                }
            }

            bool isColored = coloredGraph.InitializeColoredGraph();

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