简体   繁体   English

给定图中无向图BFS的“邻接矩阵”表示并输出顶点

[英]Given an “Adjacency Matrix” representation of a an undirected graph BFS on the graph and output the vertex

I can find BFS and DFS values in a two-dimensional array array, but there is no increase in scoring. 我可以在二维数组数组中找到BFS和DFS值,但是得分没有增加。 I could not figure out exactly which part I did wrong. 我无法弄清楚我做错了哪一部分。 When I print the values that should be, the sort is correct. 当我打印应该的值时,排序是正确的。 but the grade is still 0. I am open to your ideas. 但是成绩仍然是0。我愿意接受您的想法。

public class BFS_DFS {
    public static int[] BFS (int [][] graph) {
        int[] output = {0};
            int start=0;

            int v=graph.length;//a[][] is adj matrix declared globally
            boolean visited[]=new boolean[v];//indexing done from 1 to n
            LinkedList<Integer> queue=new LinkedList<Integer>();
            visited[start]=true;
            queue.add(start);
            while(queue.size()!=0)
            {
                int x=queue.remove();
                System.out.print(x+" ");
                for(int j=graph.length;j<graph.length;j++){
                    output[j-1]=x;
                }

                for (int i=1; i < v; i++)
                    if((graph[x][i] == 1) && (!visited[i]))
                    {
                        queue.add(i);
                        visited[i]=true;   
                    }
            }
      return  output ; 
   }

   public static void main(String[] args) {

        //DO NOT WRITE ANY CODE IN MAIN METHOD
        int [][] graph={{0,1,1,1,0,0},{1,0,0,0,1,1},{1,0,0,0,0,1},{1,0,0,0,0,0},{0,1,0,0,0,0},{0,1,1,0,0,0}};

        int [] BFSresult={0,1,2,3,4,5};
        int [] DFSresult={0,1,4,5,2,3};
        int grade=0;

        if (Arrays.equals(BFS(graph),BFSresult )) {
            System.out.println("BFS is working correctl");
            grade+=50;
        }
        else
            System.out.println("BFS is not working correctly");

        if (Arrays.equals(DFS(graph),DFSresult )) {
            System.out.println("DFS is working correctl");
            grade+=50;
        }
        else
            System.out.println("DFS is not working correctly");

        System.out.println("Your grade is->"+grade);   
    }
}

Array ( int[] output ) is not appropriate to store results of un-known length, because it has a fixed length. 数组( int[] output )不适合存储未知长度的结果,因为它具有固定的长度。
Use a collection like List instead: 使用类似List的集合:

public static int[] BFS (int [][] graph) {

    //you do not know what is the path length so use a List
    List<Integer> output = new ArrayList<>();
    int start=0;

    int v=graph.length;
    boolean visited[]=new boolean[v];//indexing done from 1 to n
    LinkedList<Integer> queue=new LinkedList<>();
    visited[start]=true;
    queue.add(start);
    while(queue.size()!=0)
    {
        int x=queue.remove();
        System.out.print(x+" ");
        output.add(x); //add to output 

        for (int i=1; i < v; i++) {
            if((graph[x][i] == 1) && (!visited[i]))
            {
                queue.add(i);
                visited[i]=true;
            }
        }
    }
    //convert back to array
    return output.stream().mapToInt(Integer::intValue).toArray();

    /*if you can't use java 8 stream, convert output to array using: 
      int[] ret = new int[output.size()];
      int i = 0;
      for (int e : output) { ret[i++] = e; }
      return ret;
    */
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM