Breadth First Search (BFS)
Is s connected to t?
Build layers of vertices connected to s
L0 = {s}
Assume L0,..,Lj have been constructed
Lj+1 set of vertices not chosen yet but are connected to Lj
Stop when new layer is empty
Lj : all nodes at distance j from sLj : all nodes at
distance j from s
BFS TreeBFS naturally defines a tree rooted at s
Lj forms the jth “level” in the tree
u in Lj+1 is child of v in Lj from which it was “discovered”
11
22 33
44 55
66
77
88
11
22 33
L0
L1
44 55 77 88L2
66L3
Proofs in the lectures
Mostly proof ideas from now on
Read the book for the formal proofs
Homeworks will still require formal proofs
Computing Connected Component
Start with R = {s}
While exists (u,v) edge v not in R and u in R
Add v to R
Output R
Today’s agenda
If w is in R iff w is connected to s
Depth First Search
Computing all connected components
A DFS run
11
22 33
44 55
66
77
88
11
22
44
55
66 33
88
77
Every non-tree edge is between a
node and its ancestor
Every non-tree edge is between a
node and its ancestor
DFS treeDFS tree
Connected components are disjoint
Either Connected components of s and t are the same or are disjoint
Algorithm to compute ALL the connected
components?
Algorithm to compute ALL the connected
components?
Run BFS on some node s. Then run BFS on t that is not connected to s Run BFS on some node s. Then run BFS on t that is not connected to s