标签:spel string ret mini nbsp ado end table dem
//Prim算法生成最小生成树
void MiniSpanTree_Prim(MGraph G)
{
int min,i,j,k;
int adjvex[MAXVEX];
int lowcost[MAXVEX];
lowcost[0] = 0;
adjvex[0] = 0;
for(i = 1;i < G.numVertexes;i++)
{
lowcost[i] = G.arc[0][i];
adjvex[i] = 0;
}
for(i = 1;i < G.numVertexes;i++)
{
min = INFINITY;
j = 1;k = 0;
while(j < G.numVertexes)
{
if(lowcost[j] != 0 && lowcost[j] < min)
{
min = lowcost[j];
k = j;
}
j++;
}
printf("(%d,%d)",adjvex[k],k);
lowcost[k] = 0;
for(j = i;j < G.numVertexes;j++)
{
if(lowcost[j]!=0 && G.arc[k][j] < lowcost[j])
{
lowcost[j] = G.arc[k][j];
adjvex[j] = k;
}
}
}
}
//Prim算法生成最小生成树
void MiniSpanTree_Prim(MGraph G)
{
int min,i,j,k;
int adjvex[MAXVEX];
int lowcost[MAXVEX];
lowcost[0] = 0;
adjvex[0] = 0;
for(i = 1;i < G.numVertexes;i++)
{
lowcost[i] = G.arc[0][i];
adjvex[i] = 0;
}
for(i = 1;i < G.numVertexes;i++)
{
min = INFINITY;
j = 1;k = 0;
while(j < G.numVertexes)
{
if(lowcost[j] != 0 && lowcost[j] < min)
{
min = lowcost[j];
k = j;
}
j++;
}
printf("(%d,%d)",adjvex[k],k);
lowcost[k] = 0;
for(j = i;j < G.numVertexes;j++)
{
if(lowcost[j]!=0 && G.arc[k][j] < lowcost[j])
{
lowcost[j] = G.arc[k][j];
adjvex[j] = k;
}
}
}
}
//Kruskal算法生成最小生成树
void MiniSpanTree_Kruskal(MGraph G)
{
int i,n,m;
Edge edges[MAXEDGE];
int parentp[MAXVEX];
//省略将邻接矩阵转化为边集数组edges并按权由小到大排序的代码
for(i = 0; i < G.numEdges;i++)
{
parent[i] = 0;
}
for(i = o;i < G.numEdges;i++)
{
n = Find(parent,edges[i].begin);
m = Find(parent,edges[i].end);
if(n != m)
{
parent[n] = m;
printf("(%d,%d) %d ",edges[i].begin,edges[i].end,edges[i].weight);
}
}
}
int Find(int *parent,int f)
{
while(parent[f] > 0)
{
f = parent[f];
}
return f;
}
//Kruskal算法生成最小生成树
void MiniSpanTree_Kruskal(MGraph G)
{
int i,n,m;
Edge edges[MAXEDGE];
int parentp[MAXVEX];
//省略将邻接矩阵转化为边集数组edges并按权由小到大排序的代码
for(i = 0; i < G.numEdges;i++)
{
parent[i] = 0;
}
for(i = o;i < G.numEdges;i++)
{
n = Find(parent,edges[i].begin);
m = Find(parent,edges[i].end);
if(n != m)
{
parent[n] = m;
printf("(%d,%d) %d ",edges[i].begin,edges[i].end,edges[i].weight);
}
}
}
int Find(int *parent,int f)
{
while(parent[f] > 0)
{
f = parent[f];
}
return f;
}
//迪杰斯特拉(Dijkstra)算法
#define MAXVEX 9
#define INFINITY 65535
typedef int Patharc[MAXVEX];
typedef int ShortPathTable[MAXVEX];
void ShortestPath_Dijkstra(MGraph G,INT V0,Patharc *P,ShortPathTable *D)
{
int v,w,k,min;
int final[MAXVEX];
for(v = 0;v < G.numVertexes;v++)
{
final[v] = 0;
(*D)[v] = G.arc[v0][v];
(*P)[v] = 0;
}
(*D)[v0] = 0;
final[vo] = 1;
for(v = 1;v < G.numVertexes;w++)
{
min = INFINITY;
for(w = 0;w < G.numVertexes;w++)
{
if(!final[w] && (*D)[w] < min)
{
k = w;
min = (*D)[w];
}
}
final[k] = 1;
for(w = 0;w < G.numVertexes;w++)
{
if(!final[w] && (min+G.arc[k][w])< (*D)[w])
{
(*D)[w] = min + G.arc[k][w];
(*P)[w] = k;
}
}
}
}
//迪杰斯特拉(Dijkstra)算法
typedef int Patharc[MAXVEX];
typedef int ShortPathTable[MAXVEX];
void ShortestPath_Dijkstra(MGraph G,INT V0,Patharc *P,ShortPathTable *D)
{
int v,w,k,min;
int final[MAXVEX];
for(v = 0;v < G.numVertexes;v++)
{
final[v] = 0;
(*D)[v] = G.arc[v0][v];
(*P)[v] = 0;
}
(*D)[v0] = 0;
final[vo] = 1;
for(v = 1;v < G.numVertexes;w++)
{
min = INFINITY;
for(w = 0;w < G.numVertexes;w++)
{
if(!final[w] && (*D)[w] < min)
{
k = w;
min = (*D)[w];
}
}
final[k] = 1;
for(w = 0;w < G.numVertexes;w++)
{
if(!final[w] && (min+G.arc[k][w])< (*D)[w])
{
(*D)[w] = min + G.arc[k][w];
(*P)[w] = k;
}
}
}
}
//弗洛伊德(Floyd算法)
typedef int PathMatirx[MAXVEX][MAXVEX];
typedef int ShortPathTable[MAXVEX][MAXVEX];
void ShortestPath_Floyd(MGraph G,Pathmatirx *P,ShortPathTable *D)
{
int v,w,k;
for(v = 0;v < G.numVertexes; ++v)
{
for(w = 0;w < G.numVertexes;++w)
{
(*D)[v][w] = G.matirx[v][w];
(*P)[v][w] = w;
}
}
for(k = 0;k < G.numVertexes;++k)
{
for(v = 0;v < G.numVertexes;++v)
{
for(w = 0;w < G.numVertexes;++w)
{
if((*D)[v][w] > (*D)[v][k]+(*D)[k][w])
{
(*D)[v][w] = (*D)[v][w]+(*D)[k][w];
(*P)[v][w] = (*P)[v][k];
}
}
}
}
}
//最短路径显示代码段
for(v = 0;v < Q.numVertexes;++v)
{
for(w = v+1;w < G.numVertexes;w++)
{
printf("v%d-v%d weight: %d ",v,w,D[v][w]);
k = P[v][w];
printf(" path: %d",v);
while(k != w)
{
printf(" -> %d",k);
k = P[k][w];
}
printf(" -> %d\n",w);
}
printf("\n");
}
//弗洛伊德(Floyd算法)
typedef int PathMatirx[MAXVEX][MAXVEX];
typedef int ShortPathTable[MAXVEX][MAXVEX];
void ShortestPath_Floyd(MGraph G,Pathmatirx *P,ShortPathTable *D)
{
int v,w,k;
for(v = 0;v < G.numVertexes; ++v)
{
for(w = 0;w < G.numVertexes;++w)
{
(*D)[v][w] = G.matirx[v][w];
(*P)[v][w] = w;
}
}
for(k = 0;k < G.numVertexes;++k)
{
for(v = 0;v < G.numVertexes;++v)
{
for(w = 0;w < G.numVertexes;++w)
{
if((*D)[v][w] > (*D)[v][k]+(*D)[k][w])
{
(*D)[v][w] = (*D)[v][w]+(*D)[k][w];
(*P)[v][w] = (*P)[v][k];
}
}
}
}
}
//最短路径显示代码段
for(v = 0;v < Q.numVertexes;++v)
{
for(w = v+1;w < G.numVertexes;w++)
{
printf("v%d-v%d weight: %d ",v,w,D[v][w]);
k = P[v][w];
printf(" path: %d",v);
while(k != w)
{
printf(" -> %d",k);
k = P[k][w];
}
printf(" -> %d\n",w);
}
printf("\n");
}
标签:spel string ret mini nbsp ado end table dem
原文地址:https://www.cnblogs.com/LyndonMario/p/9326355.html