Skip to content

Commit 0a372a6

Browse files
committed
Breadth First Search (BFS)
1 parent e402566 commit 0a372a6

File tree

1 file changed

+66
-1
lines changed

1 file changed

+66
-1
lines changed

33-Graphs_(Part-I)/GraphsP1.java

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import java.util.ArrayList;
2+
import java.util.LinkedList;
3+
import java.util.Queue;
24

35
public class GraphsP1 {
46
// Create a Graph (Using Adjacency List) --------------------------------
@@ -14,6 +16,24 @@ public Edge(int src, int dest, int wt) {
1416
}
1517
}
1618

19+
// BFS Graph Traversal -------------------------------- TC -> O(V+E)
20+
public static void bfs(ArrayList<Edge>[] graph) {
21+
Queue<Integer> q = new LinkedList<>();
22+
boolean[] visited = new boolean[graph.length];
23+
q.add(0); // source (starting point)
24+
while (!q.isEmpty()) {
25+
int curr = q.remove();
26+
if (!visited[curr]) { // visite curr
27+
System.out.print(curr + " "); // print
28+
visited[curr] = true; // visite
29+
for (int i = 0; i < graph[curr].size(); i++) { // curr neighbors add in queue
30+
Edge e = graph[curr].get(i);
31+
q.add(e.dest);
32+
}
33+
}
34+
}
35+
}
36+
1737
public static void main(String[] args) {
1838
/*
1939
* Introduction --------------------------------
@@ -91,9 +111,54 @@ public static void main(String[] args) {
91111

92112
// 2's neighbors
93113
System.out.print("2's neighbors : ");
94-
for (int i = 0; i < graph[2].size(); i++) {
114+
for (int i = 0; i < graph[2].size(); i++) { // TC -> O(E) Where E is Edge length
95115
Edge e = graph[2].get(i);
96116
System.out.print(e.dest + " ");
97117
}
118+
System.out.println("\n");
119+
120+
// Graph Traversal --------------------------------
121+
// Breadth First Search (BFS)
122+
// Depth First Search (DFS)
123+
/*
124+
* 1 —— 3
125+
* / | \
126+
* 0 | 5 —— 6
127+
* \ | /
128+
* 2 —— 4
129+
*/
130+
int V2 = 7;
131+
@SuppressWarnings("unchecked") // Remove Warning
132+
ArrayList<Edge>[] graph2 = new ArrayList[V2]; // int[] arr = new int[V];
133+
for (int i = 0; i < graph2.length; i++) {
134+
graph2[i] = new ArrayList<>(); // null -> empty arraylist initalizes
135+
}
136+
// 0-vertex
137+
graph2[0].add(new Edge(0, 1, 1));
138+
graph2[0].add(new Edge(0, 2, 1));
139+
// 1-vertex
140+
graph2[1].add(new Edge(1, 0, 1));
141+
graph2[1].add(new Edge(1, 3, 1));
142+
// 2-vertex
143+
graph2[2].add(new Edge(2, 0, 1));
144+
graph2[2].add(new Edge(2, 4, 1));
145+
// 3-vertex
146+
graph2[3].add(new Edge(3, 1, 1));
147+
graph2[3].add(new Edge(3, 4, 1));
148+
graph2[3].add(new Edge(3, 5, 1));
149+
// 4-vertex
150+
graph2[4].add(new Edge(4, 2, 1));
151+
graph2[4].add(new Edge(4, 3, 1));
152+
graph2[4].add(new Edge(4, 5, 1));
153+
// 5-vertex
154+
graph2[5].add(new Edge(5, 3, 1));
155+
graph2[5].add(new Edge(5, 4, 1));
156+
graph2[5].add(new Edge(5, 6, 1));
157+
// 6-vertex
158+
graph2[6].add(new Edge(6, 5, 1));
159+
// Breadth First Search (BFS) ---
160+
System.out.print("BFS Traversing : ");
161+
bfs(graph2);
162+
98163
}
99164
}

0 commit comments

Comments
 (0)