priority-queue

Why does Dijkstra's algorithm use decrease-key?

巧了我就是萌 提交于 2019-11-28 02:48:16
Dijkstra's algorithm was taught to me was as follows while pqueue is not empty: distance, node = pqueue.delete_min() if node has been visited: continue else: mark node as visited if node == target: break for each neighbor of node: pqueue.insert(distance + distance_to_neighbor, neighbor) But I've been doing some reading regarding the algorithm, and a lot of versions I see use decrease-key as opposed to insert. Why is this, and what are the differences between the two approaches? The reason for using decrease-key rather than reinserting nodes is to keep the number of nodes in the priority queue

Sorting PriorityQueue

廉价感情. 提交于 2019-11-28 01:44:46
问题 I am having a problem with PriorityQueues, as I am lead to believe it orders on priority however I am not sure what the priority is (I mean what the value is and where it comes from). A priorityQueue can be made with a comparator in the constructor and I have tried this but it does not work. Queue class: public JavaPriorityFlightQueue() { super(); flights = new PriorityQueue(5, new SortQueueViaPriority()); } Comparator: import java.util.Comparator; public class SortQueueViaPriority implements

Limited concurrency level task scheduler (with task priority) handling wrapped tasks

拟墨画扇 提交于 2019-11-28 00:19:11
I'm having a hard time finding a task scheduler on which I can schedule prioritised tasks but can also handle "wrapped" tasks. It is something like what Task.Run tries to solve, but you cannot specify a task scheduler to Task.Run . I have been using a QueuedTaskScheduler from the Parallel Extensions Extras Samples to solve the task priority requirement (also suggested by this post ). Here is my example: class Program { private static QueuedTaskScheduler queueScheduler = new QueuedTaskScheduler(targetScheduler: TaskScheduler.Default, maxConcurrencyLevel: 1); private static TaskScheduler ts

Priority Queue remove complexity time

北城以北 提交于 2019-11-27 22:58:23
问题 What is the complexity (big-oh) for the remove() function on the Priority Queue class in Java? I can't find anything documented anywhere, I think it's O(n), considering you have to find the element before you remove it and then reshuffle the tree. but I've seen others that disagree and think it's O(logn). Any ideas? 回答1: The confusion is actually caused by your "remove" function. In java, there are two remove functions. remove() -> This is to remove the head/root, it takes O(logN) time.

Is using std::deque or std::priority_queue thread-safe? [duplicate]

强颜欢笑 提交于 2019-11-27 22:23:41
Possible Duplicates: Is the C++ STL std::set thread-safe? Thread safety for STL queue I'm guessing it isn't, I just want to make sure. meaning 2 threads using the same std::deque using std::deque::push_back or push_front at the same time. Same question goes for std::priority_queue and the functions std::priority_queue::push and std::priority_queue::pop .. Are those containers thread-safe? Or I should personally program it to be thread-safe? Tnx a lot. From Scott Myer's Effective STL Item 12. Have realistic expectations about the thread safety of STL containers Multiple readers are safe.

The reason of using `std::greater` for creating min heap via `priority_queue`

百般思念 提交于 2019-11-27 22:12:55
I am wondering why for creating a min heap using the priority_queue , the std::greater should be used? std::priority_queue<T, std::vector<T>, std::greater<T> > min_heap; To me, since the smallest value is always located at the top of the heap, the employed class should be std::less Update: On the other hand, since the default behavior of priority_queue (max heap) is to hold the greatest value at the top, it looks to me that the std::greater should be used for the max heap creation and not for min heap creation TemplateRex The logical argument is as follows std::priority_queue is a container

How to implement Priority Queues in Python?

牧云@^-^@ 提交于 2019-11-27 20:32:29
问题 Sorry for such a silly question but Python docs are confusing.. . Link 1: Queue Implementation http://docs.python.org/library/queue.html It says thats Queue has a contruct for priority queue. But I could not find how to implement it. class Queue.PriorityQueue(maxsize=0) Link 2: Heap Implementation http://docs.python.org/library/heapq.html Here they says that we can implement priority queues indirectly using heapq pq = [] # list of entries arranged in a heap entry_finder = {} # mapping of

PriorityQueue/Heap Update

陌路散爱 提交于 2019-11-27 20:24:12
Does Java have an easy way to reevaluate a heap once the priority of an object in a PriorityQueue has changed? I can't find any sign of it in Javadoc , but there has to be a way to do it somehow, right? I'm currently removing the object then re-adding it but that's obviously slower than running update on the heap. You might need to implement such a heap yourself. You need to have some handle to the position of the item in the heap, and some methods to push the item up or down when its priority has changed. Some years ago I wrote such a heap as part of a school work. Pushing an item up or down

Making a Java PriorityQueue into a stable priority queue

故事扮演 提交于 2019-11-27 19:25:11
问题 I'm trying to implement a stable (first in first out) priority queue in Java. Supposing that the key is a name and the value is an age, I know I can make an unstable priority queue like this: Queue<Map.Entry<String, Integer>> pq = new PriorityQueue<Map.Entry<String, Integer>>(100, ageComparator); This does pretty much everything that I need it to, except that it doesn't maintain order of key-value pairs as I insert them (or remove them). I've found a "work around" by making a LinkedList,

PriorityQueue has objects with the same priority

∥☆過路亽.° 提交于 2019-11-27 18:13:20
问题 I'm using a priority queue to sort and use a large number of custom objects. The objects have a "weight" that is their natural ordering. However, different objects that are inserted into the priority queue may have the same "weight". In such cases, I want the priority queue to order them in the same order in which they were put into the queue. For example, if I add in CustomObjects A,B,C,D in that order, all with the same "weight", than the priority queue should return them in that order as