-1

can I use stack instead of a queue when running breadth-first search. Does it still compute shortest paths?

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
EKSAC
  • 1

1 Answers1

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