Daly的游戏人生

动态优先队列

    很多图算法实现中都需要用到优先队列,这些优先队列需要能动态改变堆内对应元素的值,并更新堆。比如在dijkstra算法中,每次松弛都要更新顶点的权值,但一般的优先队列无法找到顶点在堆中的位置。本文实现一种数据结构"动态优先队列",利用两个数组保存了原数据数组位置----堆位置的映射,能实现快速修改。该数据结构的大部分应用场合中,元素个数是固定的,因此代码中没有追加新元素的操作。
     代码和大部分堆操作一样,m_indices数组保存了原数组 到 堆位置映射, m_pmap保存了逆向映射。堆调整时,原数组的数据位置不变,只改变m_pmap映射。该实现是最小优先队列,在模板参数添加用于比较的Functor可改造为任意堆。

  1template<typename _Type>
  2static void gs_swap(_Type &x, _Type &y)
  3{    
  4   static _Type tmp;
  5   tmp = x;
  6   x = y;
  7   y = tmp;
  8}

  9
 10
 11/*
 12* DynamicPQ : dynamic priority queue (with indices mapping to original array position)
 13*   It's a min-heap and has following feature
 14*  1. detemine the min value
 15*  2. modify the corresponding k in original array
 16*  3. pop the top value
 17*  it can't be append new value
 18*/
 
 19template<typename _Type>
 20class DynamicPQ
 21{
 22public:
 23     /*
 24     *  @param  array   the original array to processed
 25     *  @param  max_n   the original size of the array
 26     */

 27    DynamicPQ(_Type *array, unsigned int max_n)
 28        : m_array(array), m_size(max_n)
 29    {
 30        unsigned int i;
 31        m_indices = new unsigned int[max_n];
 32        m_pmap = new unsigned int[max_n];
 33        for (i=0; i<m_size; i++{
 34            m_indices[i] = m_pmap[i] = i;
 35        }

 36        //make_heap
 37        for (i=m_size/2; i>0; i--{
 38            adjust_down(i);
 39        }

 40        adjust_down(0);
 41    }

 42
 43    ~DynamicPQ() {
 44        delete[] m_indices;
 45        delete[] m_pmap;
 46    }

 47    /*
 48    * change the key
 49    * @param k      the original index, not the position of the min-heap
 50    * @param value  new value
 51    */

 52    void modify_key(unsigned int k, _Type value)
 53    {
 54        unsigned int idx = m_indices[k];  //find the coresponding position of the heap
 55        if (value < m_array[ m_pmap[idx] ]) {
 56            m_array[ m_pmap[idx] ] = value;    //decrease key
 57            adjust_up(idx);
 58        }

 59        else {
 60            m_array[ m_pmap[idx] ] = value;    //increase key
 61            adjust_down(idx);
 62        }

 63    }

 64    _Type top() return m_array[ m_pmap[0] ];}
 65    /*
 66    * extract the min value
 67    */

 68    _Type pop_top() {
 69        gs_swap(m_pmap[0], m_pmap[m_size-1]);
 70        m_indices[ m_pmap[0] ] = m_size - 1;
 71        m_indices[ m_pmap[m_size-1] ] = 0;
 72        --m_size;
 73        adjust_down(0);    
 74        return m_array[ m_pmap[m_size]];
 75    }

 76    inline unsigned int size() return m_size; }
 77
 78protected:
 79    //update the value. up to the root
 80    void adjust_up(unsigned int i)
 81    {
 82        unsigned int parent = (i-1/ 2;
 83        while (i>0 && m_array[ m_pmap[i] ] < m_array[ m_pmap[parent] ]) {
 84            gs_swap(m_pmap[i], m_pmap[parent]);
 85            m_indices[ m_pmap[i]] = i;       //update index mapping after exchange
 86            i = parent;
 87            parent = (i-1/ 2;
 88        }

 89        m_indices[ m_pmap[i] ] = i;     //update the final position of i
 90    }

 91    //min-heapify
 92    void adjust_down(unsigned int i)
 93    {
 94        unsigned int lf = i*2 + 1;    //left child
 95        unsigned int rt = i*2 + 2;    //right child
 96        unsigned int min_pos;
 97        unsigned int tmp_i = m_pmap[i];
 98        while(lf < m_size) {            
 99            if (m_array[ m_pmap[lf] ] < m_array[ m_pmap[i] ]) 
100                min_pos = lf;
101            else 
102                min_pos = i;
103            if (rt < m_size && m_array[ m_pmap[rt] ] < m_array[ m_pmap[min_pos] ]) {
104                min_pos = rt;
105            }

106            if (min_pos != i) {
107                gs_swap(m_pmap[min_pos], m_pmap[i]);
108                m_indices[ m_pmap[i]] = i;       //update after exchange
109                i = min_pos;            //next loop
110                lf = i*2 + 1;
111                rt = i*2 + 2;
112                continue;
113            }

114            else {
115                m_indices[tmp_i] = i;   //update index
116                break;
117            }

118        }

119    }

120    
121protected:
122    _Type *m_array;       
123    unsigned int *m_indices;    //index mapping(array[k] --> heap position)
124    unsigned int *m_pmap;       //pointer mapping(heap position --> array[k])
125    unsigned int m_size;
126}
;
127


posted on 2009-11-17 11:38 Daly 阅读(955) 评论(0)  编辑 收藏 引用 所属分类: 数据结构与算法


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理