You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- `addEdge` adds an edge from `sourceNode` to `destinationNode`. If the graph is undirected (default behavior), it adds the reverse edge from `destinationNode` to `sourceNode`.
171
204
172
-
-**Explanation**:
173
-
- Constructor initializes the graph with `totalNodes` nodes.
174
-
- Resizes the `adjacencyList` to ensure every node has an associated list.
175
-
176
-
205
+
#### 2. **BFS (Iterative) Implementation:**
177
206
207
+
##### **BFS Function:**
178
208
```cpp
179
-
voidaddEdge(int sourceNode, int destinationNode, bool isDirected = false) {
- Initializes a `visited` array to track visited nodes.
206
-
- A queue `q` is used to manage BFS traversal.
207
-
208
-
209
-
210
-
```cpp
211
-
visited[startNode] = true;
212
-
q.push(startNode);
213
-
```
214
-
215
-
-**Explanation**:
216
-
- Marks the `startNode` as visited.
217
-
- Pushes the `startNode` into the queue to begin traversal.
218
-
219
-
220
-
221
-
```cpp
222
-
cout << "BFS Traversal: ";
223
-
```
224
-
225
-
-**Explanation**: Outputs a label for the BFS traversal results.
233
+
-**Explanation:**
234
+
- The function takes `startNode` as input and performs a **Breadth-First Search** (BFS) starting from that node.
235
+
-`visited` array ensures that no node is visited more than once.
236
+
- A `queue` is used to manage the nodes to visit next. We push the `startNode` into the queue and mark it as visited.
237
+
- The BFS traversal proceeds by dequeuing nodes from the front of the queue and processing them. Then, all their unvisited neighbors are pushed into the queue.
226
238
239
+
#### 3. **BFS (Recursive) Implementation:**
227
240
241
+
##### **Recursive Helper Function:**
228
242
```cpp
229
-
while (!q.empty()) {
230
-
int currentNode = q.front();
231
-
q.pop();
232
-
```
233
-
234
-
- **Explanation**:
235
-
- The loop continues until the queue is empty.
236
-
- Retrieves the front node of the queue (`currentNode`) and removes it.
- This helper function is designed to perform the recursive part of BFS.
264
+
- It processes nodes in a similar way to the iterative BFS but using recursion to handle the queue management.
265
+
- If the queue is empty, the recursion terminates. Otherwise, it pops a node from the queue, processes it, and pushes all its unvisited neighbors into the queue, making a recursive call after each step.
243
266
244
-
-**Explanation**: Prints the `currentNode` to show its processing in BFS.
- This function initializes the `visited` array and the queue, starting from `startNode`, and calls the recursive helper to perform the BFS traversal.
258
281
259
-
- **Explanation**:
260
-
- Iterates through all neighbors of the `currentNode`.
261
-
- If a neighbor has not been visited:
262
-
- Marks it as visited.
263
-
- Adds it to the queue for future processing.
264
-
265
-
266
-
282
+
#### 4. **Main Function:**
267
283
```cpp
268
284
intmain() {
269
285
int totalNodes = 6;
270
-
271
-
// Create the graph
272
286
Graph g(totalNodes);
273
-
```
274
-
275
-
-**Explanation**:
276
-
- Declares `totalNodes` as `6`.
277
-
- Creates a graph object `g` with `6` nodes.
278
-
279
287
280
-
```cpp
281
288
g.addEdge(0, 1);
282
289
g.addEdge(0, 2);
283
290
g.addEdge(1, 3);
284
291
g.addEdge(1, 4);
285
292
g.addEdge(2, 5);
286
-
```
287
-
288
-
-**Explanation**:
289
-
- Adds edges to the graph:
290
-
- Node `0` is connected to nodes `1` and `2`.
291
-
- Node `1` is connected to nodes `3` and `4`.
292
-
- Node `2` is connected to node `5`.
293
-
294
293
295
-
```cpp
296
-
g.bfs(0);
294
+
g.bfs(0); // Iterative BFS starting from node 0
295
+
g.bfsRecursive(0); // Recursive BFS starting from node 0
297
296
298
297
return 0;
299
298
}
300
299
```
300
+
- Creates a graph with 6 nodes.
301
+
- Adds edges to the graph.
302
+
- Calls both the **Iterative BFS** (`g.bfs(0)`) and **Recursive BFS** (`g.bfsRecursive(0)`) functions to perform BFS starting from node `0`.
301
303
302
-
-**Explanation**:
303
-
- Calls `bfs(0)` to perform BFS starting from node `0`.
304
-
- Returns `0` to indicate successful program execution.
305
-
306
-
307
-
### **Graph Representation**
308
-
309
-
The graph looks like this:
304
+
### **Output:**
310
305
311
306
```
312
-
0
313
-
/ \
314
-
1 2
315
-
/ \ \
316
-
3 4 5
307
+
BFS Traversal (Iterative): 0 1 2 3 4 5
308
+
BFS Traversal (Recursive): 0 1 2 3 4 5
317
309
```
318
310
311
+
### **Time and Space Complexity:**
319
312
320
-
### **Output**
321
-
322
-
```
323
-
BFS Traversal: 0 1 2 3 4 5
324
-
```
325
-
326
-
-**Explanation**:
327
-
- BFS starts at node `0`.
328
-
- Visits nodes `1` and `2` (neighbors of `0`).
329
-
- Then visits nodes `3` and `4` (neighbors of `1`) and finally node `5` (neighbor of `2`).
330
-
331
-
### **Time Complexity**
332
-
333
-
1.**Adjacency List Traversal**:
334
-
- Each edge is visited once: **O(E)**.
335
-
- Each node is visited once: **O(V)**.
336
-
337
-
Total Time Complexity: **O(V + E)**.
338
-
339
-
340
-
### **Space Complexity**
313
+
#### **Time Complexity:**
314
+
- Both **Iterative BFS** and **Recursive BFS** have the same time complexity:
315
+
-**O(V + E)**, where:
316
+
-`V` is the number of vertices (nodes) in the graph.
317
+
-`E` is the number of edges in the graph.
318
+
- We visit each vertex and edge exactly once.
341
319
342
-
1.**Visited Array**: Stores the state of each node: **O(V)**.
343
-
2.**Queue**: Can hold up to **O(V)** nodes.
344
-
3.**Adjacency List**: Takes **O(V + E)**.
320
+
####**Space Complexity:**
321
+
-**O(V)**:
322
+
- The space is mainly used for the `visited` array (of size `V`), the queue (`O(V)`), and the recursion stack in the recursive approach (`O(V)` in the worst case).
345
323
346
-
Total Space Complexity: **O(V + E)**.
324
+
### **Conclusion:**
325
+
-**Iterative BFS** is more common and efficient in terms of stack space, as it avoids recursion depth limitations.
326
+
-**Recursive BFS** is an interesting implementation that can be used for educational purposes, but it may suffer from stack overflow issues for large graphs.
0 commit comments