예제 #1
0
 public DirectedDFS(DiGraph G, IEnumerable<int> sources)
 {
     marked = new bool[G.V];
     foreach (int s in sources)
         if (!marked[s])
             dfs(G, s);
 }
예제 #2
0
 private void dfs(DiGraph G, int v)
 {
     marked[v] = true;
     foreach (int w in G.Adj(v))
         if (!marked[w])
             dfs(G, w);
 }
예제 #3
0
        static void Main(string[] args)
        {
            DiGraph dg = new DiGraph(5);

            dg.addEdge(0, 1);
            dg.addEdge(1, 2);
            dg.addEdge(1, 3);
            dg.addEdge(2, 4);
            //dg.addEdge(4, 0); //Comment it for topological
            dg.addEdge(4, 3);

            Console.WriteLine(dg);

            dg.ShowMarked();

            dg.ShowCycle();

            dg.ShowTopological();

            Console.WriteLine("{0}-{1} is strongly connected : {2}", 0, 1, dg.isStonglyConnected(0, 1));

            Console.WriteLine("{0}-{1} is reachable : {2}", 0, 3, dg.Reachable(0, 3));

            Console.ReadKey();
        }
예제 #4
0
 public DirectedDFS(DiGraph G, int s)
 {
     if (s < 0 || s > G.V-1)
         s = G.V-1;
     marked = new bool[G.V];
     dfs(G, s);
 }
예제 #5
0
        static void Main(string[] args)
        {
            DiGraph dg=new DiGraph(5);

            dg.addEdge(0, 1);
            dg.addEdge(1, 2);
            dg.addEdge(1, 3);
            dg.addEdge(2, 4);
            //dg.addEdge(4, 0); //Comment it for topological
            dg.addEdge(4, 3);

            Console.WriteLine(dg);

            dg.ShowMarked();

            dg.ShowCycle();

            dg.ShowTopological();

            Console.WriteLine("{0}-{1} is strongly connected : {2}", 0, 1, dg.isStonglyConnected(0, 1));

            Console.WriteLine("{0}-{1} is reachable : {2}", 0, 3, dg.Reachable(0, 3));

            Console.ReadKey();
        }
예제 #6
0
 public DiGraph reverse()
 {
     DiGraph R = new DiGraph(V);
     for (int v = 0; v < V; v++)
         foreach (int w in Adj(v))
             R.addEdge(w, v);
     return R;
 }
예제 #7
0
        private IEnumerable<int> order; // topological order

        #endregion Fields

        #region Constructors

        public Topological(DiGraph G)
        {
            DirectedCycle cyclefinder = new DirectedCycle(G);
            if (!cyclefinder.hasCycle())
            {
                DepthFirstOrder dfs = new DepthFirstOrder(G);
                order = dfs.ReversePost();
            }
        }
예제 #8
0
        private bool[] onStack; // vertices on recursive call stack

        #endregion Fields

        #region Constructors

        public DirectedCycle(DiGraph G)
        {
            onStack = new bool[G.V];
            edgeTo = new int[G.V];
            marked = new bool[G.V];
            for (int v = 0; v < G.V; v++)
                if (!marked[v])
                    dfs(G, v);
        }
예제 #9
0
 private void dfs(DiGraph G, int v)
 {
     pre.Enqueue(v);
     marked[v] = true;
     foreach (int w in G.Adj(v))
         if (!marked[w])
             dfs(G, w);
     post.Enqueue(v);
     reversePost.Push(v);
 }
예제 #10
0
        private Stack<int> reversePost; // vertices in reverse postorder

        #endregion Fields

        #region Constructors

        public DepthFirstOrder(DiGraph G)
        {
            pre = new Queue<int>();
            post = new Queue<int>();
            reversePost = new Stack<int>();
            marked = new bool[G.V];
            for (int v = 0; v < G.V; v++)
                if (!marked[v])
                    dfs(G, v);
        }
예제 #11
0
        private IEnumerable <int> order; // topological order

        public Topological(DiGraph G)
        {
            DirectedCycle cyclefinder = new DirectedCycle(G);

            if (!cyclefinder.hasCycle())
            {
                DepthFirstOrder dfs = new DepthFirstOrder(G);
                order = dfs.ReversePost();
            }
        }
예제 #12
0
        public static void ShowCycle(this DiGraph dg)
        {
            DirectedCycle Cycle = new DirectedCycle(dg);

            if (Cycle.hasCycle())
            {
                Console.Write("Directed Graph Cycled in : ");
                foreach (var e in Cycle.Cycle())
                {
                    Console.Write("{0} ", e);
                }
                Console.WriteLine();
            }
        }
예제 #13
0
        public static void ShowMarked(this DiGraph dg)
        {
            Console.Write("Marked: ");
            DirectedDFS reachable = new DirectedDFS(dg, dg.V);

            for (int v = 0; v < dg.V; v++)
            {
                if (reachable.Marked(v))
                {
                    Console.Write(v + " ");
                }
            }
            Console.WriteLine();
        }
예제 #14
0
        public static void ShowTopological(this DiGraph dg)
        {
            Topological top = new Topological(dg);

            if (top.isDAG())
            {
                return;
            }
            Console.Write("Topological: ");
            foreach (int v in top.Order)
            {
                Console.Write("{0} ", v);
            }
            Console.WriteLine();
        }
예제 #15
0
 private void dfs(DiGraph G, int v)
 {
     onStack[v] = true;
     marked[v] = true;
     foreach (int w in G.Adj(v))
         if (this.hasCycle())
             return;
         else if (!marked[w])
         {
             edgeTo[w] = v;
             dfs(G, w);
         }
         else if (onStack[w])
         {
             cycle = new Stack<int>();
             for (int x = v; x != w; x = edgeTo[x])
                 cycle.Push(x);
             cycle.Push(w);
             cycle.Push(v);
         }
     onStack[v] = false;
 }