In general, given that \(V_0, \dots, V_{i-1}\) have been defined, we define \(V_i\) to be the set of all vertices \(v \in V\setminus (V_0 \cup \cdots \cup V_{i-1})\) such that there is some vertex \(u\in V_{i-1}\) with \(uv\in E\). We stop when \(V_i=\emptyset\).
Low-level BFS algorithm:
bfs(v) {
for each vertex v ∈ V do { d[v] ← ∞ }
d[s] ← 0
p[s] ← s
Q ← {s} // queue Q starts off having s as its only element
while Q is not empty do {
v ← dequeue(Q)
for each edge vw do {
if d[w] = ∞ {
d[w] ← d[v] + 1
p[w] ← v
enqueue(w, Q)
}
}
}
}
Example Execution of BFS