Home > Back-end >  The figure of the minimum spanning tree - Kruskal algorithm
The figure of the minimum spanning tree - Kruskal algorithm

Time:12-12

There are bosses can see where the error is? To find for a long time didn't find out
#include
#include
# define INFIN 32767
20 # define N
Typedef char VexType;
Typedef struct
{
Int vexnum arcnum;
VexType vexs [N].
Int arcs [N] [N].
} MGraph;
Typedef struct Edge
{
Int v1, v2,
int weight;
} Edge;
Edge edges [N].
Int flag [N].
Void CreateMGraph (MGraph * g)
{
Int I, j, u, k=1;
Char v.
i=0;
While ((v=getchar ())!='#')
{
G - & gt; Vexs [I]=v;
i++;
}
G - & gt; Vexnum=I;
for(i=0; ifor(j=0; jG - & gt; Arcs [I] [j]=0;
The scanf (" % d, % d, % d ", & amp; I, & amp; J., & amp; U);
getchar();
While ((I!=1) & amp; & (j!=1) & amp; & (u!=1))
{
G - & gt; Arcs [I] [j]=u;
G - & gt; Arcs [j] [I]=u;
The scanf (" % d, % d, % d ", & amp; I, & amp; J., & amp; U);
getchar();
k++;
}
G - & gt; Arcnum=k;
}
Void SortEdges (MGraph * g)
{
Int I, j, k, L=0;
for(i=1; ifor(j=0; jIf (g - & gt; Arcs [I] [j].=0)
{
K=L;
While (k> 0 & amp; & Edges [k - 1]. Weight> G - & gt; Arcs [I] [j])
{
Edges [k]=edges [k - 1);
K -;
}
Edges [k]. Weight=g - & gt; Arcs [I] [j];
Edges [k]. V1=I;
Edges [k]. V2=j;
L++;
}
}
Void Kruskal (MGraph * g)
{
Int I, j, factor, temp;
for(i=0; ifor(i=0; iIf (flag [edges [I]. V1]!=flag [edges [I]. V2])
{
Printf (" \ n (% % c, c), % d ", the g & gt; Vexs [edges [I]. V2], g - & gt; Vexs [edges [I]. V1], edges [I] weight);
Factor=flag [edges [I]. V1];
Temp=flag [edges [I]. V2];
for(j=0; jIf (flag [j]==temp)
Flag [j]=factor;
}
}
Int main ()
{
MGraph g;
CreateMGraph (& amp; G);
SortEdges (& amp; G);
Kruskal (& amp; G);
return 0;
}
  • Related