fibonacciheap.java
来自「JAVA图论的算法包。用过觉得还不错」· Java 代码 · 共 612 行 · 第 1/2 页
JAVA
612 行
/* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Creator: Barak Naveh (barak_naveh@users.sourceforge.net)
*
* (C) Copyright 2003-2007, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation,
* Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* --------------------------
* FibonnaciHeap.java
* --------------------------
* (C) Copyright 1999-2003, by Nathan Fiedler and Contributors.
*
* Original Author: Nathan Fiedler
* Contributor(s): John V. Sichi
*
* $Id: FibonacciHeap.java 603 2008-06-28 07:51:50Z perfecthash $
*
* Changes
* -------
* 03-Sept-2003 : Adapted from Nathan Fiedler (JVS);
*
* Name Date Description
* ---- ---- -----------
* nf 08/31/97 Initial version
* nf 09/07/97 Removed FibHeapData interface
* nf 01/20/01 Added synchronization
* nf 01/21/01 Made Node an inner class
* nf 01/05/02 Added clear(), renamed empty() to
* isEmpty(), and renamed printHeap()
* to toString()
* nf 01/06/02 Removed all synchronization
*
*/
package org.jgrapht.util;
import java.util.*;
/**
* This class implements a Fibonacci heap data structure. Much of the code in
* this class is based on the algorithms in the "Introduction to Algorithms"by
* Cormen, Leiserson, and Rivest in Chapter 21. The amortized running time of
* most of these methods is O(1), making it a very fast data structure. Several
* have an actual running time of O(1). removeMin() and delete() have O(log n)
* amortized running times because they do the heap consolidation. If you
* attempt to store nodes in this heap with key values of -Infinity
* (Double.NEGATIVE_INFINITY) the <code>delete()</code> operation may fail to
* remove the correct element.
*
* <p><b>Note that this implementation is not synchronized.</b> If multiple
* threads access a set concurrently, and at least one of the threads modifies
* the set, it <i>must</i> be synchronized externally. This is typically
* accomplished by synchronizing on some object that naturally encapsulates the
* set.</p>
*
* <p>This class was originally developed by Nathan Fiedler for the GraphMaker
* project. It was imported to JGraphT with permission, courtesy of Nathan
* Fiedler.</p>
*
* @author Nathan Fiedler
*/
public class FibonacciHeap<T>
{
//~ Static fields/initializers ---------------------------------------------
private static final double oneOverLogPhi =
1.0 / Math.log((1.0 + Math.sqrt(5.0)) / 2.0);
//~ Instance fields --------------------------------------------------------
/**
* Points to the minimum node in the heap.
*/
private FibonacciHeapNode<T> minNode;
/**
* Number of nodes in the heap.
*/
private int nNodes;
//~ Constructors -----------------------------------------------------------
/**
* Constructs a FibonacciHeap object that contains no elements.
*/
public FibonacciHeap()
{
} // FibonacciHeap
//~ Methods ----------------------------------------------------------------
/**
* Tests if the Fibonacci heap is empty or not. Returns true if the heap is
* empty, false otherwise.
*
* <p>Running time: O(1) actual</p>
*
* @return true if the heap is empty, false otherwise
*/
public boolean isEmpty()
{
return minNode == null;
}
// isEmpty
/**
* Removes all elements from this heap.
*/
public void clear()
{
minNode = null;
nNodes = 0;
}
// clear
/**
* Decreases the key value for a heap node, given the new value to take on.
* The structure of the heap may be changed and will not be consolidated.
*
* <p>Running time: O(1) amortized</p>
*
* @param x node to decrease the key of
* @param k new key value for node x
*
* @exception IllegalArgumentException Thrown if k is larger than x.key
* value.
*/
public void decreaseKey(FibonacciHeapNode<T> x, double k)
{
if (k > x.key) {
throw new IllegalArgumentException(
"decreaseKey() got larger key value");
}
x.key = k;
FibonacciHeapNode<T> y = x.parent;
if ((y != null) && (x.key < y.key)) {
cut(x, y);
cascadingCut(y);
}
if (x.key < minNode.key) {
minNode = x;
}
}
// decreaseKey
/**
* Deletes a node from the heap given the reference to the node. The trees
* in the heap will be consolidated, if necessary. This operation may fail
* to remove the correct element if there are nodes with key value
* -Infinity.
*
* <p>Running time: O(log n) amortized</p>
*
* @param x node to remove from heap
*/
public void delete(FibonacciHeapNode<T> x)
{
// make x as small as possible
decreaseKey(x, Double.NEGATIVE_INFINITY);
// remove the smallest, which decreases n also
removeMin();
}
// delete
/**
* Inserts a new data element into the heap. No heap consolidation is
* performed at this time, the new node is simply inserted into the root
* list of this heap.
*
* <p>Running time: O(1) actual</p>
*
* @param node new node to insert into heap
* @param key key value associated with data object
*/
public void insert(FibonacciHeapNode<T> node, double key)
{
node.key = key;
// concatenate node into min list
if (minNode != null) {
node.left = minNode;
node.right = minNode.right;
minNode.right = node;
node.right.left = node;
if (key < minNode.key) {
minNode = node;
}
} else {
minNode = node;
}
nNodes++;
}
// insert
/**
* Returns the smallest element in the heap. This smallest element is the
* one with the minimum key value.
*
* <p>Running time: O(1) actual</p>
*
* @return heap node with the smallest key
*/
public FibonacciHeapNode<T> min()
{
return minNode;
}
// min
/**
* Removes the smallest element from the heap. This will cause the trees in
* the heap to be consolidated, if necessary.
*
* <p>Running time: O(log n) amortized</p>
*
* @return node with the smallest key
*/
public FibonacciHeapNode<T> removeMin()
{
FibonacciHeapNode<T> z = minNode;
if (z != null) {
int numKids = z.degree;
FibonacciHeapNode<T> x = z.child;
FibonacciHeapNode<T> tempRight;
// for each child of z do...
while (numKids > 0) {
tempRight = x.right;
// remove x from child list
x.left.right = x.right;
x.right.left = x.left;
// add x to root list of heap
x.left = minNode;
x.right = minNode.right;
minNode.right = x;
x.right.left = x;
// set parent[x] to null
x.parent = null;
x = tempRight;
numKids--;
}
// remove z from root list of heap
z.left.right = z.right;
z.right.left = z.left;
if (z == z.right) {
minNode = null;
} else {
minNode = z.right;
consolidate();
}
// decrement size of heap
nNodes--;
}
return z;
}
// removeMin
/**
* Returns the size of the heap which is measured in the number of elements
* contained in the heap.
*
* <p>Running time: O(1) actual</p>
*
* @return number of elements in the heap
*/
public int size()
{
return nNodes;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?