コード例 #1
0
ファイル: GraphClass.cs プロジェクト: Illner/GraphColoring
        /// <summary>
        /// Return true if the graph is (complete) bipartite
        /// Time complexity: O(V + E)
        /// </summary>
        /// <param name="graph">graph</param>
        /// <returns>(bool, bool) - the first item stands for bipartite and the second item stands for complete
        /// (true, true) - complete bipartite graph
        /// (true, false) - bipartite graph
        /// (false, - ) - the graph is not (complete) bipartite</returns>
        public static Tuple <bool, bool> IsBipartiteGraph(IGraphInterface graph)
        {
            // Variable
            IVertexInterface        vertex;
            List <IVertexInterface> neighboursVertexList;
            bool isFirstPartite, isBipartite = true;

            HashSet <IVertexInterface> firstPartite  = new HashSet <IVertexInterface>();
            HashSet <IVertexInterface> secondPartite = new HashSet <IVertexInterface>();
            Queue <IVertexInterface>   vertexQueue   = new Queue <IVertexInterface>();

            vertex = graph.GetFirstVertex();
            vertexQueue.Enqueue(vertex);
            firstPartite.Add(vertex);

            while (vertexQueue.Count != 0)
            {
                vertex = vertexQueue.Dequeue();
                neighboursVertexList = graph.Neighbours(vertex);

                if (firstPartite.Contains(vertex))
                {
                    isFirstPartite = true;
                }
                else
                {
                    isFirstPartite = false;
                }

                foreach (IVertexInterface neighbourVertex in neighboursVertexList)
                {
                    if (!firstPartite.Contains(neighbourVertex) && !secondPartite.Contains(neighbourVertex))
                    {
                        if (isFirstPartite)
                        {
                            secondPartite.Add(neighbourVertex);
                        }
                        else
                        {
                            firstPartite.Add(neighbourVertex);
                        }

                        vertexQueue.Enqueue(neighbourVertex);
                    }
                    else
                    {
                        if ((firstPartite.Contains(neighbourVertex) && isFirstPartite) || (secondPartite.Contains(neighbourVertex) && !isFirstPartite))
                        {
                            isBipartite = false;
                        }
                    }
                }
            }

            if (!isBipartite)
            {
                return(new Tuple <bool, bool>(false, false));
            }

            // Set bipartites
            graph.GetGraphProperty().SetPartites(firstPartite.ToList(), secondPartite.ToList());

            // Variable
            int countFirstPartiteVertex  = firstPartite.Count;
            int countSecondPartiteVertex = secondPartite.Count;

            // Is the graph a complete bipartite graph
            foreach (Vertex firstPartiteVertex in firstPartite)
            {
                if (graph.CountNeighbours(firstPartiteVertex) != countSecondPartiteVertex)
                {
                    return(new Tuple <bool, bool>(true, false));
                }
            }
            foreach (Vertex secondPartiteVertex in secondPartite)
            {
                if (graph.CountNeighbours(secondPartiteVertex) != countFirstPartiteVertex)
                {
                    return(new Tuple <bool, bool>(true, false));
                }
            }

            return(new Tuple <bool, bool>(true, true));
        }
コード例 #2
0
ファイル: GraphProperty.cs プロジェクト: Illner/GraphColoring
        public void VertexDelete(IVertexInterface vertex)
        {
            // Component
            if (componentsList != null)
            {
                // More components => copies of components
                if (countComponents > 1)
                {
                    // Get component which contains the vertex
                    IVertexInterface myVertexComponent = null;
                    IGraphInterface  myComponentGraph  = null;
                    foreach (IGraphInterface componentGraph in componentsList)
                    {
                        if (componentGraph.ExistsUserName(vertex.GetUserName()))
                        {
                            myComponentGraph  = componentGraph;
                            myVertexComponent = myComponentGraph.GetVertexByUserName(vertex.GetUserName());
                            break;
                        }
                    }

                    // The vertex is component
                    if (myComponentGraph.CountNeighbours(myVertexComponent) == 0)
                    {
                        componentsList.Remove(myComponentGraph);
                        countComponents--;
                        isConnected = countComponents == 1 ? true : false;
                    }
                    // The vertex is in a component with another vertices
                    else
                    {
                        componentsList  = null;
                        countComponents = null;
                        isConnected     = null;
                    }
                }
                else
                {
                    if (cutVertices != null && !cutVertices.Contains(vertex))
                    {
                        //componentsList = componentsList;
                        //countComponents = countComponents;
                        //isConnected = isConnected;
                    }
                    else
                    {
                        componentsList  = null;
                        countComponents = null;
                        isConnected     = null;
                    }
                }
            }

            // Properties
            if (isCyclic != null && (bool)isCyclic)
            {
                isCyclic = null;
            }
            if (isConnected != null && !(bool)isConnected)
            {
                isEulerian = EulerianGraphEnum.notEulerian;
            }
            else
            {
                isEulerian = EulerianGraphEnum.undefined;
            }

            // IntegralInvariants
            circuitRank    = null;
            girth          = null;
            cayleysFormula = null;

            // SequencesAndPolynomialsOthers
            if (isConnected != null && !(bool)isConnected)
            {
                spanningTreeBFS = spanningTreeBFS = new List <IEdgeInterface>();
            }
            else
            {
                spanningTreeBFS = null;
            }
            matching    = null;
            cutVertices = null;
            bridges     = null;

            // DegreeSequence
            degreeSequence         = null;
            maximumVertexDegree    = null;
            minimumVertexDegree    = null;
            averageVertexDegree    = null;
            medianVertexDegree     = null;
            isDegreeSequenceSorted = false;
            isRegular = null;

            // Chordal
            isChordal = null;
            perfectEliminationOrderingList = null;
            righNeighborhoodDictionary     = null;

            // GraphClass
            graphClass = GraphClass.GraphClass.GraphClassEnum.undefined;
        }
コード例 #3
0
        private void Invalid(IGraphInterface graph)
        {
            // Variable
            List <IVertexInterface> vertexList           = graph.AllVertices();
            List <IVertexInterface> vertexNot2DegreeList = new List <IVertexInterface>();

            vertexNot2DegreeList = vertexList.Where(v => graph.CountNeighbours(v) != 2).ToList();
            IEdgeInterface edge = new Edge(vertexList.First(), graph.Neighbours(vertexList.First()).First());

            // Vertex add
            stringBuilder.AppendLine("Vertex add");
            stringBuilder.AppendLine("Vertex exists");
            try { graph.VertexAdd(vertexList.First()); }
            catch (MyException.GraphException.GraphException e) { stringBuilder.AppendLine(e.Message); }

            // Vertex delete
            stringBuilder.AppendLine("Vertex delete");
            stringBuilder.AppendLine("Vertex doesn't exist");
            try { graph.VertexDelete(new Vertex()); }
            catch (MyException.GraphException.GraphException e) { stringBuilder.AppendLine(e.Message); }

            // Vertex contract
            stringBuilder.AppendLine("Vertex contract");
            stringBuilder.AppendLine("Vertex doesn't exist");
            try { graph.VertexContraction(new Vertex()); }
            catch (MyException.GraphException.GraphException e) { stringBuilder.AppendLine(e.Message); }

            // Vertex suppression
            stringBuilder.AppendLine("Vertex suppression");
            stringBuilder.AppendLine("Vertex doesn't exist");
            try { graph.VertexSuppression(new Vertex()); }
            catch (MyException.GraphException.GraphException e) { stringBuilder.AppendLine(e.Message); }
            stringBuilder.AppendLine("Invalid vertex degree");
            try { graph.VertexSuppression(vertexNot2DegreeList.First()); }
            catch (MyException.GraphException.GraphException e) { stringBuilder.AppendLine(e.Message); }

            // Vertex expansion
            stringBuilder.AppendLine("Vertex expansion");
            stringBuilder.AppendLine("Vertex doesn't exist");
            try { graph.VertexExpansion(new Vertex()); }
            catch (MyException.GraphException.GraphException e) { stringBuilder.AppendLine(e.Message); }

            // Edge add
            stringBuilder.AppendLine("Edge add");
            stringBuilder.AppendLine("Edge exists");
            try { graph.EdgeAdd(edge); }
            catch (MyException.GraphException.GraphException e) { stringBuilder.AppendLine(e.Message); }

            // Edge delete
            stringBuilder.AppendLine("Edge delete");
            stringBuilder.AppendLine("Edge doesn't exist");
            try { graph.EdgeDelete(new Edge(new Vertex(), new Vertex())); }
            catch (MyException.GraphException.GraphException e) { stringBuilder.AppendLine(e.Message); }

            // Edge contract
            stringBuilder.AppendLine("Edge contract");
            stringBuilder.AppendLine("Edge doesn't exist");
            try { graph.EdgeContraction(new Edge(new Vertex(), new Vertex())); }
            catch (MyException.GraphException.GraphException e) { stringBuilder.AppendLine(e.Message); }

            // Edge subdivision
            stringBuilder.AppendLine("Edge subdivision");
            stringBuilder.AppendLine("Edge doesn't exist");
            try { graph.EdgeSubdivision(new Edge(new Vertex(), new Vertex())); }
            catch (MyException.GraphException.GraphException e) { stringBuilder.AppendLine(e.Message); }
        }