Loading lessons...
DFS & BFS: Traversal Trees & Connected Components
DFS & BFS: Traversal Trees & Connected Components
Graph traversal visits every vertex reachable from a start vertex. The two fundamental strategies produce spanning trees and reveal the structure of the graph.
1. Breadth-First Search (BFS)
Explore level by level using a queue: all neighbours of the start, then their neighbours, and so on.
BFS(G, s):
mark s visited; level(s) = 0; enqueue s
while queue not empty:
u = dequeue
for each neighbour v of u (in order):
if v not visited:
mark v visited; level(v) = level(u) + 1
parent(v) = u // tree edge (u, v)
enqueue v2. Depth-First Search (DFS)
Go as deep as possible before backtracking, using recursion (or an explicit stack).
DFS(G, u):
mark u visited
for each neighbour v of u (in order):
if v not visited:
parent(v) = u // tree edge (u, v)
DFS(G, v)3. Worked Example
Graph: A-B, A-C, B-D, B-E, C-F, E-G, F-G, and a separate edge H-I. Neighbours are taken in alphabetical order.
BFS tree vs DFS tree from A, and the two connected components
BFS from A
| Dequeued | Newly discovered (enqueued) | Queue after |
|---|---|---|
| A | B, C | B, C |
| B | D, E | C, D, E |
| C | F | D, E, F |
| D | - | E, F |
| E | G | F, G |
| F | - (G already seen) | G |
| G | - | empty |
Order: A, B, C, D, E, F, G. Levels: A = 0; B, C = 1; D, E, F = 2; G = 3. BFS tree edges: AB, AC, BD, BE, CF, EG. Non-tree edge FG joins levels 2 and 3 (a cross edge).
DFS from A A → B → D (dead end, back to B) → E → G → F → C (C's neighbours A, F already visited; backtrack to A). Order: A, B, D, E, G, F, C. DFS tree edges: AB, BD, BE, EG, GF, FC. Non-tree edge AC joins C to its ancestor A (a back edge).
H and I are never reached from A-they form a second connected component.
4. DFS and BFS Trees-Properties
| Property | BFS tree | DFS tree |
|---|---|---|
| Shape | short and wide | long and thin |
| Tree path s → v | a shortest path (fewest edges) | not necessarily shortest |
| Non-tree edges (undirected) | join vertices on the same or adjacent levels (cross edges) | join a vertex to an ancestor (back edges) - never "cross" edges |
| Data structure | queue | stack / recursion |
Both trees span the component of the start vertex and have (component size − 1) edges.
5. Connected Components
count = 0
for each vertex v in V:
if v not visited:
count = count + 1
DFS(G, v) // or BFS — marks every vertex of this component
label every newly visited vertex with component "count"In the example: component 1 = {A, B, C, D, E, F, G}, component 2 = {H, I}, so 2 components. A graph is connected iff a single traversal visits every vertex.
6. Complexity
Each vertex is enqueued/visited once and each adjacency list is scanned once:
| Representation | BFS / DFS time | Space |
|---|---|---|
| adjacency list | O(V + E) | O(V) for visited + queue/stack |
| adjacency matrix | O(V²) | O(V) |
Finding all components also takes O(V + E) - each vertex and edge is handled once in total.
7. Applications
| BFS | DFS |
|---|---|
| shortest paths in unweighted graphs | cycle detection (a back edge exists) |
| testing bipartiteness (2-colour by level parity) | topological sort of a DAG |
| level-order / "degrees of separation" | finding cut vertices and bridges |
| web crawling by distance | maze solving, strongly connected components |
📝 IPU End-Term Exam Corner (5-10 Marks)
- Q1. Explain BFS and DFS with algorithms. Trace both on a given graph and draw the BFS and DFS trees. (10 Marks)
- Q2. How can DFS be used to find the connected components of a graph? Give its time complexity. (5 Marks)
- Q3. Show that the BFS tree gives shortest paths (in number of edges) from the source. (5 Marks)