DFS/BFS Tree

Introduced in CS341. Quite useful in Competitive Programming.

For example, this problem: https://codeforces.com/problemset/problem/1666/L

BFS tree (page 16/21)

Definition: the BFS tree is the subgraph made of:

  • all vertices such that
  • all edges , for as above (except )

How do we create a BFS tree?

  • Using the algorithm above, which ensures that the properties described hold
  • parent[w] keeps track of the parents

Lemma (page 16/21)

The BFS tree is a tree.

  • Note that the BFS tree is not always unique

Subclaims

  • The levels in the queue are non-decreasing
  • For all vertices , if there is an edge , then

Shortest path from BFS Tree (18/21)

For all :

  • there is a path in iff there is a path in
  • if so, the path in is a shortest path and