【发布时间】:2019-05-15 06:11:15
【问题描述】:
我正在尝试打印我的“图形”程序的深度优先遍历。 DepthFirstTraversal(int v) 方法应该从第一个索引开始,应该是 A。但是,它似乎正在跳过这个。 有什么建议吗?
我尝试将值 v 从 1 更改为 0,但这只会在相同代码的顶部打印一个额外的 0。
import java.util.Arrays;
import java.util.Iterator;
public class Graph {
private boolean[][] edges;
private int[] labels;
private int N; //number of vertices;
//constructor. This constructor will create a matrix of size n by n.
public Graph(int n) {
N=n;
edges = new boolean[n][n];
labels = new int[n];
}
//this method will allow user to add an edge
public void addEdge(int source, int target) {
edges[source][target] = true;
}
//this method will return the label of the vertex
public int getLabel(int vertex) {
return labels[vertex];
}
//this method will allow user to remove an edge
public void removeEdge(int source, int target) {
edges[source][target] = false;
}
//this method will allow user to set a label
public void setLabels(int vertex, int newLabel) {
labels[vertex] = newLabel;
}
//this method will return the size of the labels array
public int size() {
return labels.length;
}
//this method will grab the neighbors of the desired vertex
public int[] neighbors(int vertex) {
int i;
int counter = 0;
int[] result;
for (i = 0; i < labels.length; i++) {
if (edges[vertex][i])
counter++;
}
result = new int[counter];
counter = 0;
for (i = 0; i < labels.length; i++) {
result[counter++] = i;
}
return result;
}
//this method will print out the vertices starting from the first value
I tried fixing my code a little, but I ran into another problem.
I do have to use neighbors method and also I cannot use recursion.
public void DepthFirstTraversal(int v) {
boolean[] visited = new boolean[N];
Stack<Integer> stack = new Stack<>();
stack.add(v);
visited[v]=true;
while(!stack.isEmpty()){
int i = stack.pop();
if (i == 1) {
System.out.print("A" + "-");
} else if (i == 2) {
System.out.print("B" + "-");
} else if (i == 3) {
System.out.print("C" + "-");
} else if (i == 4) {
System.out.print("D" + "-");
} else if (i == 5) {
System.out.print("E" + "-");
} else if (i == 6) {
System.out.print("F" + "-");
} else if (i == 7) {
System.out.print("G" + "-");
} else if (i == 8) {
System.out.print("H" + "-");
} else if (i == 9) {
System.out.print("I" + "-");
}
System.out.print(labels[i] + " \n");
int[] neighborsOfI = neighbors(i);
for(int k=0;k<neighborsOfI.length;k++){
int neighborTest = neighborsOfI[k];
if(!visited[neighborTest]){
stack.add(neighborTest);
visited[neighborTest]=true;
}
}
}
}
}
public class graphDemo {
public static void main(String args[]){
Graph graph = new Graph(10);
graph.addEdge(1,1);
graph.addEdge(2,3);
graph.addEdge(3,5);
graph.addEdge(4,7);
graph.addEdge(5,9);
graph.addEdge(6,2);
graph.addEdge(7,3);
graph.addEdge(8,5);
graph.addEdge(9,8);
graph.setLabels(1,9);
graph.setLabels(2,3);
graph.setLabels(3,5);
graph.setLabels(4,7);
graph.setLabels(5,4);
graph.setLabels(6,8);
graph.setLabels(7,6);
graph.setLabels(8,2);
graph.setLabels(9,1);
System.out.println("Depth First Traversal from first value: \n");
graph.DepthFirstTraversal(1);
}
}
我希望深度优先遍历从 A 开始并遵循深度优先遍历直到图中的最后一个元素,但我输出的是:
从第一个值开始深度优先遍历:
A-1 I-9
【问题讨论】:
-
几件事;
v的值根本没有被使用,所以改变它也无济于事。事实上,我们看不到addEdge和setLabels会做什么以及我们不知道labels或神秘的大写变量N中的内容意味着我们无法决定这段代码是什么会为你做的。如果我们需要帮助,我建议发布完整的代码。 -
@CXgamer 刚刚修复它。很抱歉,我什至没有意识到。
-
我刚刚注意到我什至没有进行深度优先遍历...你能告诉我如何从 A 开始吗?我认为它会帮助我找到一个开始的地方
-
@CXgamer 我编辑了我的代码,现在它在某种程度上做了深度优先遍历。我不能使用递归,我必须使用邻居方法。我认为它开始起作用了,但它只运行到第二个索引。有什么建议吗?