Discrete StructureTU Board 2023
Trace the path from 0 to 6 in following graph using BFS and DFS. [figure in the original paper]
5Answer
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:
- Start at 0, enqueue it, and mark as visited.
- Dequeue 0, explore neighbors: 1, 2 (enqueue in order).
- Queue:
[1, 2]
- Queue:
- Dequeue 1, explore neighbors: 3, 4 (enqueue).
- Queue:
[2, 3, 4]
- Queue:
- Dequeue 2, explore neighbor: 5 (enqueue).
- Queue:
[3, 4, 5]
- Queue:
- Dequeue 3, explore neighbor: 6 (enqueue).
- Queue:
[4, 5, 6]
- Queue:
- Dequeue 4, explore neighbor: 6 (already visited).
- Queue:
[5, 6]
- Queue:
- Dequeue 5, explore neighbor: 6 (already visited).
- Queue:
[6]
- Queue:
- 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:
- Start at 0, push to stack, mark as visited.
- Pop 0, explore 1 (push to stack).
- Pop 1, explore 3 (push to stack).
- Pop 3, explore 6 (destination reached).
- DFS Path:
0 → 1 → 3 → 6.
- DFS Path:
If the traversal had explored 4 before 3 (alternative branch):
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.
Discussion
Loading…