Loading…
DAA · Graphs · Program 27 · In the record
Write a program in c to find the minimum cost spanning tree of a graph using Kruskal's algorithm and find its execution time using the time function.
Algorithm: MST-KRUSKAL(G, w)
1. A <- Ø
2. for each vertex v in V[G]
3. do MAKE-SET(v)
4. sort the edges of E into nondecreasing order by weight w
5. for each edge (u,v) in E, taken in nondecreasing order by weight
6. do if FIND-SET(u) != FIND-SET(v)
7. then A <- A U {(u,v)}
8. UNION(u,v)
9. return A
MAKE-SET(x)
1. parent[x] <- x
2. rank[x] <- 0
FIND-SET(x)
1. if x != parent[x]
2. then parent[x] <- FIND-SET(parent[x])
3. return parent[x]
UNION(x, y)
1. x <- FIND-SET(x), y <- FIND-SET(y)
2. if rank[x] > rank[y]
3. then parent[y] <- x
4. else parent[x] <- y
5. if rank[x] == rank[y]
6. then rank[y] <- rank[y] + 11#include<stdio.h>2#include<time.h>3 4int parent[50],rnk[50];5 6void make_set(int x) {7 parent[x]=x;8 rnk[x]=0;9}10 11int find_set(int x) {12 if(x!=parent[x])13 parent[x]=find_set(parent[x]);14 return parent[x];15}16 17void union_set(int x,int y) {18 x=find_set(x);19 y=find_set(y);20 if(rnk[x]>rnk[y])21 parent[y]=x;22 else {23 parent[x]=y;24 if(rnk[x]==rnk[y])25 rnk[y]++;26 }27}28 29int main() {30 int n,e,i,j,k=0,cost=0,count=0,tu,tv,tw;31 printf("Enter the number of vertices:");32 scanf("%d",&n);33 printf("Enter the number of edges:");34 scanf("%d",&e);35 int u[e],v[e],w[e],au[n],av[n],aw[n];36 printf("Enter the edges (vertex vertex weight):\n");37 for(i=0;i<e;i++)38 scanf("%d%d%d",&u[i],&v[i],&w[i]);39 40 clock_t start=clock();41 for(i=1;i<=n;i++)42 make_set(i);43 for(i=1;i<e;i++) {44 tu=u[i];45 tv=v[i];46 tw=w[i];47 j=i-1;48 while(j>=0) {49 count++;50 if(w[j]<=tw)51 break;52 u[j+1]=u[j];53 v[j+1]=v[j];54 w[j+1]=w[j];55 j--;56 }57 u[j+1]=tu;58 v[j+1]=tv;59 w[j+1]=tw;60 }61 for(i=0;i<e && k<n-1;i++) {62 count++;63 if(find_set(u[i])!=find_set(v[i])) {64 au[k]=u[i];65 av[k]=v[i];66 aw[k]=w[i];67 k++;68 union_set(u[i],v[i]);69 }70 }71 clock_t end=clock();72 double time_taken=(double)(end-start)/CLOCKS_PER_SEC;73 74 if(k<n-1)75 printf("\nGraph is disconnected, no spanning tree exists");76 else {77 printf("\nEdges in the minimum spanning tree (in the order added):");78 printf("\nEdge\t\tWeight");79 for(i=0;i<k;i++) {80 printf("\n%d - %d\t\t%d",au[i],av[i],aw[i]);81 cost+=aw[i];82 }83 printf("\nMinimum cost of spanning tree:%d",cost);84 }85 printf("\nNumber of comparisons:%d",count);86 printf("\nTime taken:%f seconds",time_taken);87 printf("\nTime complexity: O(E log E) with merge sort, O(E^2) with the insertion sort used here");88 printf("\nSpace complexity: O(V+E)");89 printf("\n");90 return 0;91}9 ⏎ 14 ⏎ 1 2 4 ⏎ 1 8 8 ⏎ 2 3 8 ⏎ 2 8 11 ⏎ 3 4 7 ⏎ 3 6 4 ⏎ 3 9 2 ⏎ 4 5 9 ⏎ 4 6 14 ⏎ 5 6 10 ⏎ 6 7 2 ⏎ 7 8 1 ⏎ 7 9 6 ⏎ 8 9 7Enter the number of vertices:9 Enter the number of edges:14 Enter the edges (vertex vertex weight): 1 2 4 1 8 8 2 3 8 2 8 11 3 4 7 3 6 4 3 9 2 4 5 9 4 6 14 5 6 10 6 7 2 7 8 1 7 9 6 8 9 7 Edges in the minimum spanning tree (in the order added): Edge Weight 7 - 8 1 3 - 9 2 6 - 7 2 1 - 2 4 3 - 6 4 3 - 4 7 1 - 8 8 4 - 5 9 Minimum cost of spanning tree:37 Number of comparisons:71 Time taken:0.000010 seconds Time complexity: O(E log E) with merge sort, O(E^2) with the insertion sort used here Space complexity: O(V+E)
Run program 27 in your browser
C · clang → WebAssembly. Change the code or the input, run it, break it, and see what happens.