【图的最短路径】图的最小生成树

更新时间:2015-02-20    来源:代码生成    手机版     字体:

【www.bbyears.com--代码生成】


#include
#include
#include
#include
#define ING 9999
int visited[10];/*访问标志数组*/
typedef struct ArcCell{
 int adj;/*顶点关系类型,用1表示相邻,0表示不相邻*/
}ArcCell,**AdjMatrix;/*邻接矩阵*/
typedef struct type{ 
 char data[3];/*顶点值*/
}VertexType;
typedef struct{
 VertexType *vexs;/*顶点向量*/
 AdjMatrix arcs;/*邻接矩阵*/
 int vexnum,arcnum;/*图的顶点数和边数*/
}MGraph;
struct clos{
 VertexType adjvex;
 int lowcost;
}*closedge;/*辅助数组,low..为0时就表示已访问过*/
void InitGraph(MGraph *G)/*初始图*/
{ int i,nu,mu;
 printf("n输入顶点的个数和(边)弧的个数:");
  scanf("%d%d",&nu,&mu);
  G->arcs=(ArcCell **)malloc(nu*sizeof(ArcCell *));
  for(i=0;i      G->arcs[i]=(ArcCell *)malloc(nu*sizeof(ArcCell));
  G->vexs=(VertexType *)malloc(nu*sizeof(VertexType));/*分配顶点空间*/
  G->vexnum=nu;G->arcnum=mu;/*图的顶点数和边数*/
}
void InsertGraph(MGraph *G,int i,VertexType e)/*给图顶点向量付值*/
{ if(i<0||i>G->vexnum) return;
  strcpy(G->vexs[i].data,e.data);
}
int Locate(MGraph G,VertexType v1)/*确定v1在图顶点中的位置*/
{ int i;
 for(i=0;i   if(strcmp(v1.data,G.vexs[i].data)==0) return i;
  return -1;
}
void CreateUND(MGraph *G)/*采用数组(邻接矩阵)*/
{int i,j,k,*p,d,w;
 VertexType v1,v2;
 p=(int *)malloc(G->vexnum*sizeof(int));
 for(i=0;i<10;i++) p[i]=0;
 for(i=0;ivexnum;++i)/*初始邻接表*/
 { for(j=0;jvexnum;++j) G->arcs[i][j].adj=ING;}
 printf("按顺序输入顶点和它们的权值再顶点:如 "v1 3 v2" : n");
 for(k=0;karcnum;++k)
 {printf("输入第 %d 条(边)弧: n",k+1);
  scanf("%s%d%s",v1.data,&w,v2.data);/*输入相邻的两个点值和权值*/
  i=Locate(*G,v1);j=Locate(*G,v2);/*用i 和j来确定它们的位置*/
  G->arcs[i][j].adj=w;
  G->arcs[j][i].adj=G->arcs[i][j].adj;/*有向图时就不用这个*/
 }
}
int minimum(MGraph G,struct clos *closedge)/*求出辅助数组中除0之外最小权值的顶点位置*/
{int i,min,j,k;
 for(i=0;i  if(closedge[i].lowcost!=0)
  {min=closedge[i].lowcost;k=i;break;}

本文来源:http://www.bbyears.com/wangyetexiao/13081.html

猜你感兴趣

热门标签

更多>>

本类排行