fibonacci_heap

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
namespace CLRS
{
  template <typename T>
  class FibHeap;
  template <typename T>
  class FibHeapTreeNode;
  template <typename T>
  FibHeap<T> make_fib_heap();
  template <typename T>
  void fib_heap_insert(const FibHeap<T> &,
		       const std::shared_ptr<FibHeapTreeNode<T>> &);
  template <typename T>
  std::shared_ptr<FibHeapTreeNode<T>>
  fib_heap_minimum(const FibHeap<T> &);
  template <typename T>
  FibHeap<T> fib_heap_union(const FibHeap<T> &, const FibHeap<T> &);
  template <typename T>
  std::shared_ptr<FibHeapTreeNode<T>> fib_heap_extract_min(FibHeap<T> &);
  template <typename T>
  void consolidate(FibHeap<T> &);
  template <typename T>
  void fib_heap_link(const FibHeap<T> &,
		     const std::shared_ptr<FibHeapTreeNode<T>> &,
		     const std::shared_ptr<FibHeapTreeNode<T>> &);
  template <typename T>
  void fib_heap_decrease_key(FibHeap<T> &,
			     const std::shared_ptr<FibHeapTreeNode<T>> &,
			     T);
  template <typename T>
  void cut(const FibHeap<T> &,
	   const std::shared_ptr<FibHeapTreeNode<T>> &,
	   const std::shared_ptr<FibHeapTreeNode<T>> &);
  template <typename T>
  void cascading_cut(const FibHeap<T> &,
		     const std::shared_ptr<FibHeapTreeNode<T>> &);
  template <typename T>
  void fib_heap_delete(FibHeap<T> &,
		       const std::shared_ptr<FibHeapTreeNode<T>> &);

  /*
   * doubly linked circular list helper functions
   */

  // just assign itself to the left and right of node.
  template <typename T>
  void fib_heap_list_make(const std::shared_ptr<FibHeapTreeNode<T>> &x)
  {
    x->set_left(x);
    x->set_right(x);
  }

  // just link first to the second and the left of second
  template <typename T>
  void fib_heap_list_insert(const std::shared_ptr<FibHeapTreeNode<T>> &x,
			    const std::shared_ptr<FibHeapTreeNode<T>> &y)
  {
    x->set_right(y);
    x->set_left(y->get_left());
    y->get_left()->set_right(x);
    y->set_left(x);
  }

  // just need the removing node as parameter, link its left and right
  template <typename T>
  void fib_heap_list_remove(const std::shared_ptr<FibHeapTreeNode<T>> &x)
  {
    x->get_left()->set_right(x->get_right());
    x->get_right()->set_left(x->get_left());
  }

  // make the first a child of the second, increase second's degree
  template <typename T>
  void fib_heap_list_add_child(
			       const std::shared_ptr<FibHeapTreeNode<T>> &x,
			       const std::shared_ptr<FibHeapTreeNode<T>> &y)
  {
    if(y->get_child() != nullptr)
      {
	fib_heap_list_insert(x, y->get_child());
	x->set_p(y);
      }
    else
      {
	fib_heap_list_make(x);
	y->set_child(x);
	x->set_p(y);
      }
    y->incr_degree();
  }

  // make the first not a child of the second, decrease second's degree
  template <typename T>
  void fib_heap_list_remove_child(
				  const std::shared_ptr<FibHeapTreeNode<T>> &x,
				  const std::shared_ptr<FibHeapTreeNode<T>> &y)
  {
    fib_heap_list_remove(x);
    if(x->get_right() == x)
      y->set_child(nullptr);
    else
      y->set_child(x->get_right());
    y->decr_degree();
  }

  /*
   * calculate maxmum degreee based on amount of nodes
   */
  std::size_t fib_maximum_degree(std::size_t n)
  {
    // loga(b) = log2(b) / log2(a)
    double gold = (1 + std::sqrt(2)) / 2;
    double below = std::log2(gold);
    double top = std::log2(n);
    return top / below;
  }

  template <typename T>
  class FibHeap
  {
  public:
    using FibNodeSHPTR = std::shared_ptr<FibHeapTreeNode<T>>;
    FibHeap() = default;
    std::size_t get_n() const {return n;}
    FibNodeSHPTR get_min() const {return min;}
    void incr_n() {++n;}
    void decr_n() {--n;}
    void set_n(std::size_t m) {n = m;}
    void set_min(FibNodeSHPTR m) {min = m;}
  private:
    std::size_t n = 0;
    FibNodeSHPTR min = nullptr;
  };

  template <typename T>
  class FibHeapTreeNode
  {
  public:
    using FibNodeSHPTR = std::shared_ptr<FibHeapTreeNode>;
    FibHeapTreeNode() = default;
    FibHeapTreeNode(T k): key(k) {}
    T get_key() const {return key;}
    void set_key(T k) {key = k;}
    bool get_mark() const {return mark;}
    void set_mark() {mark = true;}
    void clear_mark() {mark = false;}
    std::size_t get_degree() const {return degree;}
    void incr_degree() {++degree;}
    void decr_degree() {--degree;}
    FibNodeSHPTR get_p() const {return p;}
    void set_p(const FibNodeSHPTR &parent) {p = parent;}
    FibNodeSHPTR get_child() const {return child;}
    void set_child(const FibNodeSHPTR &c) {child = c;}
    FibNodeSHPTR get_left() const {return left;}
    void set_left(const FibNodeSHPTR &l) {left = l;}
    FibNodeSHPTR get_right() const {return right;}
    void set_right(const FibNodeSHPTR &r) {right = r;}
  private:
    T key;
    bool mark = false;
    std::size_t degree = 0;
    FibNodeSHPTR p = nullptr;
    FibNodeSHPTR child = nullptr;
    FibNodeSHPTR left = nullptr;
    FibNodeSHPTR right = nullptr;
  };

  template <typename T>
  FibHeap<T> make_fib_heap()
  {
    return FibHeap<T>();
  }

  template <typename T>
  void fib_heap_insert(FibHeap<T> &h,
		       const std::shared_ptr<FibHeapTreeNode<T>> &x)
  {
    if(h.get_min() == nullptr)
      {
	fib_heap_list_make(x);
	h.set_min(x);
      }
    else
      {
	fib_heap_list_insert(x, h.get_min());
	if(x->get_key() < h.get_min()->get_key())
	  h.set_min(x);
      }
    h.incr_n();
  }

  template <typename T>
  std::shared_ptr<FibHeapTreeNode<T>>
  fib_heap_minimum(const FibHeap<T> &h)
  {
    return h.get_min();
  }

  template <typename T>
  FibHeap<T> fib_heap_union(const FibHeap<T> &h1, const FibHeap<T> &h2)
  {
    FibHeap<T> h;
    h.set_min(h1.get_min());
    auto h_min = h.get_min();
    auto h2_n = h2.get_min();
    // concatenate
    if(h_min == nullptr)
      h.set_min(h2_n);
    else if(h2_n != nullptr)
      do
	{
	  auto temp = h2_n;
	  h2_n = h2_n->get_right();
	  fib_heap_list_insert(temp, h_min);
	}
      while(h2_n != h2.get_min());
    if(h2.get_min() != nullptr && h1.get_min() != nullptr &&
       h2.get_min()->get_key() < h1.get_min()->get_key())
      h.set_min(h2.get_min());
    h.set_n(h1.get_n() + h2.get_n());
    return h;
  }

  template <typename T>
  std::shared_ptr<FibHeapTreeNode<T>> fib_heap_extract_min(FibHeap<T> &h)
  {
    auto z = h.get_min();
    if(z != nullptr)
      {
	auto n = z->get_child();
	for(std::size_t d = 1; d <= z->get_degree(); ++d)
	  {
	    auto temp = n->get_right();
	    fib_heap_list_insert(n, z);
	    n->set_p(nullptr);
	    n = temp;
	  }
	fib_heap_list_remove(z);
	if(z->get_right() == z)
	  h.set_min(nullptr);
	else
	  {
	    h.set_min(z->get_right());
	    consolidate(h);
	  }
	h.decr_n();
      }
    return z;
  }

  template <typename T>
  void consolidate(FibHeap<T> &h)
  {
    std::size_t max_degree = fib_maximum_degree(h.get_n());
    std::shared_ptr<FibHeapTreeNode<T>> a[max_degree + 1];
    auto w = h.get_min();
    /*
     * the reason why the former commit of code below causes a bug:
     * the min of fib heap may be removed from the root list
     * and then the former while loop is infinite.
     * Fix: compute the length of root list and take it as the while contition
     * TODO: data root list length better stored in class FibHeap
     */
    std::size_t root_list_len = 0;
    do
      {
	++root_list_len;
	w = w->get_right();
      }
    while(w != h.get_min());
    // handle each node of root list
    while(root_list_len != 0)
      {
	auto x = w;
	w = w->get_right();
	std::size_t d = x->get_degree();
	while(a[d] != nullptr)
	  {
	    auto y = a[d];
	    if(y->get_key() < x->get_key())
	      {
		auto temp = x;
		x = y;
		y = temp;
	      }
	    fib_heap_link(h, y, x);
	    a[d] = nullptr;
	    ++d;
	  }
	a[d] = x;
	--root_list_len;
      }
    // now the root list must be reconstructed
    h.set_min(nullptr);
    for(std::size_t i = 0; i <= max_degree; ++i)
      {
	if(a[i] != nullptr)
	  {
	    if(h.get_min() == nullptr)
	      {
		fib_heap_list_make(a[i]);
		h.set_min(a[i]);
	      }
	    else
	      {
		fib_heap_list_insert(a[i], h.get_min());
		if(a[i]->get_key() < h.get_min()->get_key())
		  h.set_min(a[i]);
	      }
	  }
      }
  }

  template <typename T>
  void fib_heap_link(const FibHeap<T> &h,
		     const std::shared_ptr<FibHeapTreeNode<T>> &y,
		     const std::shared_ptr<FibHeapTreeNode<T>> &x)
  {
    fib_heap_list_remove(y);
    fib_heap_list_add_child(y, x);
    y->clear_mark();
  }

  template <typename T>
  void fib_heap_decrease_key(FibHeap<T> &h,
			     const std::shared_ptr<FibHeapTreeNode<T>> &x,
			     T k)
  {
    if(x->get_key() < k)
      throw std::logic_error("new key is greater than current key");
    x->set_key(k);
    auto y = x->get_p();
    if(y != nullptr && x->get_key() < y->get_key())
      {
	cut(h, x, y);
	cascading_cut(h, y);
      }
    if(x->get_key() < h.get_min()->get_key())
      h.set_min(x);
  }

  template <typename T>
  void cut(const FibHeap<T> &h,
	   const std::shared_ptr<FibHeapTreeNode<T>> &x,
	   const std::shared_ptr<FibHeapTreeNode<T>> &y)
  {
    fib_heap_list_remove_child(x, y);
    fib_heap_list_insert(x, h.get_min());
    x->set_p(nullptr);
    x->clear_mark();
  }

  template <typename T>
  void cascading_cut(const FibHeap<T> &h,
		     const std::shared_ptr<FibHeapTreeNode<T>> &y)
  {
    auto z = y->get_p();
    if(z != nullptr)
      {
	if(!y->get_mark())
	  y->set_mark();
	else
	  {
	    cut(h, y, z);
	    cascading_cut(h, z);
	  }
      }
  }

  template <typename T>
  void fib_heap_delete(FibHeap<T> &h,
		       const std::shared_ptr<FibHeapTreeNode<T>> &x,
		       T k)
  {
    fib_heap_decrease_key(h, x, k);
    fib_heap_extract_min(h);
  }
}