Trees & BFS
Trees are the simplest connected graphs — connected with no cycles. We prove their key properties, introduce spanning trees, and learn Breadth-First Search (BFS) as a systematic way to explore graphs and find shortest paths in unweighted graphs.
Learning Objectives
Key Concepts
Trees
A tree is a connected acyclic graph. The following four conditions are equivalent (proof: ):
- - is a tree (connected and acyclic)
- -For every , there is a unique - path
- - is maximally acyclic (adding any edge creates a cycle)
- - is minimally connected (removing any edge disconnects)
A forest is an acyclic graph (a disjoint union of trees). A leaf is a vertex of degree 1.
- -
Every tree on vertices has at least 2 leaves
- -
A tree on vertices has exactly edges
- -
Adding any edge to a tree creates exactly one cycle
- -
A forest with vertices and components has edges
Breadth-First Search (BFS)
BFS explores a graph level by level starting from a source vertex . It uses a queue: visit , then all neighbors of , then all unvisited neighbors of those, etc.
BFS computes the shortest path (fewest edges) from to every reachable vertex. The BFS tree is a spanning tree of the component containing .
- -
BFS runs in time where
- -
BFS gives shortest paths in unweighted graphs
- -
Non-tree edges in BFS connect vertices in the same or adjacent levels
- -
BFS can detect bipartiteness: a graph is bipartite iff BFS finds no edge within a level