IT235 Discrete Structure

Discrete StructureTU Board 2023

Trace the path from 0 to 6 in following graph using BFS and DFS. [figure in the original paper]

5

Answer

0123456
Given undirected graph (vertices and edges)

BFS Traversal from 0 to 6

BFS explores nodes level by level, starting from the source (0). The traversal order and path from 0 to 6 are as follows:

  1. Start at 0, enqueue it, and mark as visited.
  2. Dequeue 0, explore neighbors: 1, 2 (enqueue in order).
    • Queue: [1, 2]
  3. Dequeue 1, explore neighbors: 3, 4 (enqueue).
    • Queue: [2, 3, 4]
  4. Dequeue 2, explore neighbor: 5 (enqueue).
    • Queue: [3, 4, 5]
  5. Dequeue 3, explore neighbor: 6 (enqueue).
    • Queue: [4, 5, 6]
  6. Dequeue 4, explore neighbor: 6 (already visited).
    • Queue: [5, 6]
  7. Dequeue 5, explore neighbor: 6 (already visited).
    • Queue: [6]
  8. Dequeue 6 (destination reached).

BFS Path from 0 to 6: 0 → 1 → 3 → 6 (or 0 → 1 → 4 → 6 or 0 → 2 → 5 → 6). The shortest path is 0 → 1 → 3 → 6 (3 edges).


DFS Traversal from 0 to 6

DFS explores as far as possible along each branch before backtracking. Assuming left-to-right neighbor order:

  1. Start at 0, push to stack, mark as visited.
  2. Pop 0, explore 1 (push to stack).
  3. Pop 1, explore 3 (push to stack).
  4. Pop 3, explore 6 (destination reached).
    • DFS Path: 0 → 1 → 3 → 6.

If the traversal had explored 4 before 3 (alternative branch):

  1. 0 → 1 → 4 → 6 (also valid).

DFS Path from 0 to 6: 0 → 1 → 3 → 6 (or 0 → 1 → 4 → 6 depending on neighbor order). Unlike BFS, DFS does not guarantee the shortest path.


0123456
BFS Shortest Path (0 → 1 → 3 → 6)

Discussion

Loading…

More Discrete Structure questions

All Discrete Structure old questions