Skip to content

Commit 92f114c

Browse files
committed
Changed code and added test
1 parent 27e086c commit 92f114c

File tree

3 files changed

+314
-157
lines changed

3 files changed

+314
-157
lines changed

src/main/java/com/williamfiset/algorithms/datastructures/fibonacci_heap/Fibonacci.java renamed to src/main/java/com/williamfiset/algorithms/datastructures/fibonacciheap/FibonacciHeap.java

Lines changed: 9 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
package com.williamfiset.algorithms.datastructures.fibonacciheap;
2+
3+
//Disclaimer: implementation based on "http://staff.ustc.edu.cn/~csli/graduate/algorithms/book6/chap21.htm"
4+
//Credits to the respective owner for code
5+
16
import static java.lang.Math.floor;
27
import static java.lang.Math.log;
38
import static java.lang.Math.sqrt;
@@ -13,84 +18,37 @@
1318
import java.util.Set;
1419
import java.util.Stack;
1520

16-
/**
17-
* A Fibonacci Heap implementation based on
18-
* <a href="http://staff.ustc.edu.cn/~csli/graduate/algorithms/book6/chap21.htm">University of Science and Technology of
19-
* China</a> lesson.
20-
*
21-
* <p><b>Note 1</b>: this class is NOT thread safe!</p>
22-
*
23-
* <p><b>Note 2</b>: this class doesn't support {@code null} values</p>
24-
*
25-
* @param <E> the type of elements held in this collection.
26-
*/
21+
2722
public final class FibonacciHeap<E>
2823
implements Queue<E>
2924
{
3025

31-
/**
32-
* The <i>Phi</i> constant value.
33-
*/
26+
3427
private static final double LOG_PHI = log( ( 1 + sqrt( 5 ) ) / 2 );
3528

36-
/**
37-
* A simple index of stored elements.
38-
*/
3929
private final Set<E> elementsIndex = new HashSet<E>();
4030

41-
/**
42-
* The comparator, or null if priority queue uses elements'
43-
* natural ordering.
44-
*/
4531
private final Comparator<? super E> comparator;
4632

47-
/**
48-
* The number of nodes currently in {@code H} is kept in {@code n[H]}.
49-
*/
5033
private int size = 0;
5134

52-
/**
53-
* {@code t(H)} the number of trees in the root list.
54-
*/
5535
private int trees = 0;
5636

57-
/**
58-
* {@code m(H)} the number of marked nodes in {@code H}.
59-
*/
6037
private int markedNodes = 0;
6138

62-
/**
63-
* The root of the tree containing a minimum key {@code min[H]}.
64-
*/
6539
private FibonacciHeapNode<E> minimumNode;
6640

67-
/**
68-
* Creates a {@link FibonacciHeap} that orders its elements according to their natural ordering.
69-
*/
7041
public FibonacciHeap()
7142
{
7243
this( null );
7344
}
7445

75-
/**
76-
* Creates a {@link FibonacciHeap} that orders its elements according to the specified comparator.
77-
*
78-
* @param comparator the comparator that will be used to order this queue.
79-
* If null, the natural ordering of the elements will be used.
80-
*/
46+
8147
public FibonacciHeap( /* @Nullable */Comparator<? super E> comparator )
8248
{
8349
this.comparator = comparator;
8450
}
8551

86-
/**
87-
* Moves the target node in the {@code H} root nodes.
88-
*
89-
* @param node the node has to be moved in the {@code H} root nodes
90-
* @see #add(Object)
91-
* @see #consolidate()
92-
* @see #cut(FibonacciHeapNode, FibonacciHeapNode)
93-
*/
9452
private void moveToRoot( FibonacciHeapNode<E> node )
9553
{
9654
// 8' if min[H] = NIL
@@ -119,21 +77,6 @@ private void moveToRoot( FibonacciHeapNode<E> node )
11977
}
12078
}
12179

122-
/**
123-
* {@inheritDoc}
124-
*
125-
* <pre>FIB-HEAP-INSERT(H, x)
126-
* 1 degree[x] &larr; 0
127-
* 2 p[x] &larr; NIL
128-
* 3 child[x] &larr; NIL
129-
* 4 left[x] &larr; x
130-
* 5 right[x] &larr; x
131-
* 6 mark[x] &larr; FALSE
132-
* 7 concatenate the root list containing x with root list H
133-
* 8 if min[H] = NIL or key[x] &lt; key[min[H]]
134-
* 9 then min[H] &larr; x
135-
* 10 n[H] &larr; n[H] + 1</pre>
136-
*/
13780
public boolean add( E e )
13881
{
13982
if ( e == null )
@@ -311,23 +254,6 @@ public E peek()
311254
return minimumNode.getElement();
312255
}
313256

314-
/**
315-
* {@inheritDoc}
316-
*
317-
* <pre>FIB-HEAP-EXTRACT-MIN(H)
318-
* 1 z &larr; min[H]
319-
* 2 if z &ne; NIL
320-
* 3 then for each child x of z
321-
* 4 do add x to the root list of H
322-
* 5 p[x] &larr; NIL
323-
* 6 remove z from the root list of H
324-
* 7 if z = right[z]
325-
* 8 then min[H] &larr; NIL
326-
* 9 else min[H] &larr; right[z]
327-
* 10 CONSOLIDATE(H)
328-
* 11 n[H] &larr; n[H] - 1
329-
* 12 return z</pre>
330-
*/
331257
public E poll()
332258
{
333259
// 2 if z &ne; NIL
@@ -400,30 +326,6 @@ public E remove()
400326
return poll();
401327
}
402328

403-
/**
404-
* Implements the {@code CONSOLIDATE(H)} function.
405-
*
406-
* <pre>CONSOLIDATE(H)
407-
* 1 for i &larr; 0 to D(n[H])
408-
* 2 do A[i] &larr; NIL
409-
* 3 for each node w in the root list of H
410-
* 4 do x &larr; w
411-
* 5 d &larr; degree[x]
412-
* 6 while A[d] &ne; NIL
413-
* 7 do y &larr; A[d]
414-
* 8 if key[x] &gt; key[y]
415-
* 9 then exchange x &harr; y
416-
* 10 FIB-HEAP-LINK(H,y,x)
417-
* 11 A[d] &larr; NIL
418-
* 12 d &larr; d + 1
419-
* 13 A[d] &larr; x
420-
* 14 min[H] &larr; NIL
421-
* 15 for i &larr; 0 to D(n[H])
422-
* 16 do if A[i] &ne; NIL
423-
* 17 then add A[i] to the root list of H
424-
* 18 if min[H] = NIL or key[A[i]] &le; key[min[H]]
425-
* 19 then min[H] &larr; A[i]</pre>
426-
*/
427329
private void consolidate()
428330
{
429331
if ( isEmpty() )
@@ -525,17 +427,6 @@ private void consolidate()
525427
}
526428
}
527429

528-
/**
529-
* Implements the {@code FIB-HEAP-LINK(H, y, x)} function.
530-
*
531-
* <pre>FIB-HEAP-LINK(H, y, x)
532-
* 1 remove y from the root list of H
533-
* 2 make y a child of x, incrementing degree[x]
534-
* 3 mark[y] FALSE</pre>
535-
*
536-
* @param y the node has to be removed from the root list
537-
* @param x the node has to to become fater of {@code y}
538-
*/
539430
private void link( FibonacciHeapNode<E> y, FibonacciHeapNode<E> x )
540431
{
541432
// 1 remove y from the root list of H
@@ -566,18 +457,6 @@ private void link( FibonacciHeapNode<E> y, FibonacciHeapNode<E> x )
566457
markedNodes++;
567458
}
568459

569-
/**
570-
* Implements the {@code CUT(H,x,y)} function.
571-
*
572-
* <pre>CUT(H,x,y)
573-
* 1 remove x from the child list of y, decrementing degree[y]
574-
* 2 add x to the root list of H
575-
* 3 p[x] &larr; NIL
576-
* 4 mark[x] &larr; FALSE</pre>
577-
*
578-
* @param x the node has to be removed from {@code y} children
579-
* @param y the node has to be updated
580-
*/
581460
private void cut( FibonacciHeapNode<E> x, FibonacciHeapNode<E> y )
582461
{
583462
// add x to the root list of H
@@ -593,19 +472,6 @@ private void cut( FibonacciHeapNode<E> x, FibonacciHeapNode<E> y )
593472
markedNodes--;
594473
}
595474

596-
/**
597-
* Implements the {@code CASCADING-CUT(H,y)} function.
598-
*
599-
* <pre>CASCADING-CUT(H,y)
600-
* 1 z &larr; p[y]
601-
* 2 if z &ne; NIL
602-
* 3 then if mark[y] = FALSE
603-
* 4 then mark[y] &larr; TRUE
604-
* 5 else CUT(H,y,z)
605-
* 6 CASCADING-CUT(H,z)</pre>
606-
*
607-
* @param y the target node to apply CASCADING-CUT
608-
*/
609475
private void cascadingCut( FibonacciHeapNode<E> y )
610476
{
611477
// z <- p[y]
@@ -631,26 +497,12 @@ private void cascadingCut( FibonacciHeapNode<E> y )
631497
}
632498
}
633499

634-
/**
635-
* The potential of Fibonacci heap {@code H} is then defined by
636-
* {@code t(H) + 2m(H)}.
637-
*
638-
* @return The potential of this Fibonacci heap.
639-
*/
640500
public int potential()
641501
{
642502
return trees + 2 * markedNodes;
643503
}
644504

645-
/**
646-
* Compare the given objects according to to the specified comparator if not null,
647-
* according to their natural ordering otherwise.
648-
*
649-
* @param o1 the first {@link FibonacciHeap} node to be compared
650-
* @param o2 the second {@link FibonacciHeap} node to be compared
651-
* @return a negative integer, zero, or a positive integer as the first argument is
652-
* less than, equal to, or greater than the second
653-
*/
505+
654506
private int compare( FibonacciHeapNode<E> o1, FibonacciHeapNode<E> o2 )
655507
{
656508
if ( comparator != null )
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package com.williamfiset.algorithms.datastructures.fibonacciheap;
2+
3+
//Credits to the respective owner for the code
4+
5+
final class FibonacciHeapNode<E>
6+
{
7+
8+
9+
private final E element;
10+
11+
private FibonacciHeapNode<E> parent;
12+
13+
private FibonacciHeapNode<E> left = this;
14+
15+
private FibonacciHeapNode<E> right = this;
16+
17+
private FibonacciHeapNode<E> child;
18+
19+
private int degree;
20+
21+
private boolean marked;
22+
23+
public FibonacciHeapNode( E element )
24+
{
25+
// 1 degree[x] &larr; 0
26+
degree = 0;
27+
// 2 p[x] <- NIL
28+
setParent( null );
29+
// 3 child[x] <- NIL
30+
setChild( null );
31+
// 4 left[x] <- x
32+
setLeft( this );
33+
// 5 right[x] <- x
34+
setRight( this );
35+
// 6 mark[x] <- FALSE
36+
setMarked( false );
37+
38+
// set the adapted element
39+
this.element = element;
40+
}
41+
42+
public FibonacciHeapNode<E> getParent()
43+
{
44+
return parent;
45+
}
46+
47+
public void setParent( FibonacciHeapNode<E> parent )
48+
{
49+
this.parent = parent;
50+
}
51+
52+
public FibonacciHeapNode<E> getLeft()
53+
{
54+
return left;
55+
}
56+
57+
public void setLeft( FibonacciHeapNode<E> left )
58+
{
59+
this.left = left;
60+
}
61+
62+
public FibonacciHeapNode<E> getRight()
63+
{
64+
return right;
65+
}
66+
67+
public void setRight( FibonacciHeapNode<E> right )
68+
{
69+
this.right = right;
70+
}
71+
72+
public FibonacciHeapNode<E> getChild()
73+
{
74+
return child;
75+
}
76+
77+
public void setChild( FibonacciHeapNode<E> child )
78+
{
79+
this.child = child;
80+
}
81+
82+
public int getDegree()
83+
{
84+
return degree;
85+
}
86+
87+
public void incraeseDegree()
88+
{
89+
degree++;
90+
}
91+
92+
public void decraeseDegree()
93+
{
94+
degree--;
95+
}
96+
97+
public boolean isMarked()
98+
{
99+
return marked;
100+
}
101+
102+
public void setMarked( boolean marked )
103+
{
104+
this.marked = marked;
105+
}
106+
107+
public E getElement()
108+
{
109+
return element;
110+
}
111+
112+
@Override
113+
public String toString()
114+
{
115+
return element.toString();
116+
}
117+
118+
}

0 commit comments

Comments
 (0)