DSAA之图论Prim算法(六)

备注

笔者暂时不记录网络流和关键路径分析法,DSAA这里讲的很粗略。以后根据需要单独补充。

1. Minimum Spanning Tree

  • Informally, a minimum spanning tree of an undirected graph G is a tree formed from graph edges that connects all the vertices of G at lowest total cost.
  • A minimum spanning tree exists if and only if G is connected.
  • Notice that the number of edges in the minimum spanning tree is |V| - 1. The minimum spanning tree is a tree because it is acyclic, it is spanning because it covers every edge, and it is minimum for the obvious reason.

注意最小是指无向图中生成的树的边的权重之和最小,第二点指出了只有连通的无向图存在最小生成树。因为该生成树包含图所有的顶点,所以边为 ∣ V ∣ − 1 |V|-1 V1特别的最小生成树不一定唯一

2. Prim’s Algorithm

  • One way to compute a minimum spanning tree is to grow the tree in successive stages. In each stage, one node is picked as the root, and we add an edge, and thus an associated vertex, to the tree.
  • The algorithm then finds, at each stage, a new vertex to add to the tree by choosing the edge (u, v) such that the cost of (u, v) is the smallest among all edges where u is in the tree and v is not.
  • The rest of the algorithm is exactly the same, with the exception that since the definition of dv is different, so is the update rule. For this problem, the update rule is even simpler than before: After a vertex v is selected, for each unknown w adjacent to v, dw= min(dw, cw,v).

最关键的是第二、三点,每次将一个点标记为known时,更新其他与该点相邻的点。这里和Dijkstra很像,但是周边节点的dw更新准则变成 d w = m i n ( d w , C w , v ) dw= min(dw, Cw,v) dw=min(dw,Cw,v),下次将选择 d v dv dv值最小的节点。

3. 伪代码实现

DSAA没有给出该实现,但是我们按照上面的提示,很容易从以前的Dijkstra算法修改得到:

void dijkstra( TABLE T ){
    vertex v, w;
    //初始情况下,除了起始顶点dv为0,其他都是负无穷
    //特别的T应为
    /*struct cell{
       int dv;
       int pv;
       int v;
    }的数组
    */
    p_q.inital(T);
    for( ;! p_q.empty(); ){
        v = p_q.delete_min()
        if(T[v].known == TRUE)
            continue;
        T[v].known = TRUE;
        for each w adjacent to v
            if( !T[w].known )
                //使用Prim算法的更新准则
                /*9*/if( Cv,w < T[w].dist ){ /* update w */
		            //就这里变了哦
                    decrease( T[w].dist to Cv,w);
                    T[w].path = v;
                    //插入优先堆中
                    p_q.insert(T[w]) 
                }
    }
}

4. 时间复杂度分析

Be aware that Prim’s algorithm runs on undirected graphs, so when coding it, remember to put every edge in two adjacency lists. The running time is $O (|V|^2) $without heaps, which is optimal for dense graphs, and O ( ∣ E ∣ l o g ∣ V ∣ ) O (|E| log |V|) O(ElogV) using binary heaps, which is good for sparse graphs.

因为完全就是Dijkstra算法的逻辑,所以时间复杂度一样,但是Dijkstra算法是计算有向图单源最短路径问题。无向图是特殊的有向图(|E|变成两倍),这里应该清楚。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值