HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Implementing an efficent priority queue using only stacks

Submitted by: @import:stackexchange-cs··
0
Viewed 0 times
priorityefficentstacksusingonlyimplementingqueue

Problem

Is it possible to implement an efficient priority queue (as efficient as a heap) only using the stack data structure?

The usual efficiency for a priority queue which is implemented using a heap is :

  • get min - $O(1)$



  • extract min - $O(\log n)$



  • add - $O(\log n)$



Would it be possible to do something with the same complexity using only stacks?

I want to implement A* (which uses a priority queue to prioritize nodes) as a pathfinding algorithm inside Minecraft, but Minecraft doesn't allow random access, and the most efficent dynamic structure that can be implemented in it is a stack.

Solution

Unfortunately, it's not possible. The order you extract items from the stack depends only on the order they're pushed, regardless of the values in those items; a priority queue needs items to be removed in an order that depends on the value of the items.

Context

StackExchange Computer Science Q#123740, answer score: 2

Revisions (0)

No revisions yet.