MCS-208 · Unit 8

Graphs

Section 7 of 8

Block 3 · Unit 8Graphs8 min · Section 7 of 8

8.7Breadth-First and Depth-First Search

Covers IGNOU MCS-208 §8.5, 8.6

Many graph algorithms start by visiting every vertex reachable from a source in a systematic way. The two standard traversals are breadth-first search, which explores in layers, and depth-first search, which goes as deep as possible before backing up.

Breadth-first search (BFS)

  1. Mark the source visited and put it in a queue.
  2. While the queue is not empty: take out the front vertex uu and visit it; for every unvisited neighbour vv of uu, mark vv visited and add it to the queue.

Because a queue is FIFO, vertices are visited in order of their number of edges from the source; BFS therefore finds shortest paths in an unweighted graph.

Depth-first search (DFS)

DFS with discovery and finishing times
DFS(G):   for each vertex u: if u is not visited: DFS-VISIT(u)
DFS-VISIT(u):
    mark u visited;  time = time + 1;  d[u] = time      /* discovered */
    for each neighbour v of u:
        if v is not visited: DFS-VISIT(v)
    time = time + 1;  f[u] = time                       /* finished   */

DFS is naturally recursive (or uses an explicit stack, Unit 4). The discovery time d[u] is when uu is first reached; the finishing time f[u] is when all its descendants are done. If the search from the first source does not reach every vertex, DFS restarts from an unvisited vertex, producing a DFS forest.

BFS versus DFS

BFSDFS
StructureQueueStack (or recursion)
OrderLevel by levelAs deep as possible, then back up
FindsShortest paths (in number of edges)Discovery/finish times, cycles, topological order, SCCs
Time (adjacency lists)O(n+e)O(n + e)O(n+e)O(n + e)
Time (adjacency matrix)O(n2)O(n^2)O(n2)O(n^2)

Key takeaways

  • BFS: queue, level by level, shortest paths in unweighted graphs.
  • DFS: recursion/stack, deep first, discovery time dd and finishing time ff.
  • Different neighbour orders give different (equally valid) traversal orders.
  • Both are O(n+e)O(n + e) with lists, O(n2)O(n^2) with a matrix.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

Which data structure does BFS use?

Show answer

Answer: B. A queue

FIFO order makes BFS process vertices in order of distance. A stack would give a depth-first order.

Question 2 · multiple choice

In an unweighted graph, what does BFS from ss compute?

Show answer

Answer: C. The fewest edges needed to reach each vertex from ss

Vertices are discovered level by level, so the level is the shortest distance in edges. Finishing times come from DFS.

Question 3 · multiple choice

In a DFS, vertex vv is discovered while exploring from uu. Which time relation must hold?

Show answer

Answer: B. d[u]<d[v]<f[v]<f[u]d[u] < d[v] < f[v] < f[u]

vv‘s whole visit happens inside uu‘s visit, so vv is discovered after uu and finishes before uu (the “parenthesis” property).

Question 4 · multiple choice

What is the running time of DFS on a graph stored as adjacency lists?

Show answer

Answer: D. O(n+e)O(n + e)

Every vertex is visited once (nn) and every list is scanned once (ee). O(n2)O(n^2) is the cost with a matrix.

Question 5 · multiple choice

What does this program print?

C · q8-bfs.c
#include <stdio.h>

int main(void)
{
    int a[5][5] = {{0, 1, 1, 0, 0},
                   {0, 0, 0, 1, 0},
                   {0, 0, 0, 1, 1},
                   {0, 0, 0, 0, 1},
                   {0, 0, 0, 0, 0}};
    int q[5], vis[5] = {1, 0, 0, 0, 0}, front = 0, rear = 0;
    q[rear++] = 0;
    while (front < rear) {
        int u = q[front++];
        printf("%d ", u);
        for (int v = 0; v < 5; v++)
            if (a[u][v] && !vis[v]) { vis[v] = 1; q[rear++] = v; }
    }
    printf("\n");
    return 0;
}
Show answer

Answer: C. 0 1 2 3 4

From 0: 1 and 2 are queued; from 1: 3; from 2: 4 (3 already queued). “0 1 3 4 2” is a depth-first order.

Question 6 · short answer

Perform BFS and DFS on the undirected graph with edges 1–2, 1–3, 2–4, 3–4, 4–5, starting at 1 and taking neighbours in increasing order. Give the DFS discovery/finishing times.

Show worked answer

BFS: queue 1 → visit 1, queue 2, 3 → visit 2, queue 4 → visit 3 (4 already queued) → visit 4, queue 5 → visit 5. BFS order: 1 2 3 4 5. DFS: 1 (d = 1) → 2 (d = 2) → 4 (d = 3) → 3 (d = 4; its neighbours 1 and 4 are visited) finishes f = 5 → back at 4, go to 5 (d = 6), f = 7 → 4 finishes f = 8 → 2 finishes f = 9 → 1 finishes f = 10. DFS order: 1 2 4 3 5; times 1: 1/10, 2: 2/9, 4: 3/8, 3: 4/5, 5: 6/7.

↑ ↓ move↵ openTab switch type