-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprims.c
More file actions
76 lines (59 loc) · 1.73 KB
/
prims.c
File metadata and controls
76 lines (59 loc) · 1.73 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
#include <stdio.h>
#include <limits.h>
#define MAX_VERTICES 100
#define INF INT_MAX
int minKey(int key[], int mstSet[], int vertices) {
int min = INF, min_index;
for (int v = 0; v < vertices; v++) {
if (mstSet[v] == 0 && key[v] < min) {
min = key[v];
min_index = v;
}
}
return min_index;
}
void printMST(int parent[], int graph[MAX_VERTICES][MAX_VERTICES], int vertices) {
printf("Edge \tWeight\n");
for (int i = 1; i < vertices; i++) {
printf("%d - %d \t%d \n", parent[i], i, graph[i][parent[i]]);
}
}
void primMST(int graph[MAX_VERTICES][MAX_VERTICES], int vertices) {
int parent[MAX_VERTICES];
int key[MAX_VERTICES];
int mstSet[MAX_VERTICES];
for (int i = 0; i < vertices; i++) {
key[i] = INF;
mstSet[i] = 0;
}
key[0] = 0;
parent[0] = -1;
for (int count = 0; count < vertices - 1; count++) {
int u = minKey(key, mstSet, vertices);
mstSet[u] = 1;
for (int v = 0; v < vertices; v++) {
if (graph[u][v] && mstSet[v] == 0 && graph[u][v] < key[v]) {
parent[v] = u;
key[v] = graph[u][v];
}
}
}
printMST(parent, graph, vertices);
}
int main() {
int vertices;
printf("Enter the number of vertices: ");
scanf("%d", &vertices);
int graph[MAX_VERTICES][MAX_VERTICES];
printf("Enter the adjacency matrix (enter 0 if there is no edge):\n");
for (int i = 0; i < vertices; i++) {
for (int j = 0; j < vertices; j++) {
scanf("%d", &graph[i][j]);
if (graph[i][j] == 0 && i != j) {
graph[i][j] = INF;
}
}
}
primMST(graph, vertices);
return 0;
}