示例#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;
 }