/* source: https://www.thecrazyprogrammer.com/2014/03/kruskals-algorithm-for-finding-minimum-cost-spanning-tree.html */ #include typedef struct edge { int u,v,w; } edge; void kruskal(int *g, int n, edge mst[]); int find(int belongs[],int vertexno); void union1(int belongs[],int n, int c1,int c2); void sort(edge elist[], int m); void print(edge elist[], int m); int main() { int n = 6; //graph by adjacent matrix in 1D array int g[] = { 0,3,1,6,0,0, 3,0,5,0,3,0, 1,5,0,5,6,4, 6,0,5,0,0,2, 0,3,6,0,0,6, 0,0,4,2,6,0 }; edge mst[n-1]; kruskal(g, n, mst); print(mst, n-1); return 0; } void kruskal(int *g, int n, edge mst[]) { //compute the number of edges int i, m = 0; for (i=0; i< n*n; i++) { if (*(g+i) != 0) m++; } m /= 2; //create edge list of all edges edge elist[m]; int j, k = 0; for(i=1;i= n-1) break; //break when the number of added edges reaches to n-1 } } //find the component/subset the vertex belongs to. int find(int belongs[],int vertex) { return(belongs[vertex]); } //union of two components, not the most efficient implmentation void union1(int belongs[],int n, int c1, int c2) { int i; for(i=0;ielist[j+1].w) { temp=elist[j]; elist[j] = elist[j+1]; elist[j+1]=temp; } } void print(edge elist[], int m) { int i,cost=0; for(i=0;i