How to update element priorities in a heap for Prim's Algorithm?

前端 未结 2 737
野性不改
野性不改 2021-02-05 17:55

I am studying Prim\'s Algorithm. There is a part within the code the next vertex across the cut will be coming to the set of the vertices belonging to the MST. Whi

2条回答
  •  闹比i
    闹比i (楼主)
    2021-02-05 18:10

    Pointers enable efficient composite data structures

    You have something like this (using pseudocode C++):

    class Node
        bool visited
        double key
        Node* pi
        vector> adjacent //adjacent nodes and edge weights
        //and extra fields needed for PriorityQueue data structure
        // - a clean way to do this is to use CRTP for defining the base
        //   PriorityQueue node class, then inherit your graph node from that
    
    class Graph
        vector vertices
    

    CRTP: http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern

    The priority queue Q in the algorithm contains items of type Node*, where ExtractMin gets you the Node* with minimum key.

    The reason you don't have to do any linear search is because, when you get u = ExtractMin(Q), you have a Node*. So u->adjacent gets you both the v's in G.Adj[u] and the w(u,v)'s in const time per adjacent node. Since you have a pointer v to the priority queue node (which is v), you can update it's position in the priority queue in logarithmic time per adjacent node (with most implementations of a priority queue).

    To name some specific data structures, the DecreaseKey(Q, v) function used below has logarithmic complexity for Fibonnaci heaps and pairing heaps (amortized).

    • Pairing heap: http://en.wikipedia.org/wiki/Pairing_heap
      • Not too hard to code yourself - Wikipedia has most of the source code
    • Fibonacci heap: http://en.wikipedia.org/wiki/Fibonacci_heap

    More-concrete pseudocode for the algorithm

    MstPrim(Graph* G)
        for each u in G->vertices
            u->visited = false
            u->key = infinity
            u->pi = NULL
        Q = PriorityQueue(G->vertices)
        while Q not empty
            u = ExtractMin(Q)
            u->visited = true
            for each (v, w) in u->adjacent
                if not v->visited and w < v->key
                    v->pi = u
                    v->key = w
                    DecreasedKey(Q, v) //O(log n)
    

提交回复
热议问题