
- Graph Theory - Home
- Graph Theory - Introduction
- Graph Theory - History
- Graph Theory - Fundamentals
- Graph Theory - Applications
- Types of Graphs
- Graph Theory - Types of Graphs
- Graph Theory - Simple Graphs
- Graph Theory - Multi-graphs
- Graph Theory - Directed Graphs
- Graph Theory - Weighted Graphs
- Graph Theory - Bipartite Graphs
- Graph Theory - Complete Graphs
- Graph Theory - Subgraphs
- Graph Theory - Trees
- Graph Theory - Forests
- Graph Theory - Planar Graphs
- Graph Theory - Hypergraphs
- Graph Theory - Infinite Graphs
- Graph Theory - Random Graphs
- Graph Representation
- Graph Theory - Graph Representation
- Graph Theory - Adjacency Matrix
- Graph Theory - Adjacency List
- Graph Theory - Incidence Matrix
- Graph Theory - Edge List
- Graph Theory - Compact Representation
- Graph Theory - Incidence Structure
- Graph Theory - Matrix-Tree Theorem
- Graph Properties
- Graph Theory - Basic Properties
- Graph Theory - Coverings
- Graph Theory - Matchings
- Graph Theory - Independent Sets
- Graph Theory - Traversability
- Graph Theory Connectivity
- Graph Theory - Connectivity
- Graph Theory - Vertex Connectivity
- Graph Theory - Edge Connectivity
- Graph Theory - k-Connected Graphs
- Graph Theory - 2-Vertex-Connected Graphs
- Graph Theory - 2-Edge-Connected Graphs
- Graph Theory - Strongly Connected Graphs
- Graph Theory - Weakly Connected Graphs
- Graph Theory - Connectivity in Planar Graphs
- Graph Theory - Connectivity in Dynamic Graphs
- Special Graphs
- Graph Theory - Regular Graphs
- Graph Theory - Complete Bipartite Graphs
- Graph Theory - Chordal Graphs
- Graph Theory - Line Graphs
- Graph Theory - Complement Graphs
- Graph Theory - Graph Products
- Graph Theory - Petersen Graph
- Graph Theory - Cayley Graphs
- Graph Theory - De Bruijn Graphs
- Graph Algorithms
- Graph Theory - Graph Algorithms
- Graph Theory - Breadth-First Search
- Graph Theory - Depth-First Search (DFS)
- Graph Theory - Dijkstra's Algorithm
- Graph Theory - Bellman-Ford Algorithm
- Graph Theory - Floyd-Warshall Algorithm
- Graph Theory - Johnson's Algorithm
- Graph Theory - A* Search Algorithm
- Graph Theory - Kruskal's Algorithm
- Graph Theory - Prim's Algorithm
- Graph Theory - Borůvka's Algorithm
- Graph Theory - Ford-Fulkerson Algorithm
- Graph Theory - Edmonds-Karp Algorithm
- Graph Theory - Push-Relabel Algorithm
- Graph Theory - Dinic's Algorithm
- Graph Theory - Hopcroft-Karp Algorithm
- Graph Theory - Tarjan's Algorithm
- Graph Theory - Kosaraju's Algorithm
- Graph Theory - Karger's Algorithm
- Graph Coloring
- Graph Theory - Coloring
- Graph Theory - Edge Coloring
- Graph Theory - Total Coloring
- Graph Theory - Greedy Coloring
- Graph Theory - Four Color Theorem
- Graph Theory - Coloring Bipartite Graphs
- Graph Theory - List Coloring
- Advanced Topics of Graph Theory
- Graph Theory - Chromatic Number
- Graph Theory - Chromatic Polynomial
- Graph Theory - Graph Labeling
- Graph Theory - Planarity & Kuratowski's Theorem
- Graph Theory - Planarity Testing Algorithms
- Graph Theory - Graph Embedding
- Graph Theory - Graph Minors
- Graph Theory - Isomorphism
- Spectral Graph Theory
- Graph Theory - Graph Laplacians
- Graph Theory - Cheeger's Inequality
- Graph Theory - Graph Clustering
- Graph Theory - Graph Partitioning
- Graph Theory - Tree Decomposition
- Graph Theory - Treewidth
- Graph Theory - Branchwidth
- Graph Theory - Graph Drawings
- Graph Theory - Force-Directed Methods
- Graph Theory - Layered Graph Drawing
- Graph Theory - Orthogonal Graph Drawing
- Graph Theory - Examples
- Computational Complexity of Graph
- Graph Theory - Time Complexity
- Graph Theory - Space Complexity
- Graph Theory - NP-Complete Problems
- Graph Theory - Approximation Algorithms
- Graph Theory - Parallel & Distributed Algorithms
- Graph Theory - Algorithm Optimization
- Graphs in Computer Science
- Graph Theory - Data Structures for Graphs
- Graph Theory - Graph Implementations
- Graph Theory - Graph Databases
- Graph Theory - Query Languages
- Graph Algorithms in Machine Learning
- Graph Neural Networks
- Graph Theory - Link Prediction
- Graph-Based Clustering
- Graph Theory - PageRank Algorithm
- Graph Theory - HITS Algorithm
- Graph Theory - Social Network Analysis
- Graph Theory - Centrality Measures
- Graph Theory - Community Detection
- Graph Theory - Influence Maximization
- Graph Theory - Graph Compression
- Graph Theory Real-World Applications
- Graph Theory - Network Routing
- Graph Theory - Traffic Flow
- Graph Theory - Web Crawling Data Structures
- Graph Theory - Computer Vision
- Graph Theory - Recommendation Systems
- Graph Theory - Biological Networks
- Graph Theory - Social Networks
- Graph Theory - Smart Grids
- Graph Theory - Telecommunications
- Graph Theory - Knowledge Graphs
- Graph Theory - Game Theory
- Graph Theory - Urban Planning
- Graph Theory Useful Resources
- Graph Theory - Quick Guide
- Graph Theory - Useful Resources
- Graph Theory - Discussion
Graph Theory - Breadth-First Search
Breadth-First Search (BFS)
Breadth-First Search (BFS) is a graph traversal algorithm used to systematically explore nodes and edges in a graph. It starts at a selected node (often called the 'root') and explores all neighboring nodes at the current depth level before moving on to nodes at the next depth level.
It is particularly useful in scenarios such as finding the shortest path in an unweighted graph, determining the shortest distance between nodes, and exploring the reachability of nodes in a graph.
BFS uses a queue data structure to maintain the order of exploration. Unlike Depth-First Search (DFS), which dives deep into a path before backtracking, BFS explores all nodes level by level.
BFS Algorithm
The BFS algorithm follows these basic steps −
- Initialize a queue and mark the starting node as visited.
- While the queue is not empty, dequeue a node from the queue.
- Visit all unvisited neighboring nodes of the current node, mark them as visited, and enqueue them.
- Repeat this process until all reachable nodes are visited.
Let us look at an example to better understand BFS. Consider the following graph with nodes labeled A, B, C, D, and E −

We will perform BFS starting from node A. The traversal proceeds as follows −
- Start at node A. Mark it as visited and enqueue it.
- Dequeue A, visit its neighbors (B, C), mark them as visited, and enqueue them.
- Dequeue B, visit its neighbor (D), mark it as visited, and enqueue it.
- Dequeue C, visit its neighbor (E), mark it as visited, and enqueue it.
- Dequeue D and E. No further unvisited neighbors are found.
Thus, the BFS traversal order is A B C D E.
BFS on an Undirected Graph
In an undirected graph, BFS explores all nodes level by level starting from the chosen root node. It uses a queue to track nodes to visit next, ensuring all neighbors are visited before moving to the next level. This helps in finding the shortest path and checking connectivity between nodes.
Let us take a concrete example of BFS traversal on an undirected graph −

The graph is as follows −
- A is connected to B and C.
- B is connected to A, D, and E.
- C is connected to A and F.
- D is connected to B.
- E is connected to B.
- F is connected to C.
Starting BFS from node A:
- Visit A, then its neighbors B and C.
- Queue: B, C.
- Visit B, then its neighbors D and E.
- Queue: C, D, E.
- Visit C, then its neighbor F.
- Queue: D, E, F.
- Visit D and E. No further unvisited neighbors.
- Visit F. No further unvisited neighbors.
Final BFS traversal order: A B C D E F.
BFS on a Directed Graph
In directed graphs, BFS works similarly but respects the direction of edges. In directed graphs, BFS explores only the neighbors that are directly reachable from a node, following the directions of the edges.
Consider the following directed graph −

In this graph, we perform BFS starting from node A −
- Start at node A, visit its neighbors B and C.
- Queue: B, C.
- Visit B, then its neighbor D.
- Queue: C, D.
- Visit C, then its neighbor E.
- Queue: D, E.
- Visit D and E. No further unvisited neighbors.
Final BFS traversal order: A B C D E.
Applications of BFS
BFS is used in various real-world applications, such as −
- Shortest Path in Unweighted Graphs: BFS is often used to find the shortest path between two nodes in an unweighted graph. Since BFS explores level by level, the first time it reaches a node, it does so via the shortest path.
- Connected Components: BFS can help identify connected components in an undirected graph. By performing BFS from an unvisited node and marking all reachable nodes, you can determine the entire connected component.
- Level Order Traversal in Trees: BFS can be used to traverse trees in level order, ensuring that all nodes at each level are visited before moving to the next level.
- Network Broadcasting: In networking, BFS can be used to model the process of broadcasting messages from a central node to all other nodes in a network.
BFS in Finding the Shortest Path
One of the most important applications of BFS is finding the shortest path in an unweighted graph. Since BFS explores all nodes at a given depth before moving on to the next level, the first time it reaches a node, it does so via the shortest path.
For example, given an unweighted graph and two nodes, A and E, BFS can be used to determine the shortest path from A to E. The algorithm proceeds level by level, ensuring that the first time it reaches node E, it does so via the minimum number of edges.
Here is an example of finding the shortest path from node A to node E in an unweighted graph −

The shortest path from A to E is A B E, with two edges traversed. BFS ensures that this is the shortest path because it explores the graph level by level, visiting nodes closest to the starting point first.
Complexity of BFS
The time complexity of BFS is O(V + E), where V is the number of vertices and E is the number of edges in the graph. This is because BFS processes each vertex and edge exactly once.
The space complexity is O(V), as BFS requires storage for the queue and a visited list, both of which require space proportional to the number of vertices in the graph.
BFS performs efficiently on sparse and dense graphs, making it suitable for various graph-related problems.
BFS with Edge Weights
While BFS is not suitable for graphs with weighted edges when looking for the shortest path, it can still be used to find the shortest path in graphs where all edges have equal weight. In such cases, BFS guarantees finding the shortest path because it explores nodes level by level.
For weighted graphs, algorithms like Dijkstra's algorithm are more appropriate as they account for varying edge weights.