毕业设计,毕业论文代写。专业水平。钻石水准,黄金品质。

计算机专业毕业设计,论文,设计代写。电邮:elevenor@gmail.com。专业水平,质优价廉。

原创 Prim算法完整实现代码收藏

新一篇: 三维建筑物图像的二维建模 | 旧一篇: Kruskal算法完整的实现代码

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

该图用邻接矩阵表示,邻接表原理与之相同。

 1:输入示例

图二:输入时若两点之间没有公共边,则将权值设置为-1。程序设置处理的最大点数为10

图三:注意到Prim算法的解答结果有时候不是唯一的,这个结果和对图遍历时的顺序有关,但是必需注意的是所有的最小生成树其网络代价和是一样的。

下面是源代码:

/*  Prim.c

 

    Copyright (c) 2002, 2006 by ctu_85

    All Rights Reserved.

*/

/* The impact of the situation of articulation point exists can be omitted in Prim algorithm but not in Kruskal algorithm */

#include "stdio.h"

#define maxver 10

#define maxright 100

int main()

{

int G[maxver][maxver],in[maxver]={0},path[maxver][2];

int i,j,k,min=maxright;

int v1,v2,num,temp,status=0,start=0;

restart:

printf("Please enter the number of vertex(s) in the graph:\n");

scanf("%d",&num);

if(num>maxver||num<0)

{

printf("Error!Please reinput!\n");

goto restart;

}

for(j=0;j<num;j++)

for(k=0;k<num;k++)

{

if(j==k)

G[j][k]=maxright;

else

if(j<k)

{

re:

printf("Please input the right between vertex %d and vertex %d,if no edge exists please input -1:\n",j+1,k+1);

scanf("%d",&temp);

if(temp>=maxright||temp<-1)

{

printf("Invalid input!\n");

goto re;

}

if(temp==-1)

temp=maxright;

G[j][k]=G[k][j]=temp;

}

}

for(j=0;j<num;j++)

{

status=0;

for(k=0;k<num;k++)

if(G[j][k]<maxright)

{

status=1;

break;

}

if(status==0)

break;

}

do

{

printf("Please enter the vertex where Prim algorithm starts:");

scanf("%d",&start);

}while(start<0||start>num);

in[start-1]=1;

for(i=0;i<num-1&&status;i++)

{

for(j=0;j<num;j++)

for(k=0;k<num;k++)

if(G[j][k]<min&&in[j]&&(!in[k]))

{

v1=j;

v2=k;

min=G[j][k];

}

if(!in[v2])

{

path[i][0]=v1;

path[i][1]=v2;

in[v1]=1;

in[v2]=1;

min=maxright;

}

}

if(!status)

printf("We cannot deal with it because the graph is not connected!\n");

else

{

for(i=0;i<num-1;i++)

printf("Path %d:vertex %d to vertex %d\n",i+1,path[i][0]+1,path[i][1]+1);

}

return 1;

}

更多内容:

浙江大学ACM试题解答(四月)
http://blog.csdn.net/ctu_85/archive/2007/04/24/1576831.aspx
浙江大学ACM试题解答(三月)
http://blog.csdn.net/ctu_85/archive/2007/03/20/1535556.aspx
麻省理工算法导论翻译
http://blog.csdn.net/ctu_85/archive/2007/06/08/1643179.aspx
华容道游戏与算法
http://blog.csdn.net/ctu_85/archive/2007/05/15/1610722.aspx
中国象棋对战程序C语言源代码
http://blog.csdn.net/ctu_85/archive/2007/05/04/1596351.aspx

荷兰国旗算法分析
文件加密解密源代码分析
http://blog.csdn.net/ctu_85/archive/2006/12/23/1455515.aspx
更多试题解答:
两种计算Ack(m,n)的非递归算法
http://blog.csdn.net/ctu_85/archive/2006/11/29/1419396.aspx
上海交通大学1999年
http://blog.csdn.net/ctu_85/archive/2006/11/09/1376289.aspx
东北大学2001年
http://blog.csdn.net/ctu_85/archive/2006/11/09/1376287.aspx
清华大学1994年
http://blog.csdn.net/ctu_85/archive/2006/10/24/1349754.aspx
中国科学院2002年
http://blog.csdn.net/ctu_85/archive/2006/10/24/1349704.aspx
浙江大学计算机复试解答1
http://blog.csdn.net/ctu_85/archive/2006/10/15/1334936.aspx
浙江大学计算机复试解答2
http://blog.csdn.net/ctu_85/archive/2006/10/16/1336101.aspx
浙江大学计算机复试解答3
http://blog.csdn.net/ctu_85/archive/2006/11/02/1363159.aspx
软件可行性报告
http://blog.csdn.net/ctu_85/archive/2006/06/06/775894.aspx
软件需求分析报告
http://blog.csdn.net/ctu_85/archive/2006/06/06/775892.aspx
 

发表于 @ 2006年12月16日 09:14:00|评论(loading...)|编辑

新一篇: 三维建筑物图像的二维建模 | 旧一篇: Kruskal算法完整的实现代码

评论

#kgn28 发表于2007-06-28 21:50:21  IP: 125.77.173.*
请问在选点进行扩充的时候
是否有用到heap优化?
没看出来有用到heap,这样
时间复杂度会不会太高?
2007-06-29 09:10:54作者回复
这个没有涉及到优化文图,只是扩充课下
#セレブ委員会 发表于2008-11-19 11:43:54  IP: 124.26.174.*
エロアダルトスワッピング
#美代子 发表于2008-11-22 15:05:42  IP: 124.26.194.*
エロセックス出会い
发表评论  


登录
Csdn Blog version 3.1a
Copyright © ctu_85