Пример #1
0
        private void Testing(ComponentEnum componentEnum)
        {
            try
            {
                testPath = ReaderWriter.ReaderWriter.CreateTestFile(testsDictionary[componentEnum]);

                reader = new ReaderWriter.ReaderGraph(testPath, false);
                graph  = reader.ReadFile();

                stringBuilder.AppendLine(componentEnum.ToString());
                stringBuilder.AppendLine("Graph created.");
                stringBuilder.AppendLine(graph.ToString());

                stringBuilder.AppendLine("Number of components: " + graph.GetGraphProperty().GetCountComponents());
                stringBuilder.AppendLine("Is graph connected: " + graph.GetGraphProperty().GetIsConnected());
                stringBuilder.AppendLine("Circuit rank: " + graph.GetGraphProperty().GetCircuitRank());

                graphComponentList = graph.GetGraphProperty().GetComponents();

                foreach (Graph graphComponent in graphComponentList)
                {
                    stringBuilder.AppendLine("Graph component.");
                    stringBuilder.AppendLine(graphComponent.ToString());
                }
            }
            catch (KeyNotFoundException)
            {
                throw new MyException.TestsException.TestsMissingTestException(componentEnum.ToString());
            }
            catch (MyException.ReaderWriterException.ReaderWriterException e)
            {
                stringBuilder.AppendLine(e.Message);
            }
        }
Пример #2
0
        private void Testing(CycleEnum cycleEnum)
        {
            try
            {
                testPath = ReaderWriter.ReaderWriter.CreateTestFile(testsDictionary[cycleEnum]);

                reader = new ReaderWriter.ReaderGraph(testPath, false);
                graph  = reader.ReadFile();

                stringBuilder.AppendLine(cycleEnum.ToString());
                stringBuilder.AppendLine("Graph created.");
                stringBuilder.AppendLine(graph.ToString());

                stringBuilder.AppendLine("Is graph cyclic: " + graph.GetGraphProperty().GetIsCyclic());
                stringBuilder.AppendLine("Gridth: " + graph.GetGraphProperty().GetGirth());
            }
            catch (KeyNotFoundException)
            {
                throw new MyException.TestsException.TestsMissingTestException(cycleEnum.ToString());
            }
            catch (MyException.ReaderWriterException.ReaderWriterException e)
            {
                stringBuilder.AppendLine(e.Message);
            }
        }
Пример #3
0
        private void Testing(ChordalTestEnum chordalEnum)
        {
            try
            {
                testPath = ReaderWriter.ReaderWriter.CreateTestFile(testsDictionary[chordalEnum]);

                reader = new ReaderWriter.ReaderGraph(testPath, false);
                graph  = reader.ReadFile();

                stringBuilder.AppendLine(chordalEnum.ToString());
                stringBuilder.AppendLine("Graph created.");
                stringBuilder.AppendLine(graph.ToString());

                stringBuilder.AppendLine("isChordal " + graph.GetGraphProperty().GetIsChordal());

                foreach (IVertexInterface vertex in graph.GetGraphProperty().GetPerfectEliminationOrdering())
                {
                    stringBuilder.AppendLine("- " + vertex.GetIdentifier());
                }
            }
            catch (KeyNotFoundException)
            {
                throw new MyException.TestsException.TestsMissingTestException(chordalEnum.ToString());
            }
            catch (MyException.ReaderWriterException.ReaderWriterException e)
            {
                stringBuilder.AppendLine(e.Message);
            }
            catch (MyException.GraphException.GraphIsNotConnected e)
            {
                stringBuilder.AppendLine(e.Message);
            }
        }
Пример #4
0
        /// <summary>
        /// Return true if the graph is complete graph
        /// Time complexity: O(1)
        /// </summary>
        /// <param name="graph">graph</param>
        /// <returns>true if the graph is complete graph, otherwise false</returns>
        public static bool IsCompleteGraph(IGraphInterface graph)
        {
            // |E| = (|V|)C(2)
            if (graph.GetGraphProperty().GetCountEdges() == MyMath.MyMath.nCr(graph.GetGraphProperty().GetCountVertices(), 2))
            {
                return(true);
            }

            return(false);
        }
Пример #5
0
        /// <summary>
        /// Return true if the graph is tree
        /// Time complexity: O(V + E)
        /// </summary>
        /// <param name="graph">graph</param>
        /// <returns>true if the graph is tree, otherwise false</returns>
        public static bool IsTreeGraph(IGraphInterface graph)
        {
            // Graph is connected and |E| = |V| - 1 // Euler's formula
            if ((graph.GetGraphProperty().GetIsConnected()) &&
                (graph.GetGraphProperty().GetCountEdges() == graph.GetGraphProperty().GetCountVertices() - 1))
            {
                return(true);
            }

            return(false);
        }
Пример #6
0
        /// <summary>
        /// Return true if the graph is cycle
        /// Time complexity: O(V + E)
        /// </summary>
        /// <param name="graph">graph</param>
        /// <returns>true if the graph is cycle, otherwise false</returns>
        public static bool IsCycleGraph(IGraphInterface graph)
        {
            // Graph is connected and 2-regular
            if ((graph.GetGraphProperty().GetIsConnected()) &&
                (graph.GetGraphProperty().GetIsRegular()) &&
                (graph.GetGraphProperty().GetMaximumVertexDegree() == 2))
            {
                return(true);
            }

            return(false);
        }
Пример #7
0
        private void Testing(SpanningTreeEnum spanningTreeEnum)
        {
            try
            {
                testPath = ReaderWriter.ReaderWriter.CreateTestFile(testsDictionary[spanningTreeEnum]);
                reader   = new ReaderWriter.ReaderGraph(testPath, false);
                graph    = reader.ReadFile();

                stringBuilder.AppendLine(spanningTreeEnum.ToString());
                stringBuilder.AppendLine("Graph created.");
                stringBuilder.AppendLine(graph.ToString());

                stringBuilder.AppendLine("SpanningTree: ");

                List <IEdgeInterface> spanningTreeList = graph.GetGraphProperty().GetSpanningTree();

                foreach (IEdgeInterface edge in spanningTreeList)
                {
                    stringBuilder.AppendLine(edge.ToString());
                }
            }
            catch (KeyNotFoundException)
            {
                throw new MyException.TestsException.TestsMissingTestException(spanningTreeEnum.ToString());
            }
            catch (MyException.ReaderWriterException.ReaderWriterException e)
            {
                stringBuilder.AppendLine(e.Message);
            }
        }
Пример #8
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);
        }
        private void Testing(BridgesCutVerticesEnum bridgesCutVerticesEnum)
        {
            try
            {
                testPath = ReaderWriter.ReaderWriter.CreateTestFile(testsDictionary[bridgesCutVerticesEnum]);

                reader = new ReaderWriter.ReaderGraph(testPath, false);
                graph  = reader.ReadFile();

                stringBuilder.AppendLine(bridgesCutVerticesEnum.ToString());
                stringBuilder.AppendLine("Graph created.");
                stringBuilder.AppendLine(graph.ToString());

                stringBuilder.AppendLine("Number of cut vertices: " + graph.GetGraphProperty().GetCutVertices().Count);
                stringBuilder.AppendLine("Cut vertices: ");
                graph.GetGraphProperty().GetCutVertices().ForEach(x => { stringBuilder.AppendLine(x.GetUserName()); });
                stringBuilder.AppendLine("Number of bridges: " + graph.GetGraphProperty().GetBridges().Count);
                stringBuilder.AppendLine("Bridges: ");
                graph.GetGraphProperty().GetBridges().ForEach(x => { stringBuilder.AppendLine(x.GetVertex1().GetUserName() + " " + x.GetVertex2().GetUserName()); });

                graph.GetGraphProperty().Reset();
                stringBuilder.AppendLine("Number of cut vertices: " + graph.GetGraphProperty().GetCutVertices().Count);
                stringBuilder.AppendLine("Number of bridges: " + graph.GetGraphProperty().GetBridges().Count);
            }
            catch (KeyNotFoundException)
            {
                throw new MyException.TestsException.TestsMissingTestException(bridgesCutVerticesEnum.ToString());
            }
            catch (MyException.ReaderWriterException.ReaderWriterException e)
            {
                stringBuilder.AppendLine(e.Message);
            }
            catch (MyException.GraphException.GraphIsNotConnected e)
            {
                stringBuilder.AppendLine(e.Message);
            }
        }
Пример #10
0
        private void Testing(DegreeSequenceEnum degreeSequenceEnum)
        {
            try
            {
                testPath = ReaderWriter.ReaderWriter.CreateTestFile(testsDictionary[degreeSequenceEnum]);

                reader = new ReaderWriter.ReaderGraph(testPath, false);
                graph  = reader.ReadFile();

                stringBuilder.AppendLine(degreeSequenceEnum.ToString());
                stringBuilder.AppendLine("Graph created.");
                stringBuilder.AppendLine(graph.ToString());

                // Sorted
                List <int> degreeSequenceList = graph.GetGraphProperty().GetDegreeSequenceInt(true);

                stringBuilder.AppendLine("Degree sequence");
                foreach (int degree in degreeSequenceList)
                {
                    stringBuilder.Append(degree + " ");
                }

                stringBuilder.AppendLine("");
                stringBuilder.AppendLine("Minimum vertex degree: " + graph.GetGraphProperty().GetMinimumVertexDegree());
                stringBuilder.AppendLine("Maximum vertex degree: " + graph.GetGraphProperty().GetMaximumVertexDegree());
                stringBuilder.AppendLine("Average vertex degree: " + graph.GetGraphProperty().GetAverageVertexDegree());
                stringBuilder.AppendLine("Is graph regular: " + graph.GetGraphProperty().GetIsRegular());

                graph.GetGraphProperty().Reset();

                // Unsorted
                degreeSequenceList = graph.GetGraphProperty().GetDegreeSequenceInt(false);

                stringBuilder.AppendLine("Degree sequence");
                foreach (int degree in degreeSequenceList)
                {
                    stringBuilder.Append(degree + " ");
                }

                stringBuilder.AppendLine("");
                stringBuilder.AppendLine("Minimum vertex degree: " + graph.GetGraphProperty().GetMinimumVertexDegree());
                stringBuilder.AppendLine("Maximum vertex degree: " + graph.GetGraphProperty().GetMaximumVertexDegree());
                stringBuilder.AppendLine("Average vertex degree: " + graph.GetGraphProperty().GetAverageVertexDegree());
                stringBuilder.AppendLine("Is graph regular: " + graph.GetGraphProperty().GetIsRegular());
            }
            catch (KeyNotFoundException)
            {
                throw new MyException.TestsException.TestsMissingTestException(degreeSequenceEnum.ToString());
            }
            catch (MyException.ReaderWriterException.ReaderWriterException e)
            {
                stringBuilder.AppendLine(e.Message);
            }
        }
Пример #11
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));
        }
Пример #12
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);
        }