Exploring Breadth First Search: Essential Techniques for Programmers
Learn how Breadth First Search in C optimally traverses graphs and trees, suitable for both new and seasoned programmers.
Introduction to Breadth First Search
Breadth First Search (BFS) is a versatile algorithm extensively used in programming to traverse or search tree and graph data structures. It starts at the tree root (or any arbitrary node of a graph) and explores the neighbor nodes at the present depth prior to moving on to nodes at the next depth level. This layer-by-layer approach is particularly useful in finding the shortest path on unweighted graphs.
Algorithm Details with Pseudo Code and Explanation
Breadth First Search Algorithm:
BFS uses a queue to keep track of the next vertex to visit and ensures that each level of the graph is fully explored before moving to the next. This method is ideal for finding the shortest path in unweighted graphs because it explores all neighbors at the current depth, or 'moves away' from the start vertex, before moving deeper.
Pseudo Code:
function BFS(start_node):
create a queue Q
mark start_node as visited and enqueue it into Q
while Q is not empty:
vertex = Q.dequeue()
print vertex
for each adjacent vertex of vertex:
if adjacent_vertex is not visited:
mark adjacent_vertex as visited
enqueue adjacent_vertex into Q
Explanation:
- Initialization: Start by marking the start node as visited and placing it in the queue.
- Exploration Loop: While there are still nodes to visit in the queue:
- Remove the next node from the queue and process it.
- For each adjacent node that hasn't been visited, mark it as visited and add it to the queue. This step ensures that each node is processed in the order it is reached.
Implementation in C Language
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int adj[MAX][MAX], visited[MAX];
int n; // number of vertices
void BFS(int startVertex) {
int queue[MAX], front = 0, rear = -1;
queue[++rear] = startVertex;
visited[startVertex] = 1;
while (front <= rear) {
int vertex = queue[front++];
printf("%d ", vertex);
for (int i = 0; i < n; i++) {
if (adj[vertex][i] == 1 && !visited[i]) {
queue[++rear] = i;
visited[i] = 1;
}
}
}
}
void addEdge(int v1, int v2) {
adj[v1][v2] = 1;
adj[v2][v1] = 1; // For undirected graph
}
int main() {
n = 5; // Example size of the graph
addEdge(0, 1);
addEdge(0, 2);
addEdge(1, 3);
addEdge(1, 4);
for (int i = 0; i < n; i++) {
if (!visited[i])
BFS(i);
}
return 0;
}
Explanation of the C Code:
- Graph Representation: An adjacency matrix
adj
represents the graph whereadj[v1][v2] = 1
denotes an edge betweenv1
andv2
. - BFS Function: Implements BFS using a queue managed with array indices. It processes each vertex by dequeuing it, printing it, and enqueuing all unvisited adjacent vertices.
- Main Function: Initializes the graph, adds edges, and triggers BFS for each unvisited vertex, ensuring all components are covered.
Time and Space Complexity:
- Time Complexity: ๐(๐+๐ธ)O(V+E), where ๐V is the number of vertices and ๐ธE is the number of edges in the graph.
- Space Complexity: ๐(๐)O(V), primarily for storing visited nodes and the queue.
Usage
BFS is widely used in:
- Shortest Path Finding: In unweighted graphs to find the shortest path.
- Connectivity Testing: To check if all parts of a graph are reachable from a given node.
- Level Order Traversal: In trees to traverse nodes level by level.
Conclusion
Breadth First Search is a powerful and essential algorithm for navigating and analyzing graphs and trees. Its methodical approach ensures that all nodes and vertices are explored thoroughly and in the correct order, making it indispensable in various practical applications from computer networks to social networking services.