Advertisement
amit_2002

Untitled

Nov 4th, 2024
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.98 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct Edge {
  5.     int src, dest, weight;
  6. };
  7.  
  8. struct Graph {
  9.     int V, E;
  10.     struct Edge* edge;
  11. };
  12.  
  13. struct Graph* createGraph(int V, int E) {
  14.     struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph));
  15.     graph->V = V;
  16.     graph->E = E;
  17.     graph->edge = (struct Edge*)malloc(E * sizeof(struct Edge));
  18.     return graph;
  19. }
  20.  
  21. struct subset {
  22.     int parent;
  23.     int rank;
  24. };
  25.  
  26. int findParent(struct subset parent[], int i) {
  27.     if (parent[i].parent != i)
  28.         parent[i].parent = findParent(parent, parent[i].parent); // Path compression
  29.     return parent[i].parent;
  30. }
  31.  
  32. void unionSets(struct subset parent[], int x, int y) {
  33.     int xroot = findParent(parent, x);
  34.     int yroot = findParent(parent, y);
  35.  
  36.     if (parent[xroot].rank < parent[yroot].rank)
  37.         parent[xroot].parent = yroot;
  38.     else if (parent[xroot].rank > parent[yroot].rank)
  39.         parent[yroot].parent = xroot;
  40.     else {
  41.         parent[yroot].parent = xroot;
  42.         parent[xroot].rank++;
  43.     }
  44. }
  45.  
  46. int myComp(const void* a, const void* b) {
  47.     struct Edge* a1 = (struct Edge*)a;
  48.     struct Edge* b1 = (struct Edge*)b;
  49.     return a1->weight - b1->weight;
  50. }
  51.  
  52. void KruskalMST(struct Graph* graph) {
  53.     int V = graph->V;
  54.     struct Edge result[V - 1]; // MST will have V-1 edges
  55.     int e = 0; // Index for result[]
  56.     int i = 0; // Initial index for sorted edges
  57.  
  58.     qsort(graph->edge, graph->E, sizeof(graph->edge[0]), myComp);
  59.  
  60.     struct subset* parent = (struct subset*)malloc(V * sizeof(struct subset));
  61.  
  62.     for (int v = 0; v < V; v++) {
  63.         parent[v].parent = v;
  64.         parent[v].rank = 0;
  65.     }
  66.  
  67.     while (e < V - 1 && i < graph->E) {
  68.         struct Edge next_edge = graph->edge[i++];
  69.  
  70.         int x = findParent(parent, next_edge.src);
  71.         int y = findParent(parent, next_edge.dest);
  72.  
  73.         if (x != y) {
  74.             result[e++] = next_edge;
  75.             unionSets(parent, x, y);
  76.         }
  77.     }
  78.  
  79.     printf("Following are the edges in the constructed MST\n");
  80.     for (i = 0; i < e; ++i)
  81.         printf("%d -- %d == %d\n", result[i].src, result[i].dest, result[i].weight);
  82.  
  83.     free(parent);
  84. }
  85.  
  86. int main() {
  87.     int V = 9, E = 14;
  88.     struct Graph* graph = createGraph(V, E);
  89.  
  90.     graph->edge[0] = (struct Edge){0, 1, 4};
  91.     graph->edge[1] = (struct Edge){0, 7, 8};
  92.     graph->edge[2] = (struct Edge){1, 2, 8};
  93.     graph->edge[3] = (struct Edge){1, 7, 11};
  94.     graph->edge[4] = (struct Edge){2, 3, 7};
  95.     graph->edge[5] = (struct Edge){2, 8, 2};
  96.     graph->edge[6] = (struct Edge){2, 5, 4};
  97.     graph->edge[7] = (struct Edge){3, 4, 9};
  98.     graph->edge[8] = (struct Edge){3, 5, 14};
  99.     graph->edge[9] = (struct Edge){4, 5, 10};
  100.     graph->edge[10] = (struct Edge){5, 6, 2};
  101.     graph->edge[11] = (struct Edge){6, 7, 1};
  102.     graph->edge[12] = (struct Edge){6, 8, 6};
  103.     graph->edge[13] = (struct Edge){7, 8, 7};
  104.  
  105.     KruskalMST(graph);
  106.  
  107.     free(graph->edge);
  108.     free(graph);
  109.  
  110.     return 0;
  111. }
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement