forked from iiitv/algos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
breadthFirstTraversal.java
79 lines (72 loc) · 2.19 KB
/
breadthFirstTraversal.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
/*
*Implementation of Breadth First Traversal in Graph
*Time Complexity => O(v+e) v is no. of vertex and e is no. of edges
*Space Complexity => O(v)
*Adjacency List representation is used here
*/
import java.util.*;
public class breadthFirstTraversal {
public static void main(String[] args) {
Graph g=new Graph(7);
//1 Based indexing
g.addEdge(1, 2);
g.addEdge(1, 6);
g.addEdge(2, 3);
g.addEdge(2, 5);
g.addEdge(6, 3);
g.addEdge(6, 5);
g.addEdge(5, 4);
g.addEdge(3, 4);
System.out.println("BFS Traversal of the given graph is -");
g.breadthFirstTraverse(1);
}
}
class Graph
{
private int V; // No. of vertices
private LinkedList<Integer> adj[]; //Adjacency Lists
// Constructor
public Graph(int v)
{
V = v;
adj = new LinkedList[v];
for (int i=0; i<v; ++i)
adj[i] = new LinkedList();
}
// Function to add an edge into the graph
public void addEdge(int v,int w)
{
adj[v].add(w);
}
// prints BFS traversal from s
public void breadthFirstTraverse(int s)
{
// Mark all the vertices as not visited(By default
// set as false)
boolean visited[] = new boolean[V];
// Creates a queue for BFS
LinkedList<Integer> queue = new LinkedList<Integer>();
// Mark the current node as visited and enqueue it
visited[s]=true;
queue.add(s);
while (queue.size() != 0)
{
// 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 = adj[s].listIterator();
while (i.hasNext())
{
int n = i.next();
if (!visited[n])
{
visited[n] = true;
queue.add(n);
}
}
}
}
}