Fiveable

🧩Intro to Algorithms Unit 8 Review

QR code for Intro to Algorithms practice questions

8.2 Breadth-First Search (BFS) algorithm and applications

8.2 Breadth-First Search (BFS) algorithm and applications

Written by the Fiveable Content Team • Last updated August 2025
Written by the Fiveable Content Team • Last updated August 2025
🧩Intro to Algorithms
Unit & Topic Study Guides

Breadth-First Search (BFS) is a key algorithm for exploring graphs and trees. It visits vertices level by level, starting from a root, making it perfect for finding shortest paths in unweighted graphs and solving puzzles with minimum moves.

BFS uses a queue to track vertices and a visited set to avoid loops. It's great for connected components, web crawling, and social network analysis. With O(V + E) time complexity, it's efficient for large, sparse graphs in real-world applications.

Breadth-First Search Algorithm

BFS Fundamentals and Traversal Order

  • Breadth-First Search (BFS) explores vertices of a graph or tree in breadth-first order, visiting neighbors at the current depth before moving to the next depth level
  • Uses a queue data structure to track vertices for exploration, ensuring visitation in order of distance from the starting vertex
  • Starts at a chosen root vertex, marks it as visited, and enqueues it
  • Dequeues a vertex, explores all unvisited neighbors, marks them as visited, and enqueues them while the queue is not empty
  • Guarantees vertices are visited in increasing order of distance from the source vertex (optimal for finding shortest paths in unweighted graphs)
  • Terminates when the queue becomes empty, indicating all reachable vertices have been visited
  • Applies to both directed and undirected graphs, as well as trees, with modifications for different graph representations

BFS Process and Data Structures

  • Utilizes a queue for vertex exploration (standard library queue or custom implementation)
  • Employs a visited set or array to track explored vertices, preventing revisiting and infinite loops in cyclic graphs
  • Initializes by enqueuing and marking the starting vertex as visited
  • Main loop continues until the queue is empty, dequeuing vertices and processing unvisited neighbors
  • Marks adjacent unvisited vertices as visited and enqueues them for future exploration
  • Handles different graph representations efficiently (adjacency lists, adjacency matrices)
  • Incorporates additional data structures for tracking path information or distances (parent array, distance array)

Implementing BFS with a Queue

BFS Fundamentals and Traversal Order, CS 360: Lecture 16: Breadth-First Search

Queue-based Implementation

  • Initialize an empty queue and enqueue the starting vertex
  • Mark the starting vertex as visited using a boolean array or set
  • While the queue is not empty:
    • Dequeue a vertex from the front of the queue
    • Process the dequeued vertex (print, store, or perform any required operation)
    • For each unvisited neighbor of the dequeued vertex:
      • Mark the neighbor as visited
      • Enqueue the neighbor
  • Continue until the queue is empty, indicating all reachable vertices have been visited
  • Example implementation in Python:
    </>Python
    from collections import deque
    
    def bfs(graph, start):
        visited = set()
        queue = deque([start])
        visited.add(start)
    
        while queue:
            vertex = queue.popleft()
            print(vertex, end=' ')
    
            for neighbor in graph[vertex]:
                if neighbor not in visited:
                    visited.add(neighbor)
                    queue.append(neighbor)

Handling Different Graph Representations

  • Adjacency List: Efficient for sparse graphs, allows direct access to neighbors
    </>Python
    graph = {
        'A': ['B', 'C'],
        'B': ['A', 'D', 'E'],
        'C': ['A', 'F'],
        'D': ['B'],
        'E': ['B', 'F'],
        'F': ['C', 'E']
    }
  • Adjacency Matrix: Suitable for dense graphs, requires iterating through all vertices to find neighbors
    </>Python
    graph = [
        [0, 1, 1, 0, 0, 0],
        [1, 0, 0, 1, 1, 0],
        [1, 0, 0, 0, 0, 1],
        [0, 1, 0, 0, 0, 0],
        [0, 1, 0, 0, 0, 1],
        [0, 0, 1, 0, 1, 0]
    ]
  • Modify the neighbor exploration step based on the graph representation used

Applications of BFS

BFS Fundamentals and Traversal Order, Breadth-first search - Wikipedia

Shortest Path and Distance Calculation

  • Finds shortest path between two vertices in unweighted graphs by exploring vertices in order of distance from the source
  • Augments BFS with a parent array to reconstruct the path from source to any reachable vertex
  • Determines minimum number of edges between two vertices (crucial for social network analysis, routing algorithms)
  • Example: Finding shortest path in a maze represented as a grid
  • Calculates distances from the source vertex to all reachable vertices
    </>Python
    def bfs_shortest_path(graph, start, end):
        queue = deque([(start, [start])])
        visited = set([start])
    
        while queue:
            (vertex, path) = queue.popleft()
            if vertex == end:
                return path
    
            for neighbor in graph[vertex]:
                if neighbor not in visited:
                    visited.add(neighbor)
                    queue.append((neighbor, path + [neighbor]))
    
        return None

Graph Analysis and Problem Solving

  • Identifies connected components in undirected graphs by running BFS from each unvisited vertex
  • Solves puzzle problems (minimum moves in chess, Rubik's cube configurations)
  • Applies to computer networks for broadcasting and finding shortest paths in routing protocols
  • Useful in web crawling to explore web pages in a breadth-first manner from a given URL
  • Finds applications in social network analysis (friend recommendations, degrees of separation)
  • Implements flood fill algorithms in image processing and computer graphics
  • Example: Finding all connected components in an undirected graph
    </>Python
    def find_connected_components(graph):
        visited = set()
        components = []
    
        for vertex in graph:
            if vertex not in visited:
                component = []
                bfs_component(graph, vertex, visited, component)
                components.append(component)
    
        return components
    
    def bfs_component(graph, start, visited, component):
        queue = deque([start])
        visited.add(start)
    
        while queue:
            vertex = queue.popleft()
            component.append(vertex)
    
            for neighbor in graph[vertex]:
                if neighbor not in visited:
                    visited.add(neighbor)
                    queue.append(neighbor)

Time and Space Complexity of BFS

Time Complexity Analysis

  • Overall time complexity O(V + E), where V vertices and E edges in the graph
  • Worst-case time complexity O(V^2) for complete graphs represented using adjacency matrix
  • Nearly linear time complexity for sparse graphs (E ≈ V), efficient for large, sparse graphs in real-world applications
  • Efficiency varies based on graph representation:
    • Adjacency lists more efficient for sparse graphs
    • Adjacency matrices may be preferred for dense graphs
  • Performance affected by factors like cache efficiency and queue data structure implementation

Space Complexity and Practical Considerations

  • Space complexity O(V) due to queue and visited set used to track vertices
  • Additional space required for data structures like parent or distance arrays if path reconstruction needed
  • Practical performance impacted by:
    • Graph representation choice (adjacency list vs. adjacency matrix)
    • Implementation of queue data structure (array-based vs. linked list-based)
    • Cache efficiency and memory access patterns
  • Not optimal for finding shortest paths in weighted graphs (Dijkstra's or A* algorithms preferred)
  • Memory usage can be optimized by using bit vectors for visited set in graphs with a known, limited number of vertices
Pep mascot
Upgrade your Fiveable account to print any study guide

Download study guides as beautiful PDFs See example

Print or share PDFs with your students

Always prints our latest, updated content

Mark up and annotate as you study

Click below to go to billing portal → update your plan → choose Yearly → and select "Fiveable Share Plan". Only pay the difference

Plan is open to all students, teachers, parents, etc
Pep mascot
Upgrade your Fiveable account to export vocabulary

Download study guides as beautiful PDFs See example

Print or share PDFs with your students

Always prints our latest, updated content

Mark up and annotate as you study

Plan is open to all students, teachers, parents, etc
report an error
description

screenshots help us find and fix the issue faster (optional)

add screenshot

2,589 studying →