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 accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.