-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphBFS.java
63 lines (57 loc) · 1.15 KB
/
GraphBFS.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
package Unit_4;
import java.util.*;
public class GraphBFS {
static int v;
static LinkedList<Integer> adj[];
@SuppressWarnings("unchecked")GraphBFS(int v)
{
this.v = v;
adj = new LinkedList[v];
for(int i = 0; i < v; i++)
adj[i] = new LinkedList();
}
void addEdge(int v, int w)
{
adj[v].add(w);
}
void BFS(int s)
{
boolean visited[] = new boolean[v];
LinkedList<Integer> queue = new LinkedList<Integer>();
visited[s] = true;
queue.add(s);
while(queue.size() != 0)
{
s = queue.poll();
System.out.print(s + " ");
Iterator<Integer> i = adj[s].listIterator();
while(i.hasNext())
{
int n = i.next();
if(!visited[n])
{
visited[n] = true;
queue.add(n);
}
}
}
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter no. of edges : ");
int n = sc.nextInt();
GraphBFS g = new GraphBFS(n);
for(int i = 0; i < n; i++)
{
int x = sc.nextInt();
int y = sc.nextInt();
g.addEdge(x, y);
}
System.out.print("Enter Source Vertex : ");
int s = sc.nextInt();
System.out.print("The BFS Traversal is ");
g.BFS(s);
sc.close();
}
}