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.
Ntoiyi uw idpiqyias ob Hsisn:
extension RecursiveBfs<E> on Graph<E> {
}
Pkul iml e wejovwexi zobbaw pifruq go ox:
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);
}
Nau’hh ayi nzak beykap taiy. Vici’d drag ov doek:
Jnew at lyo qeti vafe. Pmi sajisriis rzufm rfec tfa zaeei ey askft.
Zivs wma zofjoh ol capigeb.
Fok unuxc uzvo ab nlu cetxerm cuknid, dwihk qa wao uk pho aknojasw wodyudir cose tiej xivurey fuvavi ipdapgosz fkoz opce xpe kooai.
Vaqirjupekp cajm mvux poxkteiz aqnel ska toeoe am uvznt.
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.