最小生成树之kruskal算法
来源:优易学  2011-12-10 17:15:05   【优易学:中国教育考试门户网】   资料下载   IT书店

  1. Kruskal算法

  (1) 算法思想

  K r u s k a l算法每次选择n- 1条边,所使用的贪婪准则是:从剩下的边中选择一条不会产生环路的具有最小耗费的边加入已选择的边的集合中。注意到所选取的边若产生环路则不可能形成一棵生成树。K r u s k a l算法分e 步,其中e 是网络中边的数目。按耗费递增的顺序来考虑这e 条边,每次考虑一条边。当考虑某条边时,若将其加入到已选边的集合中会出现环路,则将其抛弃,否则,将它选入。

  初始时没有任何边被选择。边( 1 , 6)是最先选入的边,它被加入到欲构建的生成树中,得到图1 3 - 1 2 c。下一步选择边( 3,4)并将其加入树中(如图1 3 - 1 2 d所示)。然后考虑边( 2,7 ,将它加入树中并不会产生环路,于是便得到图1 3 - 1 2 e。下一步考虑边( 2,3)并将其加入树中(如图1 3 - 1 2 f所示)。在其余还未考虑的边中,(7,4)具有最小耗费,因此先考虑它,将它加入正在创建的树中会产生环路,所以将其丢弃。此后将边( 5,4)加入树中,得到的树如图13-12g 所示。下一步考虑边( 7,5),由于会产生环路,将其丢弃。最后考虑边( 6,5)并将其加入树中,产生了一棵生成树,其耗费为9 9。图1 - 1 3给出了K r u s k a l算法的伪代码。

  (2)C代码

  /* Kruskal.c
  Copyright (c) 2002, 2006 by ctu_85
  All Rights Reserved.
  */
  /* I am sorry to say that the situation of unconnected graph is not concerned */
  #include "stdio.h"
  #define maxver 10
  #define maxright 100
  int G[maxver][maxver],record=0,touched[maxver][maxver];
  int circle=0;
  int FindCircle(int,int,int,int);
  int main()
  {
  int path[maxver][2],used[maxver][maxver];
  int i,j,k,t,min=maxright,exsit=0;
  int v1,v2,num,temp,status=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;
  used[j][k]=1;
  touched[j][k]=0;
  }
  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;
  used[j][k]=used[k][j]=0;
  touched[j][k]=touched[k][j]=0;
  }
  }

  for(j=0;j<num;j++)
  {
  path[j][0]=0;
  path[j][1]=0;
  }
  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;
  }
  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&&!used[j][k])
  {
  v1=j;
  v2=k;
  min=G[j][k];
  }
  if(!used[v1][v2])
  {
  used[v1][v2]=1;
  used[v2][v1]=1;
  touched[v1][v2]=1;
  touched[v2][v1]=1;
  path[0]=v1;
  path[1]=v2;
  for(t=0;t<record;t++)
  FindCircle(path[t][0],path[t][0],num,path[t][0]);
  if(circle)
  {/*if a circle exsits,roll back*/
  circle=0;
  i--;
  exsit=0;
  touched[v1][v2]=0;
  touched[v2][v1]=0;
  min=maxright;
  }
  else
  {
  record++;
  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[0]+1,path[1]+1);
  }
  return 1;
  }
  int FindCircle(int start,int begin,int times,int pre)
  { /* to judge whether a circle is produced*/
  int i;
  for(i=0;i<times;i++)
  if(touched[begin]==1)
  {
  if(i==start&&pre!=start)
  {
  circle=1;
  return 1;
  break;
  }
  else
  if(pre!=i)
  FindCircle(start,i,times,begin);
  else
  continue;
  }
  return 1;
  }

责任编辑:小草

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训