Answers for "bfs with arraylist as parameter"

0

bfs with arraylist as parameter

class Traversal {
    static ArrayList<Integer> bfs(ArrayList<ArrayList<Integer>> g, int N) {
        // add your code here
        ArrayList<Integer> m=new ArrayList<>();
        Queue<Integer> q=new LinkedList<Integer>();
        boolean vis[]=new boolean[g.size()];
        q.add(0);
        m.add(0);
        vis[0]=true;
        while(!q.isEmpty()){
            int s=q.poll();
            Iterator<Integer> itr=g.get(s).iterator();
            while(itr.hasNext()){
                int n=itr.next();
                if(!vis[n]){
                    q.add(n);
                    vis[n]=true;
                    m.add(n);
                }
            }
        }
      
       
        return m;
    }
}
Posted by: Guest on September-27-2020

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language