- 浏览: 2551264 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
Interview(6)Priority Queue
Priority Queue
Key and Value => Entry
public interface Entry{
public Object getKey(); //get the key values
public Object setKey(Object k);
public object getValue();
public Object setValue((Object v);
}
public class EntryDefault implements Entry {
protected Object key;
protected Object value;
public EntryDefault(Object k, Object v){
key = k; value = v;
}
public Object getKey() { return key; }
public Object setKey(Object k) {
Object oldK = key;
key = k;
return oldK;
}
public Object getValue() { return value; }
public Object setValue(Object v) {
Object oldV = value; value= v; return oldV;
}
}
Comparator Interface and Implementation
public interface Comparator {
public int compare(Object a, Object b);
}
compare method will return positive, 0 or negative.
Priority Queue Interface
public interface PriorityQueue {
public int getSize();
public boolean isEmpty();
public Entry getMin() throws ExceptionPriorityQueueEmpty;
public Entry insert(Object key, Object obj) throw ExceptionKeyInvalid;
public Entry delMin() throws ExceptionPriorityQueueEmpty;
}
Implementation on top Un Sorted List
public class PriorityQueueUnsortedList implements PriorityQueue {
private List list;
private Comparator comparator;
public PriorityQueuUnsortedList(){
this(new ComparatorDefault(), null);
}
public PriorityQueueUnsortedList(Comparator comparor){ this(c, null); }
public PriorityQueueUnsortedList(Sequeue s){ this(new ComparatorDefault(), s); }
public PriorityQueueUnsortedList(Comparator comparator, Sequence sequence){
list = new ListDLNode();
comparator = comparator;
if(null != sequence){
while(!s.isEmpty()){
Entry e = (Entry)s.removeFirst();
insert(e.getKey, e.getValue());
}
}
}
public int getSize() { return list.getSize();}
public boolean isEmpty(){ return list.isEmpty(); }
public Entry getMin() throws ExceptionPriorityQueueEmpty {
if(list.isEmtpy()){
throw new ExceptionPriorityQueueEmpty(“Error, queue is empty”);
}
Position minPos = list.first();
Position curPos = list.getNext(minPos);
while(null != curPos){
if(0 < comparator.compare(minPos.getElem(), curPos.getElem())){
minPos = curPos; //find the min one
}
}
return (Entry) minPos.getElem();
}
public Entry insert(Object key, Object obj) throws ExceptionKeyInvalid {
Entry entry = new EntryDefault(key, obj);
list.insertLast(entry); //put the element at the tail
return (entry);
}
public Entry delMin() throws ExceptionPriorityQueueEmpty {
if(list.isEmepty()){
throw new ExceptionPriorityQueueEmpty(“Error”);
}
Position minPos = list.first();
Iterator it = list.pistions();
while(it.hasNext()){
Position curPos = (Position) (it.getNext());
if( < comprator.compare(
(Entry)(minPos.getElement()).getKey(),
(Entry)(curPos.getElement()).getKey()
)){
minPos = curPos;
}
}
return (Entry)list.remove(minPos);
}
}
It is easy to insert, but it is O(n) to getMin() or delMin()
Implementation on top of Sorted List
public class PriorityQueueSortedList implements PriorityQueue {
private List list;
private Comparator comparator;
public PriorityQueueSortedList(){ this(new ComparatorDefault(), null); }
public PriorityQueueSortedList(Comparator c){ this(c, null); }
public PriorityQueueSortedList(Sequence s){ this(new ComparatorDefault(), s); }
public PriorityQueueSortedList(Comparator comparator, Sequence sequence){
list = new ListDLNode();
comparator = comparator;
if( null != s){
while( !s.isEmpty()) {
Entry e = (Entry) s. removeFirst();
insert(e.getKey(), e.getValue());
}
}
}
public int getSize() { return list.getSize(); }
public boolean isEmpty() { return list.isEmpty(); }
public Entry getMin() throws ExceptionPriorityQueueEmpty {
if(list.isEmpty()){
throw new ExceptionPriorityQueueEmpty(“Error");
}
return (Entry) list.last();
}
public Entry insert(Object key, Object obj) throws ExceptionKeyInvalid {
Entry entry = new EntryDefault(key, obj);
if(list.isEmpty() || (0 > comparator.compare((Entry) (list.first().getElem())).getKey(), entry.getKey()) ){
list.insertFirst(entry); //list is empty or current is largest
}else{
Position curPos = list.last();
while( 0 > comparator.compare((Entry) (curPos.getElem())).getKey(), entry.getKey()){
curPos = list.getPrev(curPos); //move to previous items, find the one bigger than current
}
list.insertAfter(curPos, entry);
}
return (entry);
}
public Entry delMin() throws ExceptionPriorityQueueEmpty {
if(list.isEmpty()){
throw new ExceptionPriorityQueueEmpty(“Error");
}
return (Entry) list.remove(list.last());
}
}
Heap
Heap Structure - Page 173
Binary Tree - Heap - Big Top - Small Top
public class PriorityQueueHeap implements PriorityQueue {
private CompleteBinTree h;
private Comparator comp;
public PriorityQueueHeap(){ this(new ComparatorDefault(), null); }
public PriorityQueueHeap(Comparator c){ this(c, null); }
public PriorityQueueHeap(Sequence s){ this(new ComparatorDefault(), s); }
public PriorityQueueHeap(Comparator c, Sequence s){
comp = c;
h = new CompleteBinTree_Vector(s);
if( !h.isEmpty()){
for(int i = h.getSize()/2-1; i>=0; i—){
precolateDown(h.posOfNode(i));
}
}
}
public int getSize() { return h.getSize(); }
public boolean isEmpty() { return h.isEmpty(); }
public Entry getMin() throws ExceptionPriorityQueueEmpty {
if(isEmpty()) { throw new ExceptionPriorityQueueEmpty(“Error"); }
return (Entry) h.getRoot().getElem();
}
public Entry insert(Object key, Object obj) throws ExceptionKeyInvalid {
checkKey(key);
Entry entry = new EntryDefault(key, obj);
percolateUp(h.addlast(entry);
return entry;
}
public Entry delMin() throws ExceptionPriorityQueueEmpty {
if(isEmpty()) { throw new ExceptionPriorityQueueEmpty(“Error”); }
Entry min = (Entry) h.getRoot().getElem();
if ( 1 == getSize()) {
//if it is last item
h.delLast();
}else{
h.getRoot().setElem((ComleBinaryTreeNodeRank)h.delLast().getElme()); //find the last one and place in root
percolateDown(h.getRoot());
}
return min;
}
protected void checkKey(Object key) throws ExceptionKeyInvalid {
try {
comp.compare(key, key);
}catch(Exception e){ throw new ExceptionKeyInvalid(); }
}
protected Object key(BinTreePosition v){ return ((Entry) (v.getElem())).getKey(); }
protected void swapParentChild(BinTreePosition u, BinTreePosition v){
Object temp = u.getElem();
u.setElem(v.getElem);
v.setElem(temp);
}
protected void percolateUp(BinTreePosition v){
BinTreePosition root = h.getRoot(); //find the root node
while( v != h.getRoot()){
BinTreePosition p = v.getParent(); //get the parent node for current node
if( 0 >= comp.compare(key(p), key(v))) { break; }
swapParentChild(p, v); //swap parent and child to move the current node
v = p;
}
}
protected void precolateDown(BinTreePosition v){
while (v.hasLChild()){
BinTreePosition smallerChild = v.getLChild(); //left child is smaller
if( v.hasRChild() && 0 < comp.compare(key(v.getLChild()), key(v.getRChild()){
smallerChild = v.getRChild(); //compare right child
}
if( 0 <= comp.compare(key(smallerChild), key(v) ){ break; }
swapParentChild(v, smallerChild);
v = smallerChild;
}
}
}
References:
Priority Queue
Key and Value => Entry
public interface Entry{
public Object getKey(); //get the key values
public Object setKey(Object k);
public object getValue();
public Object setValue((Object v);
}
public class EntryDefault implements Entry {
protected Object key;
protected Object value;
public EntryDefault(Object k, Object v){
key = k; value = v;
}
public Object getKey() { return key; }
public Object setKey(Object k) {
Object oldK = key;
key = k;
return oldK;
}
public Object getValue() { return value; }
public Object setValue(Object v) {
Object oldV = value; value= v; return oldV;
}
}
Comparator Interface and Implementation
public interface Comparator {
public int compare(Object a, Object b);
}
compare method will return positive, 0 or negative.
Priority Queue Interface
public interface PriorityQueue {
public int getSize();
public boolean isEmpty();
public Entry getMin() throws ExceptionPriorityQueueEmpty;
public Entry insert(Object key, Object obj) throw ExceptionKeyInvalid;
public Entry delMin() throws ExceptionPriorityQueueEmpty;
}
Implementation on top Un Sorted List
public class PriorityQueueUnsortedList implements PriorityQueue {
private List list;
private Comparator comparator;
public PriorityQueuUnsortedList(){
this(new ComparatorDefault(), null);
}
public PriorityQueueUnsortedList(Comparator comparor){ this(c, null); }
public PriorityQueueUnsortedList(Sequeue s){ this(new ComparatorDefault(), s); }
public PriorityQueueUnsortedList(Comparator comparator, Sequence sequence){
list = new ListDLNode();
comparator = comparator;
if(null != sequence){
while(!s.isEmpty()){
Entry e = (Entry)s.removeFirst();
insert(e.getKey, e.getValue());
}
}
}
public int getSize() { return list.getSize();}
public boolean isEmpty(){ return list.isEmpty(); }
public Entry getMin() throws ExceptionPriorityQueueEmpty {
if(list.isEmtpy()){
throw new ExceptionPriorityQueueEmpty(“Error, queue is empty”);
}
Position minPos = list.first();
Position curPos = list.getNext(minPos);
while(null != curPos){
if(0 < comparator.compare(minPos.getElem(), curPos.getElem())){
minPos = curPos; //find the min one
}
}
return (Entry) minPos.getElem();
}
public Entry insert(Object key, Object obj) throws ExceptionKeyInvalid {
Entry entry = new EntryDefault(key, obj);
list.insertLast(entry); //put the element at the tail
return (entry);
}
public Entry delMin() throws ExceptionPriorityQueueEmpty {
if(list.isEmepty()){
throw new ExceptionPriorityQueueEmpty(“Error”);
}
Position minPos = list.first();
Iterator it = list.pistions();
while(it.hasNext()){
Position curPos = (Position) (it.getNext());
if( < comprator.compare(
(Entry)(minPos.getElement()).getKey(),
(Entry)(curPos.getElement()).getKey()
)){
minPos = curPos;
}
}
return (Entry)list.remove(minPos);
}
}
It is easy to insert, but it is O(n) to getMin() or delMin()
Implementation on top of Sorted List
public class PriorityQueueSortedList implements PriorityQueue {
private List list;
private Comparator comparator;
public PriorityQueueSortedList(){ this(new ComparatorDefault(), null); }
public PriorityQueueSortedList(Comparator c){ this(c, null); }
public PriorityQueueSortedList(Sequence s){ this(new ComparatorDefault(), s); }
public PriorityQueueSortedList(Comparator comparator, Sequence sequence){
list = new ListDLNode();
comparator = comparator;
if( null != s){
while( !s.isEmpty()) {
Entry e = (Entry) s. removeFirst();
insert(e.getKey(), e.getValue());
}
}
}
public int getSize() { return list.getSize(); }
public boolean isEmpty() { return list.isEmpty(); }
public Entry getMin() throws ExceptionPriorityQueueEmpty {
if(list.isEmpty()){
throw new ExceptionPriorityQueueEmpty(“Error");
}
return (Entry) list.last();
}
public Entry insert(Object key, Object obj) throws ExceptionKeyInvalid {
Entry entry = new EntryDefault(key, obj);
if(list.isEmpty() || (0 > comparator.compare((Entry) (list.first().getElem())).getKey(), entry.getKey()) ){
list.insertFirst(entry); //list is empty or current is largest
}else{
Position curPos = list.last();
while( 0 > comparator.compare((Entry) (curPos.getElem())).getKey(), entry.getKey()){
curPos = list.getPrev(curPos); //move to previous items, find the one bigger than current
}
list.insertAfter(curPos, entry);
}
return (entry);
}
public Entry delMin() throws ExceptionPriorityQueueEmpty {
if(list.isEmpty()){
throw new ExceptionPriorityQueueEmpty(“Error");
}
return (Entry) list.remove(list.last());
}
}
Heap
Heap Structure - Page 173
Binary Tree - Heap - Big Top - Small Top
public class PriorityQueueHeap implements PriorityQueue {
private CompleteBinTree h;
private Comparator comp;
public PriorityQueueHeap(){ this(new ComparatorDefault(), null); }
public PriorityQueueHeap(Comparator c){ this(c, null); }
public PriorityQueueHeap(Sequence s){ this(new ComparatorDefault(), s); }
public PriorityQueueHeap(Comparator c, Sequence s){
comp = c;
h = new CompleteBinTree_Vector(s);
if( !h.isEmpty()){
for(int i = h.getSize()/2-1; i>=0; i—){
precolateDown(h.posOfNode(i));
}
}
}
public int getSize() { return h.getSize(); }
public boolean isEmpty() { return h.isEmpty(); }
public Entry getMin() throws ExceptionPriorityQueueEmpty {
if(isEmpty()) { throw new ExceptionPriorityQueueEmpty(“Error"); }
return (Entry) h.getRoot().getElem();
}
public Entry insert(Object key, Object obj) throws ExceptionKeyInvalid {
checkKey(key);
Entry entry = new EntryDefault(key, obj);
percolateUp(h.addlast(entry);
return entry;
}
public Entry delMin() throws ExceptionPriorityQueueEmpty {
if(isEmpty()) { throw new ExceptionPriorityQueueEmpty(“Error”); }
Entry min = (Entry) h.getRoot().getElem();
if ( 1 == getSize()) {
//if it is last item
h.delLast();
}else{
h.getRoot().setElem((ComleBinaryTreeNodeRank)h.delLast().getElme()); //find the last one and place in root
percolateDown(h.getRoot());
}
return min;
}
protected void checkKey(Object key) throws ExceptionKeyInvalid {
try {
comp.compare(key, key);
}catch(Exception e){ throw new ExceptionKeyInvalid(); }
}
protected Object key(BinTreePosition v){ return ((Entry) (v.getElem())).getKey(); }
protected void swapParentChild(BinTreePosition u, BinTreePosition v){
Object temp = u.getElem();
u.setElem(v.getElem);
v.setElem(temp);
}
protected void percolateUp(BinTreePosition v){
BinTreePosition root = h.getRoot(); //find the root node
while( v != h.getRoot()){
BinTreePosition p = v.getParent(); //get the parent node for current node
if( 0 >= comp.compare(key(p), key(v))) { break; }
swapParentChild(p, v); //swap parent and child to move the current node
v = p;
}
}
protected void precolateDown(BinTreePosition v){
while (v.hasLChild()){
BinTreePosition smallerChild = v.getLChild(); //left child is smaller
if( v.hasRChild() && 0 < comp.compare(key(v.getLChild()), key(v.getRChild()){
smallerChild = v.getRChild(); //compare right child
}
if( 0 <= comp.compare(key(smallerChild), key(v) ){ break; }
swapParentChild(v, smallerChild);
v = smallerChild;
}
}
}
References:
发表评论
-
Stop Update Here
2020-04-28 09:00 315I will stop update here, and mo ... -
NodeJS12 and Zlib
2020-04-01 07:44 475NodeJS12 and Zlib It works as ... -
Docker Swarm 2020(2)Docker Swarm and Portainer
2020-03-31 23:18 367Docker Swarm 2020(2)Docker Swar ... -
Docker Swarm 2020(1)Simply Install and Use Swarm
2020-03-31 07:58 368Docker Swarm 2020(1)Simply Inst ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 336Traefik 2020(1)Introduction and ... -
Portainer 2020(4)Deploy Nginx and Others
2020-03-20 12:06 430Portainer 2020(4)Deploy Nginx a ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 435Private Registry 2020(1)No auth ... -
Docker Compose 2020(1)Installation and Basic
2020-03-15 08:10 373Docker Compose 2020(1)Installat ... -
VPN Server 2020(2)Docker on CentOS in Ubuntu
2020-03-02 08:04 454VPN Server 2020(2)Docker on Cen ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 384Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 477NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 421Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 337Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 246GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 450GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 326GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 313Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 317Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 292Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 311Serverless with NodeJS and Tenc ...
相关推荐
Priority Queue and Heaps Chapter 13. Graph Algorithms Chapter 14. Sorting Chapter 15. Searching Chapter 16. Selection Algorithms [Medians] Chapter 17. Symbol Tables Chapter 18. Hashing Chapter 19. ...
CHAPTER 11: PRIORITY QUEUE CHAPTER 12: HASH-TABLE CHAPTER 13: GRAPHS CHAPTER 14: STRING ALGORITHMS CHAPTER 15: ALGORITHM DESIGN TECHNIQUES CHAPTER 16: BRUTE FORCE ALGORITHM CHAPTER 17: GREEDY ...
CHAPTER 11: PRIORITY QUEUE CHAPTER 12: HASH-TABLE CHAPTER 13: GRAPHS CHAPTER 14: STRING ALGORITHMS CHAPTER 15: ALGORITHM DESIGN TECHNIQUES CHAPTER 16: BRUTE FORCE ALGORITHM CHAPTER 17: GREEDY ...
Linked Lists, Stacks, Queues,Trees, Priority Queue and Heaps, Disjoint Sets ADT, Graph Algorithms, Sorting, Searching, Selection Algorithms [Medians], Symbol Tables, Hashing, String Algorithms, ...
CC--面试问题来自各种编程专家的算法问题的解决方案,包括LeetCode,LintCode和Hackerrank(过去的Interview Street)。LeetCode示例std:sort +自定义cmp: 。 std:fill: 。 c ++ 11 std:random + uniform_int_...
#### Priority queue(优先队列) 优先队列是一种特殊的队列,其中的元素具有优先级。当从队列中移除元素时,总是移除优先级最高的元素。优先队列常用于事件驱动模拟、作业调度等领域。 #### List(链表) 链表是一...
C ++后台开发面经整理一,...C_C ++语言1,编译链接加载内存专题2,C ++基础部分3,C ++ 11lambda表达式函数绑定可变参数右值与完美转发并发编程智能指针4,STL(1)STL使用算法双端队列堆列表地图priority_queue队列
实现的算法: 线性时间选择:排序: 数据结构: 组织cpp内置数据结构用法: 矢量,堆栈,队列,双端队列,priority_queue,map,unordered_map,pair,tuple,string,set 实施方式: 使向量的push_back()函数为O...
- **priority_queue**:优先级队列,元素按照优先级排序。 6. **算法和容器的配合**: - 使用`std::for_each()`配合函数对象可以批量处理容器中的元素。 - `std::transform()`可用于在两个容器之间或容器内的...