alpar@906: /* -*- C++ -*- klao@39: * alpar@1956: * This file is a part of LEMON, a generic C++ optimization library alpar@1956: * alpar@1956: * Copyright (C) 2003-2006 alpar@1956: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@1359: * (Egervary Research Group on Combinatorial Optimization, EGRES). klao@39: * alpar@906: * Permission to use, modify and distribute this software is granted alpar@906: * provided that this copyright notice appears in all copies. For alpar@906: * precise terms see the accompanying LICENSE file. klao@39: * alpar@906: * This software is provided "AS IS" with no warranty of any kind, alpar@906: * express or implied, and with no claim as to its suitability for any alpar@906: * purpose. klao@39: * klao@39: */ klao@39: alpar@921: #ifndef LEMON_BIN_HEAP_H alpar@921: #define LEMON_BIN_HEAP_H klao@37: klao@491: ///\ingroup auxdat klao@274: ///\file klao@274: ///\brief Binary Heap implementation. klao@274: klao@37: #include <vector> klao@37: #include <utility> klao@37: #include <functional> klao@37: alpar@921: namespace lemon { klao@37: deba@1834: /// \ingroup auxdat alpar@430: jacint@1270: /// A Binary Heap implementation. alpar@967: jacint@1270: ///This class implements the \e binary \e heap data structure. A \e heap jacint@1270: ///is a data structure for storing items with specified values called \e jacint@1270: ///priorities in such a way that finding the item with minimum priority is jacint@1270: ///efficient. \c Compare specifies the ordering of the priorities. In a heap jacint@1270: ///one can change the priority of an item, add or erase an item, etc. jacint@1270: /// jacint@1270: ///\param Item Type of the items to be stored. jacint@1270: ///\param Prio Type of the priority of the items. jacint@1270: ///\param ItemIntMap A read and writable Item int map, used internally jacint@1270: ///to handle the cross references. jacint@1270: ///\param Compare A class for the ordering of the priorities. The jacint@1270: ///default is \c std::less<Prio>. alpar@967: /// alpar@967: ///\sa FibHeap alpar@967: ///\sa Dijkstra klao@172: template <typename Item, typename Prio, typename ItemIntMap, klao@172: typename Compare = std::less<Prio> > klao@37: class BinHeap { klao@37: klao@37: public: klao@172: typedef Item ItemType; klao@37: // FIXME: stl-ben nem ezt hivjak value_type -nak, hanem a kovetkezot... klao@172: typedef Prio PrioType; klao@172: typedef std::pair<ItemType,PrioType> PairType; klao@172: typedef ItemIntMap ItemIntMapType; klao@172: typedef Compare PrioCompare; klao@37: deba@1331: /// \brief Type to represent the items states. klao@274: /// deba@1331: /// Each Item element have a state associated to it. It may be "in heap", alpar@1336: /// "pre heap" or "post heap". The latter two are indifferent from the deba@1331: /// heap's point of view, but may be useful to the user. deba@1331: /// alpar@1336: /// The ItemIntMap \e should be initialized in such way that it maps deba@1331: /// PRE_HEAP (-1) to any element to be put in the heap... klao@39: enum state_enum { klao@37: IN_HEAP = 0, klao@37: PRE_HEAP = -1, klao@37: POST_HEAP = -2 klao@37: }; klao@37: klao@37: private: klao@37: std::vector<PairType> data; klao@37: Compare comp; klao@172: ItemIntMap &iim; klao@37: klao@37: public: deba@1331: /// \brief The constructor. deba@1331: /// deba@1331: /// The constructor. deba@1331: /// \param _iim should be given to the constructor, since it is used deba@1331: /// internally to handle the cross references. The value of the map deba@1331: /// should be PRE_HEAP (-1) for each element. deba@1185: explicit BinHeap(ItemIntMap &_iim) : iim(_iim) {} jacint@1270: deba@1331: /// \brief The constructor. deba@1331: /// deba@1331: /// The constructor. deba@1331: /// \param _iim should be given to the constructor, since it is used deba@1331: /// internally to handle the cross references. The value of the map deba@1331: /// should be PRE_HEAP (-1) for each element. deba@1331: /// deba@1331: /// \param _comp The comparator function object. deba@1191: BinHeap(ItemIntMap &_iim, const Compare &_comp) deba@1185: : iim(_iim), comp(_comp) {} klao@37: klao@37: deba@1331: /// The number of items stored in the heap. deba@1331: /// deba@1331: /// \brief Returns the number of items stored in the heap. klao@37: int size() const { return data.size(); } jacint@1270: deba@1331: /// \brief Checks if the heap stores no items. deba@1331: /// deba@1331: /// Returns \c true if and only if the heap stores no items. klao@41: bool empty() const { return data.empty(); } klao@37: deba@1717: /// \brief Make empty this heap. deba@1717: /// deba@2050: /// Make empty this heap. It does not change the cross reference map. deba@2050: /// If you want to reuse what is not surely empty you should first clear deba@2050: /// the heap and after that you should set the cross reference map for deba@2050: /// each item to \c PRE_HEAP. deba@1717: void clear() { deba@1717: data.clear(); deba@1717: } deba@1717: klao@37: private: klao@37: static int parent(int i) { return (i-1)/2; } klao@37: static int second_child(int i) { return 2*i+2; } klao@214: bool less(const PairType &p1, const PairType &p2) const { klao@37: return comp(p1.second, p2.second); klao@37: } klao@37: klao@37: int bubble_up(int hole, PairType p); klao@37: int bubble_down(int hole, PairType p, int length); klao@37: klao@37: void move(const PairType &p, int i) { klao@37: data[i] = p; klao@172: iim.set(p.first, i); klao@37: } klao@37: klao@41: void rmidx(int h) { klao@41: int n = data.size()-1; klao@41: if( h>=0 && h<=n ) { klao@172: iim.set(data[h].first, POST_HEAP); klao@41: if ( h<n ) { klao@41: bubble_down(h, data[n], n); klao@41: } klao@41: data.pop_back(); klao@41: } klao@41: } klao@41: klao@37: public: deba@1331: /// \brief Insert a pair of item and priority into the heap. deba@1331: /// deba@1331: /// Adds \c p.first to the heap with priority \c p.second. deba@1331: /// \param p The pair to insert. klao@37: void push(const PairType &p) { klao@37: int n = data.size(); klao@37: data.resize(n+1); klao@37: bubble_up(n, p); klao@37: } jacint@1270: deba@1331: /// \brief Insert an item into the heap with the given heap. deba@1331: /// deba@1331: /// Adds \c i to the heap with priority \c p. deba@1331: /// \param i The item to insert. deba@1331: /// \param p The priority of the item. klao@172: void push(const Item &i, const Prio &p) { push(PairType(i,p)); } klao@37: deba@1331: /// \brief Returns the item with minimum priority relative to \c Compare. deba@1331: /// deba@1331: /// This method returns the item with minimum priority relative to \c deba@1331: /// Compare. deba@1331: /// \pre The heap must be nonempty. klao@172: Item top() const { klao@37: return data[0].first; klao@37: } jacint@1270: deba@1331: /// \brief Returns the minimum priority relative to \c Compare. deba@1331: /// deba@1331: /// It returns the minimum priority relative to \c Compare. deba@1331: /// \pre The heap must be nonempty. klao@274: Prio prio() const { klao@37: return data[0].second; klao@37: } klao@37: deba@1331: /// \brief Deletes the item with minimum priority relative to \c Compare. deba@1331: /// deba@1331: /// This method deletes the item with minimum priority relative to \c deba@1331: /// Compare from the heap. deba@1331: /// \pre The heap must be non-empty. klao@37: void pop() { klao@41: rmidx(0); klao@41: } klao@41: deba@1331: /// \brief Deletes \c i from the heap. deba@1331: /// deba@1331: /// This method deletes item \c i from the heap, if \c i was deba@1331: /// already stored in the heap. deba@1331: /// \param i The item to erase. klao@172: void erase(const Item &i) { jacint@221: rmidx(iim[i]); klao@37: } klao@37: jacint@1270: deba@1331: /// \brief Returns the priority of \c i. deba@1331: /// deba@1331: /// This function returns the priority of item \c i. deba@1331: /// \pre \c i must be in the heap. deba@1331: /// \param i The item. klao@274: Prio operator[](const Item &i) const { jacint@221: int idx = iim[i]; klao@37: return data[idx].second; klao@37: } klao@274: deba@1331: /// \brief \c i gets to the heap with priority \c p independently deba@1331: /// if \c i was already there. deba@1331: /// deba@1331: /// This method calls \ref push(\c i, \c p) if \c i is not stored deba@1331: /// in the heap and sets the priority of \c i to \c p otherwise. deba@1331: /// \param i The item. deba@1331: /// \param p The priority. klao@172: void set(const Item &i, const Prio &p) { jacint@221: int idx = iim[i]; klao@37: if( idx < 0 ) { klao@172: push(i,p); klao@37: } klao@172: else if( comp(p, data[idx].second) ) { klao@172: bubble_up(idx, PairType(i,p)); klao@37: } klao@37: else { klao@172: bubble_down(idx, PairType(i,p), data.size()); klao@37: } klao@37: } klao@37: deba@1331: /// \brief Decreases the priority of \c i to \c p. jacint@1270: deba@1331: /// This method decreases the priority of item \c i to \c p. deba@1331: /// \pre \c i must be stored in the heap with priority at least \c deba@1331: /// p relative to \c Compare. deba@1331: /// \param i The item. deba@1331: /// \param p The priority. klao@172: void decrease(const Item &i, const Prio &p) { jacint@221: int idx = iim[i]; klao@172: bubble_up(idx, PairType(i,p)); klao@37: } jacint@1270: deba@1331: /// \brief Increases the priority of \c i to \c p. deba@1331: /// deba@1331: /// This method sets the priority of item \c i to \c p. deba@1331: /// \pre \c i must be stored in the heap with priority at most \c deba@1331: /// p relative to \c Compare. deba@1331: /// \param i The item. deba@1331: /// \param p The priority. klao@172: void increase(const Item &i, const Prio &p) { jacint@221: int idx = iim[i]; klao@172: bubble_down(idx, PairType(i,p), data.size()); klao@37: } klao@37: deba@1331: /// \brief Returns if \c item is in, has already been in, or has deba@1331: /// never been in the heap. deba@1331: /// deba@1331: /// This method returns PRE_HEAP if \c item has never been in the deba@1331: /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP deba@1331: /// otherwise. In the latter case it is possible that \c item will deba@1331: /// get back to the heap again. deba@1331: /// \param i The item. klao@172: state_enum state(const Item &i) const { jacint@221: int s = iim[i]; klao@39: if( s>=0 ) klao@39: s=0; klao@39: return state_enum(s); klao@39: } klao@39: deba@1902: /// \brief Sets the state of the \c item in the heap. deba@1902: /// deba@1902: /// Sets the state of the \c item in the heap. It can be used to deba@1902: /// manually clear the heap when it is important to achive the deba@1902: /// better time complexity. deba@1902: /// \param i The item. deba@1902: /// \param st The state. It should not be \c IN_HEAP. deba@1902: void state(const Item& i, state_enum st) { deba@1902: switch (st) { deba@1902: case POST_HEAP: deba@1902: case PRE_HEAP: deba@1902: if (state(i) == IN_HEAP) { deba@1902: erase(i); deba@1902: } deba@1903: iim[i] = st; deba@1902: break; deba@1906: case IN_HEAP: deba@1906: break; deba@1902: } deba@1902: } deba@1902: klao@37: }; // class BinHeap klao@37: klao@37: klao@37: template <typename K, typename V, typename M, typename C> klao@37: int BinHeap<K,V,M,C>::bubble_up(int hole, PairType p) { klao@37: int par = parent(hole); klao@37: while( hole>0 && less(p,data[par]) ) { klao@37: move(data[par],hole); klao@37: hole = par; klao@37: par = parent(hole); klao@37: } klao@37: move(p, hole); klao@37: return hole; klao@37: } klao@37: klao@37: template <typename K, typename V, typename M, typename C> klao@37: int BinHeap<K,V,M,C>::bubble_down(int hole, PairType p, int length) { klao@37: int child = second_child(hole); klao@37: while(child < length) { klao@37: if( less(data[child-1], data[child]) ) { klao@37: --child; klao@37: } klao@37: if( !less(data[child], p) ) klao@37: goto ok; klao@37: move(data[child], hole); klao@37: hole = child; klao@37: child = second_child(hole); klao@37: } klao@37: child--; klao@37: if( child<length && less(data[child], p) ) { klao@37: move(data[child], hole); klao@37: hole=child; klao@37: } klao@37: ok: klao@37: move(p, hole); klao@37: return hole; klao@37: } klao@37: alpar@430: alpar@921: } // namespace lemon klao@37: alpar@921: #endif // LEMON_BIN_HEAP_H