private void dfs(DiGraph g, int begin) { isMarked[begin] = true; inStack[begin] = true; for (int node = 0; node < g.getAdj(begin).Count; node++) { // if (hasCycle()) return; int nodeValue = g.getAdj(begin)[node]; if (!isMarked[nodeValue]) { edgeTo[nodeValue] = begin; dfs(g, nodeValue); } else if (inStack[nodeValue]) { // 如果当前路径Stack中含有node,又再次访问的话,说明有环 // 将环保存下来 Stack <int> cycle = new Stack <int>(); for (int i = begin; i != nodeValue; i = edgeTo[i]) { cycle.Push(i); } cycle.Push(nodeValue); //cycle.Push(begin); cycleList2.Add(cycle); } } inStack[begin] = false; }
public DirectedDFS(DiGraph g, int begin) { // 从begin节点开始,进行深搜 reversePost = new List <int>(); isMarked = new bool[g.getVetx()]; edgeTo = new int[g.getVetx()]; this.begin = new List <int>(); this.begin.Add(begin); dfs(g, begin); }
public DiGraph reverse() { DiGraph g = new DiGraph(Vetx); for (int i = 0; i < Vetx; i++) { for (int j = 0; j < adj[i].Count; j++) { g.addEdge(adj[i][j], i); } } return(g); }
public void dfs(DiGraph g, int begin) { // 深搜将所有节点遍历,标记被访问过的节点 isMarked[begin] = true; for (int node = 0; node < g.getAdj(begin).Count; node++) { int nodeValue = g.getAdj(begin)[node]; if (!isMarked[nodeValue]) { edgeTo[nodeValue] = begin; dfs(g, nodeValue); } } reversePost.Add(begin); }
public DirectedDFS(DiGraph g, List <int> begins) { // 找出一堆begin中所有可达的地方 reversePost = new List <int>(); isMarked = new bool[g.getVetx()]; edgeTo = new int[g.getVetx()]; this.begin = begins; for (int i = 0; i < begin.Count; i++) { if (!isMarked[i]) { dfs(g, begin[i]); } } }
public DirectedCycle(DiGraph g) { inStack = new bool[g.getVetx()]; edgeTo = new int[g.getVetx()]; cycleList = new List <Stack <Vertex> >(); cycleList2 = new List <Stack <int> >(); isMarked = new bool[g.getVetx()]; for (int i = 0; i < g.getVetx(); i++) { if (!isMarked[i]) { dfs(g, i); } } }
private List <int> reversePost; // 拓扑排序顺序 public DirectedDFS(DiGraph g) { // 所有节点遍历 reversePost = new List <int>(); isMarked = new bool[g.getVetx()]; edgeTo = new int[g.getVetx()]; List <int> begins = new List <int>(); for (int i = 0; i < g.getVetx(); i++) { begins.Add(i); } this.begin = begins; for (int i = 0; i < g.getVetx(); i++) { if (!isMarked[i]) { dfs(g, i); } } }