-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBFS.java
95 lines (78 loc) · 2.67 KB
/
BFS.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication9;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author Ayush
*/
public class BFS {
/*
*Avoid static methods and variables
*/
static ArrayList<ArrayList<Integer>> al = new ArrayList<ArrayList<Integer>>();
static Boolean[] visited;
static int n=0; // number of nodes/vertices
static int layer=0;
static int m=0; //number of edges
static int timeLeft=1; //timeLeft before moving onto next layer
static ArrayList<Integer> queue = new ArrayList<>(); //Better to use a Queue DS
public static void main(String args[]){
/*Taking Input and stuff */
Scanner in = new Scanner(System.in);
n = in.nextInt();
m = in.nextInt();
visited = new Boolean[n];
for(int j=0;j<n;j++){
visited[j]=false;
}
int a;
int b;
for(int i=0;i<n;i++){
al.add(new ArrayList<Integer>());
}
for(int i=0;i<m;i++){
a= in.nextInt();
b= in.nextInt();
al.get(a).add(b);
al.get(b).add(a);
}
/* The real code begins here
First call for BFS is from inside the visit function Bfs(0)
*/
queue.add(0);
while(!queue.isEmpty()){
bfs(queue.get(0));
if(timeLeft==0){
layer++;
//System.out.println("layer: "+ layer);
timeLeft=queue.size();
}
}
visit(); //Acts as a cross checking function
}
public static void bfs(int n){
visited[n]=true;
System.out.println("Visiting node: " + n+ " --- timeLeft is: "+ timeLeft);
System.out.println("This Node is in layer:--- " + layer);
// System.out.println("In Layer "+ layer);
for(Integer list:al.get(n)){
if(!visited[list]){
queue.add(list);
}
}
queue.remove(0);
timeLeft--;
}
public static void visit(){
for(int i=0;i<n;i++){
if(!visited[n]){
bfs(n);
}
}
}
}