-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Priority queue in standard library #2438
Comments
+1 We had discussed having a DataStructures package (https://groups.google.com/d/msg/julia-dev/k-tRUcg1y1s/0HNQt6R4TpQJ). I think it's badly needed. |
On Friday, March 01, 2013 07:00:22 AM John Myles White wrote:
Agreed. Heap can presumably be implemented much better now using type-tagging, along --Tim |
The question is, what should go into Julia's standard library and what should go into an external package. The standard library has the advantage of being officially supported, easier to find for users, usable within the standard library itself, and of trustable quality (whereas external packages have extremely variable quality, and this will get worse as the number of packages increases). External packages have the advantages of being easier to update rapidly independent of Julia releases and easier to put experimental functionality into (i.e. the variable quality has its blessings). A |
All very good points. And I'm certainly not opposed to having the core data structures be in Base.I'd suggest having them in a module inside of Base -- something like Base.DataStructures. |
Certainly should be in the standard library. |
It would be nice if we could have the kind of priority queue that allows to update priorities of items in it as well. |
I felt like having a go at this. My implementation is on the dcj/pq branch. I really like the idiom of a priority queue that behaves like a Dict, mapping keys to priorities, but also provides a function dijkstra(A)
n = size(A)[1]
path = zeros(Int, n)
dist = fill(Inf, n)
dist[1] = 0
q = PriorityQueue(1:n, dist)
while !isempty(q)
u = dequeue!(q)
for v in 1:n
if has(q, v) && dist[u] + A[u,v] < q[v]
dist[v] = q[v] = dist[u] + A[u,v]
path[v] = u
end
end
end
return (path, dist)
end The downside with this approach is that things in the priority queue must be unique, and there is the added overhead of having to maintain a Dict. For that reason, I also added lower level heap functions ( |
Nice. |
Not in their current incarnation, since they operate on plain arrays (as in |
Ah, right. |
@dcjones your code looks nice as usual! I made a couple minor updates. We should really consider just dropping this into Base. |
+1 on putting this in |
I have no serious objections to putting this in Base as soon as possible. My only hope is that having something DataStructures will encourage people to add more there. |
@johnmyleswhite I had the same thought, but I'm not entirely sure what else I'd like to see. One thing I would be excited about, particularly now that we have |
Can this be dropped into |
Yes, I implemented a mutable binary heap in |
It would be good to have a priority queue (e.g.
PriorityQueue{K,V}
) in the standard library. Maybe based onexamples/heap.jl
?The text was updated successfully, but these errors were encountered: