Lesson 38 +35 XP

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.

TEXT
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 v

2. Depth-First Search (DFS)

Go as deep as possible before backtracking, using recursion (or an explicit stack).

TEXT
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 componentsBFS tree vs DFS tree from A, and the two connected components

BFS from A

DequeuedNewly discovered (enqueued)Queue after
AB, CB, C
BD, EC, D, E
CFD, E, F
D-E, F
EGF, 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

PropertyBFS treeDFS tree
Shapeshort and widelong and thin
Tree path s → va 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 structurequeuestack / recursion

Both trees span the component of the start vertex and have (component size − 1) edges.

5. Connected Components

TEXT
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:

RepresentationBFS / DFS timeSpace
adjacency listO(V + E)O(V) for visited + queue/stack
adjacency matrixO(V²)O(V)

Finding all components also takes O(V + E) - each vertex and edge is handled once in total.

7. Applications

BFSDFS
shortest paths in unweighted graphscycle 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 distancemaze solving, strongly connected components
🎯 END-TERM EXAM PREP

📝 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)