Beispiel #1
0
        private void ComponentsBFS(IVertexInterface root, Dictionary <IVertexInterface, int> vertexDictionary, int componentNumber)
        {
            // Variable
            Queue <IVertexInterface> vertexQueue = new Queue <IVertexInterface>();
            List <IVertexInterface>  neighboursVertexList;
            IVertexInterface         dequeuedVertex;

            vertexQueue.Enqueue(root);
            vertexDictionary[root] = componentNumber;

            while (vertexQueue.Count != 0)
            {
                dequeuedVertex = vertexQueue.Dequeue();

                neighboursVertexList = graph.Neighbours(dequeuedVertex);

                foreach (IVertexInterface vertex in neighboursVertexList)
                {
                    if (vertexDictionary[vertex] == 0)
                    {
                        vertexDictionary[vertex] = componentNumber;
                        vertexQueue.Enqueue(vertex);
                    }
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Add a new vertex
        /// Deinitialize colored graph
        /// If the vertex exists in the graph throws GraphVertexAlreadyExistsException
        /// </summary>
        /// <param name="vertex">vertex</param>
        public void VertexAdd(IVertexInterface vertex)
        {
            // Vertex exists
            if (ExistsVertex(vertex) || ExistsUserName(vertex.GetUserName()))
            {
                throw new MyException.GraphException.GraphVertexAlreadyExistsException();
            }

            VertexExtended vertexExtended = new VertexExtended(vertex.GetIdentifier());

            vertexExtended.SetColor(vertex.GetColor());
            vertexExtended.SetUserName(vertex.GetUserName());

            SetCanDeIncreaseCountVertices(true);
            GetGraphProperty().IncrementCountVertices();
            SetCanDeIncreaseCountVertices(false);

            AddVertexToAdjacencyList(vertexExtended);
            vertex = vertexExtended;

            // ColoredGraph
            coloredGraph.AddVertexInHashSets(vertex);
            if (coloredGraph.GetIsInitializedColoredGraph())
            {
                coloredGraph.DeinitializationColoredGraph();
            }

            // GraphProperty
            GetGraphProperty().VertexAdd(vertex);
        }
Beispiel #3
0
        /// <summary>
        /// Change a vertex user name
        /// If the vertex doesn't exist throws GraphVertexDoesntExistException
        /// </summary>
        /// <param name="vertex">name</param>
        /// <param name="newUserName">user name</param>
        public void RenameVertexUserName(IVertexInterface vertex, string newUserName)
        {
            // Variable
            string         oldUserName;
            VertexExtended vertexExtended = ConvertVertexToVertexExtended(vertex);

            if (ExistsUserName(newUserName))
            {
                throw new MyException.GraphException.GraphVertexUserNameAlreadyExistsException();
            }

            oldUserName = vertex.GetUserName();
            mappingUserName.Remove(vertex.GetUserName());
            vertexExtended.SetUserName(newUserName);
            mappingUserName.Add(newUserName, vertexExtended);

            // Change the name in components
            // The graph has more than one component => need to change vertex name in one of them.
            if (GetGraphProperty().GetIsInitializedComponent() && GetGraphProperty().GetCountComponents() > 1)
            {
                foreach (IGraphInterface componentGraph in GetGraphProperty().GetComponents())
                {
                    if (componentGraph.ExistsUserName(oldUserName))
                    {
                        componentGraph.RenameVertexUserName(componentGraph.GetVertexByUserName(oldUserName), newUserName);
                        break;
                    }
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// Remove a vertex
        /// Deinitialize colored graph
        /// If the vertex does not exist in the graph throws GraphVertexDoesntExistException
        /// If the graph has only one vertex throws GraphHasToHaveAtLeastOneVertexException
        /// Time complexity: O(V + E)
        /// </summary>
        /// <param name="removeVertex">vertex</param>
        public void VertexDelete(IVertexInterface removeVertex)
        {
            // Vertex doesn't exist
            if (!ExistsVertex(removeVertex))
            {
                throw new MyException.GraphException.GraphVertexDoesntExistException();
            }

            /*
             * if (GetGraphProperty().GetCountVertices() == 1)
             *  throw new MyException.GraphException.GraphHasToHaveAtLeastOneVertexException();
             */

            // Variable
            int count = 0;
            HashSet <IVertexInterface> removeVertexList = new HashSet <IVertexInterface>();

            adjacencyList.Remove(ConvertVertexToVertexExtended(removeVertex));

            foreach (HashSet <VertexExtended> vertexExtendedHashSet in adjacencyList.Values)
            {
                foreach (VertexExtended vertexExtended in vertexExtendedHashSet)
                {
                    if (vertexExtended.Equals(removeVertex))
                    {
                        removeVertexList.Add(vertexExtended);
                        break;
                    }
                }
                count += removeVertexList.Count;

                vertexExtendedHashSet.RemoveWhere(x => removeVertexList.Contains(x));
                removeVertexList.Clear();
            }

            mapping.Remove(removeVertex.GetIdentifier());
            mappingUserName.Remove(removeVertex.GetUserName());

            DecrementRealCountVertices();

            SetCanDeIncreaseCountVertices(true);
            GetGraphProperty().DecrementCountVertices();
            SetCanDeIncreaseCountVertices(false);

            SetCanDeIncreaseCountEdges(true);
            GetGraphProperty().DecrementCountEdges(count);
            SetCanDeIncreaseCountEdges(false);

            // ColoredGraph
            coloredGraph.RemoveVertexInHashSets(removeVertex);
            if (coloredGraph.GetIsInitializedColoredGraph())
            {
                coloredGraph.DeinitializationColoredGraph();
            }

            // GraphProperty
            GetGraphProperty().VertexDelete(removeVertex);
        }
Beispiel #5
0
        /// <summary>
        /// Return a list of neighbors
        /// If the graph is not initialized throwsGraphInitializationException
        /// </summary>
        /// <param name="vertex">vertex</param>
        /// <returns>list of neighbors</returns>
        public List <IVertexInterface> Neighbours(IVertexInterface vertex)
        {
            if (!isInitialized)
            {
                throw new MyException.GraphException.GraphNotInitializationException();
            }

            return(new List <IVertexInterface> (adjacencyList[ConvertVertexToVertexExtended(vertex)]));
        }
Beispiel #6
0
        /// <summary>
        /// Return count of neighbors
        /// If the graph is not initialized throwsGraphInitializationException
        /// </summary>
        /// <param name="vertex">vertex</param>
        /// <returns>count of neighbors</returns>
        public int CountNeighbours(IVertexInterface vertex)
        {
            if (!isInitialized)
            {
                throw new MyException.GraphException.GraphNotInitializationException();
            }

            return(Neighbours(vertex).Count);
        }
Beispiel #7
0
        /// <summary>
        /// Compare two vertices
        /// Return true if vertices are equal, otherwise false
        /// </summary>
        /// <param name="vertex">the second vertex</param>
        /// <returns>true if vertices are equal, otherwise false</returns>
        public bool Equals(IVertexInterface vertex)
        {
            if (identifier == vertex.GetIdentifier() && userName == vertex.GetUserName() && GetColor() == vertex.GetColor())
            {
                return(true);
            }

            return(false);
        }
Beispiel #8
0
        /// <summary>
        /// Determine the shortest cycle which contains the vertex
        /// If the cycle does not exist returns int.MaxValue
        /// </summary>
        /// <param name="root">vertex</param>
        /// <returns>the shortest cycle which contains the vertex</returns>
        private int CycleGirthBFS(IVertexInterface root)
        {
            // Variable
            int bestGirth = int.MaxValue;
            Queue <CycleNode> nodeBFSQueue = new Queue <CycleNode>();
            Dictionary <IVertexInterface, int> nodeDictionary       = new Dictionary <IVertexInterface, int>();
            List <IVertexInterface>            vertexNeighboursList = new List <IVertexInterface>();

            CycleNode rootNode = new CycleNode(root, 0);

            nodeBFSQueue.Enqueue(rootNode);
            nodeDictionary.Add(rootNode.GetVertex(), rootNode.GetDepth());

            while (nodeBFSQueue.Count != 0)
            {
                CycleNode node  = nodeBFSQueue.Dequeue();
                int       depth = node.GetDepth() + 1;

                vertexNeighboursList = graph.Neighbours(node.GetVertex());

                foreach (IVertexInterface vertexNeighbour in vertexNeighboursList)
                {
                    // We have not seen this vertex yet
                    if (!nodeDictionary.ContainsKey(vertexNeighbour))
                    {
                        nodeBFSQueue.Enqueue(new CycleNode(vertexNeighbour, depth));
                        nodeDictionary.Add(vertexNeighbour, depth);
                    }
                    else
                    {
                        // Odd cycle
                        if (nodeDictionary[vertexNeighbour] == depth - 1)
                        {
                            if (depth * 2 - 1 < bestGirth)
                            {
                                bestGirth = depth * 2 - 1;
                            }
                        }
                        else
                        {
                            // Even cycle
                            if (nodeDictionary[vertexNeighbour] == depth)
                            {
                                if (depth * 2 < bestGirth)
                                {
                                    bestGirth = depth * 2;
                                }
                            }
                        }
                    }
                }
            }

            return(bestGirth);
        }
Beispiel #9
0
 /// <summary>
 /// Return true if a vertex exists in the graph, otherwise false
 /// Time complexity: O(1)
 /// </summary>
 /// <param name="vertex">vertex</param>
 /// <returns>true if the vertex exists in the graph, otherwise false</returns>
 public bool ExistsVertex(IVertexInterface vertex)
 {
     try
     {
         return(adjacencyList.ContainsKey(ConvertVertexToVertexExtended(vertex)));
     }
     catch (MyException.GraphException.GraphVertexDoesntExistException)
     {
         return(false);
     }
 }
Beispiel #10
0
 /// <summary>
 /// Convertor from Vertex to VertexExtended
 /// If the vertex does not exist in the graph throws GraphVertexDoesntExistException
 /// </summary>
 /// <param name="vertex">vertex</param>
 /// <returns>vertexExtended</returns>
 protected VertexExtended ConvertVertexToVertexExtended(IVertexInterface vertex)
 {
     try
     {
         return(mapping[vertex.GetIdentifier()]);
     }
     catch (KeyNotFoundException)
     {
         throw new MyException.GraphException.GraphVertexDoesntExistException();
     }
 }
Beispiel #11
0
        /// <summary>
        /// Determine if the graph is cyclic
        /// Change: isCyclic
        /// Using: Union-Find
        /// Time complexity: O(V^2 + E)
        /// Space complexity: O(V + E)
        /// </summary>
        private void CycleIsCyclicUnionFind()
        {
            // Variable
            List <IVertexInterface>    allVerticesList;
            List <IVertexInterface>    vertexNeighboursList;
            HashSet <IVertexInterface> expandedVerticesHashSet;

            cycleDisjointSetDictionary = new Dictionary <IVertexInterface, IVertexInterface>(GetCountVertices());
            expandedVerticesHashSet    = new HashSet <IVertexInterface>();
            allVerticesList            = graph.AllVertices();

            // Create disjoint set
            foreach (IVertexInterface vertex in allVerticesList)
            {
                cycleDisjointSetDictionary.Add(vertex, vertex);
            }

            // Core algorithm
            isCyclic = false;

            foreach (IVertexInterface vertex in allVerticesList)
            {
                vertexNeighboursList = graph.Neighbours(vertex);

                foreach (IVertexInterface neighbourVertex in vertexNeighboursList)
                {
                    if (expandedVerticesHashSet.Contains(neighbourVertex))
                    {
                        continue;
                    }

                    IVertexInterface vertex1Representative = CycleFind(vertex);
                    IVertexInterface vertex2Representative = CycleFind(neighbourVertex);

                    if (vertex1Representative == vertex2Representative)
                    {
                        isCyclic = true;
                        return;
                    }

                    CycleUnion(vertex, neighbourVertex);
                }

                expandedVerticesHashSet.Add(vertex);
            }
        }
Beispiel #12
0
        /// <summary>
        /// Determine if the graph is cyclic
        /// Change: isCyclic
        /// Time complexity: O(V + E)
        /// Space complexity: O(V + E)
        /// </summary>
        protected void CycleIsCyclic()
        {
            // Variable
            Queue <IVertexInterface>   verticesQueue;
            HashSet <IVertexInterface> visitedVerticesHashSet;
            Dictionary <IVertexInterface, IVertexInterface> parentsDictionary;

            verticesQueue          = new Queue <IVertexInterface>();
            visitedVerticesHashSet = new HashSet <IVertexInterface>();
            parentsDictionary      = new Dictionary <IVertexInterface, IVertexInterface>();

            verticesQueue.Enqueue(graph.GetFirstVertex());
            visitedVerticesHashSet.Add(graph.GetFirstVertex());

            isCyclic = false;

            // Initialize parentsDictionary
            foreach (IVertexInterface vertex in graph.AllVertices())
            {
                parentsDictionary.Add(vertex, null);
            }

            while (verticesQueue.Count != 0)
            {
                IVertexInterface vertex = verticesQueue.Dequeue();

                foreach (IVertexInterface neighbor in graph.Neighbours(vertex))
                {
                    if (!visitedVerticesHashSet.Contains(neighbor))
                    {
                        visitedVerticesHashSet.Add(neighbor);
                        verticesQueue.Enqueue(neighbor);
                        parentsDictionary[neighbor] = vertex;
                    }
                    else
                    {
                        if (parentsDictionary[vertex] != neighbor)
                        {
                            isCyclic = true;
                            return;
                        }
                    }
                }
            }
        }
Beispiel #13
0
        /// <summary>
        /// Expand a vertex
        /// Deinitialize colored graph
        /// If the vertex does not exist in the graph throws GraphVertexDoesntExistException
        /// </summary>
        /// <param name="vertex">vertex</param>
        public void VertexExpansion(IVertexInterface vertex)
        {
            if (!ExistsVertex(vertex))
            {
                throw new MyException.GraphException.GraphVertexDoesntExistException();
            }

            // Variable
            IVertexInterface        vertex1, vertex2;
            List <IVertexInterface> neighboursList;

            neighboursList = Neighbours(vertex);

            vertex1 = new Vertex(vertex.GetUserName() + " (1)");
            vertex2 = new Vertex(vertex.GetUserName() + " (2)");

            VertexAdd(vertex1);
            VertexAdd(vertex2);

            VertexDelete(vertex);

            // ColoredGraph
            //coloredGraph.RemoveVertexInHashSets(vertex);

            // ColoredGraph
            coloredGraph.AddVertexInHashSets(vertex1);
            coloredGraph.AddVertexInHashSets(vertex2);

            foreach (IVertexInterface neighbour in neighboursList)
            {
                EdgeAdd(new Edge(vertex1, neighbour));
                EdgeAdd(new Edge(vertex2, neighbour));
            }

            EdgeAdd(new Edge(vertex1, vertex2));

            // ColoredGraph
            if (coloredGraph.GetIsInitializedColoredGraph())
            {
                coloredGraph.DeinitializationColoredGraph();
            }

            // GraphProperty
            GetGraphProperty().VertexExpansion(vertex);
        }
Beispiel #14
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);
            }
        }
Beispiel #15
0
        /// <summary>
        /// Union two components
        /// Time complexity: O(V)
        /// </summary>
        /// <param name="vertex1">first vertex</param>
        /// <param name="vertex2">second vertex</param>
        private void CycleUnion(IVertexInterface vertex1, IVertexInterface vertex2)
        {
            IVertexInterface vertex1Representative = CycleFind(vertex1);
            IVertexInterface vertex2Representative = CycleFind(vertex2);

            if (vertex1Representative == vertex2Representative)
            {
                return;
            }

            for (int i = 0; i < cycleDisjointSetDictionary.Count; i++)
            {
                IVertexInterface vertex = cycleDisjointSetDictionary.ElementAt(i).Key;
                if (cycleDisjointSetDictionary[vertex] == vertex2Representative)
                {
                    cycleDisjointSetDictionary[vertex] = vertex1Representative;
                }
            }
        }
        /// <summary>
        /// Get (BFS) spanning tree
        /// Change: spanningTreeBFS
        /// Time complexity: O(V + E)
        /// Space complexity: O(V + E)
        /// </summary>
        private void SpanningTreeBFS()
        {
            // Variable
            Queue <IVertexInterface>   vertexBFSQueue       = new Queue <IVertexInterface>();
            HashSet <IVertexInterface> visitedVertexHashSet = new HashSet <IVertexInterface>();
            List <IVertexInterface>    vertexNeighboursList;
            int countVertex = graph.GetGraphProperty().GetCountVertices();
            IVertexInterface root;

            spanningTreeBFS = new List <IEdgeInterface>();

            root = graph.GetFirstVertex();
            vertexBFSQueue.Enqueue(root);
            visitedVertexHashSet.Add(root);

            while (vertexBFSQueue.Count != 0)
            {
                IVertexInterface vertex = vertexBFSQueue.Dequeue();

                vertexNeighboursList = graph.Neighbours(vertex);

                foreach (IVertexInterface vertexNeighbour in vertexNeighboursList)
                {
                    if (visitedVertexHashSet.Contains(vertexNeighbour))
                    {
                        continue;
                    }

                    vertexBFSQueue.Enqueue(vertexNeighbour);
                    spanningTreeBFS.Add(new Edge(vertex, vertexNeighbour));
                    visitedVertexHashSet.Add(vertexNeighbour);

                    if (spanningTreeBFS.Count == countVertex - 1)
                    {
                        return;
                    }
                }
            }
        }
Beispiel #17
0
        /// <summary>
        /// Add a new edge
        /// If any vertex does not exist throws GraphVertexDoesntExist
        /// If vertex1 = vertex2 throws GraphInvalidVertexException
        /// If the edge already exists throws GraphDupliciteEdge
        /// </summary>
        /// <param name="vertex1">first vertex</param>
        /// <param name="vertex2">second vertex</param>
        protected void AddEdgeToAdjacencyList(IEdgeInterface edge)
        {
            // Variable
            IVertexInterface vertex;
            IVertexInterface vertex1 = edge.GetVertex1();
            IVertexInterface vertex2 = edge.GetVertex2();

            // If vertex1 is vertex2
            if (vertex1 == vertex2)
            {
                throw new MyException.GraphException.GraphInvalidVertexException();
            }

            // Symmetry
            for (int i = 0; i < 2; i++)
            {
                if (!adjacencyList.TryGetValue(ConvertVertexToVertexExtended(vertex1), out HashSet <VertexExtended> adjacencyHashSetVertexExtended))
                {
                    throw new MyException.GraphException.GraphVertexDoesntExistException();
                }

                if (adjacencyHashSetVertexExtended.Contains(ConvertVertexToVertexExtended(vertex2)))
                {
                    return;
                }
                // throw new MyException.GraphDupliciteEdge();

                adjacencyHashSetVertexExtended.Add(ConvertVertexToVertexExtended(vertex2));

                // Swap variables
                vertex  = vertex1;
                vertex1 = vertex2;
                vertex2 = vertex;
            }

            SetCanDeIncreaseCountEdges(true);
            graphProperty.IncrementCountEdges();
            SetCanDeIncreaseCountEdges(false);
        }
Beispiel #18
0
        /// <summary>
        /// Remove a vertex which has degree 2
        /// Deinitialize colored graph
        /// If the vertex does not exist in the graph throws GraphVertexDoesntExistException
        /// If the vertex does not have degree 2 throws GraphInvalidDegreeVertex
        /// </summary>
        /// <param name="vertex">vertex</param>
        public void VertexSuppression(IVertexInterface vertex)
        {
            if (!ExistsVertex(vertex))
            {
                throw new MyException.GraphException.GraphVertexDoesntExistException();
            }

            if (CountNeighbours(vertex) != 2)
            {
                throw new MyException.GraphException.GraphInvalidDegreeVertex();
            }

            // Variable
            IVertexInterface vertex1, vertex2;

            vertex1 = Neighbours(vertex).First();
            vertex2 = Neighbours(vertex).Last();

            VertexDelete(vertex);

            // ColoredGraph
            //coloredGraph.RemoveVertexInHashSets(vertex);

            if (!ExistsEdge(new Edge(vertex1, vertex2)))
            {
                EdgeAdd(new Edge(vertex1, vertex2));
            }

            // ColoredGraph
            if (coloredGraph.GetIsInitializedColoredGraph())
            {
                coloredGraph.DeinitializationColoredGraph();
            }

            // GraphProperty
            GetGraphProperty().VertexSuppression(vertex);
        }
Beispiel #19
0
        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;
        }
Beispiel #20
0
        /// <summary>
        /// Contract a vertex
        /// Deinitialize colored graph
        /// If the vertex does not exist in the graph throws GraphVertexDoesntExistException
        /// </summary>
        /// <param name="vertex">vertex</param>
        public void VertexContraction(IVertexInterface vertex)
        {
            if (!ExistsVertex(vertex))
            {
                throw new MyException.GraphException.GraphVertexDoesntExistException();
            }

            if (CountNeighbours(vertex) == 0)
            {
                return;
            }

            // Variable
            string nameNewVertex;
            List <IVertexInterface> neighboursVertexList, neighboursVertexUnionList, neighboursList;

            neighboursVertexUnionList = new List <IVertexInterface>();

            neighboursList = Neighbours(vertex);
            VertexDelete(vertex);

            nameNewVertex = vertex.GetUserName();
            foreach (IVertexInterface neighbour in neighboursList)
            {
                nameNewVertex += neighbour.GetUserName();
            }

            IVertexInterface newVertex = new Vertex(nameNewVertex);

            foreach (IVertexInterface neighbour in neighboursList)
            {
                neighboursVertexList = Neighbours(neighbour);

                neighboursVertexUnionList = neighboursVertexUnionList.Union(neighboursVertexList).ToList();
            }

            neighboursVertexUnionList = neighboursVertexUnionList.Except(neighboursList).ToList();

            // Delete vertices
            foreach (IVertexInterface removeVertex in neighboursList)
            {
                VertexDelete(removeVertex);

                // ColoredGraph
                // coloredGraph.RemoveVertexInHashSets(removeVertex);
            }

            // Add vertex
            VertexAdd(newVertex);

            // ColoredGraph
            coloredGraph.AddVertexInHashSets(newVertex);

            // Add edges
            foreach (IVertexInterface neighbour in neighboursVertexUnionList)
            {
                EdgeAdd(new Edge(newVertex, neighbour));
            }

            // ColoredGraph
            if (coloredGraph.GetIsInitializedColoredGraph())
            {
                coloredGraph.DeinitializationColoredGraph();
            }

            // GraphProperty
            GetGraphProperty().VertexContraction(vertex);
        }
Beispiel #21
0
 public Edge(IVertexInterface vertex1, IVertexInterface vertex2)
 {
     this.vertex1 = vertex1;
     this.vertex2 = vertex2;
 }
        private void BridgesCutVerticesRecursion(IVertexInterface vertex, HashSet <IVertexInterface> visitedVertexHashSet, Dictionary <IVertexInterface, int> discoveryTimesDictionary, Dictionary <IVertexInterface, int> lowDictionary, Dictionary <IVertexInterface, IVertexInterface> parentDictionary)
        {
            // Variable
            int children = 0;
            List <IVertexInterface> neighboursList = graph.Neighbours(vertex);

            visitedVertexHashSet.Add(vertex);

            if (!discoveryTimesDictionary.ContainsKey(vertex))
            {
                discoveryTimesDictionary.Add(vertex, 0);
            }

            if (!lowDictionary.ContainsKey(vertex))
            {
                lowDictionary.Add(vertex, 0);
            }

            discoveryTimesDictionary[vertex] = lowDictionary[vertex] = ++timeBridgesCutVertices;

            foreach (IVertexInterface neighbour in neighboursList)
            {
                if (!visitedVertexHashSet.Contains(neighbour))
                {
                    children++;

                    if (!parentDictionary.ContainsKey(neighbour))
                    {
                        parentDictionary.Add(neighbour, vertex);
                    }
                    else
                    {
                        parentDictionary[neighbour] = vertex;
                    }

                    BridgesCutVerticesRecursion(neighbour, visitedVertexHashSet, discoveryTimesDictionary, lowDictionary, parentDictionary);

                    if (!lowDictionary.ContainsKey(neighbour))
                    {
                        lowDictionary.Add(neighbour, 0);
                    }

                    lowDictionary[vertex] = Math.Min(lowDictionary[vertex], lowDictionary[neighbour]);

                    if (!cutVertices.Contains(vertex) && !parentDictionary.ContainsKey(vertex) && children > 1)
                    {
                        cutVertices.Add(vertex);
                    }

                    if (!cutVertices.Contains(vertex) && parentDictionary.ContainsKey(vertex) && lowDictionary[neighbour] >= discoveryTimesDictionary[vertex])
                    {
                        cutVertices.Add(vertex);
                    }

                    if (!bridges.Contains(new Edge(vertex, neighbour)) && lowDictionary[neighbour] > discoveryTimesDictionary[vertex])
                    {
                        bridges.Add(new Edge(vertex, neighbour));
                    }
                }
                else
                {
                    if (!parentDictionary.ContainsKey(vertex) || parentDictionary[vertex] != neighbour)
                    {
                        lowDictionary[vertex] = Math.Min(lowDictionary[vertex], discoveryTimesDictionary[neighbour]);
                    }
                }
            }
        }
Beispiel #23
0
 // Constructor
 public CycleNode(IVertexInterface vertex, int depth)
 {
     this.vertex = vertex;
     this.depth  = depth;
 }
Beispiel #24
0
        public void VertexExpansion(IVertexInterface vertex)
        {
            // Component
            if (componentsList != null)
            {
                // Need change => copies of component
                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;
                        }
                    }

                    myComponentGraph.VertexExpansion(myVertexComponent);
                }

                //componentsList = componentsList;
                //countComponents = countComponents;
                //isConnected = isConnected;
            }

            // Properties
            isCyclic   = null;
            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
            if (isChordal.HasValue && (bool)isChordal)
            {
                isChordal = null;
                perfectEliminationOrderingList = null;
                righNeighborhoodDictionary     = null;
            }

            // GraphClass
            graphClass = GraphClass.GraphClass.GraphClassEnum.undefined;
        }
Beispiel #25
0
        public void EdgeDelete(IEdgeInterface edge)
        {
            // Variable
            IVertexInterface vertex1 = graph.GetVertexByUserName(edge.GetVertex1().GetUserName());
            IVertexInterface vertex2 = graph.GetVertexByUserName(edge.GetVertex2().GetUserName());

            // Component
            if (componentsList != null)
            {
                if (countComponents > 1)
                {
                    // Get component which contains the vertex
                    IGraphInterface myComponentGraph = null;
                    foreach (IGraphInterface componentGraph in componentsList)
                    {
                        if (componentGraph.ExistsUserName(vertex1.GetUserName()))
                        {
                            myComponentGraph = componentGraph;
                            break;
                        }
                    }

                    myComponentGraph.EdgeDelete(new Edge(myComponentGraph.GetVertexByUserName(vertex1.GetUserName()), myComponentGraph.GetVertexByUserName(vertex2.GetUserName())));

                    componentsList  = null;
                    countComponents = null;
                    isConnected     = null;
                }
                else
                {
                    if (bridges != null)
                    {
                        // The edge is a bridge
                        if (bridges.Any(e => ((e.GetVertex1().Equals(vertex1) && e.GetVertex2().Equals(vertex2)) ||
                                              (e.GetVertex1().Equals(vertex2) && e.GetVertex2().Equals(vertex1)))))
                        {
                            componentsList  = null;
                            countComponents = null;
                            isConnected     = null;
                        }
                        // The edge isn't a bridge
                        else
                        {
                            //componentsList = componentsList;
                            //countComponents = countComponents;
                            //isConnected = isConnected;
                        }
                    }
                }
            }

            // Properties
            if (isCyclic != null && !(bool)isCyclic)
            {
                isCyclic = false;
            }
            else
            {
                isCyclic = null;
            }
            if (isEulerian == EulerianGraphEnum.eulerian)
            {
                isEulerian = EulerianGraphEnum.semiEulerian;
            }
            else
            {
                isEulerian = EulerianGraphEnum.undefined;
            }

            // IntegralInvariants
            circuitRank = null;
            if (girth != null && girth > 2)
            {
                girth = null;
            }
            //cayleysFormula = cayleysFormula;

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

            // DegreeSequence
            if (degreeSequence != null)
            {
                int countNeighbourVertex1 = graph.CountNeighbours(vertex1);
                int countNeighbourVertex2 = graph.CountNeighbours(vertex2);
                if (!isDegreeSequenceSorted)
                {
                    // Variable
                    int index1, index2;

                    index1 = degreeSequenceInt.FindIndex(i => i == (countNeighbourVertex1 + 1));
                    degreeSequenceInt[index1] = countNeighbourVertex1;
                    index2 = degreeSequenceInt.FindIndex(i => i == (countNeighbourVertex2 + 1));
                    degreeSequenceInt[index2] = countNeighbourVertex2;

                    index1 = degreeSequence.FindIndex(p => p.Key == vertex1);
                    index2 = degreeSequence.FindIndex(p => p.Key == vertex2);
                    degreeSequence[index1] = new KeyValuePair <IVertexInterface, int>(vertex1, countNeighbourVertex1);
                    degreeSequence[index2] = new KeyValuePair <IVertexInterface, int>(vertex2, countNeighbourVertex2);
                }
                else
                {
                    degreeSequence         = null;
                    degreeSequenceVertex   = null;
                    degreeSequenceInt      = null;
                    isDegreeSequenceSorted = false;
                }

                if (minimumVertexDegree.HasValue)
                {
                    if (countNeighbourVertex1 < minimumVertexDegree)
                    {
                        minimumVertexDegree = countNeighbourVertex1;
                    }
                    if (countNeighbourVertex2 < minimumVertexDegree)
                    {
                        minimumVertexDegree = countNeighbourVertex2;
                    }
                }

                if (maximumVertexDegree.HasValue)
                {
                    if (((countNeighbourVertex1 + 1) == maximumVertexDegree) ||
                        ((countNeighbourVertex2 + 1) == maximumVertexDegree))
                    {
                        maximumVertexDegree = null;
                    }
                }

                averageVertexDegree = null;
                medianVertexDegree  = null;
                isRegular           = null;
            }

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

            // GraphClass
            graphClass = GraphClass.GraphClass.GraphClassEnum.undefined;
        }
Beispiel #26
0
        public void VertexAdd(IVertexInterface vertex)
        {
            // Component
            if (componentsList != null)
            {
                if (countComponents > 2)
                {
                    // Create a new component with the vertex
                    IGraphEdgeListInterface newGraph = new GraphEdgeList(1);
                    newGraph.AddVertex(vertex.GetUserName());
                    newGraph.InitializeGraph();

                    componentsList.Add(newGraph);
                    countComponents++;
                    isConnected = false;
                }
                else
                {
                    componentsList  = null;
                    countComponents = null;
                    isConnected     = null;
                }
            }

            // Properties
            //isCyclic = isCyclic;
            isEulerian = EulerianGraphEnum.notEulerian;
            if (countComponents != order)
            {
                isRegular = false;
            }
            else
            {
                isRegular = true;
            }

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

            // SequencesAndPolynomialsOthers
            spanningTreeBFS = spanningTreeBFS = new List <IEdgeInterface>();
            //matching = matching;
            //cutVertices = cutVertices;
            //bridges = bridges;

            // DegreeSequence
            if (degreeSequence != null)
            {
                if (!isDegreeSequenceSorted)
                {
                    degreeSequence.Add(new KeyValuePair <IVertexInterface, int>(vertex, 0));
                    degreeSequenceVertex.Add(vertex);
                    degreeSequenceInt.Add(0);
                }
                else
                {
                    degreeSequence         = null;
                    degreeSequenceVertex   = null;
                    degreeSequenceInt      = null;
                    isDegreeSequenceSorted = false;
                }

                if (0 < minimumVertexDegree)
                {
                    minimumVertexDegree = 0;
                }

                //maximumVertexDegree = maximumVertexDegree;
                averageVertexDegree = null;
                medianVertexDegree  = null;
            }

            // Chordal
            //isChordal = isChordal;
            if (isChordal.HasValue && (bool)isChordal)
            {
                perfectEliminationOrderingList.Add(vertex);
            }

            // GraphClass
            //graphClass = graphClass;
        }
Beispiel #27
0
 /// <summary>
 /// Find a representant
 /// Time complexity: O(1)
 /// </summary>
 /// <param name="vertex">vertex</param>
 /// <returns>representant of the vertex</returns>
 private IVertexInterface CycleFind(IVertexInterface vertex)
 {
     return(cycleDisjointSetDictionary[vertex]);
 }
Beispiel #28
0
        public void EdgeSubdivision(IEdgeInterface edge)
        {
            // Variable
            IVertexInterface vertex1 = graph.GetVertexByUserName(edge.GetVertex1().GetUserName());
            IVertexInterface vertex2 = graph.GetVertexByUserName(edge.GetVertex2().GetUserName());

            // Component
            if (componentsList != null)
            {
                if (countComponents > 1)
                {
                    // Get component which contains the vertex
                    IGraphInterface myComponentGraph = null;
                    foreach (IGraphInterface componentGraph in componentsList)
                    {
                        if (componentGraph.ExistsUserName(vertex1.GetUserName()))
                        {
                            myComponentGraph = componentGraph;
                            break;
                        }
                    }

                    myComponentGraph.EdgeSubdivision(new Edge(myComponentGraph.GetVertexByUserName(vertex1.GetUserName()), myComponentGraph.GetVertexByUserName(vertex2.GetUserName())));
                }
                else
                {
                    //componentsList = componentsList;
                    //countComponents = countComponents;
                    //isConnected = isConnected;
                }
            }

            // Properties
            //isCyclic = isCyclic;
            //isEulerian = isEulerian;

            // IntegralInvariants
            //circuitRank = circuitRank;
            if (girth.HasValue && girth > 2)
            {
                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
            if (isChordal.HasValue && (bool)isChordal)
            {
                isChordal = null;
                perfectEliminationOrderingList = null;
                righNeighborhoodDictionary     = null;
            }

            // GraphClass
            graphClass = GraphClass.GraphClass.GraphClassEnum.undefined;
        }
Beispiel #29
0
 public Arc(IVertexInterface vertexFrom, IVertexInterface vertexTo)
 {
     this.vertexFrom = vertexFrom;
     this.vertexTo   = vertexTo;
 }