can I use stack instead of a queue when running breadth-first search. Does it still compute shortest paths?
Asked
Active
Viewed 434 times
-1
-
6If you use a stack, it won't be breadth-first, it will be depth-first. – Blorgbeard Nov 27 '16 at 21:04
1 Answers
0
Anything is possible.
Yes, you can surely use stacks instead of a queue as the auxiliary data structure for the Breadth-First Search.
That said, I am talking about implementing a queue using 2 stacks. Refer this answer for a short explanation.
For every enqueue and dequeue operation, you might have to perform some extra pushing and popping which may lead to a complex code for a simple BFS.
If you are restricted to using just 1 stack, you might as well try for a Depth-First Search.

Community
- 1
- 1

skrtbhtngr
- 2,223
- 23
- 29