Beispiel #1
0
 public AllPaths(DiGraph G, int s, int d)
 {
     this.S = s;
     marked = new bool[G.Vnums()];   //instack = new bool[G.Vnums()];
     stack  = new Stack <int>(); path = new List <ArrayList>();
     dfs(G, s, d);
 }
Beispiel #2
0
 private void dfs(DiGraph G, int v, int d)
 {
     if (!marked[v])
     {
         NodeList <int> w = G.adj[v];
         stack.Push(w.val);  //possible error??
         marked[w.val] = true;
     }
     foreach (int node in G.adj[v])
     {
         if (!marked[node])
         {
             stack.Push(node);
             marked[node] = true;
             if (node == d)
             {
                 path.Add(new ArrayList());
                 foreach (int i in stack)
                 {
                     path[path.Count - 1].Add(i);
                 }
                 marked[stack.Pop()] = false;
                 continue;
             }
             dfs(G, node, d);
         }
     }
     marked[stack.Pop()] = false;
 }
Beispiel #3
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;
     }
 }
Beispiel #4
0
        private IEnumerable <int> order; //顶点的拓扑顺序
        public Topological(DiGraph G)
        {
            DirectedCycle cyclefind = new DirectedCycle(G);

            if (!cyclefind.hasCycle())
            {
                //这里是核心,使用了DFOrder 类 中的 逆后序方法
                //一幅 有向无环图 的拓扑顺序 即为所有顶点的 逆后序排列
                DFOrder dfs = new DFOrder(G);
                order = dfs.ReversePost();
            }
        }
Beispiel #5
0
 public DirectedDFS(DiGraph G, NodeList <int> sources)  //链表sources 中的点 可达的所有点
 {
     marked = new bool[G.Vnums()];
     while (sources != null)
     {
         if (!marked[sources.val])
         {
             dfs(G, sources.val);
         }
         sources = sources.next;
     }
 }
Beispiel #6
0
        private bool[] onStack;    //递归调用的栈 上的所有点

        public DirectedCycle(DiGraph G)
        {
            marked  = new bool[G.Vnums()];
            edgeTo  = new int[G.Vnums()];
            onStack = new bool[G.Vnums()];
            for (int v = 0; v < G.Vnums(); v++)
            {
                if (!marked[v])
                {
                    dfs(G, v);
                }
            }
        }
Beispiel #7
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);
     }
 }
Beispiel #8
0
 private Stack <int> reversePost; //逆后序:.后,将顶点压入栈
 public DFOrder(DiGraph G)
 {
     pre         = new Queue <int>();
     post        = new Queue <int>();
     reversePost = new Stack <int>();
     marked      = new bool[G.Vnums()];
     for (int v = 0; v < G.Vnums(); v++)
     {
         if (!marked[v])
         {
             dfs(G, v);
         }
     }
 }
Beispiel #9
0
        private void dfs(DiGraph G, int v)
        {
            marked[v] = true;
            NodeList <int> w = G.adj[v];

            while (w != null)
            {
                if (!marked[w.val])
                {
                    marked[w.val] = true;
                    dfs(G, w.val);
                }
            }
        }
Beispiel #10
0
        public DiGraph reverse()   //反转,为了得到入边集合
        {
            DiGraph R = new DiGraph(V);

            for (int v = 0; v < V; v++)
            {
                NodeList <int> w = adj[v];
                while (w != null)
                {
                    R.addEdge(w.val, v);
                    w = w.next;
                }
            }
            return(R);
        }
Beispiel #11
0
 public DirectedDFS(DiGraph G, int s)
 {
     marked = new bool[G.Vnums()];
     dfs(G, s);
 }