Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should PriorityQueue allow duplicates? #4372

Closed
johnmyleswhite opened this issue Sep 26, 2013 · 5 comments
Closed

Should PriorityQueue allow duplicates? #4372

johnmyleswhite opened this issue Sep 26, 2013 · 5 comments
Labels
needs decision A decision on this change is needed

Comments

@johnmyleswhite
Copy link
Member

My understanding of priority queues is that they should allow duplicate keys: this is, for example, how the STL and Java priority queue implementations work as far as I can tell (cf. http://stackoverflow.com/questions/251438/stl-priority-queue-with-duplicate-keys-is-it-possible, http://stackoverflow.com/questions/10469701/how-to-configure-java-priority-queue-to-ignore-duplicates). Right now, our implementation doesn't do this because it acts more like a Dict than a queue.

Could we possibly rename our current implementation to PriorityQueueDict and then implement a more standard PriorityQueue that behaves like a queue/array?

@stevengj
Copy link
Member

Note that you can use heappush! etcetera if you don't want it to act like a Dict.

@dcjones
Copy link
Contributor

dcjones commented Sep 26, 2013

The intention with making it behave like a Dict was to make things like Dijkstra or A* that require updating element priorities easier to write, which is an idea from clojure's priority-map. That struck me as a more common problem than needing to have multiple copies of the same element in the queue. Searching stack overflow for questions about updating priorities in C++ or Java, you get a bunch of answers suggesting you write your own priority queue.

So I'd like to keep the current priority queue in some form. Two priority queue types is one way to address this, we could also change the semantics of the current implementation so that duplicates are allowed q[x] = 100, changes the priority of just one occurrence of x (i.e. most recently enqueued).

@lindahua
Copy link
Contributor

Two different things are involved here: (1) the priority weight/value, which should allow duplicates, and (2) the handle that refers to a particular node in the heap (I think that's what @dcjones mentioned as the key).

The DataStructures.jl package takes a different approach, implementing both a non-mutable heap and a mutable heap. The mutable heap returns a handle when you push an element, with which the user can update the value later (e.g. in Dijkstra's algorithm).

@tkelman
Copy link
Contributor

tkelman commented Jan 5, 2017

to be reopened at DataStructures.jl (cc @ararslan)

@ararslan
Copy link
Member

ararslan commented Jan 5, 2017

The issue has been reopened at DataStructures: JuliaCollections/DataStructures.jl#246

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs decision A decision on this change is needed
Projects
None yet
Development

No branches or pull requests

8 participants