Return-Path: X-Original-To: apmail-commons-commits-archive@minotaur.apache.org Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 1A0D24E85 for ; Tue, 12 Jul 2011 00:57:35 +0000 (UTC) Received: (qmail 25796 invoked by uid 500); 12 Jul 2011 00:57:34 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 25579 invoked by uid 500); 12 Jul 2011 00:57:33 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 25572 invoked by uid 99); 12 Jul 2011 00:57:33 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 12 Jul 2011 00:57:33 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 12 Jul 2011 00:57:31 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id DBBB0238896F for ; Tue, 12 Jul 2011 00:57:11 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1145414 - in /commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/collections: FibonacciHeap.java FibonacciHeapNode.java Date: Tue, 12 Jul 2011 00:57:11 -0000 To: commits@commons.apache.org From: simonetripodi@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110712005711.DBBB0238896F@eris.apache.org> Author: simonetripodi Date: Tue Jul 12 00:57:11 2011 New Revision: 1145414 URL: http://svn.apache.org/viewvc?rev=1145414&view=rev Log: adjusting FibonacciHeap implementation according to http://staff.ustc.edu.cn/~csli/graduate/algorithms/book6/chap21.htm not completed yet, still a TODO Modified: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/collections/FibonacciHeap.java commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/collections/FibonacciHeapNode.java Modified: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/collections/FibonacciHeap.java URL: http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/collections/FibonacciHeap.java?rev=1145414&r1=1145413&r2=1145414&view=diff ============================================================================== --- commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/collections/FibonacciHeap.java (original) +++ commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/collections/FibonacciHeap.java Tue Jul 12 00:57:11 2011 @@ -19,14 +19,17 @@ package org.apache.commons.graph.collect * under the License. */ +import java.util.ArrayList; import java.util.Collection; import java.util.Comparator; import java.util.Iterator; +import java.util.List; import java.util.Queue; /** * A Fibonacci Heap implementation based on - * Princeton lesson. + * University of Science and Technology of + * China lesson. */ public final class FibonacciHeap implements Queue @@ -38,11 +41,25 @@ public final class FibonacciHeap */ private final Comparator comparator; + /** + * The number of nodes currently in {@code H} is kept in {@code n[H]}. + */ private int size = 0; + /** + * {@code t(H)} the number of trees in the root list. + */ private int trees = 0; - private FibonacciHeapNode root; + /** + * {@code m(H)} the number of marked nodes in {@code H}. + */ + private int markedNodes = 0; + + /** + * The root of the tree containing a minimum key {@code min[H]}. + */ + private FibonacciHeapNode minimumNode; public FibonacciHeap() { @@ -66,31 +83,20 @@ public final class FibonacciHeap FibonacciHeapNode node = new FibonacciHeapNode( e ); - if ( root == null ) + if ( minimumNode == null ) { - root = node; + minimumNode = node; } else { - node.setPrevious( root ); - node.setNext( root.getNext() ); - root.setNext( node ); - node.getNext().setPrevious( root ); + node.setLeft( minimumNode ); + node.setRight( minimumNode.getRight() ); + minimumNode.setRight( node ); + node.getRight().setLeft( minimumNode ); - int comparison; - if ( comparator != null ) + if ( compare( minimumNode.getValue(), e ) < 0 ) { - comparison = comparator.compare( root.getValue(), e ); - } - else - { - Comparable rootComparable = (Comparable) root.getValue(); - comparison = rootComparable.compareTo( e ); - } - - if ( comparison < 0 ) - { - root = node; + minimumNode = node; } } @@ -201,7 +207,11 @@ public final class FibonacciHeap */ public E element() { - return null; + if ( size == 0 ) + { + return null; + } + return minimumNode.getValue(); } /** @@ -217,11 +227,7 @@ public final class FibonacciHeap */ public E peek() { - if ( size == 0 ) - { - return null; - } - return root.getValue(); + return element(); } /** @@ -229,7 +235,7 @@ public final class FibonacciHeap */ public E poll() { - return null; + return remove(); } /** @@ -237,12 +243,94 @@ public final class FibonacciHeap */ public E remove() { - return null; + if ( size == 0 ) + { + return null; + } + + FibonacciHeapNode currentRoot = minimumNode; + + int degree = currentRoot.getDegree(); + FibonacciHeapNode currentChild = currentRoot.getChild(); + FibonacciHeapNode pointer; + + while ( degree > 0 ) + { + pointer = currentChild.getRight(); + + currentChild.getLeft().setRight( currentChild.getRight() ); + currentChild.getRight().setLeft( currentChild.getLeft() ); + + currentChild.setLeft( minimumNode ); + currentChild.setRight( minimumNode.getRight() ); + minimumNode.setRight( currentChild ); + currentChild.getRight().setLeft( currentChild ); + + currentChild.setParent( null ); + currentChild = pointer; + degree--; + } + + currentRoot.getLeft().setRight( currentRoot.getRight() ); + currentRoot.getRight().setLeft( currentRoot.getLeft() ); + + if ( currentRoot == currentRoot.getRight() ) + { + minimumNode = null; + } + else + { + minimumNode = currentRoot.getRight(); + consolidate(); + } + + size--; + + return currentRoot.getValue(); } private void consolidate() { + if ( minimumNode == null ) + { + return; + } + + List> nodeSequence = new ArrayList>(); + + FibonacciHeapNode pointer = minimumNode.getRight(); + + while ( pointer != minimumNode ) + { + int degree = pointer.getDegree(); + + + + pointer = pointer.getRight(); + } + } + /** + * The potential of Fibonacci heap {@code H} is then defined by + * {@code t(H) + 2m(H)}. + * + * @return The potential of this Fibonacci heap. + */ + public int potential() + { + return trees + 2 * markedNodes; + } + + private int compare( E o1, E o2 ) + { + if ( comparator != null ) + { + return comparator.compare( o1, o2 ); + } + Comparable o1Comparable = (Comparable) o1; + return o1Comparable.compareTo( o2 ); + } + } Modified: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/collections/FibonacciHeapNode.java URL: http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/collections/FibonacciHeapNode.java?rev=1145414&r1=1145413&r2=1145414&view=diff ============================================================================== --- commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/collections/FibonacciHeapNode.java (original) +++ commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/collections/FibonacciHeapNode.java Tue Jul 12 00:57:11 2011 @@ -19,6 +19,9 @@ package org.apache.commons.graph.collect * under the License. */ +/** + * + */ final class FibonacciHeapNode { @@ -26,14 +29,27 @@ final class FibonacciHeapNode private FibonacciHeapNode parent; - private FibonacciHeapNode previous; - - private FibonacciHeapNode next; + /** + * {@code left[x]} + */ + private FibonacciHeapNode left = this; + + /** + * {@code right[x]} + */ + private FibonacciHeapNode right = this; private FibonacciHeapNode child; + /** + * The number of children in the child list of node {@code x} is stored in {@code degree[x]}. + */ private int degree = 0; + /** + * {@code mark[x]} indicates whether node {@code x} has lost a child since + * the last time {@code x} was made the child of another node. + */ private boolean marked = false; public FibonacciHeapNode( E value ) @@ -51,24 +67,24 @@ final class FibonacciHeapNode this.parent = parent; } - public FibonacciHeapNode getPrevious() + public FibonacciHeapNode getLeft() { - return previous; + return left; } - public void setPrevious( FibonacciHeapNode previous ) + public void setLeft( FibonacciHeapNode left ) { - this.previous = previous; + this.left = left; } - public FibonacciHeapNode getNext() + public FibonacciHeapNode getRight() { - return next; + return right; } - public void setNext( FibonacciHeapNode next ) + public void setRight( FibonacciHeapNode right ) { - this.next = next; + this.right = right; } public FibonacciHeapNode getChild()