Beispiel #1
0
        private static void FindMotherVertexInGraph()
        {
            var graph = new Digraph();

            graph.AddEdge(0, 1);
            graph.AddEdge(0, 2);
            graph.AddEdge(1, 3);
            graph.AddEdge(4, 1);
            graph.AddEdge(6, 4);
            graph.AddEdge(5, 6);
            graph.AddEdge(5, 2);
            graph.AddEdge(6, 0);

            Console.WriteLine(graph.FindMother());
        }
Beispiel #2
0
        /**
         * Returns the reverse of the digraph.
         *
         * @return the reverse of the digraph
         */
        public Digraph <T> Reverse()
        {
            Digraph <T> reverse = new Digraph <T>();

            //foreach (T v in Vertices)
            //{
            //    _ = v.Clone();
            //}
            foreach (T v in Vertices)
            {
                foreach (T s in Successors(v))
                {
                    reverse.AddEdge(new DirectedEdge <T>(s, v));
                }
            }
            return(reverse);
        }
Beispiel #3
0
        private static void DetecteCycleInDirectedGraph()
        {
            var graph = new Digraph();

            graph.AddEdge(0, 1);
            graph.AddEdge(0, 2);
            graph.AddEdge(1, 2);
            graph.AddEdge(2, 0);
            graph.AddEdge(2, 3);
            graph.AddEdge(3, 3);

            Console.WriteLine(graph.IsCyclic());
        }
Beispiel #4
0
        private static void GraphFindAllPaths()
        {
            var graph = new Digraph();

            graph.AddEdge(0, 1);
            graph.AddEdge(0, 2);
            graph.AddEdge(0, 3);
            graph.AddEdge(2, 0);
            graph.AddEdge(2, 1);
            graph.AddEdge(1, 3);

            var results = graph.FindAllPaths(2, 3);

            foreach (var path in results)
            {
                Console.WriteLine(string.Join(",", path));
            }
        }
Beispiel #5
0
        private static void TestGraph()
        {
            var graph = new Digraph();

            /*graph.AddEdge(5, 2);
             * graph.AddEdge(5, 0);
             * graph.AddEdge(4, 0);
             * graph.AddEdge(4, 1);
             * graph.AddEdge(2, 3);
             * graph.AddEdge(3, 1);
             *
             * var stack = graph.TopologicalSort();
             *
             * while (stack.Count > 0)
             * {
             *  Console.Write(stack.Pop() + " ");
             * }*/

            graph.AddEdge(0, 1);
            graph.AddEdge(0, 2);
            graph.AddEdge(1, 2);
            graph.AddEdge(2, 0);
            graph.AddEdge(2, 3);
            graph.AddEdge(3, 3);

            var result = graph.TransitiveClosure();

            for (var i = 0; i < result.GetLength(0); i++)
            {
                for (var j = 0; j < result.GetLength(1); j++)
                {
                    Console.Write(result[i, j] + " ");
                }
                Console.WriteLine();
            }
        }