-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFS.java
More file actions
86 lines (66 loc) · 2.13 KB
/
BFS.java
File metadata and controls
86 lines (66 loc) · 2.13 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
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
import java.util.*;
import java.io.*;
public class dfs {
// Class for defining the edges
static class Edge{
int src,dest;
public Edge(int source, int dest){
this.src = source;
this.dest = dest;
}
}
// Class for initializing the graph
static class Graph{
List<List<Integer>> list;
public Graph(List<Edge> edges, int size){
list = new ArrayList<>();
for(int i=0;i<size; i++){
list.add(new ArrayList<>());
}
for(Edge e: edges){
list.get(e.src).add(e.dest);
}
}
}
static void BFS(int s, Graph graph, int nodes){
// Mark all the vertices as not visited(By default
// set as false)
boolean visited[] = new boolean[nodes];
// Create a queue for BFS
Queue<Integer> queue = new LinkedList<Integer>();
// Mark the current node as visited and enqueue it
visited[s-1]=true;
queue.add(s);
while (!queue.isEmpty()){
// Dequeue a vertex from queue and print it
s = queue.poll();
System.out.print(s+" ");
// Get all adjacent vertices of the dequeued vertex s
// If a adjacent has not been visited, then mark it
// visited and enqueue it
Iterator<Integer> i = graph.list.get(s).listIterator();
while (i.hasNext())
{
int n = i.next();
if (!visited[n-1])
{
visited[n-1] = true;
queue.add(n);
}
}
}
}
public static void main(String[] args) throws IOException {
int nodes = 4;
ArrayList<Edge> edges = new ArrayList<Edge>(){
{
add(new Edge(0,1)); add(new Edge(0, 2));
add(new Edge(1, 2)); add(new Edge(2,0));
add(new Edge(2,3)); add(new Edge(3,3));
}
};
Graph graph = new Graph(edges, nodes);
// Starting from node 2
DFS(2, graph, nodes);
}
}