1、Data Structures and Algorithms The Minimum Spanning Tree of Graphs Implementation of Prim AlgorithmData Structures and Algorithms1Algorithm Thought We use a weighted adjacency matrix to represent the connected network,and then set up an auxiliary array closedge (closedge ), An array elementss subscr
2、ipt corresponds to the ordinal number of vertex in the current V-U set,the value of elements records the vertex ordinal number adjvex and the weight lowcost of the lowest (nearest) edge connected to the vertex in U set. That is, for each vertex of v V-U, closedgev records all of the closest edges in
3、 the group from U to V-U adjacent to V.7.5 Application of graphChapter 7 Graphsminimum spanning treeData Structures and Algorithms2Auxiliary array closedge: mincost(u,v)|u U,v V-Uclosedgev.lowcost = 0 v Uclosedgev.adjvex store the vertex sequence number closest to v in U set。 7.5 Application of grap
4、hChapter 7 Graphsminimum spanning treeAlgorithm Thought*7.4 图 的 应 用第 7 章 图1.图 的 连 通 性 问 题 图 的 生 成 树 与 最 小 生 成 树算 法 一 : 普 里 姆 算 法 ( Prim )算 法 思 想 演 示a b cdeg f1918 27 2112 714 316 58 19 14 1819 5 7 12 5 3 7 3 8 21 14 12 8 16 21 27 18 16 27 lowcostadjvex 7g6f5e4d3c2b1aclosedgev 0 1a19 1a14 1a18 05e2 5
5、e8 5e6 04d 7 4d3 4d21 03c5 0 0 0 数 据 结 构 与 算 法Data Structures and Algorithms4struct vertexData adjvex; int lowcost; closedgeMAX_VERTEX_NUM; 7.5 Application of graphChapter 7 Graphsminimum spanning treeAlgorithm ThoughtData Structures and Algorithms5MiniSpanTree_Prim(AdjMatrix gn, VertexData u) k=Loc
6、ateVertex(gn, u); closedgek.lowcost=0; for (i=0;ign.vexnum;i+) if ( i!=k) closedgei.adjvex=u; closedgei.lowcost=gn.arcski.adj; 7.5 Application of graphChapter 7 Graphsminimum spanning treeAlgorithm ThoughtData Structures and Algorithms67.5 图 的 应 用1.图 的 连 通 性 问 题 图 的 生 成 树 与 最 小 生 成 树算 法 一 : 普 里 姆 算
7、法 ( Prim )算 法 实 现 for (e=1;e=gn.vexnum-1;e+) k0=Minium(closedge); u0= closedgek0.adjvex; v0= gn.vexsk0 printf(u0, v0); closedgek0.lowcost=0; for ( i=0 ;ivexnum;i+) if ( gn.arcsk0i.adj closedgei.lowcost) closedgei.lowcost= gn.arcsk0i.adj; closedgei.adjvex=v0; Time Complexity O(n2)Chapter 7 Graphs数 据 结 构 与 算 法 Thanks!