priority-queue

What is the time complexity of constructing a PriorityQueue from a collection?

情到浓时终转凉″ 提交于 2019-12-18 06:01:26
问题 What is the complexity of Java's PriorityQueue constructor with a Collection ? I used the constructor: PriorityQueue(Collection<? extends E> c) Is the complexity O(n) or O(n*log(n))? 回答1: The time complexity to initialize a PriorityQueue from a collection, even an unsorted one, is O(n). Internally this uses a procedure called siftDown() to "heapify" an array in-place. (This is also called pushdown in the literature.) This is counterintuitive. It seems like inserting an element into a heap is

Testing PriorityBlockingQueue in ThreadPoolExecutor

喜夏-厌秋 提交于 2019-12-18 04:23:11
问题 I realized my ThreadPoolExecutor with PriorityBlockingQueue like in this example: https://stackoverflow.com/a/12722648/2206775 and wrote a test: PriorityExecutor executorService = (PriorityExecutor) PriorityExecutor.newFixedThreadPool(16); executorService.submit(new Runnable() { @Override public void run() { try { Thread.sleep(1000); Thread.sleep(1000); System.out.println("1"); } catch (InterruptedException e) { e.printStackTrace(); } } }, 1); executorService.submit(new Runnable() { @Override

How to tell a std::priority_queue to refresh its ordering?

試著忘記壹切 提交于 2019-12-18 03:17:05
问题 I have a priority queue of pointers to a struct city . I modify the objects pointed by these pointers outside the priority queue, and want to tell the priority queue to "reorder" itself according to the new values. What should I do? Example: #include <iostream> #include <queue> using namespace std; struct city { int data; city *previous; }; struct Compare { bool operator() ( city *lhs, city *rhs ) { return ( ( lhs -> data ) >= ( rhs -> data ) ); } }; typedef priority_queue< city *, vector<

What is the difference between binary heaps and binomial heaps?

我的梦境 提交于 2019-12-17 23:30:08
问题 I need to know the main difference between binary and binomial heaps regardless of the their structure difference that binary heaps can have only two child (tree representation) and binomial heaps can have any number of children. I am actually just wondering that what so special in organizing the binomial tree structure in such a way that the first child have on one node second have two third have four and so on? What if, if we use some normal tree for heaps without restriction of two child

Priority queue with dynamic item priorities

馋奶兔 提交于 2019-12-17 19:33:46
问题 I need to implement a priority queue where the priority of an item in the queue can change and the queue adjusts itself so that items are always removed in the correct order. I have some ideas of how I could implement this but I'm sure this is quite a common data structure so I'm hoping I can use an implementation by someone smarter than me as a base. Can anyone tell me the name of this type of priority queue so I know what to search for or, even better, point me to an implementation? 回答1: I

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

南笙酒味 提交于 2019-12-17 16:19:43
问题 This question already has answers here : Closed 9 years ago . 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

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

不羁的心 提交于 2019-12-17 09:41:11
问题 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

Working out the SQL to query a priority queue table

痴心易碎 提交于 2019-12-14 00:17:12
问题 I am implementing a small queue to handle which process gets to run first. I am using a table in a database to do this. Here is the structure of the table (I'm mocking it up in SQLite): "id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , "identifier" VARCHAR NOT NULL , "priority_number" INTEGER DEFAULT 15, "timestamp" DATETIME DEFAULT CURRENT_TIMESTAMP, "description" VARCHAR I am trying to write SQL to give me the row of which process can run next. Here is some sample data: id identifier

C++ declare a priority queue in an else/if?

让人想犯罪 __ 提交于 2019-12-13 18:06:10
问题 I have two different priority queues that use different comparators. I would like an if/else with a boolean value to determine which p.q. will be set to a variable (for example, "pq"); For example I have priority_queue<test, vector<test>, CompareTest1> pq; How would I place this in an if/else so if a boolean is flagged pq will be set like... priority_queue<test, vector<test>, CompareTest2> pq; using the different comparator. Thanks. 回答1: This cannot be done because if you give a different

How to have an unbound sortable queue utilized by a fixed amount of threads?

走远了吗. 提交于 2019-12-13 16:05:56
问题 Please assume the following: public static void main(String[] args) { ThreadPoolExecutor pool = new ThreadPoolExecutor(0, 5, 1L, TimeUnit.SECONDS, new PriorityBlockingQueue<Runnable>()); DashboardHtmlExport d = new DashboardHtmlExport(); for (int i = 0; i < 40; i++) { System.out.println("Submitting: " + i); try { Thread.sleep(cooldown); } catch (InterruptedException e) { e.printStackTrace(); } pool.submit(d.new A(i)); } } private class A implements Runnable, Comparable<A> { private int order;