Пример #1
0
        /// <summary>
        /// Return a subgraph
        /// If the graph is not initialized throws GraphInitializationException
        /// Time complexity: O(V + E)
        /// Space complexity: O(V + E)
        /// </summary>
        /// <param name="graph">graph</param>
        /// <param name="vertexList">list of vertices which will be in the subgraph</param>
        /// <returns>subgraph</returns>
        public static IGraphInterface SubGraph(IGraphInterface graph, List <IVertexInterface> vertexList)
        {
            // Variable
            IGraphEdgeListInterface subGraph;
            List <IVertexInterface> neighboursList;

            if (!graph.GetIsInitialized())
            {
                throw new MyException.GraphException.GraphInitializationException();
            }

            subGraph = new GraphEdgeList(vertexList.Count);
            subGraph.SetName("Subgraph - " + graph.GetName());

            foreach (IVertexInterface vertex in vertexList)
            {
                neighboursList = graph.Neighbours(vertex).Intersect(vertexList).ToList();

                if (neighboursList.Count == 0)
                {
                    subGraph.AddVertex(vertex.GetUserName());
                    continue;
                }

                foreach (IVertexInterface neighbour in neighboursList)
                {
                    subGraph.AddEdge(vertex.GetUserName(), neighbour.GetUserName());
                }
            }

            subGraph.InitializeGraph();

            return(subGraph);
        }
Пример #2
0
        /// <summary>
        /// Return a complement graph
        /// If the graph is not initialized throws GraphInitializationException
        /// </summary>
        /// <param name="graph">graph</param>
        /// <returns>complement graph</returns>
        public static IGraphInterface ComplementGraph(IGraphInterface graph)
        {
            // Variable
            IGraphEdgeListInterface complementGraph;
            List <IVertexInterface> vertexList;
            List <IVertexInterface> neighboursList;
            List <IVertexInterface> intersectionVertexAndNeighboursList;

            if (!graph.GetIsInitialized())
            {
                throw new MyException.GraphException.GraphInitializationException();
            }

            complementGraph = new GraphEdgeList(graph.GetRealCountVertices());
            complementGraph.SetName("Complement graph - " + graph.GetName());
            vertexList = graph.AllVertices();

            // Add edges
            foreach (IVertexInterface vertex in vertexList)
            {
                neighboursList = graph.Neighbours(vertex);
                neighboursList.Add(vertex);

                intersectionVertexAndNeighboursList = vertexList.FindAll(v => !neighboursList.Contains(v)).ToList();

                if (intersectionVertexAndNeighboursList.Count == 0)
                {
                    complementGraph.AddVertex(vertex.GetUserName());
                    continue;
                }

                foreach (IVertexInterface neighbour in intersectionVertexAndNeighboursList)
                {
                    complementGraph.AddEdge(vertex.GetUserName(), neighbour.GetUserName());
                }
            }

            complementGraph.InitializeGraph();

            return(complementGraph);
        }
Пример #3
0
        /// <summary>
        /// Copy a graph
        /// If the graph is not initialized throws GraphInitializationException
        /// Time complexity: O(V + E)
        /// Space complexity: O(V + E)
        /// </summary>
        /// <param name="graph">graph</param>
        /// <returns>graph copy</returns>
        public static IGraphInterface CopyGraph(IGraphInterface graph)
        {
            if (!graph.GetIsInitialized())
            {
                throw new MyException.GraphException.GraphInitializationException();
            }

            // Variable
            IGraphEdgeListInterface graphCopy;
            List <IVertexInterface> neighboursVertexList;
            List <IVertexInterface> allVerticesList = graph.AllVertices();

            graphCopy = new GraphEdgeList(graph.GetGraphProperty().GetCountVertices());

            graphCopy.SetName(graph.GetName());

            foreach (IVertexInterface vertex1 in allVerticesList)
            {
                neighboursVertexList = graph.Neighbours(vertex1);

                if (neighboursVertexList.Count == 0)
                {
                    graphCopy.AddVertex(vertex1.GetUserName());
                    continue;
                }

                foreach (IVertexInterface vertex2 in neighboursVertexList)
                {
                    graphCopy.AddEdge(vertex1.GetUserName(), vertex2.GetUserName());
                }
            }

            graphCopy.InitializeGraph();

            return(graphCopy);
        }
Пример #4
0
        /// <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));
        }
Пример #5
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); }
        }
Пример #6
0
        /// <summary>
        /// Return a line graph
        /// If the graph is not initialized throws GraphInitializationException
        /// If teh graph has 0 edges throws GraphLineGraphInvalidCountOfEdges
        /// </summary>
        /// <param name="graph">graph</param>
        /// <returns>line graf</returns>
        public static IGraphInterface LineGraph(IGraphInterface graph)
        {
            // Variable
            int    idVertex1, idVertex2;
            string idNewVertex, userNameNewVertex;
            string userNameVertex1, userNameVertex2;
            IGraphEdgeListInterface lineGraph;
            List <IVertexInterface> vertexList;
            List <IVertexInterface> neighboursList;
            Dictionary <string, IVertexInterface> vertexMap;
            List <IVertexInterface> neighboursNewList;

            if (!graph.GetIsInitialized())
            {
                throw new MyException.GraphException.GraphInitializationException();
            }

            if (graph.GetGraphProperty().GetCountEdges() == 0)
            {
                throw new MyException.GraphException.GraphLineGraphInvalidCountOfEdges(graph.GetGraphProperty().GetCountEdges().ToString());
            }

            lineGraph = new GraphEdgeList(graph.GetGraphProperty().GetCountEdges());
            lineGraph.SetName("Line graph - " + graph.GetName());

            vertexMap  = new Dictionary <string, IVertexInterface>();
            vertexList = graph.AllVertices();

            foreach (IVertexInterface vertex in vertexList)
            {
                idVertex1       = vertex.GetIdentifier();
                userNameVertex1 = vertex.GetUserName();
                neighboursList  = graph.Neighbours(vertex);

                neighboursNewList = new List <IVertexInterface>();

                foreach (IVertexInterface neighbour in neighboursList)
                {
                    idVertex2       = neighbour.GetIdentifier();
                    userNameVertex2 = neighbour.GetUserName();

                    if (idVertex1 < idVertex2)
                    {
                        idNewVertex       = idVertex1.ToString() + idVertex2.ToString();
                        userNameNewVertex = userNameVertex1 + userNameVertex2;
                    }
                    else
                    {
                        idNewVertex       = idVertex2.ToString() + idVertex1.ToString();
                        userNameNewVertex = userNameVertex2 + userNameVertex1;
                    }

                    if (!vertexMap.TryGetValue(idNewVertex, out IVertexInterface newVertex))
                    {
                        newVertex = new Vertex(userNameNewVertex);
                        vertexMap.Add(idNewVertex, newVertex);
                    }

                    neighboursNewList.Add(newVertex);
                }

                if (neighboursList.Count == 1)
                {
                    lineGraph.AddVertex(neighboursNewList.First().GetUserName());
                    continue;
                }

                for (int i = 0; i < neighboursNewList.Count - 1; i++)
                {
                    for (int j = i + 1; j < neighboursNewList.Count; j++)
                    {
                        lineGraph.AddEdge(neighboursNewList[i].GetUserName(), neighboursNewList[j].GetUserName());
                    }
                }
            }

            lineGraph.InitializeGraph();

            return(lineGraph);
        }