Pergunta de entrevista da empresa Amazon

What's the difference between Breadth-first algorithm and depth-first algorithm ?

Resposta da entrevista

Sigiloso

10 de set. de 2016

A BFS keeps a queue of vertices that have been discovered and are yet to be visited, and then repeatedly visits the front vertex on the queue, discovering all of its neighbours and adding those to the back of the queue. This means that the earlier a vertex is discovered, the earlier it is visited, giving the 'breadth-first' shape of the search. A DFS simply replaces the queue of the BFS with a stack, so that newly discovered vertices are visited right away, and vertices discovered longer ago are only returned to once the new vertices have been visited. This causes the search to 'dive' right to the end of the first search path before considering any other vertices.

2