标签:style blog http color io os ar for sp
斐波那契堆是可合并堆
在不涉及删除的操作(除去EXTRACT和DELETE)中,操作仅需O(1)的平摊运行时间
当EXTRACT和DELETE的操作数目较小时斐波那契堆能得到较好的运行效率。
斐波那契堆不能有效地支持SEARCH操作
用于解决诸如最小生成树和寻找单源最短路径等问题的快速算法都要用到斐波那契堆。
斐波那契堆由一组最小堆构成,这些最小堆是有根的无序树。
结点结构:
key: 关键字,作为排序、判断结点大小的标准
left, right:用于维护双链表,所有的根结点形成一个双链表,每个结点的孩子们形成双链表
parent, child : 维护父子关系
mark : 这个域与维护堆结构无关,只与具体的算法策略有关,不在这里讲
degree: 记录该结点有几个孩子
斐波那契堆
n:堆中结点的个数
min:批向最小的结点
引理19.1中给出的二项树的性质对无序二项树仍然成立P278
有n个结点FibHeap,结点的最大度数D(n) = lgn(向下取整)
将合并堆的操作尽可能地推后
在一个包含n个结点的斐波那契堆中,结点的最大度数D(n)为O(lgn)
FIB-HEAP-INSERT和FIB-HEAP-UNION只是最基础的链表合入操作,因为合并操作要尽可能地拖后
FIB-HEAP-EXTRACT-MIN除了要完成本职工作外,还要作合并调整
CONSOLIDATE是作合并调整的函数
它将度数相同的根链接起来,直到对应每个度数至多只有一个根
遍历每一个根结点去判断,如果两个根结点的度是一样的,让大的结点作为小的结点的孩子
为了防止堆太宽,需要策略来调整堆,使根结点成为别的根结点的孩子,该策略就是CONSOLIDATE
同理,为了防止堆太深,也需要有相应的策略去调整,在适当的时候,把某个结点的孩子变成根
这一策略就是CUT和CASCADING-CUT,mark在实现这一策略的过程中起到辅助作用。
原理:当一个非根结点被切掉了2个孩子,就把它升为根结点
在删除一个结点时,怎么区分是第一个被删除的孩子,还是第二个?此时需要用mark来标记
因为翻译不好,严重影响理解
一旦第二个孩子也失掉后,x与其父结点之间的联系就被切断了,并成为一个新根。
原文:As soon as the second child has been lost, we cut x from its parent, making it a new root.
mark的命名不能体现它的作用,影响理解,如果换一个好一点的名字,就不用那么大段的文字去说明
外部函数不需要FIB-HEAP-这样的前缀,因为本来就是为它写的接口
内部函数的名字要说明函数的作用,因为内部函数是被自己调用的,不要给自己添麻烦
提取了一些对双链表的常用操作
CUT和CASCADING-CUT合并成一个函数,因为它们其实是一个功能,就是根据策略把孩子结点升为根结点
CUT和CASCADING-CUT中的x和y是父子关系,而且重点是子,父是只为了方便处理,不需要作为参数传进来,在函数里面重新获取一个就可以了。多传一个函数,就一个出错源
对于带参数的函数,增加一返回值。用于告知调用者是否成功,或什么原因导致失败
FIB-HEAP-DECREASE-KEY和FIB-HEAP-DELETE这两个函数作用不大。
因为它们的入参是node*。要想调用这两个函数,就必须先获取目标结点的指针。
可是没有一个接口返回指向结点的指针,怎么找到我的目标结点的指针呢?
调用者必须自己在创建结点后保持这个结点,这样不合理
#include <iostream> using namespace std; enum errorType { SUCCESS = 0, PARAM_ERROR, POINTER_NULL }; struct node { int key; node *pParent; node *pChild; node *pLeft; node *pRight; int degree; bool isOneChildDeleted; bool isHandled; node(int n) :key(n),pParent(NULL),pChild(NULL),pLeft(this),pRight(this),degree(0),isOneChildDeleted(false),isHandled(0){} }; class CFibHeap { int dataCount; node *pMinNode; void consolidate(); void link(node *y, node *x); void moveNodeToRootList(node *pNode); bool isNodeInHeap(node *pNode); void addNodeToRootList(node *pNode); void addListToRootList(node *pNode); void removeNodeFromRootList(node *pRootNode); void addChildToParent(node *pChild, node *pParent); void deleteChildFromParent(node *pNode); bool isNeedMoveToRootLost(node *pNode); public: CFibHeap():dataCount(0),pMinNode(NULL){}; errorType insert(node *x); int getDataCount(); errorType merge(CFibHeap *H2); node* extract(); errorType decreaseKey(node *pNodeToDecrease, int newKey); errorType deleteNode(node *pNode); };
#include <cmath> #include "FibHeap.h" #define MIN_KEY -0x7fffffff errorType CFibHeap::insert(node *pNode) { if(pNode == NULL) return POINTER_NULL; addNodeToRootList(pNode); dataCount++; return SUCCESS; } int CFibHeap::getDataCount() { return dataCount; } errorType CFibHeap::merge(CFibHeap *H2) { if(H2 == NULL ) return POINTER_NULL; if( H2->getDataCount() == 0) return SUCCESS; if(pMinNode == NULL) { pMinNode = H2->pMinNode; dataCount = H2->dataCount; return SUCCESS; } addListToRootList(H2->pMinNode); dataCount += H2->dataCount; return SUCCESS; } node* CFibHeap::extract() { if(pMinNode == NULL) return NULL; node *pChildOfMin = pMinNode->pChild; if(pChildOfMin != NULL) { while(pChildOfMin != NULL && pChildOfMin->pParent == pMinNode) { pChildOfMin->pParent = NULL; pChildOfMin = pChildOfMin->pRight; } pMinNode->pChild = NULL; addListToRootList(pChildOfMin); } node *pRetNode = pMinNode; removeNodeFromRootList(pRetNode); if(pMinNode != NULL) consolidate(); dataCount--; return pRetNode; } errorType CFibHeap::decreaseKey(node *pNodeToDecrease, int newKey) { if(pNodeToDecrease == NULL) return POINTER_NULL; if(pNodeToDecrease->key < newKey) return PARAM_ERROR; if(isNodeInHeap(pNodeToDecrease) == false) return PARAM_ERROR; pNodeToDecrease->key = newKey; node *pParent = pNodeToDecrease->pParent; if(pParent != NULL && pNodeToDecrease->key < pParent->key) moveNodeToRootList(pNodeToDecrease); if(pNodeToDecrease->key < pMinNode->key) pMinNode = pNodeToDecrease; return SUCCESS; } errorType CFibHeap::deleteNode(node *pNode) { if(pNode == NULL) return POINTER_NULL; if(isNodeInHeap(pNode) == false) return PARAM_ERROR; decreaseKey(pNode, MIN_KEY); extract(); return SUCCESS; } void CFibHeap::consolidate() { int degreeOfHeap = log(dataCount*1.0) / log(2.0), i; node **A = new node*[degreeOfHeap+1]; for(int i = 0; i <= degreeOfHeap; i++) A[i] = NULL; node *pNodeOfList = pMinNode; node *pCurrentNode = pNodeOfList; do { pCurrentNode->isHandled = false; pCurrentNode = pCurrentNode->pRight; } while (pCurrentNode != pMinNode); while(pNodeOfList->isHandled == false) { pNodeOfList->isHandled = true; pCurrentNode = pNodeOfList; pNodeOfList = pNodeOfList->pRight; int degreeOfCurrentNode = pCurrentNode->degree; while(A[degreeOfCurrentNode] != NULL) { node *pNodeInA = A[degreeOfCurrentNode]; if(pNodeInA->key < pCurrentNode->key) swap(*pNodeInA, *pCurrentNode); link(pNodeInA, pCurrentNode); A[degreeOfCurrentNode] = NULL; degreeOfCurrentNode++; } A[degreeOfCurrentNode] = pCurrentNode; } pMinNode = NULL; for(int i = 0; i <= degreeOfHeap; i++) if(A[i] != NULL) addNodeToRootList(A[i]); delete []A; } void CFibHeap::link(node *y, node *x) { removeNodeFromRootList(y); addChildToParent(y, x); y->isOneChildDeleted = false; } void CFibHeap::moveNodeToRootList(node *pNode) { node *pParent = pNode->pParent; deleteChildFromParent(pNode); addNodeToRootList(pNode); pNode->isOneChildDeleted = false; if(pParent->isOneChildDeleted == false) pParent->isOneChildDeleted = true; else if(pParent->pParent != NULL) moveNodeToRootList(pParent); } bool CFibHeap::isNodeInHeap(node *pNode) { node *pRootOfNode = pNode; while(pRootOfNode->pParent != NULL) pRootOfNode = pRootOfNode->pParent; node *pCurrentNode = pRootOfNode; do { if(pCurrentNode == pMinNode) break; else pCurrentNode = pCurrentNode->pRight; } while (pCurrentNode != pRootOfNode); if(pCurrentNode != pMinNode) return false; else return true; } void CFibHeap::addNodeToRootList(node *pNode) { pNode->pParent = NULL; if(pMinNode != NULL) { pNode->pRight = pMinNode->pRight; pNode->pLeft = pMinNode; pMinNode->pRight->pLeft = pNode; pMinNode->pRight = pNode; } if(pMinNode == NULL || pNode->key < pMinNode->key) { pMinNode = pNode; } } void CFibHeap::addListToRootList(node *pNodeOfList) { node *t = pMinNode->pRight; pMinNode->pRight = pNodeOfList; pNodeOfList->pLeft->pRight = t; t->pLeft = pNodeOfList->pLeft; pNodeOfList->pLeft = pMinNode; if(pMinNode->key > pNodeOfList->key) pMinNode = pNodeOfList; } void CFibHeap::removeNodeFromRootList(node *pRootNode) { if(pRootNode->pRight == pRootNode) pMinNode = NULL; else { if(pMinNode == pRootNode) pMinNode = pRootNode->pRight; pRootNode->pRight->pLeft = pRootNode->pLeft; pRootNode->pLeft->pRight = pRootNode->pRight; } } void CFibHeap::addChildToParent(node *pChild, node *pParent) { if(pParent->pChild == NULL) { pParent->pChild = pChild; pChild->pRight = pChild; pChild->pLeft = pChild; } else { node *pOriginChildOfParent = pParent->pChild; pChild->pRight = pOriginChildOfParent->pRight; pChild->pLeft = pOriginChildOfParent; pOriginChildOfParent->pRight->pLeft = pChild; pOriginChildOfParent->pRight = pChild; } pChild->pParent = pParent; pParent->degree++; } void CFibHeap::deleteChildFromParent(node *pNode) { node *pParent = pNode->pParent; if(pNode->pRight == pNode) pParent->pChild = NULL; else { if(pParent->pChild == pNode) pParent->pChild = pNode->pRight; pNode->pRight->pLeft = pNode->pLeft; pNode->pLeft->pRight = pNode->pRight; } pParent->degree--; }
McGee和FibHeap的区别在于合并的时机。
Fibheap认为合并调整应该尽量地推迟,而McGee则在每次堆中结点有增加的时候就作合并调整。
个人认为,合并调整操作的意义是防止堆过宽而影响性能。但是从算法过程上看,根结点的个数多少不会影响INSERT和UNION的性能,因此没有必要。
比较认可FibHeap的做法。
根据P300的描述,只有非根结点才可能被打上标记,如果根结点有标记,一定是它是非根结点的时候打上标记,然后被移到根结点的位置。
把结点移至根结点是通过上面代码中的函数addNodeToRootList和addListToRootList完成的,目标缩小至这两个函数周围
让根结点成为有标记结点,须满足以下两个条件
(1)调用这两个函数前,该结点是非根结点
(2)调用后没有清标记
结论:x是pMinData的孩子,根据P300的步骤被打上标记后,执行extract()时又成为根结点
20.4最大度的界
标签:style blog http color io os ar for sp
原文地址:http://www.cnblogs.com/windmissing/p/4051385.html