-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMax_Fib_Heap.java
More file actions
491 lines (440 loc) · 14.2 KB
/
Max_Fib_Heap.java
File metadata and controls
491 lines (440 loc) · 14.2 KB
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
import java.util.ArrayList;
import java.util.List;
/*
* Author- Priyam Saikia (UFID 9414-5292)
*
Version History-
11/09/2018 - Priyam Saikia - Initial Version
11/15/2018 - Priyam Saikia - Removed return_max function
Functionality-
This class is an implementation of the Max Fibonacci Heap Data Structure
Functions:
1. void insert(Node node)
2. void increase_key(Node node, int k)
3. void update_max()
4. void cut(Node node)
5. void cascade_cut(Node node)
6. Node remove_max()
7. void meld()
8. Node merge_heaps(Node node1, Node node2)
9. void move_children(Node node)
10. void delete(Node node)
*/
public class Max_Fib_Heap {
// **************************************************************************
// Pointer to store the top of the heap - Max Value Node of the Fibonacci PQ
// **************************************************************************
private Node maxNode;
// **************************************************************************
// Class: Node
// **************************************************************************
// Node Class to define the node structure for a Fib Heap Data Structure
// **************************************************************************
public class Node {
// Degree - number of Children
int degree = 0;
// Keyword
String keyword;
// Frequency of the given keyword
int freq;
// Flag to keep track of children lost
boolean childCut = false;
// Sibling, Parent and Child Pointers
Node left, right, child, parent;
// Initialization
Node(String keyword, int freq){
this.right = this;
this.left = this;
this.parent = null;
this.degree = 0;
this.keyword = keyword;
this.freq = freq;
this.childCut = false;
}
}
// **************************************************************************
// Function: insert
// Return: void
// Input: Node node
// **************************************************************************
// Function to insert a node into the Max Fib Heap
// **************************************************************************
public void insert (Node node) {
// There are some nodes in the Fib Heap
if (maxNode != null) {
// Add to the immediate right of maxNode
node.right = maxNode.right;
maxNode.right = node;
node.left = maxNode;
node.right.left = node;
// update maxNode
if (node.freq > maxNode.freq) {
maxNode = node;
}
} else {
// There are no nodes in the Fib Heap yet
maxNode = node;
}
/*System.out.println("This node: " + node.keyword);
System.out.println("Right: " + node.right.keyword);
System.out.println("Left: " + node.left.keyword);
*/
}
// **************************************************************************
// Function: increase_key
// Return: void
// Input: Node node, int k
// **************************************************************************
// Function to increase the frequency of a node to k
// **************************************************************************
public void increase_key (Node node, int k) {
//k = new value
node.freq = k;
// if node is not a root, check if it exceeds parent's frequency
if (node.parent != null) {
// Exceeds
if (node.freq > node.parent.freq) {
Node P = node.parent;
// Cut Node and place in Top-level list
cut(node);
// Check of parent requires Cascading cut
cascade_cut(P);
// Update maxNode
update_max();
}
} else {
// Node is a root node in top level list, can increase as much as it wants
// Update maxNode
if (node.freq > maxNode.freq) {
maxNode = node;
}
}
}
// **************************************************************************
// Function: update_max
// Return: void
// Input: --
// **************************************************************************
// Function to update maxNode
// **************************************************************************
public void update_max() {
Node temp = maxNode.right;
Node orig = maxNode;
// Traverse through the top-level list and update the maxNode
while (temp!=orig) {
if (temp.freq > maxNode.freq) {
maxNode = temp;
}
temp = temp.right;
}
}
// **************************************************************************
// Function: cut
// Return: void
// Input: Node node
// **************************************************************************
// Function to perform cut operation on a Node and remove it from a
// heap and move to top level-list
// **************************************************************************
public void cut (Node node) {
Node parent = node.parent;
//remove node from parent's child list
if(parent != null) {
// More than one child exists
if (parent.degree > 1) {
if (parent.child == node) {
parent.child = node.right;
}
node.right.left = node.left;
node.left.right = node.right;
parent.degree = parent.degree - 1;
} else {
//only child
parent.child = null;
parent.degree = 0;
}
}
//add node to top list;
node.parent = null;
insert(node);
}
// **************************************************************************
// Function: cascade_cut
// Return: void
// Input: Node node
// **************************************************************************
// Function to perform cascade cut operation on a parent Node to check
// if it has a ChildCut value of True.
// **************************************************************************
public void cascade_cut (Node node) {
// if it is not a root node
if (node.parent != null) {
// Childcut Value is False - set to true
if (node.childCut==false) {
node.childCut = true;
} else {
// Childcut Value is True - Cut node from heap and move to top-level list
Node P = node.parent;
cut(node);
// Perform CasecadeCut on parent
cascade_cut(P);
}
}
}
// **************************************************************************
// Function: remove_max
// Return: Node
// Input: --
// **************************************************************************
// Function to remove max value of the heap
// **************************************************************************
public Node remove_max() {
// Temporary node
Node temp = new Node("",0);
// Keep node to return later
temp = maxNode;
if (maxNode != null) {
temp = maxNode;
// more top level nodes exist
if (maxNode.right != maxNode) {
//move all children to top level list
move_children(maxNode);
//remove node;
maxNode.right.left = maxNode.left;
maxNode.left.right = maxNode.right;
maxNode = maxNode.right; //random temp max
// Perform Meld() and Update_max() operations
meld();
update_max();
} else {
// maxNode was the only heap
// So, make children top level list
if(maxNode.degree>0) {
maxNode = maxNode.child;
maxNode.parent = null;
Node curr_node = maxNode.right;
while(curr_node != maxNode) {
curr_node.parent = null;
curr_node = curr_node.right;
}
update_max();
} else {
// Max was the only value in the list
maxNode = null;
}
}
} else {
// return error - no heaps
//System.out.println("No value in list yet!");
}
// return removed maxNode
return temp;
}
// **************************************************************************
// Function: meld
// Return: void
// Input: --
// **************************************************************************
// Function to perform Meld operation via degree wise merging using degree
// table after removeMax operation
// **************************************************************************
public void meld () {
int num_roots = 0;
List<Node> degTable = new ArrayList<Node>();
// Find number of roots in top level list
Node thisNode = maxNode;
while (thisNode.right != maxNode) {
num_roots++;
thisNode = thisNode.right;
}
if (maxNode != null) {
Node curr_node = maxNode;
Node next_node;
Boolean meld_on = true;
while (num_roots>0) {
// Increase degree table size dynamically as heaps grow bigger
while (degTable.size()<curr_node.degree+1) {
degTable.add(null);
}
meld_on = true;
next_node = curr_node.right;
// While same degree heaps exists
while(meld_on) {
while (degTable.size()<curr_node.degree+1) {
degTable.add(null);
}
int curr_degree = curr_node.degree;
Node other_heap = degTable.get(curr_degree);
if (other_heap == null) {
degTable.set(curr_degree, curr_node);
if (curr_node.freq > maxNode.freq) {
maxNode = curr_node;
}
meld_on = false;
//break;
} else {
if (other_heap == next_node) {
next_node = next_node.right;
}
curr_node = merge_heaps(curr_node, other_heap);
// Set older degree null as we merged the heaps
degTable.set(curr_degree, null);
// Increase table size dynamically
if (degTable.size()<curr_node.degree+1) {
while (degTable.size()<curr_node.degree+1) {
degTable.add(null);
}
}
Node another_heap = degTable.get(curr_node.degree);
if(another_heap == null) {
meld_on = false;
} else {
// phir se karo
meld_on = true;
}
}
}
// Set current degree as we merged the heaps
degTable.set(curr_node.degree, curr_node);
if (curr_node.freq > maxNode.freq)
maxNode = curr_node;
curr_node = next_node;
num_roots--;
}
}
}
// **************************************************************************
// Function: merge_heaps
// Return: Node
// Input: Node node1, Node node2
// **************************************************************************
// Merge two given heaps of same degree based on which one has higher frequency value
// **************************************************************************
public Node merge_heaps (Node node1, Node node2) {
if (node1.freq > node2.freq) {
node2.parent = node1;
//removing smaller node from top level list
node2.left.right = node2.right;
node2.right.left = node2.left;
Node temp = node1.child;
// If children of bigger node exists, make smaller node their sibling
if (temp != null) {
node2.right = temp;
node2.left = temp.left;
temp.left.right = node2;
temp.left = node2;
//node1.child = node2;
//temp.parent = null;
} else {
// smaller node is the only child of bigger node
node2.right = node2;
node2.left = node2;
node1.child = node2;
}
// Increase degree value
node1.degree = node1.degree + 1;
// Newly created child - ChildCut value -s set to false
node2.childCut = false;
maxNode = node1;
return node1;
} else {
node1.parent = node2;
//removing from top level list
node1.left.right = node1.right;
node1.right.left = node1.left;
Node temp = node2.child;
if (temp != null) {
node1.right = temp;
node1.left = temp.left;
temp.left.right = node1;
temp.left = node1;
//node2.child = node1;
//temp.parent = null;
} else {
node1.right = node1;
node1.left = node1;
node2.child = node1;
}
node2.degree = node2.degree + 1;
node1.childCut = false;
maxNode = node2;
return node2;
}
}
// **************************************************************************
// Function: move_children
// Return: void
// Input: Node node
// **************************************************************************
// Function to remove all children from a Node
// **************************************************************************
public void move_children(Node node) {
Node child = node.child;
if (child !=null) {
Node temp1 = child;
Node temp2 = maxNode.right;
child.left.right = maxNode.right;
maxNode.right.left = child.left;
child.left = maxNode;
maxNode.right = child;
while(temp1!=temp2) {
temp1.parent = null;
temp1 = temp1.right;
}
}
node.child = null;
}
// **************************************************************************
// Function: delete
// Return: void
// Input: Node node
// **************************************************************************
// Arbitary Delete
// **************************************************************************
public void delete (Node node) {
if (node.degree > 0) {
//children exists - move them
move_children(node);
} else {
node.child = null;
}
if (node.parent != null) {
// Not a root node
Node parent = node.parent;
//remove node from parent's child list
if (parent.degree > 1) {
if (parent.child == node) {
parent.child = node.right;
}
node.right.left = node.left;
node.left.right = node.right;
parent.degree = parent.degree - 1;
} else {
//only child
parent.child = null;
parent.degree = 0;
}
} else {
// if it is a root node
if (node.right != node) {
// remove from top level list
node.right.left = node.left;
node.left.right = node.right;
// Update maxNode
if (maxNode == node) {
maxNode = node.right;
update_max();
}
node = null;
} else {
//empty list
maxNode = null;
node = null;
}
}
}
// return maxNode
//public Node return_max() {
// return maxNode;
//}
}