# 有向图中打印所有的环路

说简单了，就是算法中的一个简单问题，在有向图中找到所有的环路。请教了宿舍的算法高手just，加上我自己的理解，产生了一些思路：

1.    DFS树，找所有后退边

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 import java.util.ArrayList; public class test {      static private final int POINT_NUM = 9;      static private int[] visited={0,0,0,0,0,0,0,0,0};      static private int[][] e={                                                 {0,0,0,0,0,0,0,0,1},                             {0,0,0,1,1,0,0,0,0},                             {1,0,0,0,0,0,0,0,0},                             {0,0,0,0,0,0,0,1,0},                             {0,0,0,0,0,0,0,1,0},                             {0,1,0,0,0,0,0,0,0},                             {1,0,0,0,0,0,0,0,0},                             {0,0,1,0,0,0,1,0,0},                             {0,0,0,0,0,1,0,0,0}};     static ArrayList trace=new ArrayList();     static boolean hasCycle=false;     public static void main(String[] args) {         findCycle(0);         if(!hasCycle)             System.out.println("No Cycle.");     }     static void findCycle(int v)     {         if(visited[v] == 1){             if((j=trace.indexOf(v))!=-1)             {                 hasCycle=true;                 System.out.print("Cycle:");                 while(j

2.    真的对吗？

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 import java.util.ArrayList; public class test {      static private final int POINT_NUM = 9;      static private int[] visited={0,0,0,0,0,0,0,0,0};      static private int[][] e={                                                 {0,0,0,0,0,0,0,0,1},                             {0,0,0,1,1,0,0,0,0},                             {1,0,0,0,0,0,0,0,0},                             {0,0,0,0,0,0,0,1,0},                             {0,0,0,0,0,0,0,1,0},                             {0,1,0,0,0,0,0,0,0},                             {1,0,0,0,0,0,0,0,0},                             {0,0,1,0,0,0,1,0,0},                             {0,0,0,0,0,1,0,0,0}};     static ArrayList trace=new ArrayList();     static boolean hasCycle=false;     public static void main(String[] args) {         findCycle(0);         if(!hasCycle)             System.out.println("No Cycle.");     }     static void findCycle(int v)     {        if(!trace.contains(v) && visited[v] == 1){            visited[v] = 0;        }        if(visited[v] == 1){          if((j=trace.indexOf(v))!=-1)          {                 hasCycle=true;                 System.out.print("Cycle:");                 while(j

3.    进一步探索

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 import java.util.ArrayList; public class test {      static private final int POINT_NUM = 9;      static private int[][] e={                                                 {0,0,0,0,0,0,0,0,1},                             {0,0,0,1,1,0,0,0,0},                             {1,0,0,0,0,0,0,0,0},                             {0,0,0,0,0,0,0,1,0},                             {0,0,0,0,0,0,0,1,0},                             {0,1,0,0,0,0,0,0,0},                             {1,0,0,0,0,0,0,0,0},                             {0,0,1,0,0,0,1,0,0},                             {0,0,0,0,0,1,0,0,0}};     static ArrayList trace=new ArrayList();     static boolean hasCycle=false;     public static void main(String[] args) {         findCycle(0);         if(!hasCycle)             System.out.println("No Cycle.");     }     static void findCycle(int v)     {                    if((j=trace.indexOf(v))!=-1)        {                 hasCycle=true;                 System.out.print("Cycle:");                 while(j