Java 深度优先搜索:检查电路
我有这个代码,我正在树中寻找一个电路。我想不出怎么做。我想知道我的实现是否很差。传递变量是非常混乱的。如何检查此输入是否包含电路Java 深度优先搜索:检查电路,java,matrix,graph,depth-first-search,Java,Matrix,Graph,Depth First Search,我有这个代码,我正在树中寻找一个电路。我想不出怎么做。我想知道我的实现是否很差。传递变量是非常混乱的。如何检查此输入是否包含电路 package circuitfinder; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; /** * * @author */ class TheStack { private final int HEIGHT = 20; priva
package circuitfinder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
/**
*
* @author
*/
class TheStack
{
private final int HEIGHT = 20;
private int[] st;
private int top;
public TheStack()
{
st = new int[HEIGHT];
top = -1;
}
public void push(int j)
{ st[++top] = j; }
public int pop()
{ return st[top--]; }
public int peek()
{ return st[top]; }
public boolean isEmpty()
{ return (top == -1); }
}
class Nodes
{
public char label;
public boolean wasVisited;
public Nodes(char lab)
{
label = lab;
wasVisited = false;
}
}
class TheGraph
{
private final int MAX_VERTS = 20;
private Nodes[] nodeList;
private int[][] adjacencym;
private int numberOfVertices;
private TheStack theStack;
public boolean circuit;
ArrayList<Integer> searcher = new ArrayList<Integer>();
public TheGraph()
{
circuit = false;
nodeList = new Nodes[MAX_VERTS];
adjacencym = new int[MAX_VERTS][MAX_VERTS];
numberOfVertices = 0;
for(int y=0; y<MAX_VERTS; y++)
for(int x=0; x<MAX_VERTS; x++)
adjacencym[x][y] = 0;
theStack = new TheStack();
} // end constructor
public void addNode(char lab)
{
nodeList[numberOfVertices++] = new Nodes(lab);
}
public void addEdge(int start, int end)
{
adjacencym[start][end] = 1;
adjacencym[end][start] = 1;
}
public void dephthSearch()
{
nodeList[0].wasVisited = true;
theStack.push(0);
while( !theStack.isEmpty() )
{
int v = getAdjUnvisitedVertex( theStack.peek());
if(v == -1){
theStack.pop();
}
else
{
nodeList[v].wasVisited = true;
theStack.push(v);
}
}
for(int j=0; j<numberOfVertices; j++)
nodeList[j].wasVisited = false;
}
public int getAdjUnvisitedVertex(int v)
{
for(int j=0; j<numberOfVertices; j++){
if(adjacencym[v][j]==0 && nodeList[j].wasVisited==false){
return j;
}}
return -1;
}
}
class CircuitFinder
{
public static void main(String[] args)
{
TheGraph graphs = new TheGraph();
graphs.addNode('1');
graphs.addNode('2');
graphs.addNode('3');
graphs.addNode('4');
graphs.addEdge(0, 1);
graphs.addEdge(1, 2);
graphs.addEdge(0, 3);
graphs.addEdge(3, 4);
graphs.addEdge(4, 0);
graphs.dephthSearch();
System.out.println("Graph has Circuit: "+graphs.circuit);
}
}
封装电路查找器;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.Map;
/**
*
*@作者
*/
上等货
{
私人最终内部高度=20;
私人互联网【】街;
私人int top;
公共债券
{
st=新的整数[高度];
top=-1;
}
公共无效推送(int j)
{st[++top]=j;}
公共int-pop()
{return st[top-->;}
公共int peek()
{返回st[top];}
公共布尔值为空()
{return(top==-1);}
}
类节点
{
公共字符标签;
公众参观;
公共节点(字符实验室)
{
标签=实验室;
wasviest=false;
}
}
类图
{
私人最终整数最大值=20;
私有节点[]节点列表;
私有int[][]邻接ym;
私有整数顶点;
私家侦探侦探侦探侦探侦探侦探侦探侦探侦探侦探侦探侦探侦探侦探侦探侦探侦探侦探侦探侦探侦探侦探;
公共布尔电路;
ArrayList searcher=新的ArrayList();
公共图形()
{
电路=假;
节点列表=新节点[最大顶点];
adjacencym=新整数[MAX_VERTS][MAX_VERTS];
顶点数=0;
对于(int y=0;y,您可以保留一个包含已访问节点的布尔数组。当您在depthSearch中处理节点并遇到已访问的节点时,您将找到一个循环。
或者,您可以在nodes类中保留一个标志以实现相同的效果。
基本的想法是,如果你遇到一个已经访问过的节点,你就处在一个循环中,因为你遇到了一个后缘。
这个链接会让它更清晰。这是一个图形,但要记住,图形和树之间的主要区别是前者可以包含循环,你就能够找到它。Y你没有在你的代码中操纵电路,而这正是你在main方法中寻找的。所以我认为:
<java>
public void dephthSearch()
{
nodeList[0].wasVisited = true;
theStack.push(0);
while( !theStack.isEmpty() )
{
int v = getAdjUnvisitedVertex( theStack.peek());
if(nodeList[v].wasVisited) { //If node is visited again you set circuit = true
circuit = true; // Since finding cycle is your job,you can break from this while loop as soon as you hit circuit = true.
break;
}
if(v == -1){
theStack.pop();
}
else
{
nodeList[v].wasVisited = true;
theStack.push(v);
}
}
for(int j=0; j<numberOfVertices; j++)
nodeList[j].wasVisited = false;
}
}</java>
公共无效深度搜索()
{
节点列表[0]。wasvisted=true;
钉压(0);
而(!theStack.isEmpty())
{
int v=getAdjUnvisitedVertex(theStack.peek());
如果(nodeList[v].wasviest){//如果再次访问节点,则设置circuit=true
circuit=true;//因为查找循环是您的工作,所以只要点击circuit=true,您就可以中断此while循环。
打破
}
如果(v==-1){
theStack.pop();
}
其他的
{
节点列表[v].wasvisted=true;
钉压(v);
}
}
对于(int j=0;jj),您面临的问题是什么?请更具体一些。
<java>for(int j=0; j<numberOfVertices; j++){
if(adjacencym[v][j]==0 && nodeList[j].wasVisited==false){
return j;
}</java>
to
<java>
for(int j=0; j<numberOfVertices; j++){
if(adjacencym[v][j]==1){ //If there exists an edge you return j.The later half is removed because if nodeList[j] was true then it means there exists a cycle which you check in depthSearch();
return j;
}</java>