-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathBfs.java
More file actions
54 lines (48 loc) · 1.5 KB
/
Bfs.java
File metadata and controls
54 lines (48 loc) · 1.5 KB
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
import java.util.*;
public class Bfs {
// this is graph class to create a graph
class Graph{
public HashMap<Integer,ArrayList<Integer>> adjList = new HashMap<>();
public void addEdge(int n1,int n2){
if(!adjList.containsKey(n1)){
adjList.put(n1,new ArrayList<>());
}
adjList.get(n1).add(n2);
if(!adjList.containsKey(n2)){
adjList.put(n2,new ArrayList<>());
}
adjList.get(n2).add(n1);
}
}
// method to implement bfs algorithm
private static void bfs(int src,Graph g){
Queue<Integer> q = new LinkedList<>();
HashMap<Integer,Boolean> visited = new HashMap<>();
q.add(src);
visited.put(src,true);
while(!q.isEmpty()){
int node = q.poll();
System.out.print(node+" ");
for(int nbr : g.adjList.get(node)){
if(!visited.containsKey(nbr) || !visited.get(nbr)){
q.add(nbr);
visited.put(nbr,true);
}
}
}
}
// main method to create a graph
public static void main(String[] args) {
Graph graph = new Bfs().new Graph();
//adding nodes
// can be edited according to user
graph.addEdge(1, 2);
graph.addEdge(2, 3);
graph.addEdge(3, 8);
graph.addEdge(3, 5);
graph.addEdge(5, 4);
graph.addEdge(3, 4);
// using bfs
bfs(1,graph);
}
}