Advertisement
kartikkukreja

Indexed Min-Priority Queue

May 21st, 2013
810
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.57 KB | None | 0 0
  1. #include <cstdio>
  2.  
  3. /*
  4.  * Indexed min priority queue
  5.  * Supports insertion in O(log N), deletion of any key (regardless of whether
  6.  * the key is the minimum key or not) in O(log N) and changes to key values
  7.  * in O(log N), where N is the number of
  8.  * elements currently in the PQ
  9.  */
  10. class MinIndexedPQ {
  11.     int NMAX, N, *heap, *index, *keys;
  12.  
  13.     void swap(int i, int j) {
  14.         int t = heap[i]; heap[i] = heap[j]; heap[j] = t;
  15.         index[heap[i]] = i; index[heap[j]] = j;
  16.     }
  17.  
  18.     void bubbleUp(int k)    {
  19.         while(k > 1 && keys[heap[k/2]] > keys[heap[k]])   {
  20.             swap(k, k/2);
  21.             k = k/2;
  22.         }
  23.     }
  24.  
  25.     void bubbleDown(int k)  {
  26.         int j;
  27.         while(2*k <= N) {
  28.             j = 2*k;
  29.             if(j < N && keys[heap[j]] > keys[heap[j+1]])
  30.                 j++;
  31.             if(keys[heap[k]] <= keys[heap[j]])
  32.                 break;
  33.             swap(k, j);
  34.             k = j;
  35.         }
  36.     }
  37.  
  38. public:
  39.     // Create an empty MinIndexedPQ which can contain atmost NMAX elements
  40.     MinIndexedPQ(int NMAX)  {
  41.         this->NMAX = NMAX;
  42.         N = 0;
  43.         keys = new int[NMAX + 1];
  44.         heap = new int[NMAX + 1];
  45.         index = new int[NMAX + 1];
  46.         for(int i = 0; i <= NMAX; i++)
  47.             index[i] = -1;
  48.     }
  49.    
  50.     ~MinIndexedPQ() {
  51.         delete [] keys;
  52.         delete [] heap;
  53.         delete [] index;
  54.     }
  55.  
  56.     // check if the PQ is empty
  57.     bool isEmpty()  {
  58.         return N == 0;
  59.     }
  60.  
  61.     // check if i is an index on the PQ
  62.     bool contains(int i)    {
  63.         return index[i] != -1;
  64.     }
  65.  
  66.     // return the number of elements in the PQ
  67.     int size()  {
  68.         return N;
  69.     }
  70.  
  71.     // associate key with index i; 0 < i < NMAX
  72.     void insert(int i, int key) {
  73.         N++;
  74.         index[i] = N;
  75.         heap[N] = i;
  76.         keys[i] = key;
  77.         bubbleUp(N);
  78.     }
  79.  
  80.     // returns the index associated with the minimal key
  81.     int minIndex()  {
  82.         return heap[1];
  83.     }
  84.  
  85.     // returns the minimal key
  86.     int minKey()    {
  87.         return keys[heap[1]];
  88.     }
  89.  
  90.     // delete the minimal key and return its associated index
  91.     // Warning: Don't try to read from this index after calling this function
  92.     int deleteMin() {
  93.         int min = heap[1];
  94.         swap(1, N--);
  95.         bubbleDown(1);
  96.         index[min] = -1;
  97.         heap[N+1] = -1;
  98.         return min;
  99.     }
  100.  
  101.     // returns the key associated with index i
  102.     int keyOf(int i)    {
  103.         return keys[i];
  104.     }
  105.  
  106.     // change the key associated with index i to the specified value
  107.     void changeKey(int i, int key)  {
  108.         keys[i] = key;
  109.         bubbleUp(index[i]);
  110.         bubbleDown(index[i]);
  111.     }
  112.  
  113.     // decrease the key associated with index i to the specified value
  114.     void decreaseKey(int i, int key)    {
  115.         keys[i] = key;
  116.         bubbleUp(index[i]);
  117.     }
  118.  
  119.     // increase the key associated with index i to the specified value
  120.     void increaseKey(int i, int key)    {
  121.         keys[i] = key;
  122.         bubbleDown(index[i]);
  123.     }
  124.  
  125.     // delete the key associated with index i
  126.     void deleteKey(int i)   {
  127.         int ind = index[i];
  128.         swap(ind, N--);
  129.         bubbleUp(ind);
  130.         bubbleDown(ind);
  131.         index[i] = -1;
  132.     }
  133. };
  134.  
  135. int main()  {
  136.     MinIndexedPQ pq(10);
  137.     for(int i=0; i<10; i++)
  138.         pq.insert(i, 10 - i);
  139.  
  140.     printf("%d %d\n", pq.minKey(), pq.size());
  141.     pq.deleteKey(8);
  142.     printf("%d %d\n", pq.minKey(), pq.size());
  143.     pq.deleteMin();
  144.     printf("%d %d\n", pq.minKey(), pq.size());
  145.  
  146.     return 0;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement