求有向图中环的算法
- import java.util.ArrayList;
- import java.util.Arrays;
- public class TestCycle {
- private int n;
- private int[] visited;//节点状态,值为0的是未访问的
- private int[][] e;//邻接矩阵
- private ArrayList< Integer> trace=new ArrayList< Integer>();//从出发节点到当前节点的轨迹
- private boolean hasCycle=false;
- public TestCycle(int n,int[][] e){
- this.n=n;
- visited=new int[n];
- Arrays.fill(visited,0);
- this.e=e;
- }
-
- void findCycle(int v) //递归DFS
- {
- if(visited[v]==1)
- {
- int j;
- if((j=trace.indexOf(v))!=-1)
- {
- hasCycle=true;
- System.out.print("Cycle:");
- while(j< trace.size())
- {
- System.out.print(trace.get(j)+" ");
- j++;
- }
- System.out.print("
- ");
- return;
- }
- return;
- }
- visited[v]=1;
- trace.add(v);
-
- for(int i=0;i< n;i++)
- {
- if(e[v][i]==1)
- findCycle(i);
- }
- trace.remove(trace.size()-1);
- }
-
- public boolean getHasCycle(){
- return hasCycle;
- }
- public static void main(String[] args) {
- int n=7;
- int[][] e={
- {0,1,1,0,0,0,0},
- {0,0,0,1,0,0,0},
- {0,0,0,0,0,1,0},
- {0,0,0,0,1,0,0},
- {0,0,1,0,0,0,0},
- {0,0,0,0,1,0,1},
- {1,0,1,0,0,0,0}};//邻接矩阵,值大家任意改.
- TestCycle tc=new TestCycle(n,e);
- tc.findCycle(1);
- if(!tc.hasCycle)
- System.out.println("No Cycle.");
- }
- }
复制代码 D:java>java TestCycle
Cycle:4 2 5
Cycle:1 3 4 2 5 6 0
Cycle:2 5 6 0
Cycle:2 5 6
源码下载:http://file.javaxxz.com/2014/12/6/000723156.zip |