kruskal algo P4

 #include <stdio.h>

#include <stdlib.h>


#define MAX_EDGES 1000


typedef struct Edge {

    int src, dest, weight;

} Edge;


typedef struct Graph {

    int V, E;

    Edge edges[MAX_EDGES];

} Graph;


typedef struct Subset {

    int parent, rank;

} Subset;


Graph* createGraph(int V, int E) {

    Graph* graph = (Graph*) malloc(sizeof(Graph));

    graph->V = V;

    graph->E = E;

    return graph;

}


int find(Subset subsets[], int i) {

    if (subsets[i].parent != i) {

        subsets[i].parent = find(subsets, subsets[i].parent);

    }

    return subsets[i].parent;

}


void Union(Subset subsets[], int x, int y) {

    int xroot = find(subsets, x);

    int yroot = find(subsets, y);


    if (subsets[xroot].rank < subsets[yroot].rank) {

        subsets[xroot].parent = yroot;

    } else if (subsets[xroot].rank > subsets[yroot].rank) {

        subsets[yroot].parent = xroot;

    } else {

        subsets[yroot].parent = xroot;

        subsets[xroot].rank++;

    }

}


int compare(const void* a, const void* b) {

    Edge* a_edge = (Edge*) a;

    Edge* b_edge = (Edge*) b;

    return a_edge->weight - b_edge->weight;

}


void kruskalMST(Graph* graph) {

    Edge mst[graph->V];  // To store the resultant MST

    int e = 0;  // An index variable, used for result[]

    int i = 0;  // An index variable, used for sorted edges


    // Step 1: Sort all the edges in non-decreasing order of their weight.

    qsort(graph->edges, graph->E, sizeof(Edge), compare);


    // Allocate memory for creating V subsets

    Subset* subsets = (Subset*) malloc(graph->V * sizeof(Subset));


    // Create V subsets with single elements

    for (int v = 0; v < graph->V; ++v) {

        subsets[v].parent = v;

        subsets[v].rank = 0;

    }


    // Number of edges to be taken is equal to V-1

    while (e < graph->V - 1 && i < graph->E) {

        // Step 2: Pick the smallest edge. Check if it forms a cycle with the spanning-tree formed so far.

        Edge next_edge = graph->edges[i++];


        int x = find(subsets, next_edge.src);

        int y = find(subsets, next_edge.dest);


        // If including this edge does not cause a cycle, include it in the result and increment the index of the result for the next edge

        if (x != y) {

            mst[e++] = next_edge;

            Union(subsets, x, y);

        }

        // Else discard the next_edge

    }


    // Print the contents of result[] to display the built MST

    printf("Minimum Spanning Tree:\n");

    for (i = 0; i < e; ++i) {

        printf("(%d, %d) -> %d\n", mst[i].src, mst[i].dest, mst[i].weight);

    }


    free(subsets);

}


int main() {

    int V, E;

    printf("Enter number of vertices and edges: ");

    scanf("%d %d", &V, &E);


    if (E > MAX_EDGES) {

        printf("Error: Number of edges exceeds the maximum limit.\n");

        return 1;

    }


    Graph* graph = createGraph(V, E);


    printf("Enter edges and their weights:\n");

    for (int i = 0; i < E; ++i) {

        scanf("%d %d %d", &graph->edges[i].src, &graph->edges[i].dest, &graph->edges[i].weight);

    }


    kruskalMST(graph);


    free(graph); // Free dynamically allocated memory for graph

    return 0;

}


Comments

Popular posts from this blog

topological sort P6

prim's algo P5