priority-queue

Java PriorityQueue with fixed size

被刻印的时光 ゝ 提交于 2019-12-28 03:45:10
问题 I am calculating a large number of possible resulting combinations of an algortihm. To sort this combinations I rate them with a double value und store them in PriorityQueue. Currently, there are about 200k items in that queue which is pretty much memory intesive. Acutally, I only need lets say the best 1000 or 100 of all items in the list. So I just started to ask myself if there is a way to have a priority queue with a fixed size in Java. I should behave like this: Is the item better than

Efficiency of the STL priority_queue

旧街凉风 提交于 2019-12-28 02:03:52
问题 I have an application (C++) that I think would be well served by an STL priority_queue . The documentation says: Priority_queue is a container adaptor, meaning that it is implemented on top of some underlying container type. By default that underlying type is vector, but a different type may be selected explicitly. and Priority queues are a standard concept, and can be implemented in many different ways; this implementation uses heaps. I had previously assumed that top() is O(1) , and that

C++ priority queue in ascending order by specific method for objects

核能气质少年 提交于 2019-12-25 18:17:41
问题 Lets say I have the a class called MyClass and every MyClass object has a method called xVal . What I want is a priority queue of MyClass objects sorted in ascending order of MyClass.xVal() So far I have this: priority_queue<MyClass, vector<MyClass>, greater<MyClass>> queue; Of course, it doesn't do what I expect.I complies but uses some random ordering for my objects. Would appreciate it if someone can point out what I am doing wrong. Thanks. 回答1: The CPP Reference link to Priority Queue

Why do the values inside the PriorityQueue change after the poll method in java? [duplicate]

白昼怎懂夜的黑 提交于 2019-12-25 16:32:49
问题 This question already has answers here : Java: PriorityQueue returning incorrect ordering from custom comparator? [duplicate] (2 answers) Closed 2 years ago . This is the code and the output is below it why do the value "5" and "6" i mean how are the new priorities set after poll method in a PriorityQueue (similarly the other elements in the queue) . I am preparing for java certification exam and i always tend to choose the wrong answer due to this concept, any help is welcomed. import java

Why do the values inside the PriorityQueue change after the poll method in java? [duplicate]

柔情痞子 提交于 2019-12-25 16:32:37
问题 This question already has answers here : Java: PriorityQueue returning incorrect ordering from custom comparator? [duplicate] (2 answers) Closed 2 years ago . This is the code and the output is below it why do the value "5" and "6" i mean how are the new priorities set after poll method in a PriorityQueue (similarly the other elements in the queue) . I am preparing for java certification exam and i always tend to choose the wrong answer due to this concept, any help is welcomed. import java

Why do the values inside the PriorityQueue change after the poll method in java? [duplicate]

不羁的心 提交于 2019-12-25 16:32:08
问题 This question already has answers here : Java: PriorityQueue returning incorrect ordering from custom comparator? [duplicate] (2 answers) Closed 2 years ago . This is the code and the output is below it why do the value "5" and "6" i mean how are the new priorities set after poll method in a PriorityQueue (similarly the other elements in the queue) . I am preparing for java certification exam and i always tend to choose the wrong answer due to this concept, any help is welcomed. import java

How to decrease key for a particular edge in a priority_queue<PI, vector<PI> ,greater<PI> >,trying to implement prim's algorithm?

送分小仙女□ 提交于 2019-12-25 07:18:58
问题 #include <bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define LL long long int #define pb push_back #define mp make_pair #define PI pair<int,int> #define PL pair<LL,LL> #define PIS pair< int,string> #define test int t;cin>>t;while(t--) #define ff first #define ss second #define INF 1000000000 #define input(a,n) for(i=1;i<=n;i++)cin>>a[i]; #define output(a,n) for(i=1;i<=n;i++)cout<<a[i]<<" "; vector< vector<LL> >v(3002, vector<LL>(3002,

Trying to sort a priority queue by one of the stored nodes' values

随声附和 提交于 2019-12-25 03:25:00
问题 So I'm new to priority queues. In an algorithm I'm trying to implement, I want to sort a priority queue according to the stored nodes' functionValue. I don't understand how the priority queue will know to sort the nodes by that value, as opposed to one of the other eight instance variables of my node objects. I'm pretty sure I define a Comparator object to define the comparison/ sorting rules, but I can't make heads or tails of the Oracle class library for Comparator. Here are the properties

python Priority Queue implementation

大憨熊 提交于 2019-12-25 02:12:19
问题 i'm having trouble creating an insert function with the following parameters. The insert function should take in a priority queue, and an element and inserts it using the priority rules - The priority queue will take a series of tasks and order them based on their importance. Each task has an integer priority from 10 (highest priority) to 1 (lowest priority). If two tasks have the same priority, the order should be based on the order they were inserted into the priority queue (earlier first).

How to find if a HeapQueue contains a value in Java?

时光总嘲笑我的痴心妄想 提交于 2019-12-24 19:41:05
问题 I am having trouble writing a method with finding if a MaxHeapPriorityQueue contains a value. The instructions read: The contains(E) method should return true if the given value is found in the queue. It should use its private helper method to search the queue recursively. Here is what I have so far public class MaxHeapPriorityQueue<E extends Comparable<E>> { private E[] elementData; private int size; @SuppressWarnings("unchecked") public MaxHeapPriorityQueue() { elementData = (E[]) new