王朝百科
分享
 
 
 

Prim算法

王朝百科·作者佚名  2010-01-16  
宽屏版  字体: |||超大  

Prim算法用于求无向图的最小生成树

设图G =(V,E),其生成树的顶点集合为U。

①、把v0放入U。

②、在所有u∈U,v∈V-U的边(u,v)∈E中找一条最小权值的边,加入生成树。

③、把②找到的边的v加入U集合。如果U集合已有n个元素,则结束,否则继续执行②。

其算法的时间复杂度为O(n^2)

Prim算法实现:

(1)集合:设置一个数组set(i=0,1,..,n-1),初始值为 0,代表对应顶点不在集合中(注意:顶点号与下标号差1)

(2)图用邻接矩阵或邻接表表示,路径不通用无穷大表示,在计算机中可用一个大整数(如 1 << 30)代替。

采用堆可以将复杂度降为O(m log n),如果采用Fibonaci堆可以将复杂度降为O(n log n + m)

算法实现#include<fstream>

#define MaxNum 765432100;

using namespace std;

ifstream fin("Prim.in");

ofstream fout("Prim.out");

int p,q;

bool is_arrived[501];

int Length,Vertex,SetNum,State;

int Map[501][501],Dist[501];

int FindMin()

{

int p;

int Minm,Temp;

Minm=MaxNum;

Temp=0;

for(p=1;p<=Vertex;p++)

if ((Dist[p]<Minm)&&(!is_arrived[p]))

{

Minm=Dist[p];

Temp=p;

}

return Temp;

}

int main()

{

memset(is_arrived,0,sizeof(is_arrived));

fin >> Vertex;

for(p=1;p<=Vertex;p++)

for(q=1;q<=Vertex;q++)

{

fin >> Map[p][q];

if (Map[p][q]==0) Map[p][q]=MaxNum

}

Length=0;

is_arrived[1]=true;

for(p=1;p<=Vertex;p++)

Dist[p]=Map[1][p];

SetNum=1;

do

{

State=FindMin();

if (State!=0)

{

SetNum=SetNum+1;

is_arrived[State]=true;

Length=Length+Dist[State];

for(p=1;p<=Vertex;p++)

if ((Map[State][p]<Dist[p])&&(!is_arrived[p]))

Dist[p]=Map[State][p];

}

else

break;

}

while (SetNum!=Vertex);

if (SetNum!=Vertex)

fout << "The graph is not connected!";

else

fout << Length;

fin.close();

fout.close();

return 0;

}

Sample Input

7

00 20 50 30 00 00 00

20 00 25 00 00 70 00

50 25 00 40 25 50 00

30 00 40 00 55 00 00

00 00 25 55 00 10 70

00 70 50 00 10 00 50

00 00 00 00 70 50 00

Sample Output

160

//用于搜索最短连接路径的快速方法

void prime()

{

int i,j,k=0;

int v0=1;

int min;

for( i=1; i<=cases; i++ )

{

lowcost=cost[v0];

closest=v0;

}

lowcost[v0]=-1;

for( i=1; i<cases; i++ )

{

min=max;

for( j=1; j<=cases; j++ )

{

if( lowcost[j]<min && lowcost[j]!=-1)

{

min=lowcost[j];

k=j;

}

}

sum+=lowcost[k];

//printf("sum=%d

",sum);

//printf("k=%d

",k);

lowcost[k]=-1;

for( j=1; j<=cases; j++ )

{

if( cost[k][j]<lowcost[j] && lowcost[j]!=-1 )

{

lowcost[j]=cost[k][j];

closest[j]=k;

}

}

}

}

PASCAL代码

procedure prim(v0:integer);

var

lowcost,closest:array[1..maxn] of integer;

i,j,k,min:integer;

begin

for i:=1 to n do begin

lowcost[i]:=cost[v0,i];

closest[i]:=v0;

end;

for i:=1 to n-1 do begin

{寻找离生成树最近的未加入顶点k}

min:=maxint;

for j:=1 to n do

if (lowcost[j]<min) and (lowcost[j]<>0) then begin

min:=lowcost[j];

k:=j;

end;

lowcost[k]:=0; {将顶点k加入生成树}

{生成树中增加一条新的边k到closest[k]}

{修正各点的lowcost和closest值}

for j:=1 to n do

if cost[k,j]<lowcost[j] then begin

lowcost[j]:=cost[k,j];

closest[j]:=k;

end;

end;

end;

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
如何用java替换看不见的字符比如零宽空格&#8203;十六进制U+200B
 干货   2023-09-10
网页字号不能单数吗,网页字体大小为什么一般都是偶数
 干货   2023-09-06
java.lang.ArrayIndexOutOfBoundsException: 4096
 干货   2023-09-06
Noto Sans CJK SC字体下载地址
 干货   2023-08-30
window.navigator和navigator的区别是什么?
 干货   2023-08-23
js获取referer、useragent、浏览器语言
 干货   2023-08-23
oscache遇到404时会不会缓存?
 干货   2023-08-23
linux下用rm -rf *删除大量文件太慢怎么解决?
 干货   2023-08-08
刀郎新歌破世界纪录!
 娱乐   2023-08-01
js实现放大缩小页面
 干货   2023-07-31
生成式人工智能服务管理暂行办法
 百态   2023-07-31
英语学习:过去完成时The Past Perfect Tense举例说明
 干货   2023-07-31
Mysql常用sql命令语句整理
 干货   2023-07-30
科学家复活了46000年前的虫子
 探索   2023-07-29
英语学习:过去进行时The Past Continuous Tense举例说明
 干货   2023-07-28
meta name="applicable-device"告知页面适合哪种终端设备:PC端、移动端还是自适应
 干货   2023-07-28
只用css如何实现打字机特效?
 百态   2023-07-15
css怎么实现上下滚动
 干货   2023-06-28
canvas怎么画一个三角形?
 干货   2023-06-28
canvas怎么画一个椭圆形?
 干货   2023-06-28
canvas怎么画一个圆形?
 干货   2023-06-28
canvas怎么画一个正方形?
 干货   2023-06-28
中国河南省郑州市金水区蜘蛛爬虫ip大全
 干货   2023-06-22
javascript简易动态时间代码
 干货   2023-06-20
感谢员工的付出和激励的话怎么说?
 干货   2023-06-18
 
>>返回首页<<
 
 
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有