DFS and BFS with adjacency matrix counted in Java

泄露秘密 提交于 2019-12-25 01:32:34

问题


Currently trying to build a program that can implement both DFS and BFS in Java by taking in an adjacency matrix from a file and printing out the following info: order that vertices are first encountered, order that vertices become dead ends, number of components, and the tree edges.

Here is my code:

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();
        }
    }   
}

And here is the output from running my code:

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: 

And here are my issues. For the DFS, my order of first encountered should be 1 2 6 7 4 3 5 8, but this is not what I'm getting. Additionally, my order of dead ends should be 8 7 5 4 1 2 6 3, but this is also off. My tree edges managed to come out correct.

For the BFS, I can't get anything to print, and debugging my DFS and BFS methods hasn't given me the answer yet. I'd be very grateful from some help.

来源:https://stackoverflow.com/questions/49377108/dfs-and-bfs-with-adjacency-matrix-counted-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!