fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct Edge {
  5. int u, v, weight;
  6. } Edge;
  7.  
  8. typedef struct Graph {
  9. int V, E;
  10. Edge* edge;
  11. } Graph;
  12.  
  13. // Function to compare edges based on their weights
  14. int cmp(const void* a, const void* b) {
  15. Edge* edgeA = (Edge*)a;
  16. Edge* edgeB = (Edge*)b;
  17. return edgeA->weight - edgeB->weight;
  18. }
  19.  
  20. // Function to find the parent of a vertex in the DSU
  21. int find(int parent[], int i) {
  22. if (parent[i] == -1)
  23. return i;
  24. return find(parent, parent[i]);
  25. }
  26.  
  27. // Function to union two vertices in the DSU
  28. void unionSet(int parent[], int x, int y) {
  29. int xset = find(parent, x);
  30. int yset = find(parent, y);
  31. parent[xset] = yset;
  32. }
  33.  
  34. // Function to implement Kruskal's Algorithm
  35. void kruskal(Graph* graph) {
  36. int V = graph->V;
  37. Edge result[V];
  38. int e = 0;
  39. int i = 0;
  40.  
  41. // Sort all edges in non-decreasing order of their weights
  42. qsort(graph->edge, graph->E, sizeof(graph->edge[0]), cmp);
  43.  
  44. // Create a DSU data structure
  45. int* parent = (int*)malloc(V * sizeof(int));
  46. for (int v = 0; v < V; v++)
  47. parent[v] = -1;
  48.  
  49. // Iterate through all edges
  50. while (e < V - 1 && i < graph->E) {
  51. Edge next_edge = graph->edge[i++];
  52.  
  53. int x = find(parent, next_edge.u);
  54. int y = find(parent, next_edge.v);
  55.  
  56. // If the two vertices are not in the same connected component
  57. if (x != y) {
  58. result[e++] = next_edge;
  59. unionSet(parent, x, y);
  60. }
  61. }
  62.  
  63. // Print the MST
  64. printf("The edges of Minimum Cost Spanning Tree are:\n");
  65. for (int j = 0; j < e; j++)
  66. printf("Edge cost from %d to %d: %d\n", result[j].u, result[j].v, result[j].weight);
  67.  
  68. printf("Minimum cost of spanning tree = %d\n", result[e - 1].weight);
  69. }
  70.  
  71. int main() {
  72. int V, E;
  73. printf("Enter the number of vertices: ");
  74. scanf("%d", &V);
  75.  
  76. printf("Enter the number of edges: ");
  77. scanf("%d", &E);
  78.  
  79. Graph* graph = createGraph(V, E);
  80.  
  81. printf("Enter the edges (source destination weight):\n");
  82. for (int i = 0; i < E; i++) {
  83. scanf("%d%d%d", &graph->edge[i].u, &graph->edge[i].v, &graph->edge[i].weight);
  84. }
  85.  
  86. // Find and print the MST using Kruskal's Algorithm
  87. kruskal(graph);
  88.  
  89. return 0;
  90. }
Success #stdin #stdout 0.02s 25856KB
stdin
Standard input is empty
stdout
#include <stdio.h>
#include <stdlib.h>

typedef struct Edge {
    int u, v, weight;
} Edge;

typedef struct Graph {
    int V, E;
    Edge* edge;
} Graph;

// Function to compare edges based on their weights
int cmp(const void* a, const void* b) {
    Edge* edgeA = (Edge*)a;
    Edge* edgeB = (Edge*)b;
    return edgeA->weight - edgeB->weight;
}

// Function to find the parent of a vertex in the DSU
int find(int parent[], int i) {
    if (parent[i] == -1)
        return i;
    return find(parent, parent[i]);
}

// Function to union two vertices in the DSU
void unionSet(int parent[], int x, int y) {
    int xset = find(parent, x);
    int yset = find(parent, y);
    parent[xset] = yset;
}

// Function to implement Kruskal's Algorithm
void kruskal(Graph* graph) {
    int V = graph->V;
    Edge result[V];
    int e = 0;
    int i = 0;

    // Sort all edges in non-decreasing order of their weights
    qsort(graph->edge, graph->E, sizeof(graph->edge[0]), cmp);

    // Create a DSU data structure
    int* parent = (int*)malloc(V * sizeof(int));
    for (int v = 0; v < V; v++)
        parent[v] = -1;

    // Iterate through all edges
    while (e < V - 1 && i < graph->E) {
        Edge next_edge = graph->edge[i++];

        int x = find(parent, next_edge.u);
        int y = find(parent, next_edge.v);

        // If the two vertices are not in the same connected component
        if (x != y) {
            result[e++] = next_edge;
            unionSet(parent, x, y);
        }
    }

    // Print the MST
    printf("The edges of Minimum Cost Spanning Tree are:\n");
    for (int j = 0; j < e; j++)
        printf("Edge cost from %d to %d: %d\n", result[j].u, result[j].v, result[j].weight);

    printf("Minimum cost of spanning tree = %d\n", result[e - 1].weight);
}

int main() {
    int V, E;
    printf("Enter the number of vertices: ");
    scanf("%d", &V);

    printf("Enter the number of edges: ");
    scanf("%d", &E);

    Graph* graph = createGraph(V, E);

    printf("Enter the edges (source destination weight):\n");
    for (int i = 0; i < E; i++) {
        scanf("%d%d%d", &graph->edge[i].u, &graph->edge[i].v, &graph->edge[i].weight);
    }

    // Find and print the MST using Kruskal's Algorithm
    kruskal(graph);

    return 0;
}