问题描述
我正在尝试在Java上创建一个程序,该程序将允许我遍历有向图的节点并输出边缘所经过的路径。我不确定是否需要使用递归还是可以通过简单的循环来完成。
这是我到目前为止的代码:
public class Main {
// instance variables - replace the example below with your own
int vertices;
int startValue;
boolean loop = false;
private int matrix[][] = new int[vertices][vertices];
private String map[] = new String[7];
//private int size;
public Main() {
//this.vertices = vertices;
// Can imput matrix here
//Scanner myVar = new Scanner(system.in);
int matrix[][] = {
{ 0,1 },{ 1,0 },{ 0,1,0 }
};
//size = 7;
String map[] = { "A","B","C","D","E","F","G" };
checkLoop(matrix);
//printMatrix(matrix);
System.out.println("\n");
makeGraph(matrix,map);
}
private void checkLoop(int matrix[][]) {
int sum = 0;
int aTemp;
loop = true;
for (int a = 0; a <= 6; a++) {
sum = 0;
for (int b = 0; b <= 6; b++) {
sum = matrix[a][b] + sum;
}
aTemp = a;
if (sum == 0) {
startValue = aTemp; //Could be a
System.out.println(aTemp);
loop = false;
return;
}
}
if (loop == true) {
System.out.println("Cycle Detected");
}
}
private void addEdge(int start,int finish) {
matrix[start][finish] = 1;
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
private void makeGraph(int matrix[][],String map[]) {
getNode(startValue,map);
//System.out.println(startValue);
//int a = 0;
int b = startValue;
for (int a = 0; a <= 6; a++) {
//boolean to exit if no link.
if (matrix[a][b] == 1) {
//System.out.println(matrix[a][b]);
//goThroughEdges(a,b,matrix);
getNode(a,map);
b = a;
}
}
}
private void getNode(int b,String map[]) {
System.out.print(map[b]);
}
public void printMatrix(int matrix[][]) {
for (int a = 0; a < 7; a++) {
for (int b = 0; b < 7; b++) {
System.out.print(matrix[a][b] + " ");
}
System.out.println("\n");
}
System.out.println("\n");
return;
}
}
矩阵的预期输出为EFDCBAG。任何帮助将不胜感激。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)