bfs with arraylist as parameter

Solutions on MaxInterview for bfs with arraylist as parameter by the best coders in the world

showing results for - "bfs with arraylist as parameter"
Giulio
02 Apr 2016
1class Traversal {
2    static ArrayList<Integer> bfs(ArrayList<ArrayList<Integer>> g, int N) {
3        // add your code here
4        ArrayList<Integer> m=new ArrayList<>();
5        Queue<Integer> q=new LinkedList<Integer>();
6        boolean vis[]=new boolean[g.size()];
7        q.add(0);
8        m.add(0);
9        vis[0]=true;
10        while(!q.isEmpty()){
11            int s=q.poll();
12            Iterator<Integer> itr=g.get(s).iterator();
13            while(itr.hasNext()){
14                int n=itr.next();
15                if(!vis[n]){
16                    q.add(n);
17                    vis[n]=true;
18                    m.add(n);
19                }
20            }
21        }
22      
23       
24        return m;
25    }
26}
27