Data Structure
  • 資料結構自學筆記
  • 1 - Stack & Queue
    • 1.1 - Stack
    • 1.2 - Queue
    • 1.3 - Stack and Queue
  • 2 - Tree & Binary Tree
    • 2.1 - Tree
    • 2.2 - Binary Tree
    • 2.3 - Binary Tree Traversal
    • 2.4 - Binary Search Tree
    • 2.5 - Heap
    • 2.6 - Thread Binary Tree
    • 2.7 - Tree and Binary Tree Conversion
    • 2.8 Advanced Trees
      • 2.8.1 - Min-Max Heap
      • 2.8.2 - Deap
      • 2.8.3 - Symmetric Min-Max Heap
      • 2.8.4 - Extended Binary Tree
      • 2.8.5 - AVL Tree
      • 2.8.6 - M-Way Search Tree
      • 2.8.7 - B Tree
      • 2.8.8 - Red-Black Tree
      • 2.8.9 - Optimal Binary Search Tree
      • 2.8.10 - Splay Tree
      • 2.8.11 - Leftest Heap
      • 2.8.12 - Binomial Heap
  • 3 - Search & Sort
    • 3.1 - Searching
    • 3.2 - Elementary Sorting
      • 3.2.1 - Insertion Sort
      • 3.2.2 - Selection Sort
      • 3.2.3 - Bubble Sort
      • 3.2.4 - Shell Sort
    • 3.3 - Sophisticated Sorting
      • 3.3.1 - Quick Sort
      • 3.3.2 - Merge Sort
      • 3.3.3 - Heap Sort
      • 3.3.4 - Radix Sort
      • 3.3.5 - Bucket Sort
      • 3.3.6 - Counting Sort
    • 3.4 - Summary
  • 4 - Graph
    • 4.1 - Intro
    • 4.2 - Graph Traversal
    • 4.3 - Spanning Tree
      • 4.3.1 - Kruskal's algorithm
      • 4.3.2 - Prim's algorithm
      • 4.3.3 - Sollin's algorithm
    • 4.4 - Shortest Path Length
      • 4.4.1 - Dijkstra's algorithm
      • 4.4.2 - Bellman-Ford algorithm
      • 4.4.3 - Floyd-Warshall algorithm
    • 4.5 - AOV Network
    • 4.6 - AOE Network
    • 4.7 - Others
Powered by GitBook
On this page
  1. 4 - Graph
  2. 4.3 - Spanning Tree

4.3.2 - Prim's algorithm

G=(V,E),V={1,2,...,v},U={1}G=(V, E),V=\{1,2,...,v\},U=\{1\}G=(V,E),V={1,2,...,v},U={1}

Steps:

  1. 從起點開始,挑出最小成本的邊 (u,v),u∊U,v∊V−U(u,v),u∊U,v∊V-U(u,v),u∊U,v∊V−U

  2. (u,v)(u,v)(u,v)加入 SSS中,將 vvv加入 UUU中

  3. 重複1~2直到 V=UV=UV=U 或是無邊可挑

若 SSS的邊數小於 v−1v-1v−1 ,則 GGG 無spanning tree。

This is pseudo code in C.
void prim(G, W, start){
    // G: adjacency matrix
    // W: the weight set of edges in graph
    // start: the start point
    // initial
    struct vertex node[NUM_NODES];
    for (int u = 0; u < NUM_NODES, u++){
        node[u].key = INT_MAX;
        node[u].pi = -1;
    }
    node[start].key = 0;
    create_priority_queue(q, node); //依照key值放入頂點
    // prim's
    while(!IsEmpty(q)){
        int u = dequeue(q);
        for (int v = 0; v<NUM_NODES; v++){
            //確認(u,v)邊存在,v在Queue中,(u,v)邊的權重小於v的key值
            if (G[u][v] == 1 && find(v) && W[u][v]<node[v].key){
                node[v].pi = u;
                node[v].key = W[u][v];
            }
        }
    }
}
  • Time Complexity: O(V2)O(V^2)O(V2)

    使用Binary Heap、Fibnacci Heap可得更低時間。

e.g.

U={1}, V−U={2,3,4,5,6,7}(u,v):(1,6),(1,2)⇒(1,6)U=\{1\},\ V-U=\{2,3,4,5,6,7\}\\ (u,v):(1,6), (1,2)\Rightarrow (1,6)U={1}, V−U={2,3,4,5,6,7}(u,v):(1,6),(1,2)⇒(1,6)

U={1,6},V−U={2,3,4,5,7}(u,v):(1,2),(5,6)⇒(5,6)U=\{1,6\},V-U=\{2,3,4,5,7\}\\ (u,v):(1,2),(5,6) \Rightarrow(5,6)U={1,6},V−U={2,3,4,5,7}(u,v):(1,2),(5,6)⇒(5,6)

U={1,5,6},V−U={2,3,4,7}(u,v):(1,2),(5,7),(4,5)⇒(4,5)U=\{1,5,6\},V-U=\{2,3,4,7\}\\ (u,v):(1,2),(5,7),(4,5) \Rightarrow (4,5)U={1,5,6},V−U={2,3,4,7}(u,v):(1,2),(5,7),(4,5)⇒(4,5)

U={1,4,5,6},V−U={2,3,7}(u,v)=(1,2),(5,7),(4,7),(3,4)⇒(3,4)U=\{1,4,5,6\}, V-U=\{2,3,7\}\\ (u,v)=(1,2),(5,7),(4,7),(3,4)\Rightarrow (3,4)U={1,4,5,6},V−U={2,3,7}(u,v)=(1,2),(5,7),(4,7),(3,4)⇒(3,4)

U={1,3,4,5,6},V−U={2,7}(u,v)=(1,2),(5,7),(4,7),(2,3)⇒(2,3)U=\{1,3,4,5,6\}, V-U=\{2,7\}\\ (u,v)=(1,2),(5,7),(4,7),(2,3)\Rightarrow (2,3)U={1,3,4,5,6},V−U={2,7}(u,v)=(1,2),(5,7),(4,7),(2,3)⇒(2,3)

U={1,2,3,4,5,6},V−U={7}(u,v)=(1,2),(5,7),(4,7),(2,7)⇒(2,7)U=\{1,2,3,4,5,6\}, V-U=\{7\}\\ (u,v)=(1,2),(5,7),(4,7),(2,7)\Rightarrow (2,7)U={1,2,3,4,5,6},V−U={7}(u,v)=(1,2),(5,7),(4,7),(2,7)⇒(2,7)

U={1,2,3,4,5,6,7},V−U={}⇒endU=\{1,2,3,4,5,6,7\}, V-U=\{\}\Rightarrow endU={1,2,3,4,5,6,7},V−U={}⇒end

Previous4.3.1 - Kruskal's algorithmNext4.3.3 - Sollin's algorithm

Last updated 6 years ago