-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibonacciHeap.java
More file actions
379 lines (345 loc) · 10.7 KB
/
Copy pathFibonacciHeap.java
File metadata and controls
379 lines (345 loc) · 10.7 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
import java.lang.Math;
/**
* FibonacciHeap
* <p>
* An implementation of fibonacci heap over non-negative integers.
*/
public class FibonacciHeap {
private HeapNode min = null;
private int size = 0;
private int marked = 0;
static int links = 0;
static int cuts = 0;
/**
* public boolean empty()
* <p>
* precondition: none
* <p>
* The method returns true if and only if the heap
* is empty.
*/
public boolean empty() {
return min == null;
}
/**
* public HeapNode insert(int key)
* <p>
* Creates a node (of type HeapNode) which contains the given key, and inserts it into the heap.
*/
public HeapNode insert(int key) {
HeapNode node = new HeapNode(key);
if (!empty()) {
this.concate(node, this.min, false);
} else {
this.min = node;
}
this.checkNDUpdateMin(node);
size += 1;
return node;
}
/**
* public void deleteMin()
* <p>
* Delete the node containing the minimum key.
*/
public void deleteMin() {
if (size == 0) {
return;
}
if (size == 1) {
min = null;
} else {
HeapNode origMin = this.min;
if (origMin.child != null) {
this.concate(origMin.child, this.min, true);
}
this.min = this.min.next;
this.disconnect(origMin);
HeapNode first = this.min;
HeapNode temp = first;
do {
if (temp.mark) {
marked -= 1;
}
temp.mark = false;
checkNDUpdateMin(temp);
temp.parent = null;
temp = temp.next;
} while (temp != first);
this.successiveLinking();
}
this.size -= 1;
return;
}
/*
counts the number of trees in the heap
*/
private int numOfTrees() {
int count = 0;
HeapNode first = this.min;
HeapNode temp = first;
if (!empty()) {
do {
count += 1;
temp = temp.next;
} while (temp != first);
}
return count;
}
/*
compares the nodeToCheck.key to min.key to see if the nodeToCheck.key is the minimal
key of the tree, and updates accordingly.
*/
private void checkNDUpdateMin(HeapNode nodeToCheck) {
if (nodeToCheck.getKey() < this.min.getKey()) {
this.min = nodeToCheck;
}
}
/**
* public HeapNode findMin()
* <p>
* Return the node of the heap whose key is minimal.
*/
public HeapNode findMin() {
return this.min;
}
/**
* public void meld (FibonacciHeap heap2)
* <p>
* Meld the heap with heap2
*/
public void meld(FibonacciHeap heap2) {
this.concate(heap2.min, this.min, true);
this.checkNDUpdateMin(heap2.min);
return;
}
/**
* if concateAllList is true, it concatenates the list - nodeToConnect with this,
* <p>
* if concateAllList is false, it concatenates the single node - nodeToConnect with this
*/
public void concate(HeapNode nodeToConnect, HeapNode listToConnect, boolean concateAllList) {
HeapNode list1_first = listToConnect;
HeapNode list2_first = nodeToConnect;
HeapNode list1_last = listToConnect.prev;
HeapNode list2_last = concateAllList ? nodeToConnect.prev : nodeToConnect;
list1_first.prev = list2_last;
list2_last.next = list1_first;
list2_first.prev = list1_last;
list1_last.next = list2_first;
}
/**
* public int size()
* <p>
* Return the number of elements in the heap
*/
public int size() {
return this.size;
}
/**
* public int[] countersRep()
* <p>
* Return a counters array, where the value of the i-th entry is the number of trees of order i in the heap.
*/
public int[] countersRep() {
int arrSize = 0;
HeapNode nextNode = this.min;
do {
if (arrSize < nextNode.rank) {
arrSize = nextNode.rank;
nextNode = nextNode.next;
}
}
while (nextNode != this.min);
int[] arr = new int[arrSize + 1];
HeapNode temp = this.min;
do {
int index = temp.rank;
arr[index] += 1;
temp = temp.next;
} while (temp != this.min);
return arr;
}
/**
* public void delete(HeapNode x)
* <p>
* Deletes the node x from the heap.
*/
public void delete(HeapNode x) {
int NodeToDeleteKey = x.getKey();
int NodeMinKey = min.getKey();
decreaseKey(x, (1 + (NodeToDeleteKey - NodeMinKey)));
deleteMin();
return;
}
/**
* public void decreaseKey(HeapNode x, int delta)
* <p>
* The function decreases the key of the node x by delta. The structure of the heap should be updated
* to reflect this chage (for example, the cascading cuts procedure should be applied if needed).
*/
public void decreaseKey(HeapNode x, int delta) {
x.key -= delta;
int nodeKey = x.getKey();
if (x.parent != null) {
int parentKey = x.parent.getKey();
if (nodeKey < parentKey) {
cascading(x);
}
}
checkNDUpdateMin(x);
return;
}
/*
*makes the cascading cuts in decreaseKey
*and updates the relevant fields
*/
private void cascading(HeapNode nodeToCascade) {
HeapNode parent = nodeToCascade.parent;
disconnect(nodeToCascade);
concate(nodeToCascade, this.min, false);
if (nodeToCascade.mark) {
this.marked -= 1;
nodeToCascade.mark = false;
}
this.cuts += 1;
if (parent.parent != null) { //parent is not a root
if (parent.mark) {
cascading(parent);
} else {
parent.mark = true;
//parent.rank -= 1;
this.marked += 1;
}
}
}
/*
*disconnects the single node from his linkedList
*/
private HeapNode disconnect(HeapNode nodeToDisconnect) {
HeapNode parent = nodeToDisconnect.parent;
/* if parent.child is nodeToDisconnect
we would like parent.child to be the next child
because we want to keep all the other elements in child*/
if (parent != null) {
parent.rank -= 1;
if (parent.child == nodeToDisconnect) {
parent.child = nodeToDisconnect.next;
if (nodeToDisconnect.next == nodeToDisconnect) {
parent.child = null;
}
}
}
HeapNode prevNode = nodeToDisconnect.prev;
HeapNode nextNode = nodeToDisconnect.next;
prevNode.next = nextNode;
nextNode.prev = prevNode;
nodeToDisconnect.prev = nodeToDisconnect;
nodeToDisconnect.next = nodeToDisconnect;
nodeToDisconnect.parent = null;
return nodeToDisconnect;
}
/**
* public int potential()
* <p>
* This function returns the current potential of the heap, which is:
* Potential = #trees + 2*#marked
* The potential equals to the number of trees in the heap plus twice the number of marked nodes in the heap.
*/
public int potential() {
int trees = this.numOfTrees();
return (trees + 2 * marked);
}
/*
* makes a number of link operations which each gets as input two trees of the same rank,
* and generates a tree of rank bigger by one,
* by hanging the tree which has larger value in its root
* on the tree which has smaller value in its root
* Successive Linking - One Pass
*/
private void successiveLinking() {
int log = (int) (Math.log(size) / Math.log(2));
HeapNode[] buckets = new HeapNode[log + 2];
for (int ind = 0; ind < log + 2; ind++) {
buckets[ind] = null;
}
HeapNode first = this.min;
HeapNode curr = first;
do {
HeapNode temp = curr; //did it because curr may not stay at the roots list
curr = curr.next;
int i = temp.rank;
if (buckets[i] == null) {
buckets[i] = temp;
} else {
link(temp, buckets[i]);
buckets[i] = null;
}
} while (curr != first);
}
/*
* link two nodes with the same rank
* the new root is the node with the smaller key
* return the new root node
*/
private HeapNode link(HeapNode node1, HeapNode node2) {
HeapNode bigger = node1;
HeapNode smaller = node2;
if (node1.getKey() < node2.getKey()) { //check which node will be the root (the smaller)
bigger = node2;
smaller = node1;
}
disconnect(bigger); //disconnects the bigger from the roots list
bigger.parent = smaller;
if (smaller.child == null) {
smaller.child = bigger;
} else {
concate(bigger, smaller.child, false); //join the node to the child list
}
smaller.rank += 1;
links += 1;
return smaller; //return the root
}
/**
* public static int totalLinks()
* <p>
* This static function returns the total number of link operations made during the run-time of the program.
* A link operation is the operation which gets as input two trees of the same rank, and generates a tree of
* rank bigger by one, by hanging the tree which has larger value in its root on the tree which has smaller value
* in its root.
*/
public static int totalLinks() {
return links;
}
/**
* public static int totalCuts()
* <p>
* This static function returns the total number of cut operations made during the run-time of the program.
* A cut operation is the operation which diconnects a subtree from its parent (during decreaseKey/delete methods).
*/
public static int totalCuts() {
return cuts;
}
/**
* public class HeapNode
* <p>
* If you wish to implement classes other than FibonacciHeap
* (for example HeapNode), do it in this file, not in
* another file
*/
public class HeapNode {
public int key;
int rank = 0;
boolean mark = false;
HeapNode child = null;
HeapNode next = this;
HeapNode prev = this;
HeapNode parent = null;
public HeapNode(int key) {
this.key = key;
}
public int getKey() {
return this.key;
}
}
}