Esempio n. 1
0
        protected void CreateVertexSequence()
        {
            // Variable
            Graph.IVertexInterface        vertex     = null;
            List <Graph.IVertexInterface> VertexList = new List <Graph.IVertexInterface>();

            Graph.IGraphInterface copyGraph;

            copyGraph = Graph.GraphOperation.GraphOperation.CopyGraph(graph);
            while (copyGraph.GetRealCountVertices() != 0)
            {
                int minDegree = int.MaxValue;
                foreach (Graph.IVertexInterface myVertex in copyGraph.GetGraphProperty().GetDegreeSequenceVertex(false))
                {
                    if (minDegree > copyGraph.CountNeighbours(myVertex))
                    {
                        vertex    = myVertex;
                        minDegree = copyGraph.CountNeighbours(myVertex);
                    }
                }

                VertexList.Add(graph.GetVertexByUserName(vertex.GetUserName()));
                copyGraph.VertexDelete(vertex);
            }

            VertexList.Reverse();

            vertexSequenceList = VertexList;
        }
Esempio n. 2
0
        public void Color()
        {
            if (coloredGraph.GetIsInitializedColoredGraph())
            {
                throw new MyException.GraphException.ColoredGraphAlreadyInitializedException();
            }

            coloredGraph.ResetColors();

            coloredGraph.SetSaturation(true);

            Graph.IVertexInterface vertex = coloredGraph.GetSaturationDegreeSequence();

            while (vertex != null)
            {
                coloredGraph.ColorVertex(vertex, coloredGraph.GreedyColoring(vertex));
                vertex = coloredGraph.GetSaturationDegreeSequence();
            }

            bool isColored = coloredGraph.InitializeColoredGraph();

            if (!isColored)
            {
                throw new MyException.GraphColoringAlgorithmException.AlgorithmGraphIsNotColored();
            }
        }
        /// <summary>
        /// Return a count of neighbors of my neighbors
        /// </summary>
        /// <param name="vertex">vertex</param>
        /// <returns>count of neighbors of my neighbors</returns>
        private int SumOfNeighborsOfMyNeighbors(Graph.IVertexInterface vertex)
        {
            // Variable
            int sum = 0;
            List <Graph.IVertexInterface> neighbors = graph.Neighbours(vertex);


            foreach (Graph.IVertexInterface neighbor in neighbors)
            {
                sum = graph.CountNeighbours(neighbor);
            }

            return(sum);
        }
Esempio n. 4
0
        // For standard graph visualization
        private string ConvertGraph()
        {
            // Variable
            int maximumDegree;
            int minimumDegree;
            List <Graph.IVertexInterface> cutVerticesList;
            List <Graph.IEdgeInterface>   bridgesList;
            List <Graph.IVertexInterface> verticesList;
            List <Graph.IEdgeInterface>   spanningTreeList;

            Graph.IVertexInterface simplicialVertex = null;

            stringBuilder = new StringBuilder();

            // Sceleton
            if (graphList.Count == 0)
            {
                stringBuilder.AppendLine("graph");
            }
            else
            {
                stringBuilder.AppendLine("graph \"" + graphList[0].GetName() + "\"");
            }
            stringBuilder.AppendLine("{");

            foreach (Graph.IGraphInterface graph in graphList)
            {
                if (graph.GetGraphProperty().GetCountVertices() == 0)
                {
                    continue;
                }

                // Vertices
                string text;
                int    vertexDegree;
                bool   useColor = false;

                if (graph.GetColoredGraph().GetIsInitializedColoredGraph())
                {
                    useColor = graph.GetColoredGraph().GetCountUsedColors() < MAXCOLORS ? true : false;
                }

                // Graph properties
                if (graph.GetGraphProperty().GetIsChordal())
                {
                    simplicialVertex = graph.GetGraphProperty().GetPerfectEliminationOrdering().Last();
                }
                verticesList     = graph.AllVertices();
                cutVerticesList  = graph.GetGraphProperty().GetCutVertices();
                bridgesList      = graph.GetGraphProperty().GetBridges();
                spanningTreeList = graph.GetGraphProperty().GetSpanningTree();
                minimumDegree    = graph.GetGraphProperty().GetMinimumVertexDegree();
                maximumDegree    = graph.GetGraphProperty().GetMaximumVertexDegree();

                // Vertices
                stringBuilder.AppendLine("node[style = filled shape = circle fillcolor = " + fillColorDefault + " color = " + colorDefault + " penwidth = " + widthDefault + "]");
                foreach (Graph.IVertexInterface vertex in verticesList)
                {
                    text         = "\"";
                    vertexDegree = graph.CountNeighbours(vertex);

                    text += vertex.GetUserName() + "\" [";

                    if (showMaximumAndMinimumDegreeVertices && vertexDegree == minimumDegree)
                    {
                        text += "shape = doublecircle ";
                    }

                    if (showMaximumAndMinimumDegreeVertices && vertexDegree == maximumDegree)
                    {
                        text += "shape = doubleoctagon ";
                    }

                    if (showSimplicialVertex && simplicialVertex == vertex)
                    {
                        text += "shape = square ";
                    }

                    if (showCutVerticesAndBridges && cutVerticesList.Contains(vertex))
                    {
                        text += "color = " + colorCutVertex + " , penwidth = " + widthCutVertex + " ";
                    }

                    if (useColor && vertex.GetColor() != Graph.VertexExtended.GetDefaultColor())
                    {
                        text += "fillcolor = " + colorsDictionary[vertex.GetColor()] + " ";
                    }

                    text += "]";

                    stringBuilder.AppendLine(text);
                }

                // Edges
                HashSet <Graph.IVertexInterface> visitedVerticesHashSet = new HashSet <Graph.IVertexInterface>();

                foreach (Graph.IVertexInterface vertex in verticesList)
                {
                    List <Graph.IVertexInterface> neighboursList = graph.Neighbours(vertex);

                    foreach (Graph.IVertexInterface neighbour in neighboursList)
                    {
                        // Neighbour has been written
                        if (visitedVerticesHashSet.Contains(neighbour))
                        {
                            continue;
                        }

                        // Neighbour has not been written
                        stringBuilder.AppendLine("\"" + vertex.GetUserName() + "\" --  \"" + neighbour.GetUserName() + "\"");

                        if (showCutVerticesAndBridges && bridgesList.Any(e => ((e.GetVertex1().Equals(vertex) && e.GetVertex2().Equals(neighbour)) ||
                                                                               (e.GetVertex1().Equals(neighbour) && e.GetVertex2().Equals(vertex)))))
                        {
                            stringBuilder.AppendLine(" [color = " + colorBridges + ", penwidth = " + widthBridges + "]");
                        }
                        else
                        {
                            stringBuilder.AppendLine();
                        }

                        if (showSpanningTree && spanningTreeList.Any(e => ((e.GetVertex1().Equals(vertex) && e.GetVertex2().Equals(neighbour)) ||
                                                                           (e.GetVertex1().Equals(neighbour) && e.GetVertex2().Equals(vertex)))))
                        {
                            stringBuilder.AppendLine(" [penwidth = " + widthSpanningTree + "]");
                        }
                    }

                    visitedVerticesHashSet.Add(vertex);
                }
            }
            stringBuilder.AppendLine("}");

            return(stringBuilder.ToString());
        }
        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();
            }
        }