【问题标题】:Implementing Consolidate in Fibonacci heap在斐波那契堆中实现合并
【发布时间】:2013-03-03 11:33:08
【问题描述】:

Introduction to Algorithms 的伪代码声明:

for each node w in the root list of H
  link trees of the same degree

但是如何有效地实现 for each root node 部分呢?原始根在整个合并过程中与其他相同程度的根相连,很难通过根节点的循环列表。如何确定是否检查了每个根节点?

【问题讨论】:

    标签: algorithm tree fibonacci-heap


    【解决方案1】:

    一种简单的方法是使用三步流程:

    1. 断开循环链接,使列表现在只是一个普通的双向链表。
    2. 遍历双向链表并处理每棵树。这很棘手,因为正如您所提到的,每个节点上的 forward 和 next 指针可能会在迭代期间发生变化。
    3. 关闭循环。

    您可以按照以下方式执行每个步骤:

    断开循环链接:

    rootList->prev->next = NULL;
    rootList->prev = NULL;
    

    遍历双向链表。

    Node* current = rootList;
    while (current != NULL) {
        /* Cache the next node to visit so that even if the list changes, we can still
         * remember where to go next.
         */
        Node* next = current->next;
    
        /* ... main Fibonacci heap logic ... */
    
        current = next;
    }
    

    修复双向链表:

    Node* curr = rootList;
    if (curr != NULL) { // If list is empty, no processing necessary.
        while (curr->next != NULL) {
            curr = curr->next;
        }
        curr->next = rootList;
        rootList->prev = curr;
    }
    

    希望这会有所帮助!

    【讨论】:

    • 与你的 rootList->prev->next = NULL;您删除了我稍后在删除此节点时需要使用的链接(使其成为其他节点的子节点)
    猜你喜欢
    • 2012-12-29
    • 1970-01-01
    • 2013-04-15
    • 2012-12-16
    • 2010-11-24
    • 2016-11-18
    • 2013-03-01
    • 1970-01-01
    • 2010-12-19
    相关资源
    最近更新 更多