Loading…
DAA · Graphs · Program 26 · In the record
Write a program in c to find the minimum cost spanning tree of a graph using Prim's algorithm and find its execution time using the time function.
Algorithm: MST-PRIM(G, w, r)
1. for each u in V[G]
2. do key[u] <- infinity
3. p[u] <- NIL
4. key[r] <- 0
5. Q <- V[G]
6. while Q != Ø
7. do u <- EXTRACT-MIN(Q)
8. for each v in Adj[u]
9. do if v in Q and w(u,v) < key[v]
10. then p[v] <- u
11. key[v] <- w(u,v)
EXTRACT-MIN(Q)
1. u <- the vertex in Q with minimum key[u]
2. Q <- Q - {u}
3. return u1#include<stdio.h>2#include<time.h>3#define INF 999994 5int n,count=0;6int w[50][50],key[50],p[50],Q[50];7 8int extract_min() {9 int v,u=0;10 for(v=1;v<=n;v++) {11 count++;12 if(Q[v]==1 && (u==0 || key[v]<key[u]))13 u=v;14 }15 Q[u]=0;16 return u;17}18 19int main() {20 int i,u,v,r,q_size,step=0,cost=0;21 printf("Enter the number of vertices:");22 scanf("%d",&n);23 printf("Enter the %dx%d weight matrix (0 for no edge):\n",n,n);24 for(u=1;u<=n;u++)25 for(v=1;v<=n;v++)26 scanf("%d",&w[u][v]);27 printf("Enter the root vertex:");28 scanf("%d",&r);29 30 int order[n+1];31 32 clock_t start=clock();33 for(u=1;u<=n;u++) {34 key[u]=INF;35 p[u]=0;36 }37 key[r]=0;38 for(u=1;u<=n;u++)39 Q[u]=1;40 q_size=n;41 while(q_size!=0) {42 u=extract_min();43 q_size--;44 if(key[u]==INF)45 break;46 step++;47 order[step]=u;48 for(v=1;v<=n;v++)49 if(w[u][v]!=0) {50 count++;51 if(Q[v]==1 && w[u][v]<key[v]) {52 p[v]=u;53 key[v]=w[u][v];54 }55 }56 }57 clock_t end=clock();58 double time_taken=(double)(end-start)/CLOCKS_PER_SEC;59 60 if(step<n)61 printf("\nGraph is disconnected, no spanning tree exists");62 else {63 printf("\nEdges in the minimum spanning tree (in the order added):");64 printf("\nEdge\t\tWeight");65 for(i=2;i<=n;i++) {66 v=order[i];67 printf("\n%d - %d\t\t%d",p[v],v,key[v]);68 cost+=key[v];69 }70 printf("\nMinimum cost of spanning tree:%d",cost);71 }72 printf("\nNumber of comparisons:%d",count);73 printf("\nTime taken:%f seconds",time_taken);74 printf("\nTime complexity: O(V^2) with adjacency matrix, O(E log V) with adjacency list and binary heap");75 printf("\nSpace complexity: O(V^2) with adjacency matrix, O(V+E) with adjacency list");76 printf("\n");77 return 0;78}9 ⏎ 0 4 0 0 0 0 0 8 0 ⏎ 4 0 8 0 0 0 0 11 0 ⏎ 0 8 0 7 0 4 0 0 2 ⏎ 0 0 7 0 9 14 0 0 0 ⏎ 0 0 0 9 0 10 0 0 0 ⏎ 0 0 4 14 10 0 2 0 0 ⏎ 0 0 0 0 0 2 0 1 6 ⏎ 8 11 0 0 0 0 1 0 7 ⏎ 0 0 2 0 0 0 6 7 0 ⏎ 1Enter the number of vertices:9 Enter the 9x9 weight matrix (0 for no edge): 0 4 0 0 0 0 0 8 0 4 0 8 0 0 0 0 11 0 0 8 0 7 0 4 0 0 2 0 0 7 0 9 14 0 0 0 0 0 0 9 0 10 0 0 0 0 0 4 14 10 0 2 0 0 0 0 0 0 0 2 0 1 6 8 11 0 0 0 0 1 0 7 0 0 2 0 0 0 6 7 0 Enter the root vertex:1 Edges in the minimum spanning tree (in the order added): Edge Weight 1 - 2 4 2 - 3 8 3 - 9 2 3 - 6 4 6 - 7 2 7 - 8 1 3 - 4 7 4 - 5 9 Minimum cost of spanning tree:37 Number of comparisons:109 Time taken:0.000005 seconds Time complexity: O(V^2) with adjacency matrix, O(E log V) with adjacency list and binary heap Space complexity: O(V^2) with adjacency matrix, O(V+E) with adjacency list
Run program 26 in your browser
C · clang → WebAssembly. Change the code or the input, run it, break it, and see what happens.