priority queue

[b]priority queue[/b]

priority queue , 基于 heap,封装了一些操作而实现 优先级队列 的功能,

------
[b]分类:[/b]

* max-queue:
对应 max-heap,
用于取出最大的值,
应用:
任务队列 (高优先级先执行)

* min-queue
对应 min-heap,
用于取出最小的值,
应用:
事件触发队列 (触发时间小的先执行)

------
[b]priority queue 的 operation[/b]

[b]max-queue:[/b]
* INSERT(S, x)
将 x 放入 set S 中,
可记为: S ← S ∪ {x}
* MAXIMUM(S)
返回 set S 中 有最大 key 的 元素
* EXTRACT-MAX(S)
删除 并 返回 set S 中 有最大 key 的 元素
* INCREASE-KEY(S, x, k)
增大 元素 x 的 key 到 k,(k >= x'key),
相当于提高优先级
*

[b]min-queue:[/b]
* INSERT(S, x)
将 x 放入 set S 中,
可记为: S ← S ∪ {x}
* MINIMUM(S)
返回 set S 中 有最小 key 的 元素
* EXTRACT-MIN(S)
删除 并 返回 set S 中 有最小 key 的 元素
* DECREASE-KEY(S, x, k)
减小 元素 x 的 key 到 k,(k <= x'key),
相当于提前
*

------
[b]max-queue 的 实现[/b]

* HEAP-MAXIMUM(A)
直接取 heap 顶部的元素 即可,
即 return A[1],
时间复杂度:O(1)

* HEAP-EXTRACT-MAX(A)
首先取 heap 顶部元素,然后再 执行一次 max-heap 操作,将剩余的最大值放到 heap 顶部,
时间复杂度:O(lg n)

* HEAP-INCREASE-KEY(A, i, key)
将 key 增大,然后向上和 parent note 节点比较,直到找到比自己大的 parent note,
时间复杂度:O(lg n)

* MAX-HEAP-INSERT(A, key)
首先将新 元素的 key 设为 -∞,放在 heap 的最后,
然后用调用 1次 HEAP-INCREASE-KEY(A, i, key),即可 将 key 设置到希望的值,
时间复杂度:O(lg n)
*

总结:
max-queue 的任意操作,都可以用一组 O(lg n) 操作实现,

------
[b]min-queue 的实现[/b]

类似 max-queue

------
[b]例子:[/b]

* [b]js[/b]

var arr_max_priority_queue = [ 78, 13, 6, 177, 26, 90, 288, 45, 62, 83 ];

// 依赖于前面的 heap_sort.js 中,HeapSort 类

/**
* max-priority-queue
* 用于取出最大的值,如 任务队列
* 算法复杂度:max-queue 的任意基本操作(除初始 建立 max-heap 外),都可以用一组 O(lg n) 操作实现,
*
* @param keyArr
* @return
*/
function MaxPriorityQueue(arr) {
this.arr = arr;
this.heapsort = new HeapSort(arr);
}

/**
* 执行1次 max-heap 操作
*
* @param arr
* @return
*/
MaxPriorityQueue.prototype.buildMaxHeap = function() {
this.heapsort.buildMaxHeap(this.arr);
};

/**
* 执行排序
* @return
*/
MaxPriorityQueue.prototype.sort = function() {
this.arr = this.heapsort.sort().reverse();
};

/**
* 获得 最大优先级的值,即 heap 顶部的元素
*
* @return
*/
MaxPriorityQueue.prototype.maximum = function() {
return arr.length > 0 ? arr[0] : undefined;
};
/**
* 获得 最大优先级的值,并将该值从 queue 中删除,即 heap 顶部的元素
*
* @return
*/
MaxPriorityQueue.prototype.extractMax = function() {
if (arr.length > 0) { // no element
return undefined;
}
var max = this.arr.shift();
this.buildMaxHeap();
return max;

};

/**
* 值增大后,将元素在 heap 中 上移
*
* @param index
* @param value
* @return
*/
MaxPriorityQueue.prototype.increase = function(index, value) {
this.arr[index] = value;
var curIndex = index;
var parentIndex;
while (curIndex != 0) {
parentIndex = this.heapsort.parent(curIndex + 1) - 1;
if (this.arr[curIndex] > this.arr[parentIndex]) {
var tmp = this.arr[curIndex];
this.arr[curIndex] = this.arr[parentIndex];
this.arr[parentIndex] = tmp;
curIndex = parentIndex;
} else { // ok,break while
break;
}
}
};

/**
* 插入1个值,先放在最后,然后调用 increase() 将其放到适当位置
*
* @param element
* @return
*/
MaxPriorityQueue.prototype.insert = function(element) {
this.arr.push(element);
this.increase(this.arr.length - 1, element);
};


* [b]html[/b]

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="js/heap_sort.js"></script>
<script type="text/javascript" src="js/max_priority_queue.js"></script>
</head>
<body>
<input type="button" value="heap sort" onclick="alert(new HeapSort(arr_heap).sort());" />
<input type="button" value="max priority queue" onclick="var mpq = new MaxPriorityQueue(arr_max_priority_queue);mpq.sort();mpq.insert(190);alert(mpq.arr);" />
</body>
</html>


------
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值