|
| 1 | +--- |
| 2 | +id: BFS-traversal-of-graph |
| 3 | +title: BFS Traversal of Graph (Geeks for Geeks) |
| 4 | +sidebar_label: Problems - BFS Traversal of Graph |
| 5 | +tags: |
| 6 | + - Intermediate |
| 7 | + - Graph |
| 8 | + - Breadth-First Search |
| 9 | + - Geeks for Geeks |
| 10 | + - CPP |
| 11 | + - Python |
| 12 | + - DSA |
| 13 | +description: "This is a solution to the BFS Traversal of Graph problem on Geeks for Geeks." |
| 14 | +--- |
| 15 | + |
| 16 | +## Problem Description |
| 17 | + |
| 18 | +Given a graph, perform Breadth-First Search (BFS) traversal starting from a given source vertex. |
| 19 | + |
| 20 | +## Examples |
| 21 | + |
| 22 | +**Example:** |
| 23 | + |
| 24 | +Consider the following graph: |
| 25 | + |
| 26 | +``` |
| 27 | + 1 |
| 28 | + / \ |
| 29 | + 2 3 |
| 30 | + / \ |
| 31 | + 4 5 |
| 32 | +``` |
| 33 | + |
| 34 | +**Output:** 1 2 3 4 5 |
| 35 | + |
| 36 | +## Your Task |
| 37 | + |
| 38 | +Your task is to complete the function `bfs()`, which takes the graph, the number of vertices, and the source vertex as its arguments and prints the BFS traversal of the graph starting from the source vertex. |
| 39 | + |
| 40 | +Expected Time Complexity: O(V + E), where V is the number of vertices and E is the number of edges in the graph. |
| 41 | +Expected Auxiliary Space: O(V). |
| 42 | + |
| 43 | +## Constraints |
| 44 | + |
| 45 | +- `1 <= number of vertices <= 10^3` |
| 46 | +- `0 <= value of vertices <= 10^3` |
| 47 | + |
| 48 | +## Problem Explanation |
| 49 | + |
| 50 | +Here's the step-by-step breakdown of the BFS traversal process: |
| 51 | + |
| 52 | +1. **Initialize visited array**: Create a visited array to keep track of visited vertices. |
| 53 | +2. **Initialize queue**: Create a queue to store vertices to be visited next. |
| 54 | +3. **Perform BFS**: Start BFS traversal from the source vertex. |
| 55 | +4. **Enqueue source**: Enqueue the source vertex into the queue and mark it as visited. |
| 56 | +5. **Process vertices**: While the queue is not empty, dequeue a vertex, print it, and enqueue its unvisited adjacent vertices. |
| 57 | + |
| 58 | +### Code Implementation |
| 59 | + |
| 60 | +<Tabs> |
| 61 | + <TabItem value="Python" label="Python" default> |
| 62 | + <SolutionAuthor name="@ngmuraqrdd"/> |
| 63 | + ```python |
| 64 | + from collections import deque |
| 65 | + |
| 66 | + def bfs(graph, v, source): |
| 67 | + visited = [False] * v |
| 68 | + queue = deque([source]) |
| 69 | + visited[source] = True |
| 70 | + |
| 71 | + while queue: |
| 72 | + u = queue.popleft() |
| 73 | + print(u, end=" ") |
| 74 | + |
| 75 | + for neighbor in graph[u]: |
| 76 | + if not visited[neighbor]: |
| 77 | + visited[neighbor] = True |
| 78 | + queue.append(neighbor) |
| 79 | + |
| 80 | + def bfsTraversal(graph, V, source): |
| 81 | + bfs(graph, V, source) |
| 82 | + ``` |
| 83 | + </TabItem> |
| 84 | + |
| 85 | + <TabItem value="C++" label="C++" default> |
| 86 | + <SolutionAuthor name="@ngmuraqrdd"/> |
| 87 | + ```cpp |
| 88 | + #include <iostream> |
| 89 | + #include <vector> |
| 90 | + #include <queue> |
| 91 | + #include <cstring> |
| 92 | + using namespace std; |
| 93 | + |
| 94 | + void bfs(vector<int> graph[], int v, int source) { |
| 95 | + bool visited[v]; |
| 96 | + memset(visited, false, sizeof(visited)); |
| 97 | + queue<int> q; |
| 98 | + |
| 99 | + q.push(source); |
| 100 | + visited[source] = true; |
| 101 | + |
| 102 | + while (!q.empty()) { |
| 103 | + int u = q.front(); |
| 104 | + q.pop(); |
| 105 | + cout << u << " "; |
| 106 | + |
| 107 | + for (int neighbor : graph[u]) { |
| 108 | + if (!visited[neighbor]) { |
| 109 | + visited[neighbor] = true; |
| 110 | + q.push(neighbor); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + void bfsTraversal(vector<int> graph[], int V, int source) { |
| 117 | + bfs(graph, V, source); |
| 118 | + } |
| 119 | + ``` |
| 120 | + </TabItem> |
| 121 | +</Tabs> |
| 122 | +
|
| 123 | +## Solution Logic |
| 124 | +
|
| 125 | +1. **Initialize visited array and queue**: Create an array to mark visited vertices initially as False and a queue to store vertices to be visited. |
| 126 | +2. **Perform BFS**: Start BFS traversal from the source vertex. |
| 127 | +3. **Enqueue source**: Enqueue the source vertex into the queue and mark it as visited. |
| 128 | +4. **Process vertices**: While the queue is not empty, dequeue a vertex, print it, and enqueue its unvisited adjacent vertices. |
| 129 | +
|
| 130 | +## Time Complexity |
| 131 | +
|
| 132 | +O(V + E), where V is the number of vertices and E is the number of edges in the graph. Each vertex and edge are visited only once. |
| 133 | +
|
| 134 | +## Space Complexity |
| 135 | +
|
| 136 | +O(V), where V is the number of vertices. The space is used to store the visited array and the queue. |
| 137 | +
|
| 138 | +## Resources |
| 139 | +
|
| 140 | +- **GFG Problem:** [GFG Problem](https://www.geeksforgeeks.org/breadth-first-search-or-bfs-for-a-graph/) |
| 141 | +- **LeetCode Problem:** [LeetCode Problem](https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/) |
| 142 | +- **Author's Geeks for Geeks Profile:** [MuraliDharan](https://www.geeksforgeeks.org/user/ngmuraqrdd/) |
| 143 | +
|
| 144 | +This format ensures that all necessary details about the problem and its solution are clearly presented and easy to follow. |
0 commit comments