简单来说Tree is a non cyclic data structure,当有循环时,它变成Graph。
以上是图表的示例。您可以使用邻接列表或邻接矩阵来表示它。 Graphs的基础知识可以参考http://krishnalearnings.blogspot.in/2015/11/basics-of-graph-in-computer-science.html。
在上图中,我们可以将其表示如下(在您的问题中,您使用了邻接列表的表示形式)。
int[][] graph = { {0,1,0,0,0,0},
{0,0,1,0,0,0},
{0,0,0,1,1,0},
{0,0,0,0,0,0},
{0,0,0,0,0,1},
{0,1,0,0,0,0},
};
1 表示从对应的行编号顶点到列编号顶点的边。
我们可以编写一个简单的方法来检测循环的存在,并在图中打印任何一个循环。
static int[] cycleElements;
static int cycleElementIndex = 0;
static boolean cycleFound = false;
static final int NEW = 0;
static final int PUSHED = 1;
static final int POPPED = 2;
public static int findCycle(int[][] graph,int N, int u, int[] states){
for(int v = 0; v < N; v++){
if(graph[u][v] == 1){
if(states[v] == PUSHED){
// cycle found
cycleFound = true;
return v;
}else if(states[v] == NEW){
states[v] = PUSHED;
int poppedVertex = findCycle(graph, N, v, states);
states[v] = POPPED;
if(cycleFound){
if(poppedVertex == u){
cycleElements[cycleElementIndex++] = v;
cycleElements[cycleElementIndex++] = u;
cycleFound = false;
}else{
cycleElements[cycleElementIndex++] = v;
return poppedVertex;
}
}
}
}
}
return -1;
}
public static void main(String[] args) {
int N = 6;
int[][] graph = { {0,1,0,0,0,0},
{0,0,1,0,0,0},
{0,0,0,1,1,0},
{0,0,0,0,0,0},
{0,0,0,0,0,1},
{0,1,0,0,0,0},
};
cycleElements = new int[N];
int[] states = new int[N];
states[0] = PUSHED;
findCycle(graph,N,0,states);
for(int i = 0; i < cycleElementIndex; i++){
System.out.println(cycleElements[i]);
}
}
如果您愿意,您可以轻松地将上述方法转换为邻接列表,尽管表示并不重要(与邻接矩阵相比,只有邻接列表可以节省您的空间)