problem about priority-queue-Collection of common programming errors


  • Hanna
    java priority-queue
    This is my first post here so feel free to point me in the right direction regarding formulating a question here.My issue is with the java.util.PriorityQueue.I have a queue that I initialize;myComparable comp = new myComparable();PriorityQueue<someObject> prioritized = new PriorityQueue<someObject>(11, comp);I dont think it matters for the question what is in my queue or how myComparable is implemented. I then get unexpected output:prioritizedObject = prioritized.poll();for(someObjec

  • rolve
    java priority-queue comparator
    Possible Duplicate:Java: strange order of queue made from priority queue I tired to turn a priority queue into a queue by implementing the following comparator:Hack: The QueueComparator makes a PriorityQueue behaves like queue(FIFO) by always return 1 Since the “natural ordering” of a priority queue has the least element at the head and a conventional comparator returns -1 when the first is less than the second, the hacked comparator always return 1 so that the current (last) square will be pla

  • templatetypedef
    java equals priority-queue compareto
    I am trying to use the priority queue, but the remove() is not working: My code:PriorityQueue<OwnClass> pq=new PriorityQueue<OwnClass>(); OwnClass a=new OwnClass(1); OwnClass b=new OwnClass(2); OwnClass c=new OwnClass(3); pq.add(a); pq.add(b); pq.add(c); System.out.println(“head:”+pq.peek()); pq.remove(new OwnClass(1)); System.out.println(pq.peek());And the class implementation:class OwnClass implements Comparable{int x;public OwnClass(int x){this.x=x;}public int compareTo(Object ar

  • Matteo M.
    c++ c++11 std priority-queue stdvector
    I’m sure that the code is pretty self-explicative, so I go straight to the point. Please ask for more details if the code is unclear.Foo.h =====#include <iostream>class Foo { public:virtual ~Foo(){};Foo();Foo(const int b);bool operator<(const Foo&) const;friend std::ostream& operator<<(std::ostream&, const Foo&);int b; };Foo.cpp =======#include “Foo.h”Foo::Foo() { }Foo::Foo(const int b) {this->b = b; }bool Foo::operator<(const Foo& other) const {return b

  • templatetypedef
    language-agnostic data-structures priority-queue
    I recently heard about a type of priority queue called a “pagoda” that allegedly has excellent runtime guarantees. In fact, some of the references I’ve found on it have suggested that it’s one of the fastest priority queue implementations available. Surprisingly, though, I can’t seem to find a single good resource on this data structure anywhere on Google or Bing.Does anyone know of any good resources (analysis, source code, etc.) on this data structure?Thanks so much!

  • d33tah
    c++ segmentation-fault priority-queue a-star
    I’m trying to implement a 4×4 sliding-block puzzle solver that utilizes the A* algorithm. Even though was trying to code in as neat a way as possible, a segfault slipped into the code that points to a failed attempt to push a pointer to an std::priority_queue instance. Here’s the full code followed by my debugging attempts, I had no idea how to cut it into a working test case:#include <iostream> #include <iomanip>#include <cstring> #include <cmath>#include <vector>

  • hammar
    java heap priority-queue
    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.

  • svick
    c# multithreading task-parallel-library priority-queue producer-consumer
    Is there any prior work of adding tasks to the TPL runtime with a varying priority?If not, generally speaking, how would I implement this?Ideally I plan on using the producer-consumer pattern to add “todo” work to the TPL. There may be times where I discover that a low priority job needs to be upgraded to a high priority job (relative to the others).If anyone has some search keywords I should use when searching for this, please mention them, since I haven’t yet found code that will do what I ne

  • templatetypedef
    algorithm math data-structures priority-queue fibonacci-heap
    In the decrease-key operation of a Fibonacci Heap, if it is allowed to lose s > 1 children before cutting a node and melding it to the root list (promote the node), does this alter the overall runtime complexity? I think there are no changes in the complexity since the change in potential will be the same. But I am not sure if I am right.And how can this be proved by the amortized analysis?

  • templatetypedef
    algorithm data-structures priority-queue graph-algorithm dijkstra
    Dijkstra’s algorithm was taught to me was as followswhile pqueue is not empty:distance, node = pqueue.delete_min()if node has been visited:breakelse:mark node as visitedif node == target:breakfor 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?

  • vikky.rk
    java generics collections priority-queue
    Why is PriorityQueue in Java defined as, PriorityQueue<T>and not as,PriorityQueue<T extends Comparable<? super T>It rather gives a ClassCastException at runtime if do not queue objects of type Comparable. (and if I am not using a custom Comparator).Why not catch it at compile time?

  • Christian
    c++ boost stl bind priority-queue
    I want to have a priority queue with custom ordering, but lazy as I am, I don’t want to define a comparator class implementing operator().I really would like something like this to compile:std::priority_queue<int, std::vector<int>, boost::bind(some_function, _1, _2, obj1, obj2)> queue;where some_function is a bool returning function taking four arguments, the first and second being ints of the queue, and the two last ones some objects needed for calculating the ordering (const refere

  • Anshu Kandhari
    priority-queue dijkstra
    I have tried using Djikstra’s Algorithm on Cyclic weighted graph without using Priority queue (Heap) and it worked.Then I searched google that “why the hell do we need a priority queue to implement this??” As a result of the search I went through Wikipedia where I got to know that the original implementation does not uses Priority queue and runs in O(|V|2) i.e V square time .now if we just remove priority queue and use normal queue the running time is linear i.e. O(V+E). Please someone suggest

  • sleekFish
    c++ segmentation-fault heap priority-queue
    Given below is a driver program I have written for using my own implementation of a heap. #include<iostream> #include”Heap.h”int main(){Heap h(30);h.insert(1);h.insert(3);h.insert(5);h.insert(6);h.insert(5);h.insert(8);h.display();std::cout<<h.extractMin(); // Statement 1h.display();}This gives the desired result as:========= Heap Contents ========= 1 3 5 6 5 8 1 ========= Heap Contents ========= 3 6 5 8 5However, if I change the statement 1 to:std::cout<&l

  • Rahul
    c priority-queue
    I’ve been trying to implement a priority queue with the help of a linked list. However, I am unable to add data to the list when i use the add() function which i have used in my program below. Some help would be great!The program requires me to sort various data into separate queues..with all elements in the same queue having the same priority.i.e : Data: A, Priority : 1Data: B, Priority : 2Data: C, Priority : 1then it should store the data as follows : Q1 : A,C Q2 : BMy program is as follows. I

  • doelleri
    c++ scope segmentation-fault priority-queue
    I am getting a segmentation fault. I believe it has something to do with how I am doing comparisons. I am stumped and new to C++. The very last line, pq.pop() is the call that enters the stack trace and causes the failure. Nothing is printed out when executing ./a.outclass Node {public:Node() {}Node(int b) {bound = b;}Node(int b, Node * p) {bound = b;parent = p;}void addChild(Node& n) {n.parent = this; }Node * parent;int bound; };class CompareNode {public:bool operator()(Node *n, Node *o)

  • bsg
    c++ stl struct priority-queue bad-alloc
    I am working on a query processor that reads in long lists of document id’s from memory and looks for matching id’s. When it finds one, it creates a DOC struct containing the docid (an int) and the document’s rank (a double) and pushes it on to a priority queue. My problem is that when the word(s) searched for has a long list, when I try to push the DOC on to the queue, I get the following exception: Unhandled exception at 0x7c812afb in QueryProcessor.exe: Microsoft C++ exception: std::bad_allo

  • Mat
    c++ function memory priority-queue
    I am having some issues with my code. It seems I cannot access a top of a priority queue only in a specific function.I get this error : Unhandled exception at at 0x77644B32 in ConsoleApplication5.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x00D7E6A8.Let me be a little more clear with an example: if i do a cout << *nameofmyqueue->top() ; in a function called firstFunction it’ll work perfectly. but if i use it in otherFunction, i’ll get an access memory error. Here is

  • bsg
    c++ stl priority-queue bad-alloc
    I am writing a query processor which allocates large amounts of memory and tries to find matching documents. Whenever I find a match, I create a structure to hold two variables describing the document and add it to a priority queue. Since there is no way of knowing how many times I will do this, I tried creating my structs dynamically using new. When I pop a struct off the priority queue, the queue (STL priority queue implementation) is supposed to call the object’s destructor. My struct code ha

  • Chap
    java priority-queue fifo
    I’m trying to create a Priority Blocking Queue in Java that maintains FIFO order for elements with the same priority. The Oracle doc provides some help with that, but I’m still very tangled up. I should note that the following topics are all very new to me: Generics, Interfaces as Types, and static nested classes. All of these come into play in the following class definition. Generics, especially, are confusing, and I’m sure I’ve totally messed up with them here.I have included comments to i

  • Ella Shar
    c++ class priority-queue
    I am trying to write a class and I finally got it to compile, but visual studio still shows there are errors (with a red line).The problem is at (I wrote @problem here@ around the places where visual studio draws a red line):1. const priority_queue<int,vector<int>,greater<int> @>@ * CM::getHeavyHitters() { 2. return & @heavyHitters@ ; 3. }And it says:”Error: expected an identifier” (at the first line) “Error: identifier “heavyHitters” is undefined” (at the second line)T

  • kshen
    java heap priority-queue compareto
    I’m trying to implement a heap using a PriorityQueue as follows:PriorityQueue<Node> heap = new PriorityQueue<Node>(); Set<String> allWords = codebook.getAllWords(); for(String word : allWords) {heap.add(new Node(word, codebook.getProbability(word)));System.out.println(heap.toString()); }Where I’ve defined Node as as private class inside the same class that holds the above method. Node is defined as:private static class Node implements Comparable {protected Node left;protected N

  • Jun
    c++ stl priority-queue
    I have below code trying to dump the values stored in a priority_queue in C++:priority_queue<Point, vector<Point>, myCmp> pq; int i; for (i = 0; i < 10; i++) {Point p(i,i);pq.push(p); } const Point& p0 = pq.top(); while(!pq.empty()) {cout<<p0.x<<p0.y<<endl;pq.pop(); }//I’m getting output like “00 11 22 33 … 99″As the comment in the code said, my reference variable p0 is keep changing every time the queue pops a value. That does not make sense to me, because

  • rippeltippel
    java android priority-queue
    I need to update some fixed-priority elements in a PriorityQueue based on their ID. I think it’s quite a common scenario, here’s an example snippet (Android 2.2):for (Entry e : mEntries) {if (e.getId().equals(someId)) {e.setData(newData);} }I’ve then made Entry “immutable” (no setter methods) so that a new Entry instance is created and returned by setData(). I modified my method into this:for (Entry e : mEntries) {if (e.getId().equals(someId)) {Entry newEntry = e.setData(newData);mEntries.remove

  • hash
    java generics priority-queue sortedlist
    I am implementing a sorted list using linked lists. My node class looks like thispublic class Node<E>{E elem;Node<E> next, previous; }In the sorted list class I have the add method, where I need to compare generic objects based on their implementation of compareTo() methods, but I get this syntax error “The method compareTo(E) is undefined for type E”. I have tried implemnting the compareTo method in Node, but then I can’t call any of object’s methods, because E is generic type. Her

  • J. Frankenstein
    javascript priority-queue google-closure google-closure-library
    I’ve created a unique priority queue with an enqueue method like this:huzz.ak.UniquePriorityQueue.prototype.enqueue =function(priority, value) {var node = {‘valid’: true, ‘value’: value, ‘priority’: priority};var key = value.key;if (this.pointers_[key] !== undefined) {this.pointers_[key].valid = false;}this.pointers_[key] = node;this.priorityQueue_.enqueue(priority, node); };When I output the values they come out in a random order:while (true) {p = this.priorityQueue_.dequeue();this.logger_.log(

  • hammar
    c++ stl priority-queue
    If I have a STL priority_queue of structs, where priority is based on some attribute of the struct, and I change the attribute of one of the structs, such that the new order would be different, would the priority queue know to resort itself? Or would I have to remove it from the queue and push it in again? I read somewhere that the sorting is done when push() and pop() are called, but I’d like to make sure.

  • polerto
    c++ c++11 const priority-queue pop
    std::priority_queue::top returns a constant value. However, I would like to remove the top element from the priority queue and be able to modify it somewhere else.priority_queue<SomeClass, vector<SomeClass>, SomeClassCompare > pQueue; … SomeClass *toBeModified = &(pQueue.top()); pQueue.pop(); toBeModified->setMember(3); // I would like to do thisIs there a way I can grab the top element from a priority queue (and remove from the queue) and modify it as I wish?

  • Matti Groot
    c++ queue heap priority-queue heap-corruption
    As the title already says, I have a problem with a heap corruption in my C++ code. I know there are a lot of topics that cover heap corruption problems, and I have visited a lot of them, I read up on a lot of sites about these matters and I’ve even used Visual Leak Detector to find the location of the memory leak. I still can’t seem to figure out why I have a heap corruption.My code:#include <iostream> #include “stdafx.h” #include “cstdlib” #include <vld.h> #include <math.h>us

  • Neil G
    c++ stl boost heap priority-queue
    I have a priority queue of events, but sometimes the event priorities change, so I’d like to maintain iterators from the event requesters into the heap. If the priority changes, I’d like the heap to be adjusted in log(n) time. I will always have exactly one iterator pointing to each element in the heap.

Web site is in building