测试图中是否有环
一个图中是否有环跟利用深度优先搜索中是否出现回边是同一个问题
//determine if the graph is acyclicpublic static<T> boolean acyclic(DiGraph<T>g){//use for calls to dfsVisit()LinkedList<T> dfsList=new LinkedList();Iterator<T> graphIter=null;T vertex=null;//initialize all vertices to WHITEg.colorWhite();try{graphIter=g.vertexSet().iterator();while(graphIter.hasNext()){vertex=graphIter.next();if(g.getColor(vertex)==VertexColor.WHITE){dfsVisit(g,vertex,dfsList,true);}}}catch(IllegalPathStateException e){return false;}return true;}