priority-queue

Strange Queue.PriorityQueue behaviour with multiprocessing in Python 2.7.6

放肆的年华 提交于 2019-12-04 04:48:44
As you know from the title, I'm trying to use PriorityQueue with multiprocessing. More precisely, I wanted to make shared PriorityQueue, wrote some code and it doesn't run as I expected. Look at the code: import time from multiprocessing import Process, Lock from Queue import PriorityQueue def worker(queue): lock = Lock() with lock: for i in range(100): queue.put(i) print "worker", queue.qsize() pr_queue = PriorityQueue() worker_process = Process(target = worker, args = (pr_queue,)) worker_process.start() time.sleep(5) # nope, race condition, you shall not pass (probably) print "main", pr

Data structure that always keeps n-best elements

爷,独闯天下 提交于 2019-12-04 03:11:50
I need a data structure that always holds the n largest items inserted so far (in no particular order). So, if n is 3, we could have the following session where I insert a few numbers and the content of the container changes: [] // now insert 1 [1] // now insert 0 [1,0] // now insert 4 [1,0,4] // now insert 3 [1,4,3] // now insert 0 [1,4,3] // now insert 3 [4,3,3] You get the idea. What's the name of the data structure? What's the best way to implement this? Or is this in some library? I am thinking to use a container that has a priority_queue for its elements (delegation), which uses the

Concurrent mutable priority queue

。_饼干妹妹 提交于 2019-12-04 02:00:16
Is there a concurrent mutable priority queue? Ideally, I'm looking for a C++ implementation, but, for starters, a pointer to an algorithm would be very helpful. To be clear, I'm looking for a priority queue where I can adjust the priorities of the elements. In particular, TBB's concurrent_priority_queue doesn't provide the necessary functionality. (For that matter, neither does STL's priority_queue , even if we ignore the concurrency.) Boost.Heap library provides serial functionality that I want, but without concurrency. Naturally, I'm looking for something finer grained than just locking the

Does changing a priority queue element result in resorting the queue?

依然范特西╮ 提交于 2019-12-03 20:09:48
问题 I have a priority_queue, and I want to modify some of it's contents (the priority value), will the queue be resorted then? It depends if it resorts on push/pop (more probable, becouse you just need to "insert", not resort whole), or when accessing top or pop. I really want to change some elements in the queue. Something like that: priority_queue<int> q; int a=2,b=3,c=5; int *ca=&a, *cb=&b, cc=&c; q.push(a); q.push(b); q.push(c); //q is now {2,3,5} *ca=4; //what happens to q? // 1) {3,4,5} //

Working out the SQL to query a priority queue table

本秂侑毒 提交于 2019-12-03 16:11:35
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 priority_number timestamp description 1 test1 15 2009-01-20 17:14:49 NULL 2 test2 15 2009-01-20 17:14:56 NULL

How to update elements within a heap? (priority queue)

末鹿安然 提交于 2019-12-03 12:37:34
When using a min/max-heap algorithm, priorities may change. One way to handle this is to removal and insert the element to update the queue order. For priority queues implemented using arrays, this can be a performance bottleneck that seems avoidable, especially for cases when the change to priority is small. Even if this is not a standard operation for a priority queue , this is a custom implementation which can be modified for my needs. Are there well known best-practice methods for updating elements in the min/max-heap? Background Info: I'm no expert in binary-trees, I inherited some code

Valgrind: invalid read of size 4 -> sigsegv, works fine without valgrind and in visual studio

旧城冷巷雨未停 提交于 2019-12-03 11:03:21
I have implemented a compression algorithm (using huffman coding) which uses a priority queue of nodes (a struct i defined). Now, when i just run the code in linux or in visual studio, everything works fine. When I check for memory leaks in visual studio, none are given. The problem now is, when I use valgrind to analyse my program, it terminates with signal 11 (sigsegv). The first error encountered is an 'invalid read of size 4' in the method delete min. Other errors after that are: Adress is 0 bytes inside a block of size 453 freed, invalid write of size 4, invalid free,delete or realloc.

How to use priority queues in Scala?

梦想与她 提交于 2019-12-03 11:02:22
I am trying to implement A* search in Scala (version 2.10), but I've ran into a brick wall - I can't figure out how to use Scala's Priority Queue. It seems like a simple task, but searching on Google didn't turn up anything (except for a single code sample that stopped working back in version 2.8) I have a set of squares, represented by (Int, Int) s, and I need to insert them with priorities represented by Int s. In Python it's pretty simple, since you just have a list of key, value pairs and use the heapq functions to sort it. But it appears that Scala's tuples aren't even comparable. So how

initialization for STL priority queue

大憨熊 提交于 2019-12-03 09:41:37
I'm still confused about priority queue in STL. Here is the objective I wanna achieve, say: I have a structure called Record, which contains a string word and a int counter. For example: I have many records of these (in the sample program, only 5), now I want to keep top N records(in sample, 3). I know now that I could overload operator < in Record, and put all records in a vector, and then initialize the priority_queue like: priority_queue< Record, vector<Record>, less<Record> > myQ (myVec.begin(),myVec.end()); However, as I understood, it's not easy to control the size of vector myVec

What would you use the heapq Python module for in real life?

别来无恙 提交于 2019-12-03 09:13:01
问题 After reading Guido's Sorting a million 32-bit integers in 2MB of RAM using Python, I discovered the heapq module, but the concept is pretty abstract to me. One reason is that I don't understand the concept of a heap completely, but I do understand how Guido used it. Now, beside his kinda crazy example, what would you use the heapq module for? Must it always be related to sorting or minimum value? Is it only something you use because it's faster than other approaches? Or can you do really