Пример #1
0
        private void Testing(SubGraphEnum subGraphEnum)
        {
            try
            {
                testPath      = ReaderWriter.ReaderWriter.CreateTestFile(testsDictionary[subGraphEnum].Item1);
                countVertices = testsDictionary[subGraphEnum].Item2;

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

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

                IGraphInterface subGraph = GraphOperation.SubGraph(graph, graph.AllVertices().Take(countVertices).ToList());

                stringBuilder.AppendLine("Subgraph created.");
                stringBuilder.AppendLine(subGraph.ToString());
            }
            catch (KeyNotFoundException)
            {
                throw new MyException.TestsException.TestsMissingTestException(subGraphEnum.ToString());
            }
            catch (MyException.ReaderWriterException.ReaderWriterException e)
            {
                stringBuilder.AppendLine(e.Message);
            }
        }
Пример #2
0
        private void Testing(ColoredGraphEnum coloredGraphEnum)
        {
            try
            {
                testPath = ReaderWriter.ReaderWriter.CreateTestFile(testsDictionary[coloredGraphEnum]);
                reader   = new ReaderWriter.ReaderGraph(testPath, false);
                graph    = reader.ReadFile();

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

                switch (coloredGraphEnum)
                {
                case ColoredGraphEnum.valid:
                    Valid(graph);
                    stringBuilder.AppendLine("Graph modified.");
                    break;

                case ColoredGraphEnum.invalid:
                    Invalid(graph);
                    stringBuilder.AppendLine("Graph modified.");
                    break;

                case ColoredGraphEnum.interchange1:
                case ColoredGraphEnum.interchange2:
                case ColoredGraphEnum.interchange3:
                case ColoredGraphEnum.interchange4:
                case ColoredGraphEnum.interchange5:
                    graph.GetColoredGraph().GreedyColoring(graph.AllVertices(), GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithInterchangeEnum.interchange);
                    stringBuilder.AppendLine("Graph colored.");
                    break;

                default:
                    throw new MyException.TestsException.TestsMissingTestException(coloredGraphEnum.ToString());
                }

                stringBuilder.AppendLine(graph.GetColoredGraph().ToString());
            }
            catch (KeyNotFoundException)
            {
                throw new MyException.TestsException.TestsMissingTestException(coloredGraphEnum.ToString());
            }
            catch (MyException.GraphException.GraphException e)
            {
                stringBuilder.AppendLine(e.ToString());
            }
        }
Пример #3
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);
        }
Пример #4
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);
        }
Пример #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
        private void Valid(IGraphInterface graph)
        {
            // Variable
            List <IVertexInterface> vertexList = graph.AllVertices();

            graph.VertexContraction(vertexList.First());
            vertexList = graph.AllVertices();

            graph.VertexSuppression(vertexList.First());
            vertexList = graph.AllVertices();

            graph.VertexExpansion(vertexList.First());
            vertexList = graph.AllVertices();

            graph.VertexSuppression(vertexList.Last());
            vertexList = graph.AllVertices();

            graph.VertexDelete(vertexList.First());
            vertexList = graph.AllVertices();

            graph.VertexSuppression(vertexList.Last());
            vertexList = graph.AllVertices();

            IVertexInterface v1 = new Vertex("V1");

            graph.VertexAdd(v1);
            vertexList = graph.AllVertices();

            graph.EdgeAdd(new Edge(vertexList.First(), v1));
            vertexList = graph.AllVertices();

            graph.VertexContraction(v1);
            vertexList = graph.AllVertices();

            graph.EdgeSubdivision(new Edge(vertexList.First(), vertexList.Skip(1).First()));
            vertexList = graph.AllVertices();

            graph.EdgeAdd(new Edge(vertexList.First(), vertexList.Skip(1).First()));
            vertexList = graph.AllVertices();

            graph.EdgeContraction(new Edge(vertexList.First(), vertexList.Skip(1).First()));
            vertexList = graph.AllVertices();

            graph.EdgeDelete(new Edge(vertexList.First(), vertexList.Skip(1).First()));
            vertexList = graph.AllVertices();

            graph.VertexDelete(vertexList.Skip(1).First());
            vertexList = graph.AllVertices();

            graph.VertexSuppression(vertexList.First());
            vertexList = graph.AllVertices();

            graph.VertexDelete(vertexList.First());
            vertexList = graph.AllVertices();

            graph.VertexExpansion(vertexList.First());
            vertexList = graph.AllVertices();

            graph.EdgeContraction(new Edge(vertexList.First(), vertexList.Skip(1).First()));
            vertexList = graph.AllVertices();

            IVertexInterface v2 = new Vertex("V2");

            graph.VertexAdd(v2);
            vertexList = graph.AllVertices();

            graph.VertexDelete(vertexList.Skip(1).First());

            graph.VertexAdd(new Vertex("V3"));
        }
Пример #7
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);
        }
Пример #8
0
        private void Invalid(IGraphInterface graph)
        {
            // Variable
            List <IVertexInterface> vertexList   = graph.AllVertices();
            IColoredGraphInterface  coloredGraph = graph.GetColoredGraph();

            coloredGraph.GreedyColoring(vertexList);
            coloredGraph.InitializeColoredGraph();

            // VertexColor - initialization
            stringBuilder.AppendLine("VertexColor - initialization");
            try { coloredGraph.ColorVertex(vertexList.First(), 2); }
            catch (MyException.GraphException.GraphException e) { stringBuilder.AppendLine(e.Message); }

            // GreedingColoring - initialization
            stringBuilder.AppendLine("GreedyColoring - initialization");
            try { coloredGraph.GreedyColoring(vertexList); }
            catch (MyException.GraphException.GraphException e) { stringBuilder.AppendLine(e.Message); }

            // ResetColorVertex - initialization
            stringBuilder.AppendLine("Reset - initialization");
            try { coloredGraph.ResetColorVertex(vertexList.First()); }
            catch (MyException.GraphException.GraphException e) { stringBuilder.AppendLine(e.Message); }

            // Reset - initialization
            stringBuilder.AppendLine("Reset - initialization");
            try { coloredGraph.ResetColors(); }
            catch (MyException.GraphException.GraphException e) { stringBuilder.AppendLine(e.Message); }

            // Initialization - initialization
            stringBuilder.AppendLine("Initialization - initialization");
            try { coloredGraph.InitializeColoredGraph(); }
            catch (MyException.GraphException.GraphException e) { stringBuilder.AppendLine(e.Message); }

            coloredGraph.DeinitializationColoredGraph();

            // Deinitialization - initialization
            stringBuilder.AppendLine("Deinitialization - initialization");
            try { coloredGraph.DeinitializationColoredGraph(); }
            catch (MyException.GraphException.GraphException e) { stringBuilder.AppendLine(e.Message); }

            // ColorVertex - Doesn't exist
            stringBuilder.AppendLine("VertexColor - Doesn't exist");
            try { coloredGraph.ColorVertex(new Vertex(), 3); }
            catch (MyException.GraphException.GraphException e) { stringBuilder.AppendLine(e.Message); }

            // GreedyColoring - Doesn't exist
            stringBuilder.AppendLine("GreedyColoring - Doesn't exist");
            try { coloredGraph.GreedyColoring(new Vertex()); }
            catch (MyException.GraphException.GraphException e) { stringBuilder.AppendLine(e.Message); }

            // CheckValidColor - Doesn't exist
            stringBuilder.AppendLine("CheckValidColor - Doesn't exist");
            try { coloredGraph.CheckValidColor(new Vertex()); }
            catch (MyException.GraphException.GraphException e) { stringBuilder.AppendLine(e.Message); }

            // IsVertexColored - Doesn't exist
            stringBuilder.AppendLine("IsVertexColored - Doesn't exist");
            try { coloredGraph.IsVertexColored(new Vertex()); }
            catch (MyException.GraphException.GraphException e) { stringBuilder.AppendLine(e.Message); }

            // GetColorVertex - Doesn't exist
            stringBuilder.AppendLine("GetColorVertex - Doesn't exist");
            try { coloredGraph.GetColorVertex(new Vertex()); }
            catch (MyException.GraphException.GraphException e) { stringBuilder.AppendLine(e.Message); }
        }
Пример #9
0
        private void Valid(IGraphInterface graph)
        {
            // Variable
            List <IVertexInterface> vertexList   = graph.AllVertices();
            IColoredGraphInterface  coloredGraph = graph.GetColoredGraph();

            if (coloredGraph.InitializeColoredGraph())
            {
                throw new MyException.TestsException.SomethingWrongTestException();
            }

            coloredGraph.ColorVertex(vertexList.First(), coloredGraph.GreedyColoring(vertexList.First()));

            if (coloredGraph.InitializeColoredGraph())
            {
                throw new MyException.TestsException.SomethingWrongTestException();
            }

            coloredGraph.ResetColorVertex(vertexList.First());

            coloredGraph.GreedyColoring(vertexList);

            if (!coloredGraph.InitializeColoredGraph())
            {
                throw new MyException.TestsException.SomethingWrongTestException();
            }

            coloredGraph.DeinitializationColoredGraph();
            if (!coloredGraph.InitializeColoredGraph())
            {
                throw new MyException.TestsException.SomethingWrongTestException();
            }
            coloredGraph.DeinitializationColoredGraph();

            coloredGraph.ResetColors();
            coloredGraph.ResetColors();

            if (coloredGraph.InitializeColoredGraph())
            {
                throw new MyException.TestsException.SomethingWrongTestException();
            }

            coloredGraph.ColorVertex(vertexList.First(), 1);
            coloredGraph.ColorVertex(vertexList.ElementAt(1), 1);

            if (coloredGraph.CheckValidColor().Count == 0)
            {
                throw new MyException.TestsException.SomethingWrongTestException();
            }

            coloredGraph.ColorVertex(vertexList.First(), 2);

            if (!coloredGraph.IsVertexColored(vertexList.First()))
            {
                throw new MyException.TestsException.SomethingWrongTestException();
            }

            if (coloredGraph.IsVertexColored(vertexList.ElementAt(2)))
            {
                throw new MyException.TestsException.SomethingWrongTestException();
            }

            coloredGraph.ResetColorVertex(vertexList.First());

            if (coloredGraph.IsVertexColored(vertexList.First()))
            {
                throw new MyException.TestsException.SomethingWrongTestException();
            }

            coloredGraph.ResetColorVertex(vertexList.ElementAt(2));

            if (coloredGraph.IsVertexColored(vertexList.ElementAt(2)))
            {
                throw new MyException.TestsException.SomethingWrongTestException();
            }

            coloredGraph.ColorVertex(vertexList.First(), 1);

            if (coloredGraph.CheckValidColor(vertexList.First()))
            {
                throw new MyException.TestsException.SomethingWrongTestException();
            }

            if (!coloredGraph.CheckValidColor(vertexList.ElementAt(2)))
            {
                throw new MyException.TestsException.SomethingWrongTestException();
            }

            if (coloredGraph.GreedyColoring(vertexList.First()) != 2)
            {
                throw new MyException.TestsException.SomethingWrongTestException();
            }

            if (coloredGraph.GetUnColoredVertexList().Count != 4)
            {
                throw new MyException.TestsException.SomethingWrongTestException();
            }

            if (coloredGraph.GetColoredVertexList().Count != 2)
            {
                throw new MyException.TestsException.SomethingWrongTestException();
            }

            if (coloredGraph.GetCountUsedColors() != 1)
            {
                throw new MyException.TestsException.SomethingWrongTestException();
            }

            coloredGraph.GreedyColoring(vertexList);

            if (coloredGraph.GetCountUsedColors() != 2)
            {
                throw new MyException.TestsException.SomethingWrongTestException();
            }

            coloredGraph.ColorVertex(vertexList.First(), VertexExtended.GetDefaultColor());

            if (coloredGraph.CheckValidColor().Count != 0)
            {
                throw new MyException.TestsException.SomethingWrongTestException();
            }

            coloredGraph.GreedyColoring(vertexList);
            coloredGraph.InitializeColoredGraph();

            if (!coloredGraph.GetIsInitializedColoredGraph())
            {
                throw new MyException.TestsException.SomethingWrongTestException();
            }

            graph.VertexAdd(new Vertex());

            if (coloredGraph.GetIsInitializedColoredGraph())
            {
                throw new MyException.TestsException.SomethingWrongTestException();
            }

            vertexList = graph.AllVertices();

            if (vertexList.Last().GetColor() != VertexExtended.GetDefaultColor())
            {
                throw new MyException.TestsException.SomethingWrongTestException();
            }

            coloredGraph.ColorVertex(vertexList.Last(), 2);
            coloredGraph.ColorVertex(vertexList.First(), 2);

            if (coloredGraph.InitializeColoredGraph())
            {
                throw new MyException.TestsException.SomethingWrongTestException();
            }

            coloredGraph.GreedyColoring(vertexList);

            if (!coloredGraph.InitializeColoredGraph())
            {
                throw new MyException.TestsException.SomethingWrongTestException();
            }

            graph.VertexDelete(vertexList.Last());
            graph.VertexAdd(new Vertex());
            vertexList = graph.AllVertices();

            if (coloredGraph.GetColoredVertexList().Count != 6)
            {
                throw new MyException.TestsException.SomethingWrongTestException();
            }

            if (coloredGraph.GetUnColoredVertexList().Count != 1)
            {
                throw new MyException.TestsException.SomethingWrongTestException();
            }

            coloredGraph.ColorVertex(vertexList.Last(), 3);

            if (coloredGraph.GetCountUsedColors() != 3)
            {
                throw new MyException.TestsException.SomethingWrongTestException();
            }

            graph.VertexDelete(vertexList.Last());

            if (coloredGraph.GetColoredVertexList().Count != 6)
            {
                throw new MyException.TestsException.SomethingWrongTestException();
            }

            if (coloredGraph.GetUnColoredVertexList().Count != 0)
            {
                throw new MyException.TestsException.SomethingWrongTestException();
            }

            if (coloredGraph.GetCountUsedColors() != 2)
            {
                throw new MyException.TestsException.SomethingWrongTestException();
            }
        }