目前正在尝试通过从文件中获取邻接矩阵并打印出以下信息来构建可以在Java中实现DFS和BFS的程序:首先遇到顶点的顺序,命令顶点成为死角,数量为组件和树边缘。
这是我的代码:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class ProjectDriver {
public static int count;
public static void main(String[] args) throws FileNotFoundException {
ArrayList<Integer> dfsDeadEnd = new ArrayList<Integer>();
ArrayList<Integer> dfsVertices = new ArrayList<Integer>();
ArrayList<Integer> bfsVertices = new ArrayList<Integer>();
int dfsComponents = 0;
int bfsComponents = 0;
Scanner scanner = new Scanner(new File("sample1.txt"));
int n = 8;
int[][] edges = new int[n][n];
boolean[] visited = new boolean[n];
count = 0;
for(int i=0; i < n; i++) {
for (int j=0; j < n; j++) {
edges[i][j] = scanner.nextInt();
}
}
for(int i = 0; i < n; i++){
visited[i] = false;
}
int[][] BFStreeEdgeGraph = new int[n][n];
int[][] DFStreeEdgeGraph = new int[n][n];
int[][] crossGraph = new int[n][n];
for(int i = 0; i <= n-1; i++){
for(int j = 0; j <= n-1; j++){
DFStreeEdgeGraph[i][j] = 0;
BFStreeEdgeGraph[i][j] = 0;
crossGraph[i][j] = 0;
}
}
for(int i = 0; i <= n-1; i++){
if(visited[i] == false){
dfs(edges,i,visited, dfsVertices, dfsDeadEnd, DFStreeEdgeGraph);
dfsDeadEnd.add(i);
dfsComponents++;
}
}
for(int i = 0; i <= n-1; i++) {
if(visited[i] == false) {
bfs(edges, i, visited, bfsVertices, BFStreeEdgeGraph);
bfsComponents++;
}
}
System.out.println();
System.out.println("DFS: Number of Connected Components: " + dfsComponents);
System.out.print("DFS: Order of First Encountered: ");
for(int i : dfsVertices){
System.out.print((i+1) + " ");
}
System.out.println();
System.out.print("DFS: Order of First Dead-Ends: ");
for(int i : dfsDeadEnd){
System.out.print((i+1) + " ");
}
System.out.println();
System.out.println();
System.out.println("Tree edges:");
displayGraph(DFStreeEdgeGraph, n);
System.out.println();
System.out.println();
System.out.println("BFS: Number of Connected Components: " + bfsComponents);
System.out.print("BFS: Order of First encountered: ");
for(int i : bfsVertices){
System.out.print((i+1) + " ");
}
System.out.println();
System.out.println();
}
public static void dfs(int[][] edges, int vertex, boolean[] visited, ArrayList<Integer> dfsVertices, ArrayList<Integer> dfsDeadEnd, int[][] DFStreeEdgeGraph) {
visited[vertex] = true;
dfsVertices.add(count, vertex);
count = count + 1;
for(int w = 0; w <= edges.length-1; w++) {
if(edges[vertex][w] == 1 && !visited[w]) {
DFStreeEdgeGraph[vertex][w] = 1;
dfs(edges, w, visited, dfsVertices, dfsDeadEnd, DFStreeEdgeGraph);
dfsDeadEnd.add(w);
}
}
}
public static void bfs(int[][] edges, int vertex, boolean[] visited, ArrayList<Integer> bfsVertices, int[][] BFStreeEdgeGraph) {
bfsVertices.add(count, vertex);
count = count + 1;
for(int w = 0; w < edges.length; w++) {
if(edges[vertex][w] != 0 && !visited[w]) {
visited[vertex] = true;
}
}
for(int w = bfsVertices.indexOf(vertex) + 1; w < bfsVertices.size(); w++) {
int value = bfsVertices.get(w);
bfs(edges, value, visited, bfsVertices, BFStreeEdgeGraph);
}
}
public static void displayGraph(int[][] graph, int n) {
for(int i = 0; i <= n-1; ++i){
System.out.print(" ");
for(int j = 0; j <= n-1; ++j){
System.out.print(graph[i][j] + " ");
}
System.out.println();
}
}
}
以下是运行我的代码的输出:
Input graph:
0 1 0 0 1 1 0 0
1 0 0 0 0 1 1 0
0 0 0 1 0 0 1 0
0 0 1 0 0 0 0 1
1 0 0 0 0 1 0 0
1 1 0 0 1 0 0 0
0 1 1 0 0 0 0 1
0 0 0 1 0 0 1 0
DFS: Number of Connected Components: 1
DFS: Order of First Encountered: 1 2 6 5 7 3 4 8
DFS: Order of First Dead-Ends: 5 6 8 4 3 7 2 1
Tree edges:
0 1 0 0 0 0 0 0
0 0 0 0 0 1 1 0
0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0
0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0
BFS: Number of Connected Components: 0
BFS: Order of First encountered:
这是我的问题。对于DFS,我首次遇到的订单应该是1 2 6 7 4 3 5 8
,但这不是我得到的。另外,我的死胡同订单应该是8 7 5 4 1 2 6 3
,但这也是关闭的。我的树边设法正确。
对于BFS,我无法打印任何内容,调试我的DFS和BFS方法还没有给我答案。我会非常感激一些帮助。