简体   繁体   English

提高优先级队列堆中的关键字搜索时间复杂度

[英]Improving Key Search Time Complexity in a Priority Queue Heap

I have a custom Task class which contains a priority value as well as some additional fields, shown below: 我有一个自定义Task类,其中包含一个优先级值以及一些其他字段,如下所示:

class Task{

    int ID;
    int Priority;
    int Time;

    public Task(int i, int p, int t){
        this.ID = i;
        this.Priority = p;
        this.Time = t;
    }

    //Getters, etc
}

These are stored in a max heap by priority, which works fine. 这些按优先级存储在最大堆中,效果很好。 However, if I want to find a Task object with a specific ID value, that has to be done in O(n) time due to the linear search (using a basic array of Tasks as a heap): 但是,如果我想查找具有特定ID值的Task对象,由于进行线性搜索(使用Tasks的基本数组作为堆),因此必须在O(n)时间内完成:

public int getTimeOfID(int ID){            

    for(int i = 1; i < heapSize+1; i++){
        if (heap[i].getTaskID() == taskID){
            return heap[i].getTimeLeft();
        }
    }

    return -1;
}

I've come across several references to a "modified heap" that could be used to improve ID search to O(1) time, but haven't found a concrete implementation example. 我遇到了一些对“修改后的堆”的引用,这些引用可用于将ID搜索提高到O(1)时间,但是没有找到具体的实现示例。 Is this possible to do, and if so, how would I do it? 这可能吗?如果可以,我该怎么做? A Java or pseudcode example would be greatly appreciated, but even just the name of a relevant data structure to begin my search would be helpful. Java或pseudcode示例将不胜感激,但是即使只是开始搜索的相关数据结构的名称也将有所帮助。 Thanks for any assistance. 感谢您的协助。

EDIT: Additional code added as requested: 编辑:根据要求添加了其他代码:

//initial variables

private Task[] heap;
private int heapSize, capacity;
int maxTasksHigh;

//Constructor

public PQ(int maxTasks){        
    this.capacity = maxTasks+1;
    heap = new Task[this.capacity];
    heapSize = 0;
    maxTasksHigh = maxTasks;
}

//Addition

public void add(int ID, int time){        
    Task newTask = new Task(ID, time);
    heapSize++;
    heap[heapSize] = newTask;
    int target = heapSize;

    heap[target] = newTask;
    reorder(target);
}

//etc.

What you can do is add a HashMap to map between an ID and the Task object in the Max Heap. 您可以做的是添加一个HashMap以在ID和最大堆中的Task对象之间进行映射。

Then when adding or removing an item you add or remove it from the HashMap<String, Task> . 然后,在添加或删除项目时,可以从HashMap<String, Task>添加或删除它。 These operations will take O(1) so will not harm the time complexity of the Max Heap. 这些操作将占用O(1)因此不会损害Max Heap的时间复杂度。 By using the HashMap in addition to the Max Heap you can check if a given ID exists and retrieve its item in O(1) . 通过使用最大堆以外的HashMap ,您可以检查给定的ID存在,并在O(1)检索其项。

A word of caution: If you return the reference to the object in the Max Heap through these extra methods an outsider can change the value of the item and therefore break the Max Heap. 请注意:如果通过这些额外方法将对Max Heap中对象的引用返回给外部对象,则局外人可以更改项目的值,从而破坏Max Heap。 Solve it by returning a deep clone of the object or by having your Task immutable. 通过返回对象的深层克隆或使您的Task不可变来解决该Task


Update after adding code: 添加代码后更新:

  • Create a new member of the class of HashMap<String, Task> and initialize it in the constructor. 创建HashMap<String, Task>类的新成员,然后在构造函数中对其进行初始化。
  • In the add method check if a.containsKey() for the given Task . 在add方法中,检查给定Task a.containsKey() If not add it to the Max Heap and to the HashMap . 如果没有,则将其添加到Max Heap和HashMap
  • Update logic of other methods as needed. 根据需要更新其他方法的逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM