我的优先级队列有问题:
std::priority_queue <NodePrio, std::vector<NodePrio>, sortNodesByPrio> PQ;
Run Code Online (Sandbox Code Playgroud)
哪里
struct NodePrio
{
Node *node;
double priority;
NodePrio() : node(NULL), priority(0) {}
NodePrio(Node *node_, double priority_) : node(node_), priority(priority_) {}
};
Run Code Online (Sandbox Code Playgroud)
和
class sortNodesByPrio
{
public:
bool operator () (const NodePrio &n1, const NodePrio &n2) const;
}
bool sortNodesByPrio::operator () (const NodePrio &n1, const NodePrio &n2) const
{
return n1.priority < n2.priority;
}
Run Code Online (Sandbox Code Playgroud)
经过反复推动新元素
PQ.push(NodePrio(node, distance));
Run Code Online (Sandbox Code Playgroud)
从任何时间点他们都没有排序(见下文)...我试图调试代码,比较器代码已反复执行...
Step1:
push (node, 55.33);
PQ:
[0] 55.33
Step2:
push (node, 105.91);
PQ:
[0] 105.91
[1] 55.33 …Run Code Online (Sandbox Code Playgroud) 我们需要一个容器,在附加一个新元素时,按元素的优先级对其元素进行排序,该元素能够在给定id的情况下检索元素.
(优先级队列的问题是它不能让你根据id而不是优先级检索元素)
谢谢
我想在我的优先级队列中找到一个节点,但我找不到解决方案:(如果你有解决方案,我很感兴趣.
谢谢你的帮助.
我正在研究路径查找算法,并希望实现优先级队列来加速它.
我正在Node根据属性将我的Object 添加到队列中fScore.最小的fScore总是添加到队列的顶部.
我有什么选择?最好使用stl实现c ++优先级队列吗?如果是这样,我将如何设置它以接收我的objective-c对象Node以及如何指定列表的订单(Node.fScore).
谢谢
我已经在https://docs.python.org/2/library/heapq.html#priority-queue-implementation-notes中给出的Priority Queue Implementation的帮助下在python中实现了LFU缓存
我在帖子末尾给出了代码。
但是我觉得代码有一些严重的问题:
1.给一个场景,假设只有一个页面被连续访问(比如说50次)。但是此代码将始终将已添加的节点标记为“已删除”,并将其再次添加到堆中。因此,基本上,同一页面将有50个不同的节点。因此,极大地增加了堆大小。
2.这个问题几乎与http://www.geeksforgeeks.org/flipkart-interview-set-2-sde-2/的电话采访Q1相似,
并且该人提到双向链接列表与堆。谁能解释我,如何?
from llist import dllist
import sys
from heapq import heappush, heappop
class LFUCache:
heap = []
cache_map = {}
REMOVED = "<removed-task>"
def __init__(self, cache_size):
self.cache_size = cache_size
def get_page_content(self, page_no):
if self.cache_map.has_key(page_no):
self.update_frequency_of_page_in_cache(page_no)
else:
self.add_page_in_cache(page_no)
return self.cache_map[page_no][2]
def add_page_in_cache(self, page_no):
if (len(self.cache_map) == self.cache_size):
self.delete_page_from_cache()
heap_node = [1, page_no, "content of page " + str(page_no)]
heappush(self.heap, heap_node)
self.cache_map[page_no] = heap_node
def delete_page_from_cache(self):
while self.heap:
count, page_no, page_content …Run Code Online (Sandbox Code Playgroud) 我正在使用PriorityQueue结构来获取用户设置的一些字段,这是代码的一部分:
package gqg;
import java.util.Queue;
public class Student {
//variables (ID, Name, ...), constructors, getters and setters...
Queue<Student> StudentQueue = new PriorityQueue<Student>();
public void Add() { //method to add the student at Queue
for(int x=0; x<1; x++) {
Student st = new Student();
System.out.println("+---------------------------+\n"
+ "| Students Registration |\n"
+ "+---------------------------+");
System.out.println("| Type the student's ID:");
stu.setID(user.nextInt());
System.out.println("| Type the student's name:");
stu.setName(user.next());
System.out.println("| Type the student's age:");
stu.setAge(user.nextInt());
//and other fields...
StudentQueue.add(st);
}
System.out.println("Done. Student has been added successfuly\n"); …Run Code Online (Sandbox Code Playgroud) 每次要为priority_queue使用自定义比较函数时,都必须将容器传递给它.在我看来,你应该总是传递vector<T>给它.现在起初我认为这是某种冗余,但事实并非如此.将容器传递给a priority_queue有什么用?我该如何使用它?
我想知道是否有推荐的方法来检索https://github.com/yigit/android-priority-jobqueue中的活动作业列表
这样,如果我有持续的工作仍在等待,我可以让用户知道哪些工作.
java android priority-queue job-queue android-priority-jobqueue
我正在尝试使用队列类中的PriorityQueue.但是,我在将自定义对象放入PQ时遇到问题.我已经实现了__cmp__以下功能:
def __cmp__(self, other):
return (self.priority > other.priority) - (self.priority < other.priority)
Run Code Online (Sandbox Code Playgroud)
我希望PriorityQueue按优先级字段排序,在init函数中指定:
def __init__(self, board, priority=0):
self.priority = priority
# Other logic
Run Code Online (Sandbox Code Playgroud)
但是,当我运行代码将一个State对象插入PQ时,我收到此错误: TypeError: '<' not supported between instances of 'State' and 'State'
这是运行PQ的代码.
if op.precond(S):
new_state = op.state_transf(S)
if not (OPEN.queue.__contains__(new_state)) and not (new_state in CLOSED):
GVALUES[Problem.hash(new_state)] = get_distance_value(op, new_state)
HEUR_VALUES[Problem.hash(new_state)] = get_AStar_value(new_state)
print("NEW STATE: " + str(new_state))
OPEN.put(new_state)
print("OPEN: " + str(OPEN.queue))
Run Code Online (Sandbox Code Playgroud)
其中OPEN是priorityQueue.
任何帮助都将非常感谢...因为将值插入PQ应该非常简单.
priority-queue ×10
c++ ×5
heap ×2
java ×2
python ×2
android ×1
caching ×1
containers ×1
ios ×1
job-queue ×1
objective-c ×1
python-3.x ×1
queue ×1
sorting ×1
stl ×1