-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsequential_mst.c
More file actions
136 lines (112 loc) · 3.49 KB
/
sequential_mst.c
File metadata and controls
136 lines (112 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Structure to represent an edge in the graph
typedef struct {
int src, dest, weight;
} Edge;
// Structure to represent a graph
typedef struct {
int V, E; // Number of vertices and edges
Edge *edges; // Array of edges
} Graph;
// Function to create a graph
Graph* createGraph(int V, int E) {
Graph *graph = (Graph *)malloc(sizeof(Graph));
graph->V = V;
graph->E = E;
graph->edges = (Edge *)malloc(E * sizeof(Edge));
return graph;
}
// Structure to represent a subset for union-find
typedef struct {
int parent;
int rank;
} Subset;
// Function to find the root of an element (with path compression)
int find(Subset subsets[], int i) {
if (subsets[i].parent != i) {
subsets[i].parent = find(subsets, subsets[i].parent);
}
return subsets[i].parent;
}
// Function to unite two subsets
void unionSets(Subset subsets[], int x, int y) {
int rootX = find(subsets, x);
int rootY = find(subsets, y);
if (subsets[rootX].rank < subsets[rootY].rank) {
subsets[rootX].parent = rootY;
} else if (subsets[rootX].rank > subsets[rootY].rank) {
subsets[rootY].parent = rootX;
} else {
subsets[rootY].parent = rootX;
subsets[rootX].rank++;
}
}
// Comparison function for qsort
int compareEdges(const void *a, const void *b) {
Edge *edgeA = (Edge *)a;
Edge *edgeB = (Edge *)b;
return edgeA->weight - edgeB->weight;
}
// Kruskal's MST algorithm
void kruskalMST(Graph *graph) {
int V = graph->V;
Edge result[V]; // Store the MST edges
int e = 0; // Index for result[]
int i = 0; // Index for sorted edges
// Step 1: Sort edges by weight
qsort(graph->edges, graph->E, sizeof(graph->edges[0]), compareEdges);
// Allocate memory for subsets
Subset *subsets = (Subset *)malloc(V * sizeof(Subset));
// Initialize subsets
for (int v = 0; v < V; v++) {
subsets[v].parent = v;
subsets[v].rank = 0;
}
// Process edges
while (e < V - 1 && i < graph->E) {
Edge nextEdge = graph->edges[i++];
int x = find(subsets, nextEdge.src);
int y = find(subsets, nextEdge.dest);
// If including this edge doesn't cause a cycle
if (x != y) {
result[e++] = nextEdge;
unionSets(subsets, x, y);
}
}
// Display the MST
printf("Edges in the Minimum Spanning Tree:\n");
int totalWeight = 0;
for (int j = 0; j < e; j++) {
printf("%d -- %d == %d\n", result[j].src, result[j].dest, result[j].weight);
totalWeight += result[j].weight;
}
printf("Total weight of the MST: %d\n", totalWeight);
free(subsets);
}
int main() {
int V, E;
// Input the graph
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 (src dest weight):\n");
for (int i = 0; i < E; i++) {
scanf("%d %d %d", &graph->edges[i].src, &graph->edges[i].dest, &graph->edges[i].weight);
}
// Measure execution time
clock_t start, end;
start = clock();
printf("\nRunning Kruskal's MST Algorithm...\n");
kruskalMST(graph);
end = clock();
// Calculate and display execution time
double time_taken = ((double)(end - start)) / CLOCKS_PER_SEC;
printf("Execution time: %f seconds\n", time_taken);
free(graph->edges);
free(graph);
return 0;
}