Advertisement
kartikkukreja

Multiway Merge with Indexed PQ

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