簡體   English   中英

Java-鄰接矩陣和DFS

[英]Java - Adjacency Matrix and DFS

我一直在研究在Java中實現DFS的程序(通過從文件中獲取鄰接矩陣作為輸入)。 基本上,假設頂點按數字順序行進,我想打印頂點變為死角的順序,圖中連接的組件數,樹邊緣和后邊緣。 但是我還沒有完全到那兒。 當我運行程序時,輸出為“ 1”,僅此而已。 我已經嘗試調試DFS類的某些部分,但是我仍然無法弄清楚哪里出了問題。 這是我的代碼:

基本的“驅動程序”類:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Driver {

public static void main(String[] args) throws FileNotFoundException {
    Scanner scanner = new Scanner(new File("sample1.txt"));
    scanner.useDelimiter("[\\s,]+");

    int input = scanner.nextInt();
    int[][] adjMatrix = new int[8][8];

    for(int i=0; i < input; i++) {
        for (int j=0; j < input; j++) {
             adjMatrix[i][j] = scanner.nextInt();
        }
    }
    scanner.close();

    new DFS(adjMatrix);

}

}

DFS類:

import java.util.Stack;

public class DFS {
Stack<Integer> stack;
int first;
int[][] adjMatrix;
int[] visited = new int[7];

public DFS(int[][] Matrix) {

    this.adjMatrix = Matrix;
    stack = new Stack<Integer>();
    int[] node = {0, 1, 2, 3, 4, 5, 6};
    int firstNode = node[0];
    depthFirstSearch(firstNode, 7);
}

public void depthFirstSearch(int first,int n){
     int v,i;

     stack.push(first);

     while(!stack.isEmpty()){
         v = stack.pop();
         if(visited[v]==0) {
             System.out.print("\n"+(v+1));
             visited[v]=1;
         }
         for (i=0;i<n;i++){
             if((adjMatrix[v][i] == 1) && (visited[i] == 0)){
                 stack.push(v);
                 visited[i]=1;
                 System.out.print(" " + (i+1));
                 v = i;
             }
         }
     }
}
}

輸入文件中的矩陣如下所示:

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

看一下這一部分:

 int input = scanner.nextInt(); int[][] adjMatrix = new int[8][8]; for(int i=0; i < input; i++) { for (int j=0; j < input; j++) { adjMatrix[i][j] = scanner.nextInt(); } } 

首先,您input一個數字, input 然后,您將讀取input行,在每一行的input列中。

這是您的輸入數據:

 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 

第一個數字是什么,將由scanner.nextInt()讀取。 它是0。因此循環將不執行任何操作。

在輸入的數字前加上數字8,即:

 8 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 

順便說一句,驗證您已正確讀取矩陣是個好主意。 這是一種簡單的方法:

for (int[] row : adjMatrix) {
    System.out.println(Arrays.toString(row));
}

此實現中還有其他幾個問題:

  • 數字7出現在幾個地方。 在深度優先搜索算法中,這實際上是一個至關重要的值,而且實際上是錯誤的。 它應為8。並且不應對其進行硬編碼,而應從矩陣的大小中得出。
  • 在構造函數中進行計算不是一個好習慣。 構造函數的目的是創建一個對象。 可以將“深度優先”邏輯移到靜態實用程序方法中,當前代碼中沒有什么可以保證專用類的。

解決了上述問題,並修復了一些小問題,可以將實現的編寫更簡單,更簡潔:

public static void dfs(int[][] matrix) {
    boolean[] visited = new boolean[matrix.length];

    Deque<Integer> stack = new ArrayDeque<>();
    stack.push(0);

    while (!stack.isEmpty()) {
        int v = stack.pop();
        if (!visited[v]) {
            System.out.print("\n" + (v + 1));
            visited[v] = true;
        }
        for (int i = 0; i < matrix.length; i++) {
            if (matrix[v][i] == 1 && !visited[i]) {
                visited[i] = true;
                stack.push(v);
                v = i;
                System.out.print(" " + (i + 1));
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM