我创建了一个 Kruskal 函数,以便它可以在图形上找到最小的路径。但是我用冒泡排序做到了,我正在尝试使用 qsort 使其工作并使我的结构动态化,这样它可以让我更有效率。
使用的所有功能都可以正常工作,但如果您对此有疑问,请询问我,以便我发布。我正在使用联合查找作为pd(dynamcc partition)
链接。
我的代码是:
struct _temp{
float weight;
int source;
int destination;
};
typedef struct _temp Temp;
struct _graph{
int NumberVertex;
int NumberEdges;
float* NeighborMatrix;
};
typedef struct _graph Graph;
struct din_part{
int n;
int*v;
};
typedef struct din_part PartDin;
Graph* grKruskal(Graph* g)
{
Graph* tree_min = graph_Create(g->NumberVertex);
PartDin* p = pd_Create(g->NumberVertex);
int i, j,number_edges=0,n=0,representative;
Temp edges[300],temp;
for (i = 0; i < (g->NumberVertex); i++)
{
for (j = n; j < (g->NumberVertex); j++)
{
if(g->NeighborMatrix[g->NumberVertex * i + j]!=0)
{
edges[number_edges].weight=g->NeighborMatrix[g->NumberVertex * i + j];
edges[number_edges].source=i;
edges[number_edges].destination=j;
number_edges++;
}
}
n++;
}
if(number_edges>0){
for(i=0; i<number_edges; i++)
{
int change=0;
for(j=0 ;j<number_edges-1; j++)
{
if(edges[j].weight >edges[j+1].weight){ // blubble_sort
temp=edges[j];
edges[j]= edges[j+1];
edges[j+1]=temp;
change=1;
}
}
if(!change)
{
for(i=0;i<number_edges;i++)
{
if(pd_Representative(p,edges[i].source)!=pd_Representative(p,edges[i].destination))
{
representative=pd_Union(p,edges[i].source,edges[i].destination);
graph_InsertEdge(arvore_min,edges[i].source,edges[i].destination,edges[i].weight);
}
}
p=pd_Free (p);
return tree_min;
}
}
}
}