Tag: 递归有

使用递归回溯在有向图中查找所有周期

我正在使用递归回溯来查找有向图中的循环。 这里有一个建议的伪代码, 在这里: dfs(adj,node,visited): if (visited[node]): if (node == start): “found a path” return; visited[node]=YES; for child in adj[node]: dfs(adj,child,visited) visited[node]=NO; 使用start节点调用上面的函数: visited = {} dfs(adj,start,visited) 虽然与Tarjans algorithm相比,这不是最有效的算法,但这对我来说很容易理解。 目前,此代码没有检测到的数字周期数。 我用Java实现了这个: //this is the main method that calls the helper DFS which runs on each node public int allCyclesDirectedmain(){ //this initializes all vertices clearAll(); int[] count […]