-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathBFS.java
More file actions
55 lines (43 loc) · 1.58 KB
/
BFS.java
File metadata and controls
55 lines (43 loc) · 1.58 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
package graphs;
import java.util.*;
/* NOTE:
- Breadth-first search (BFS) is an algorithm for traversing or searching tree or graph data structures.
- BFS make use of Queue data structure to keep track of path.
- And it requires a boolean array for maintaining visitid nodes.
*/
/* STEPS:
- Mark all the vertices as false (by default)
- Create a queue
- Mark current node true(to represent visited) and add it to queue(enqueue)
- While queue is not empty
- Delete from queue(Dequeue) and store it in source.
- For each node(vertex) in graph
. if an adjacent is not visited
. visit that adjacent node and add it to queue
*/
public class BFS {
public static Queue<Integer> breadthFirstSearch(int[][] adj, int source, int totalNodes) {
boolean[] visited = new boolean[totalNodes];
Queue<Integer> queue = new LinkedList<>();
visited[source] = true;
queue.add(source);
while (queue.size() != 0) {
source = queue.poll();
for (int num : adj[source]) {
if (!visited[num]) {
visited[num] = true;
queue.add(num);
}
}
}
return queue;
}
public static void main(String[] args) {
int[][] adj = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
Queue<Integer> bfsTraversal = breadthFirstSearch(adj, 4, adj.length);
System.out.println("Breadth First Search Traversal: " + bfsTraversal);
}
}