Exemple #1
0
        public void CyclesDetector_ShouldDetermineWhetherGraphContainsAnyCycle(GraphBase <int> graph, int initialVertex, bool expectedResult)
        {
            var cyclesDetector = new CyclesDetector <GraphBase <int>, int>();

            var result = cyclesDetector.Execute(graph, initialVertex);

            Assert.Equal(expectedResult, result);
        }
Exemple #2
0
        public void CyclesDetector_ShouldThrowArgumentException_WhenNotExistingInitialVertexSpecified(GraphBase <int> graph, int initialVertex)
        {
            var cyclesDetector = new CyclesDetector <GraphBase <int>, int>();

            Assert.Throws <ArgumentException>(() =>
            {
                cyclesDetector.Execute(graph, initialVertex);
            });
        }
Exemple #3
0
        public void CyclesDetector_ShouldThrowArgumentNullException_WhenTargetGraphIsNull()
        {
            var cyclesDetector = new CyclesDetector <GraphBase <int>, int>();

            Assert.Throws <ArgumentNullException>(() =>
            {
                cyclesDetector.Execute(null, default);
            });
        }
Exemple #4
0
        public static void DoTest()
        {
            string[] V;
            DirectedSparseGraph <string>   DAG;
            UndirectedSparseGraph <string> CyclicGraph;
            DirectedSparseGraph <string>   DigraphWithCycles;

            // Init graph object
            DigraphWithCycles = new DirectedSparseGraph <string>();

            // Init V
            V = new string[6] {
                "r", "s", "t", "x", "y", "z"
            };

            // Insert V
            DigraphWithCycles.AddVertices(V);

            // Insert E
            DigraphWithCycles.AddEdge("r", "s");
            DigraphWithCycles.AddEdge("r", "t");
            DigraphWithCycles.AddEdge("s", "t");
            DigraphWithCycles.AddEdge("s", "x");
            DigraphWithCycles.AddEdge("t", "x");
            DigraphWithCycles.AddEdge("t", "y");
            DigraphWithCycles.AddEdge("t", "z");
            DigraphWithCycles.AddEdge("x", "y");
            DigraphWithCycles.AddEdge("x", "z");
            DigraphWithCycles.AddEdge("y", "z");
            DigraphWithCycles.AddEdge("z", "r");
            DigraphWithCycles.AddEdge("z", "s");

            var isCyclic = CyclesDetector.IsCyclic(DigraphWithCycles);

            Debug.Assert(isCyclic, "Wrong status! The graph has cycles.");

            // PRINT THE GRAPH
            Console.WriteLine("[*] Directed Graph:");
            Console.WriteLine(DigraphWithCycles.ToReadable() + "\r\n");
            Console.WriteLine("Was the previous graph cyclic? " + isCyclic);

            Console.WriteLine("\r\n*********************************************\r\n");


            /***************************************************************************************/


            CyclicGraph = new UndirectedSparseGraph <string>();

            V = new[] { "A", "B", "C", "D", "E" };

            // Insert new values of V
            CyclicGraph.AddVertices(V);

            // Insert new value for edges
            CyclicGraph.AddEdge("A", "C");
            CyclicGraph.AddEdge("B", "A");
            CyclicGraph.AddEdge("B", "C");
            CyclicGraph.AddEdge("C", "E");
            CyclicGraph.AddEdge("C", "D");
            CyclicGraph.AddEdge("D", "B");
            CyclicGraph.AddEdge("E", "D");

            isCyclic = CyclesDetector.IsCyclic(CyclicGraph);
            Debug.Assert(isCyclic, "Wrong status! The graph has cycles.");

            // PRINT THE GRAPH
            Console.WriteLine("[*] Undirected Graph:");
            Console.WriteLine(CyclicGraph.ToReadable() + "\r\n");
            Console.WriteLine("Was the previous graph cyclic? " + isCyclic);

            Console.WriteLine("\r\n*********************************************\r\n");


            /***************************************************************************************/


            DAG = new DirectedSparseGraph <string>();

            V = new[] { "A", "B", "C", "D", "E", "X" };

            // Insert new values of V
            DAG.AddVertices(V);

            // Insert new value for edges
            DAG.AddEdge("A", "B");
            DAG.AddEdge("A", "X");
            DAG.AddEdge("B", "C");
            DAG.AddEdge("C", "D");
            DAG.AddEdge("D", "E");
            DAG.AddEdge("E", "X");

            isCyclic = CyclesDetector.IsCyclic(DAG);
            Debug.Assert(isCyclic == false, "Wrong status! The graph has no cycles.");

            // PRINT THE GRAPH
            Console.WriteLine("[*] DAG (Directed Asyclic Graph):");
            Console.WriteLine(DAG.ToReadable() + "\r\n");
            Console.WriteLine("Was the previous graph cyclic? " + isCyclic);

            Console.ReadLine();
        }
        // GET: /<controller>/
        public IActionResult Index()
        {
            string[] V;
            DirectedSparseGraph <string> DAG;
            //UndirectedSparseGraph<string> CyclicGraph;
            DirectedSparseGraph <string> DigraphWithCycles;

            // Init graph object
            DigraphWithCycles = new DirectedSparseGraph <string>();

            // Init V
            V = new string[6] {
                "r", "s", "t", "x", "y", "z"
            };

            result = result + "Vertices: " + "'r', 's', 't', 'x', 'y', 'z'" + "\n";

            // Insert V
            DigraphWithCycles.AddVertices(V);

            // Insert E
            DigraphWithCycles.AddEdge("r", "s");
            DigraphWithCycles.AddEdge("r", "t");
            DigraphWithCycles.AddEdge("s", "t");
            DigraphWithCycles.AddEdge("s", "x");
            DigraphWithCycles.AddEdge("t", "x");
            DigraphWithCycles.AddEdge("t", "y");
            DigraphWithCycles.AddEdge("t", "z");
            DigraphWithCycles.AddEdge("x", "y");
            DigraphWithCycles.AddEdge("x", "z");
            DigraphWithCycles.AddEdge("y", "z");
            DigraphWithCycles.AddEdge("z", "r");
            DigraphWithCycles.AddEdge("z", "s");

            var isCyclic = CyclesDetector.IsCyclic <string>(DigraphWithCycles);

            // PRINT THE GRAPH
            result = result + "[*] Directed Graph:";
            result = result + DigraphWithCycles.ToReadable() + "\r\n";
            result = result + "Is directed graph above cyclic? " + "=> answer is " + isCyclic + "\n\n";


            ///***************************************************************************************/


            //CyclicGraph = new UndirectedSparseGraph<string>();

            //V = new string[] { "A", "B", "C", "D", "E" };
            //result = result + "Vertices: " + "'A', 'B', 'C', 'D', 'E'" + "\n";

            //// Insert new values of V
            //CyclicGraph.AddVertices(V);

            //// Insert new value for edges
            //CyclicGraph.AddEdge("A", "C");
            //CyclicGraph.AddEdge("B", "A");
            //CyclicGraph.AddEdge("B", "C");
            //CyclicGraph.AddEdge("C", "E");
            //CyclicGraph.AddEdge("C", "D");
            //CyclicGraph.AddEdge("D", "B");
            //CyclicGraph.AddEdge("E", "D");

            //isCyclic = CyclesDetector.IsCyclic<string>(CyclicGraph);

            //// PRINT THE GRAPH
            //result = result + "[*] Undirected Graph:";
            //result = result + CyclicGraph.ToReadable() + "\r\n";
            //result = result + "Is un-directed graph above cyclic? " + "=> answer is " + isCyclic;

            //Console.WriteLine("\r\n*********************************************\r\n");


            ///***************************************************************************************/


            DAG = new DirectedSparseGraph <string>();

            V      = new string[] { "A", "B", "C", "D", "E", "X" };
            result = result + "Vertices: " + "'A', 'B', 'C', 'D', 'E', 'X'" + "\n";

            // Insert new values of V
            DAG.AddVertices(V);

            // Insert new value for edges
            DAG.AddEdge("A", "B");
            DAG.AddEdge("A", "X");
            DAG.AddEdge("B", "C");
            DAG.AddEdge("C", "D");
            DAG.AddEdge("D", "E");
            DAG.AddEdge("E", "X");

            isCyclic = CyclesDetector.IsCyclic <string>(DAG);
            //Debug.Assert(isCyclic == false, "Wrong status! The graph has no cycles.");

            // PRINT THE GRAPH
            result = result + "[*] DAG(Directed Asyclic Graph): ";
            result = result + DAG.ToReadable() + "\r\n";
            result = result + "Is directed graph above cyclic? " + "=> answer is " + isCyclic;


            HtmlString html = StringHelper.GetHtmlString(result);

            return(View(html));
        }
Exemple #6
0
        public static void DoTest()
        {
            string[] V;
            DirectedSparseGraph <string>   DAG;
            UndirectedSparseGraph <string> CyclicGraph;
            DirectedSparseGraph <string>   DigraphWithCycles;

            // Init graph object
            DigraphWithCycles = new DirectedSparseGraph <string>();

            // Init V
            V = new string[6] {
                "r", "s", "t", "x", "y", "z"
            };

            // Insert V
            DigraphWithCycles.AddVertices(V);

            // Insert E
            DigraphWithCycles.AddEdge("r", "s");
            DigraphWithCycles.AddEdge("r", "t");
            DigraphWithCycles.AddEdge("s", "t");
            DigraphWithCycles.AddEdge("s", "x");
            DigraphWithCycles.AddEdge("t", "x");
            DigraphWithCycles.AddEdge("t", "y");
            DigraphWithCycles.AddEdge("t", "z");
            DigraphWithCycles.AddEdge("x", "y");
            DigraphWithCycles.AddEdge("x", "z");
            DigraphWithCycles.AddEdge("y", "z");
            DigraphWithCycles.AddEdge("z", "r");
            DigraphWithCycles.AddEdge("z", "s");

            var isCyclic = CyclesDetector.IsCyclic <string>(DigraphWithCycles);

            Assert.True(isCyclic == true, "Wrong status! The graph has cycles.");

            CyclicGraph = new UndirectedSparseGraph <string>();

            V = new string[] { "A", "B", "C", "D", "E" };

            // Insert new values of V
            CyclicGraph.AddVertices(V);

            // Insert new value for edges
            CyclicGraph.AddEdge("A", "C");
            CyclicGraph.AddEdge("B", "A");
            CyclicGraph.AddEdge("B", "C");
            CyclicGraph.AddEdge("C", "E");
            CyclicGraph.AddEdge("C", "D");
            CyclicGraph.AddEdge("D", "B");
            CyclicGraph.AddEdge("E", "D");

            isCyclic = CyclesDetector.IsCyclic <string>(CyclicGraph);
            Assert.True(isCyclic == true, "Wrong status! The graph has cycles.");

            DAG = new DirectedSparseGraph <string>();

            V = new string[] { "A", "B", "C", "D", "E", "X" };

            // Insert new values of V
            DAG.AddVertices(V);

            // Insert new value for edges
            DAG.AddEdge("A", "B");
            DAG.AddEdge("A", "X");
            DAG.AddEdge("B", "C");
            DAG.AddEdge("C", "D");
            DAG.AddEdge("D", "E");
            DAG.AddEdge("E", "X");

            isCyclic = CyclesDetector.IsCyclic <string>(DAG);
            Assert.True(isCyclic == false, "Wrong status! The graph has no cycles.");
        }
        public static void DoTest()
        {
            // Init graph object
            var digraphWithCycles = new DirectedSparseGraph <string>();

            // Init V
            var v = new string[6] {
                "r", "s", "t", "x", "y", "z"
            };

            // Insert V
            digraphWithCycles.AddVertices(v);

            // Insert E
            digraphWithCycles.AddEdge("r", "s");
            digraphWithCycles.AddEdge("r", "t");
            digraphWithCycles.AddEdge("s", "t");
            digraphWithCycles.AddEdge("s", "x");
            digraphWithCycles.AddEdge("t", "x");
            digraphWithCycles.AddEdge("t", "y");
            digraphWithCycles.AddEdge("t", "z");
            digraphWithCycles.AddEdge("x", "y");
            digraphWithCycles.AddEdge("x", "z");
            digraphWithCycles.AddEdge("y", "z");
            digraphWithCycles.AddEdge("z", "r");
            digraphWithCycles.AddEdge("z", "s");

            var isCyclic = CyclesDetector.IsCyclic <string>(digraphWithCycles);

            Assert.True(isCyclic == true, "Wrong status! The graph has cycles.");

            var cyclicGraph = new UndirectedSparseGraph <string>();

            v = new string[] { "A", "B", "C", "D", "E" };

            // Insert new values of V
            cyclicGraph.AddVertices(v);

            // Insert new value for edges
            cyclicGraph.AddEdge("A", "C");
            cyclicGraph.AddEdge("B", "A");
            cyclicGraph.AddEdge("B", "C");
            cyclicGraph.AddEdge("C", "E");
            cyclicGraph.AddEdge("C", "D");
            cyclicGraph.AddEdge("D", "B");
            cyclicGraph.AddEdge("E", "D");

            isCyclic = CyclesDetector.IsCyclic <string>(cyclicGraph);
            Assert.True(isCyclic == true, "Wrong status! The graph has cycles.");

            var dag = new DirectedSparseGraph <string>();

            v = new string[] { "A", "B", "C", "D", "E", "X" };

            // Insert new values of V
            dag.AddVertices(v);

            // Insert new value for edges
            dag.AddEdge("A", "B");
            dag.AddEdge("A", "X");
            dag.AddEdge("B", "C");
            dag.AddEdge("C", "D");
            dag.AddEdge("D", "E");
            dag.AddEdge("E", "X");

            isCyclic = CyclesDetector.IsCyclic <string>(dag);
            Assert.True(isCyclic == false, "Wrong status! The graph has no cycles.");
        }