public void Test_ReversePost()
        {
            DepthFirstOrder order = new DepthFirstOrder(this.CreateDigraph());

            AssertUtilities.Sequence(new Int32[6] {
                0, 1, 2, 3, 4, 5
            }, order.ReversePost());
        }
Esempio n. 2
0
        private bool[] marked; // reached vertices

        #endregion Fields

        #region Constructors

        public KosarajuSCC(DiGraph G)
        {
            marked = new bool[G.V];
            id = new int[G.V];
            DepthFirstOrder order = new DepthFirstOrder(G.reverse());
            foreach (int s in order.ReversePost())
                if (!marked[s])
                { dfs(G, s); count++; }
        }
    public Topological(DirectedGraph g)
    {
        DirectedCycle cycleFinder = new DirectedCycle(g);

        if (!cycleFinder.HasCycle())
        {
            DepthFirstOrder dfs = new DepthFirstOrder(g);
            m_order = dfs.ReversePost();
        }
    }
            public Topological(DirectedGraph graph)
            {
                var cycleFinder = new DirectedCycle(graph);

                if (!cycleFinder.HasCycle())
                {
                    var depthFirstOrder = new DepthFirstOrder(graph);
                    order = depthFirstOrder.ReversePost();
                }
            }
Esempio n. 5
0
        private int count;     // number of strong components
        public KosarajuSCC(DiGraph G)
        {
            marked = new bool[G.V];
            id     = new int[G.V];
            DepthFirstOrder order = new DepthFirstOrder(G.reverse());

            foreach (int s in order.ReversePost())
            {
                if (!marked[s])
                {
                    dfs(G, s); count++;
                }
            }
        }
Esempio n. 6
0
        public KosarajuSharirCC(DirectedGraph graph)
        {
            marked = new bool[graph.V];
            _id    = new int[graph.V];
            DepthFirstOrder dfo = new DepthFirstOrder(graph.Reverse());

            foreach (int v in dfo.ReversePost())
            {
                if (!marked[v])
                {
                    dfs(graph, v);
                    Count++;
                }
            }
        }
Esempio n. 7
0
    public KosarajuSCC(DirectedGraph g)
    {
        m_marked = new bool[g.V()];
        m_id     = new int[g.V()];
        DepthFirstOrder order = new DepthFirstOrder(g.Reverses());

        foreach (var s in order.ReversePost())
        {
            if (!m_marked[s])
            {
                Dfs(g, s);
                m_count++;
            }
        }
    }
Esempio n. 8
0
        public KosarajuStrongConnectedComponents(Digraph g)
        {
            marked = new bool[g.Vcount];
            id     = new int[g.Vcount];
            var order = new DepthFirstOrder(g.Reverse());

            foreach (var i in order.ReversePost())
            {
                if (!marked[i])
                {
                    Dfs(g, i);
                    count++;
                }
            }
        }
Esempio n. 9
0
        public KosarajuSCC(Digraph digraph)
        {
            marked = new bool[digraph.Vertices];
            id     = new int[digraph.Vertices];

            var order = new DepthFirstOrder(digraph.Reverse());

            foreach (var s in order.ReversePost())
            {
                if (!marked[s])
                {
                    Dfs(digraph, s);
                    count++;
                }
            }
        }
Esempio n. 10
0
        public KosarajuSCC(Digraph G)
            : base(G)
        {
            _marked = new bool[G.V()];
            _id     = new int[G.V()];
            DepthFirstOrder order = new DepthFirstOrder(G.Reverse());

            foreach (var v in order.ReversePost())
            {
                if (!_marked[v])
                {
                    Dfs(G, v);
                    _count++;
                }
            }
        }
Esempio n. 11
0
        private readonly bool[] _marked; // marked[v] = has vertex v been visited?

        #endregion Fields

        #region Constructors

        /// <summary>
        /// Computes the strong components of the digraph <tt>G</tt>.
        /// </summary>
        /// <param name="g">g the digraph</param>
        public KosarajuSharirSCC(Digraph g)
        {
            // compute reverse postorder of reverse graph
            var dfs = new DepthFirstOrder(g.Reverse());

            // run DFS on G, using reverse postorder to guide calculation
            _marked = new bool[g.V];
            _id = new int[g.V];
            foreach (int v in dfs.ReversePost())
            {
                if (!_marked[v])
                {
                    Dfs(g, v);
                    _count++;
                }
            }

            // check that id[] gives strong components
            //assert check(G);
        }
Esempio n. 12
0
        private readonly int _count;         // number of strongly-connected components

        /// <summary>
        /// Computes the strong components of the digraph <tt>G</tt>.
        /// </summary>
        /// <param name="g">g the digraph</param>
        public KosarajuSharirSCC(Digraph g)
        {
            // compute reverse postorder of reverse graph
            var dfs = new DepthFirstOrder(g.Reverse());

            // run DFS on G, using reverse postorder to guide calculation
            _marked = new bool[g.V];
            _id     = new int[g.V];
            foreach (int v in dfs.ReversePost())
            {
                if (!_marked[v])
                {
                    Dfs(g, v);
                    _count++;
                }
            }

            // check that id[] gives strong components
            //assert check(G);
        }
Esempio n. 13
0
        public void Run()
        {
            Console.WriteLine("Choose file:");    // Prompt
            Console.WriteLine("1 - tinyDAG.txt"); // Prompt
            Console.WriteLine("or quit");         // Prompt

            var    fileNumber = Console.ReadLine();
            string fileName;

            switch (fileNumber)
            {
            case "1":
                fileName = "tinyDAG.txt";
                break;

            case "quit":
                return;

            default:
                return;
            }


            var @in   = new In($"Files\\Graphs\\{fileName}");
            var lines = @in.ReadAllLines();

            var lineIterator = 0;
            var v            = 0;
            var e            = 0;
            var edges        = new List <EdgeD>();

            foreach (var line in lines)
            {
                if (lineIterator == 0)
                {
                    v = Convert.ToInt32(line);
                }
                if (lineIterator == 1)
                {
                    e = Convert.ToInt32(line);
                }
                if (lineIterator > 1)
                {
                    var lineSplitted = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    var ve           = Convert.ToInt32(lineSplitted[0]);
                    var we           = Convert.ToInt32(lineSplitted[1]);
                    var edge         = new EdgeD(ve, we);
                    edges.Add(edge);
                }

                lineIterator++;
            }

            var digraph = new Digraph(v, e, edges);

            Console.WriteLine(digraph);

            var dfs = new DepthFirstOrder(digraph);

            Console.WriteLine("   v  pre post");
            Console.WriteLine("--------------");
            for (var vi = 0; vi < digraph.V; vi++)
            {
                Console.Write($"{vi} {dfs.Pre(vi)} {dfs.Post(vi)}{Environment.NewLine}");
            }

            Console.Write("Preorder:  ");
            foreach (int vi in dfs.Pre())
            {
                Console.Write($"{vi} ");
            }
            Console.WriteLine();

            Console.Write("Postorder: ");
            foreach (int vi in dfs.Post())
            {
                Console.Write($"{vi} ");
            }
            Console.WriteLine();

            Console.Write("Reverse postorder: ");
            foreach (int vi in dfs.ReversePost())
            {
                Console.Write($"{vi} ");
            }
            Console.ReadLine();
        }