lkuty

Priority Queue

Aug 8th, 2020
1,287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.38 KB | None | 0 0
  1. // File Main.java
  2. public class Main {
  3.   public static void main(String[] args) {
  4.     PriorityQueue<Integer, Integer> q = new PriorityQueue<Integer, Integer>();
  5.     q.insert(10, 1);
  6.     q.insert(40, 4);
  7.     q.insert(50, 5);
  8.     q.insert(200, 20);
  9.     q.insert(70, 7);
  10.     System.out.println("Min priority elem: " + q.extract_min());
  11.     q.decrease_priority(200, 2);
  12.     System.out.println("Min priority elem: " + q.extract_min());
  13.     System.out.println("Min priority elem: " + q.extract_min());
  14.     q.decrease_priority(70, 1);
  15.     System.out.println("Min priority elem: " + q.extract_min());
  16.     System.out.println("Min priority elem: " + q.extract_min());
  17.   }
  18. }
  19.  
  20. // File PriorityQueue.java
  21. import java.util.HashMap;
  22. import java.util.Vector;
  23.  
  24. public class PriorityQueue<V, P extends Comparable<P>> {
  25.  
  26.   private class Elem implements Comparable<Elem> {
  27.     public V value;
  28.     public P priority;
  29.     public int index = -1;
  30.  
  31.     public Elem(V value, P priority) {
  32.       this.value = value;
  33.       this.priority = priority;
  34.     }
  35.     public int compareTo(Elem arg) {
  36.       return priority.compareTo(arg.priority);
  37.     }
  38.   }
  39.  
  40.   private class Heap {
  41.     private Vector<Elem> c = new Vector<Elem> ();
  42.  
  43.     public void push(Elem elem) {
  44.       c.add(elem);
  45.       elem.index = c.size() - 1;
  46.       min_heapify(c.size() - 1);
  47.     }
  48.  
  49.     public Elem get(int index) {
  50.       return c.get(index);
  51.     }
  52.  
  53.     public void min_heapify(int index) {
  54.       int q = index;
  55.       int p = (q - 1) / 2;
  56.       while (q != 0 && c.get(p).compareTo(c.get(q)) > 0) {
  57.         Elem tmp = c.get(p);
  58.         c.set(p, c.get(q)); c.get(p).index = p;
  59.         c.set(q, tmp); c.get(q).index = q;
  60.         q = p;
  61.         p = (q - 1) / 2;
  62.       }
  63.     }
  64.  
  65.     public Elem pop() {
  66.       Elem tmp = c.firstElement();
  67.       c.set(0, c.get(c.size() - 1)); c.get(0).index = 0;
  68.       c.remove(c.size() - 1);
  69.       int n = c.size() - 1;
  70.       int p = 0;
  71.       int g = 1;
  72.       int d = 2;
  73.       while (true) {
  74.         if (d <= n && (c.get(d).compareTo(c.get(g)) <= 0 && c.get(d).compareTo(c.get(p)) < 0)) {
  75.           Elem tmp2 = c.get(p);
  76.           c.set(p, c.get(d)); c.get(p).index = p;
  77.           c.set(d, tmp2); c.get(d).index = d;
  78.           p = d; g = 2 * p + 1; d = g + 1;
  79.         }
  80.         else if ((d == n + 1 && c.get(g).compareTo(c.get(p)) < 0) ||
  81.                  (d <= n && (c.get(g).compareTo(c.get(p)) < 0 &&
  82.                        c.get(g).compareTo(c.get(d)) <= 0))) {
  83.           Elem tmp2 = c.get(p);
  84.           c.set(p, c.get(g)); c.get(p).index = p;
  85.           c.set(g, tmp2); c.get(g).index = g;
  86.           p = g; g = 2 * p + 1; d = g + 1;
  87.         }
  88.         else break;
  89.       }
  90.       return tmp;
  91.     }
  92.   }
  93.  
  94.   private Heap h = new Heap();
  95.   private HashMap<V, Elem> hm = new HashMap<V, Elem>();
  96.  
  97.   public void insert(V value, P priority) {
  98.     Elem elem = new Elem(value, priority);
  99.     h.push(elem);
  100.     hm.put(value, elem);
  101.   }
  102.  
  103.   public V extract_min() {
  104.     Elem elem = h.pop();
  105.     hm.remove(elem.value);
  106.     return elem.value;
  107.   }
  108.  
  109.   public void decrease_priority(V value, P priority) {
  110.     int index = hm.get(value).index;
  111.     h.get(index).priority = priority;
  112.     h.min_heapify(index);
  113.   }
  114. }
  115.  
  116. /*
  117. $ javac PriorityQueue.java
  118. $ javac Main.java
  119. $ java Main
  120. Min priority elem: 10
  121. Min priority elem: 200
  122. Min priority elem: 40
  123. Min priority elem: 70
  124. Min priority elem: 50
  125. */
  126.  
Advertisement
Add Comment
Please, Sign In to add comment