-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibonacciHeap.java
More file actions
444 lines (401 loc) · 14.3 KB
/
FibonacciHeap.java
File metadata and controls
444 lines (401 loc) · 14.3 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
import java.util.*;
//import java.lang.Math;
/**
* Fibonacci Heap class.
* @author Alisha Sprinkle
* @version 11/9/2019
*/
public class FibonacciHeap {
private FibonacciNode minNode;
private int nodeCount;
/**
* Constructor.
*/
public FibonacciHeap() {
minNode = null;
nodeCount = 0;
}
/**
* Insert.
* @param newNode a thing
*/
public void insert(FibonacciNode newNode) {
// heap is not empty
if (minNode != null) {
// Add in the new Node to the right of our min
newNode.left = minNode;
newNode.right = minNode.right;
minNode.right = newNode;
// newNode right is now the minNodes right
// it could have already had more right elements
if (newNode.right != null) {
// set the right elements left
newNode.right.left = newNode;
}
// it didnt have right elements
else {
newNode.right = minNode;
minNode.left = newNode;
}
// Move minNode to the newNode if key is smaller
if (newNode.key < minNode.key) {
minNode = newNode;
}
}
else {
minNode = newNode;
}
nodeCount++;
}
/**
* Cut.
* @param cutNode a thing
* @param fromNode a thing
*/
public void cut(FibonacciNode cutNode, FibonacciNode fromNode) {
// Old technique did not work
// I forgot to include a marked flag
// and I was getting confused about who I was cutting
// variable name change should help
// remove cutNode fromNode.
// Decrease the degree of fromNode since node is gone
cutNode.left.right = cutNode.right;
cutNode.right.left = cutNode.left;
fromNode.degree--;
// We could hae removed the last thing from the fromNode
// No more children then
if (fromNode.degree == 0) {
fromNode.child = null;
}
// Could have removed a directchild.
// Need to move fromNode child to the node to the
// right of the cutNode
if (fromNode.child == cutNode) {
fromNode.child = cutNode.right;
}
// cutNode now needs to be in the root list
// Same as saying attach it to minNode
cutNode.left = minNode;
cutNode.right = minNode.right;
minNode.right = cutNode;
cutNode.right.left = cutNode;
// now that cutNode is in the root list it
// does not have a arent
cutNode.parent = null;
cutNode.marked = false;
}
/**
* Cascade Cut.
* @param cutNode a thing
*/
public void cascadeCut(FibonacciNode cutNode) {
// In my implementation of decreaseKey I was only cutting
// Cascade cut is used to help keep thet time complexity small
// Allows you to cut the edge joining a node to a parent
// the you make the new node a root.
FibonacciNode cutNodeParent = cutNode.parent;
if (cutNodeParent != null) {
// if the parent is not marked we will mark it
if (cutNode.marked == false) {
cutNode.marked = true;
}
// if it was marked cut it from the parent
else {
cut(cutNode, cutNodeParent);
// now we need to cut the parent as well
// hence the cascade
cascadeCut(cutNodeParent);
}
}
}
/**
* Make Child.
* @param child a thing
* @param parent a thing
*/
public void makeChild(FibonacciNode child, FibonacciNode parent) {
// take child out of the root list
child.left.right = child.right;
child.right.left = child.left;
child.parent = parent;
if (parent.child == null) {
child.right = child;
child.left = child;
parent.child = child;
}
else {
child.left = parent.child;
child.right = parent.child.right;
parent.child.right = child;
parent.right.left = child;
}
child.marked = false;
// the parent has a new child so degree increases
parent.degree++;
}
/**
* Decrease Key.
* @param decNode a thing
* @param key a thing
*/
public void decreaseKey(FibonacciNode decNode, int key) {
// set the key
decNode.key = key;
FibonacciNode decNodeParent = decNode.parent;
if ((decNodeParent != null) && (decNode.key < decNodeParent.key)) {
cut(decNode, decNodeParent);
// it isnt enough to only cut the node from the parent
// parent may have to be cut as well
cascadeCut(decNodeParent);
}
if (decNode.key < minNode.key) {
minNode = decNode;
}
}
/**
* Find Minimum.
* @return minNode a thing
*/
public FibonacciNode findMin() {
return minNode;
}
/**
* Delete Minimum.
*/
public void deleteMin() {
FibonacciNode tempMin = minNode;
FibonacciNode tempMinPtr = tempMin;
FibonacciNode tempMinChild = null;
if (tempMin != null) {
if (tempMin.child != null) {
tempMinChild = tempMin.child;
do {
tempMinPtr = tempMinChild.right;
minNode.left.right = tempMinChild;
tempMinChild.right = minNode;
tempMinChild.left = minNode.left;
minNode.left = tempMinChild;
if (tempMinChild.key < minNode.key) {
minNode = tempMinChild;
}
tempMinChild.parent = null;
tempMinChild = tempMinPtr;
}
while (tempMinPtr != tempMin.child);
}
tempMin.left.right = tempMin.right;
tempMin.right.left = tempMin.left;
minNode = tempMin.left;
if (tempMin == tempMin.right && tempMin.child == null) {
minNode = null;
}
else {
minNode = tempMin.left;
consolidate();
}
nodeCount--;
}
if (nodeCount == 0) {
minNode = null;
}
}
/**
* Delete.
* @param dNode a thing
*/
public void delete(FibonacciNode dNode) {
if (dNode == null) {
System.out.println("Can't delete a null node!");
}
if (dNode == minNode) {
deleteMin();
}
else {
decreaseKey(dNode, -1);
deleteMin();
}
}
/**
* Consolidate.
*/
public void consolidate() {
int sizeInt = nodeCount + 50;
List<FibonacciNode> degreeTable = new ArrayList<FibonacciNode>(sizeInt);
for (int i = 0; i < sizeInt; i++) {
degreeTable.add(null);
}
// need the number of roots
int rootCount = 0;
FibonacciNode tempMin = minNode;
if (tempMin != null) {
rootCount++;
tempMin = tempMin.right;
while (tempMin != minNode) {
rootCount++;
tempMin = tempMin.right;
}
}
while (rootCount > 0) {
int tempMinDegree = tempMin.degree;
FibonacciNode tempMinRight = tempMin.right;
while (true) {
FibonacciNode compNode = degreeTable.get(tempMinDegree);
if (compNode == null) {
break;
}
if (tempMin.key > compNode.key) {
FibonacciNode temp = compNode;
compNode = tempMin;
tempMin = temp;
}
makeChild(tempMin, compNode);
degreeTable.set(tempMinDegree, null);
tempMinDegree++;
}
degreeTable.set(tempMinDegree, tempMin);
tempMin = tempMinRight;
rootCount--;
}
minNode = null;
for (int i = 0; i < sizeInt; i++) {
FibonacciNode cNode = degreeTable.get(i);
if (cNode == null) {
continue;
}
if (minNode != null) {
cNode.left.right = cNode.right;
cNode.right.left = cNode.left;
cNode.left = minNode;
cNode.right = minNode.right;
minNode.right = cNode;
cNode.right.left = cNode;
if (cNode.key < minNode.key) {
minNode = cNode;
}
}
else {
minNode = cNode;
}
}
}
/**
* DisplayHeap.
*/
public void displayHeap() {
FibonacciNode pointer = minNode;
String tree = "";
if (pointer == null) {
tree += "Heap is Empty";
}
else {
do {
tree += pointer.key;
pointer = pointer.right;
if (pointer != minNode) {
tree += "->";
}
}
while (pointer != minNode && pointer.right != null);
tree += "\n" + "The heap has " + nodeCount + " nodes.";
}
System.out.println(tree);
}
/**
* Test1.
* @param fh a thing
*/
public void test1(FibonacciHeap fh) {
System.out.println("Running test 1");
FibonacciNode n1 = new FibonacciNode(6);
FibonacciNode n2 = new FibonacciNode(4);
FibonacciNode n3 = new FibonacciNode(8);
fh.insert(n1);
fh.insert(n2);
fh.insert(n3);
System.out.println("-----------------------------------------------------");
System.out.println("Testing insert. Heap should contain 4,6,8");
fh.displayHeap();
System.out.println("-----------------------------------------------------\n");
System.out.println("-----------------------------------------------------");
System.out.println("Decreasing key of n2 (4) to 3");
fh.decreaseKey(n2, 3);
fh.displayHeap();
System.out.println("-----------------------------------------------------\n");
System.out.println("-----------------------------------------------------");
System.out.println(("Testing find min. Should return 3"));
System.out.println(fh.findMin().key);
System.out.println("-----------------------------------------------------\n");
System.out.println("-----------------------------------------------------");
System.out.println("Testing delete min. Heap should contain 6,8");
fh.deleteMin();
System.out.println("The new min should be 6. Min is: " + fh.findMin().key);
fh.displayHeap();
System.out.println("-----------------------------------------------------\n");
System.out.println("-----------------------------------------------------");
System.out.println("Testing delete on 6. Heap should contain 8");
fh.delete(n1);
System.out.println("The new min should be 8. Min is: " + fh.findMin().key);
fh.displayHeap();
System.out.println("-----------------------------------------------------\n");
System.out.println("-----------------------------------------------------");
System.out.println("Testing delete on 8. Heap should contain nothing.");
fh.delete(n3);
System.out.println("The new min should be null. Min is: " + fh.findMin());
fh.displayHeap();
System.out.println("-----------------------------------------------------\n");
}
/**
* Test2.
* @param fh a thing
*/
public void test2(FibonacciHeap fh) {
System.out.println("Running test 2");
FibonacciNode n1 = new FibonacciNode(6);
FibonacciNode n2 = new FibonacciNode(4);
FibonacciNode n3 = new FibonacciNode(8);
fh.insert(n1);
fh.insert(n2);
fh.insert(n3);
System.out.println("-----------------------------------------------------");
System.out.println("Testing insert. Heap should contain 4,6,8");
fh.displayHeap();
System.out.println("-----------------------------------------------------\n");
System.out.println("-----------------------------------------------------");
System.out.println("Decreasing key of n2 (4) to 3");
fh.decreaseKey(n2, 3);
fh.displayHeap();
System.out.println("-----------------------------------------------------\n");
System.out.println("-----------------------------------------------------");
System.out.println(("Testing find min. Should return 3"));
System.out.println(fh.findMin().key);
System.out.println("-----------------------------------------------------\n");
System.out.println("-----------------------------------------------------");
System.out.println("Testing delete min. Heap should contain 6,8");
fh.deleteMin();
System.out.println("The new min should be 6. Min is: " + fh.findMin().key);
fh.displayHeap();
System.out.println("-----------------------------------------------------\n");
System.out.println("-----------------------------------------------------");
System.out.println("Testing delete on 8. Heap should contain 6");
fh.delete(n3);
System.out.println("The new min should be 6. Min is: " + fh.findMin().key);
fh.displayHeap();
System.out.println("-----------------------------------------------------\n");
System.out.println("-----------------------------------------------------");
System.out.println("Testing delete on 6. Heap should contain nothing.");
fh.delete(n2);
System.out.println("The new min should be null. Min is: " + fh.findMin());
fh.displayHeap();
System.out.println("-----------------------------------------------------\n");
}
/**
* Main method.
* @param args commandline arguments (not used)
*/
public static void main(String[] args) {
System.out.println("Creating Fibonacci Heap");
FibonacciHeap fh = new FibonacciHeap();
fh.test1(fh);
fh.test2(fh);
}
}