Minimum Spanning Tree MST

Problem

Build railways of the least total length to connect all cities.

Greedy Solution Overview

Initialize the MST with an arbitary vertice
while !(MST has spaned the graph)
    Add the shortest path to existing MST
end while

Prim’s Algorithm

Prim: Linear Search Version

// keep a table, initialize it as following
// vertice 0 is the first-added vertice
vertice    is_included    tentative_distance    connected_from
      0           true                     0                -1
      1          false             MAX_INT32                -1
      2          false             MAX_INT32                -1
      3            ...                   ...               ...

int i = 0; // last connected vertice
do {
    // Start with the last added vertice i
    // update the table for vertices connected to i
    // Complexity: O(V)
    for ( int item : connected_to(i) ){
        if ( tentative_distance[item] < distance(i, item) ){
            tentative_distance[item] = distance(i, item);
            connected_from[item] = i;
        }
    }
    
    // Add a unconnected vertice with the minimum tentative distance
    // Complexity: O(V)
    int min = MAX_INT32;
    for ( int item : unconnected() ){
        if ( tentative_distance[item] < min ){
            min = tentative_distance[item];
            i = item;
        }
    }
    is_included[i] = true;
    
} while ( !unconnected().empty() );
// loop V times, until all vertices are connected
// Overall Complexity: O(V^2)

Prim: Heap Version

  • Replace tentative_distance with a heap
  • Step 1: Update the table for previously connected vertice
    • Involved updating an element in a heap
    • O ( log ⁡ ( V ) ) O(\log(V)) O(log(V)) each
    • Worst case: update for every edge O ( E ) O(E) O(E)
    • Complexity: O ( E log ⁡ ( V ) ) O(E\log(V)) O(Elog(V))
  • Step 2: select an unconnected vertice with the smallest tentative_distance
    • O ( log ⁡ ( V ) ) O(\log(V)) O(log(V)) each
    • V V V times
    • Complexity: O ( V log ⁡ ( V ) ) O(V\log(V)) O(Vlog(V))
  • Step 3: Set is_included for that vertice true
    • O ( 1 ) O(1) O(1) each
    • V V V times
    • Complexity: O ( V ) O(V) O(V)
  • Overall Complexity: O ( E log ⁡ ( V ) ) O(E\log(V)) O(Elog(V))
  • Better than O ( V 2 ) O(V^2) O(V2) when the graph is sparse

Kruskal’s Algorithm

  • Step 1: Sort all edges O ( E log ⁡ ( E ) ) O(E\log(E)) O(Elog(E))
  • Step 2: Loop through all edges: (for V V V times)
    • Test if the shortest edge leads to a loop
      • Union_find
      • less than O ( log ⁡ ( E ) ) O(\log(E)) O(log(E))
    • If it forms a loop, discard that edge O ( 1 ) O(1) O(1)
    • If it does not form a loop, add that edge O ( 1 ) O(1) O(1)
  • Overall Complexity: O ( E log ⁡ ( E ) ) ≈ O ( E log ⁡ ( V ) ) O(E\log(E))\approx O(E\log(V)) O(Elog(E))O(Elog(V)) when graph is sparse

Conclusion

  • When graph is dense, use Prim (linear version)
  • When graph is sparse, use Kruskal
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值