-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13_Prims_Algorithm.c
More file actions
51 lines (51 loc) · 1.25 KB
/
13_Prims_Algorithm.c
File metadata and controls
51 lines (51 loc) · 1.25 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
//13_Prims_Algorithm.c
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define MAX_VERTICES 26
int graph[MAX_VERTICES][MAX_VERTICES];
int parent[MAX_VERTICES];
int key[MAX_VERTICES];
int visited[MAX_VERTICES];
void prim(int n) {
int i, j, u, v, min;
for (i = 0; i < n; i++) {
key[i] = INT_MAX;
visited[i] = 0;
}
key[0] = 0;
parent[0] = -1;
for (i = 0; i < n - 1; i++) {
min = INT_MAX;
for (j = 0; j < n; j++) {
if (!visited[j] && key[j] < min) {
min = key[j];
u = j;
}
}
visited[u] = 1;
for (v = 0; v < n; v++) {
if (graph[u][v] && !visited[v] && graph[u][v] < key[v]) {
key[v] = graph[u][v];
parent[v] = u;
}
}
}
printf("Edges in the MST:\n");
for (i = 1; i < n; i++) {
printf("%c - %c (%d)\n", parent[i] + 'A', i + 'A', graph[i][parent[i]]);
}
}
int main() {
int n, i, j;
printf("Enter the number of vertices (max 26): ");
scanf("%d", &n);
printf("Enter the adjacency matrix:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &graph[i][j]);
}
}
prim(n);
return 0;
}