101 Logo
onenoughtone

Introduction to Breadth-First Search

What is Breadth-First Search?

Breadth-First Search (BFS) is a graph traversal algorithm that explores all vertices of a graph at the present depth before moving on to vertices at the next depth level.

In simpler terms, BFS explores a graph layer by layer, visiting all neighbors of a vertex before moving on to their neighbors.

Key Characteristics

  • Queue-Based: Uses a queue data structure to keep track of vertices to visit
  • Level by Level: Explores all vertices at the current level before moving to the next level
  • Shortest Path: Guarantees the shortest path in unweighted graphs
  • Complete: Will find a solution if one exists in a finite graph
  • Memory Intensive: Requires more memory than depth-first search

How BFS Works

  1. Start at a source vertex and mark it as visited
  2. Enqueue the source vertex into a queue
  3. While the queue is not empty:
    • Dequeue a vertex from the queue
    • Process the vertex (e.g., print it)
    • Enqueue all unvisited neighbors of the vertex and mark them as visited

Basic BFS Implementation

Here's a simple implementation of BFS for a graph represented as an adjacency list:

function bfs(graph, startVertex) { // Set to keep track of visited vertices const visited = new Set(); // Queue for BFS const queue = []; // Mark the start vertex as visited and enqueue it visited.add(startVertex); queue.push(startVertex); // Result array to store the BFS traversal order const result = []; // While there are vertices in the queue while (queue.length > 0) { // Dequeue a vertex from the queue const currentVertex = queue.shift(); // Process the vertex (add to result) result.push(currentVertex); // Get all adjacent vertices of the dequeued vertex // If an adjacent vertex has not been visited, mark it // as visited and enqueue it for (const neighbor of graph[currentVertex]) { if (!visited.has(neighbor)) { visited.add(neighbor); queue.push(neighbor); } } } return result; } // Example usage: const graph = { 'A': ['B', 'C'], 'B': ['A', 'D', 'E'], 'C': ['A', 'F'], 'D': ['B'], 'E': ['B', 'F'], 'F': ['C', 'E'] }; const traversalOrder = bfs(graph, 'A'); console.log(traversalOrder); // Output: ['A', 'B', 'C', 'D', 'E', 'F']

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.

We visit each vertex once and each edge once.

Space Complexity:

O(V), where V is the number of vertices in the graph.

In the worst case, the queue might contain all vertices of the graph.

Applications of BFS

  • Shortest Path: Finding the shortest path between two vertices in an unweighted graph
  • Connected Components: Finding all connected components in an undirected graph
  • Level Order Traversal: Traversing a tree level by level
  • Bipartite Graph: Checking if a graph is bipartite
  • Web Crawling: Crawling web pages and their links
  • Social Networks: Finding all friends within a certain degree of separation
IntroVisualizePatternsPractice
101 Logo
onenoughtone