Example #1
0
        private Stack <int> _reversePost = new Stack <int>(); // vertices in reverse postorder

        public DepthFirstOrder(Digraph G)
        {
            _marked = new bool[G.V()];
            for (int v = 0; v < G.V(); v++)
            {
                if (!_marked[v])
                {
                    Dfs(G, v);
                }
            }
        }
Example #2
0
 public DirectedCycle(Digraph G)
 {
     _marked  = new bool[G.V()];
     _onStack = new bool[G.V()];
     _edgeTo  = new int[G.V()];
     for (int v = 0; v < G.V(); v++)
     {
         if (!_marked[v] && !HasCycle())
         {
             Dfs(G, v);
         }
     }
 }
Example #3
0
 public DirectedDFS(Digraph G, IEnumerable <int> sources)
 {
     _marked = new bool[G.V()];
     foreach (var s in sources)
     {
         if (!_marked[s])
         {
             Dfs(G, s);
         }
     }
 }
Example #4
0
        public void RunSearch(string path, IEnumerable <int> sources)
        {
            Digraph     G   = new Digraph(path);
            DirectedDFS dfs = new DirectedDFS(G, sources);

            for (int v = 0; v < G.V(); v++)
            {
                if (dfs.Marked(v))
                {
                    Console.Write("{0} ", v);
                }
            }
            Console.WriteLine();
        }
Example #5
0
        public void RunScc(string path)
        {
            Digraph G   = new Digraph(path);
            SCC     scc = new KosarajuSCC(G);

            Bag <int>[] components = new Bag <int> [scc.Count()];
            for (int i = 0; i < scc.Count(); i++)
            {
                components[i] = new Bag <int>();
            }
            for (int i = 0; i < G.V(); i++)
            {
                components[scc.Id(i)].Add(i);
            }
            for (int i = 0; i < scc.Count(); i++)
            {
                Console.WriteLine(string.Join(",", components[i]));
            }
        }