Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

bheap.inc
Go to the documentation of this file.
1 
29 #pragma once
30 
31 namespace abacus {
32 
33 
34 template<class Type, class Key>
36  :
37  heap_(size),
38  keys_(size),
39  n_(0)
40 { }
41 
42 
43 template<class Type, class Key>
45  const ArrayBuffer<Type> &elems,
46  const ArrayBuffer<Key> &keys)
47  :
48  heap_(elems),
49  keys_(keys),
50  n_(keys.size())
51 {
52  for (int i = father(n_-1); i>=0; --i)
53  heapify(i);
54 }
55 
56 
57 template<class Type, class Key>
58 std::ostream& operator<<(std::ostream& out, const AbaBHeap<Type, Key>& rhs)
59 {
60  for(int i = 0; i < rhs.n_; i ++)
61  out << rhs.heap_[i] << " (" << rhs.keys_[i] << ") ";
62  out << std::endl;
63  return out;
64 }
65 
66 
67 template<class Type, class Key>
68 void AbaBHeap<Type, Key>::insert(Type elem, Key key)
69 {
71  OGDF_ASSERT(n_ < size());
72 
73  // go up towards the root and insert \a elem
74  /* The position \a n_ of the array representing the heap becomes the
75  * new leaf of corresponding binary tree. However, inserting the new
76  * element at this position could destroy the heap property.
77  * Therefore, we go up to the root until we find the position
78  * for inserting the new element. While going up to this position
79  * we move earlier inserted elements already to their new position.
80  */
81  int i = n_;
82  int f = father(i);
83 
84  while (i > 0 && keys_[f] > key) {
85  heap_[i] = heap_[f];
86  keys_[i] = keys_[f];
87  i = f;
88  f = father(i);
89  }
90  heap_[i] = elem;
91  keys_[i] = key;
92  ++n_;
93 }
94 
95 
96 template<class Type, class Key>
98 {
99  // is the heap empty?
100  /* The check on an empty heap is only performed if is the code
101  * is compiled with the precompiler flag <tt>OGDF_DEBUG</tt>.
102  */
103  OGDF_ASSERT(!empty());
104 
105  return heap_[0];
106 }
107 
108 
109 template<class Type, class Key>
111 {
112  // is the heap empty?
113  /* The check on an empty heap is only performed if is the code
114  * is compiled with the precompiler flag <tt>OGDF_DEBUG</tt>.
115  */
116  OGDF_ASSERT(!empty());
117 
118  return keys_[0];
119 }
120 
121 
122 template<class Type, class Key>
124 {
125  Type min = getMin();
126 
127  --n_;
128 
129  if (n_ != 0) {
130  heap_[0] = heap_[n_];
131  keys_[0] = keys_[n_];
132  heapify(0);
133  }
134 
135  return min;
136 }
137 
138 
139 template<class Type, class Key>
141 {
142  n_ = 0;
143 }
144 
145 
146 template<class Type, class Key>
147 inline int AbaBHeap<Type, Key>::size() const
148 {
149  return heap_.size();
150 }
151 
152 
153 template <class Type, class Key>
154 inline int AbaBHeap<Type, Key>::number() const
155 {
156  return n_;
157 }
158 
159 
160 template<class Type, class Key>
161 inline bool AbaBHeap<Type, Key>::empty() const
162 {
163  if(n_ == 0) return true;
164  return false;
165 }
166 
167 
168 template<class Type, class Key>
169 void AbaBHeap<Type, Key>::realloc(int newSize)
170 {
171  heap_.realloc(newSize);
172  keys_.realloc(newSize);
173 }
174 
175 
176 template<class Type, class Key>
177 void AbaBHeap<Type, Key>::check() const
178 {
179  for(int i = 0; i < n_/2; i++)
180  if (keys_[i] > keys_[leftSon(i)] || (rightSon(i) < n_ && heap_[i] > heap_[rightSon(i)])) {
181  Logger::ifout() << "AbaBHeap:check(): " << i << " violates heap\n";
182  OGDF_THROW_PARAM(AlgorithmFailureException, ogdf::AlgorithmFailureCode::BHeap);
183  }
184 }
185 
186 
187 template <class Type, class Key>
188 inline int AbaBHeap<Type, Key>::leftSon(int i) const
189 {
190  return 2*i + 1;
191 }
192 
193 
194 template <class Type, class Key>
195 int AbaBHeap<Type, Key>::rightSon(int i) const
196 {
197  return 2*i + 2;
198 }
199 
200 
201 template <class Type, class Key>
202 inline int AbaBHeap<Type, Key>::father(int i) const
203 {
204  return (i-1)/2;
205 }
206 
207 
208 template <class Type, class Key>
210 {
212  int smallest; // smallest heap element of i,l, and r
213  Type tmp; // temporary object for swapping elements of the heap
214  Key ktmp; // temporary object for swapping the keys
215 
216  // restore the heap property
217  /* Node \a i violates the heap property if it has a son with
218  * a smaller key. If we swap the element and the key of node \a i
219  * with the element and the key of the smaller one of its two sons,
220  * then the heap property is restored for node \a i. However, it
221  * might be now destroyed for node \a smallest. So we
222  * iterate this process until no swap is performed.
223  */
224  while (i < n_) {
226  int l = leftSon(i);
227  int r = rightSon(i);
228  if (l < n_ && keys_[i] > keys_[l]) smallest = l;
229  else smallest = i;
230  if (r < n_ && keys_[smallest] > keys_[r]) smallest = r;
231 
232  if (smallest != i) {
234  tmp = heap_[i];
235  heap_[i] = heap_[smallest];
236  heap_[smallest] = tmp;
237 
238  ktmp = keys_[i];
239  keys_[i] = keys_[smallest];
240  keys_[smallest] = ktmp;
241 
242  i = smallest;
243  }
244  else break;
245  }
246 }
247 
248 }
abacus::AbaBHeap::insert
void insert(Type elem, Key key)
Inserts an item with a key into the heap.
OGDF_ASSERT
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:54
abacus::AbaBHeap::AbaBHeap
AbaBHeap(int size)
A constructor.
abacus::operator<<
std::ostream & operator<<(std::ostream &out, const Active< BaseType, CoType > &rhs)
abacus::AbaBHeap::getMinKey
Key getMinKey() const
Returns the key of the minimum element of the heap.
ogdf::gml::Key
Key
Definition: GML.h:55
abacus
Definition: abacusroot.h:48
ogdf::dot::Attribute::Type
@ Type
abacus::AbaBHeap::n_
int n_
Definition: bheap.h:191
abacus::AbaBHeap::clear
void clear()
Empties the heap.
abacus::AbaBHeap::heapify
void heapify(int i)
Is the central function to maintain the heap property.
ogdf::tlp::Attribute::size
@ size
abacus::AbaBHeap::empty
bool empty() const
Return true if there are no elements in the heap, false otherwise.
abacus::AbaBHeap::father
int father(int i) const
Returns the index of the father of element i.
abacus::AbaBHeap::rightSon
int rightSon(int i) const
Returns the index of the right son of node i.
abacus::AbaBHeap::leftSon
int leftSon(int i) const
Returns the index of the left son of node i.
r
int r[]
Definition: hierarchical-ranking.cpp:8
abacus::AbaBHeap::getMin
Type getMin() const
Returns the minimum element of the heap.
OGDF_THROW_PARAM
#define OGDF_THROW_PARAM(CLASS, PARAM)
Replacement for throw.
Definition: exceptions.h:67
ogdf::AlgorithmFailureCode::BHeap
@ BHeap
abacus::AbaBHeap::realloc
void realloc(int newSize)
Changes the size of the heap.
ogdf::Logger::ifout
static std::ostream & ifout()
stream for forced output (global; used by internal libraries, e.g. Abacus)
Definition: Logger.h:216
abacus::AbaBHeap::size
int size() const
Returns the maximal number of elements which can be stored in the heap.
abacus::AbaBHeap::check
void check() const
Throws an exception if the heap properties are violated.
abacus::AbaBHeap::number
int number() const
Returns the number of elements in the heap.
abacus::AbaBHeap::extractMin
Type extractMin()
Accesses and removes the minimum element from the heap.