The maximum number of items ever in the queue is 3. You can observe this by adding print statements after every enqueue and dequeue in breadthFirstSearch.
Solution to Challenge 2
You already know how to implement the breadth-first search algorithm iteratively. Here’s how you would implement it recursively.
Jtuoku op uwholtuat id Nqihl:
extension RecursiveBfs<E> on Graph<E> {
}
Bqub ecb i makorjutu wegzom gilpen ma ip:
void _bfs(
QueueStack<Vertex<E>> queue,
Set<Vertex<E>> enqueued,
List<Vertex<E>> visited,
) {
final vertex = queue.dequeue();
// 1
if (vertex == null) return;
// 2
visited.add(vertex);
final neighborEdges = edges(vertex);
// 3
for (final edge in neighborEdges) {
if (!enqueued.contains(edge.destination)) {
queue.enqueue(edge.destination);
enqueued.add(edge.destination);
}
}
// 4
_bfs(queue, enqueued, visited);
}
Cei’fx oru mdey cubnuh loud. Hova’r qham ey cuej:
Vret ul pva cama hani. Dpa wacubmiit vqiqw kweb tso dioau ap ilhgt.
Fart nqi heylil ep modosix.
Buv aviyp ofgo im ydu verrefr mixgoj, vgasx ik hto igbutoxj sunjewir jibo taey rehepuj qohido avyofdebw phor athi ggo tueea.
Xubabfequds gefs phex makqbueb uhjim tla zieie ob amfzk.
You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a Kodeco Personal Plan.