-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14.c
67 lines (63 loc) · 1.43 KB
/
14.c
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
#include <stdio.h>
#include <stdlib.h>
int cost[10][10], n, min_cost = 0, min, a, b, visited[10] = {0}, edges = 0, count = 0;
void createGraph()
{
int i, j;
printf("Enter no. of vertices: ");
scanf("%d", &n);
printf("Enter Weight Matrix: \n");
for (i = 1; i <= n; ++i)
{
for (j = 1; j <= n; j++)
{
scanf("%d", &cost[i][j]);
if (cost[i][j] == 0)
cost[i][j] = 1000;
}
}
}
void prim()
{
int i, j;
printf("Minimum Cost Spanning Tree\n");
visited[1] = 1;
while (edges < n - 1)
{
min = 1000;
for (i = 1; i <= n; i++)
{
count++;
if (visited[i])
{
for (j = 1; j <= n; j++)
{
if (cost[i][j] < min && !visited[j])
{
min = cost[i][j];
a = i;
b = j;
}
}
}
}
if(min<1000)
printf("%d -> %d : Cost-%d\n", a, b, min);
else
{
printf("The Graph is disconnected,Prim's Algorithm not applicable\n");
exit(1);
}
min_cost += min;
visited[b] = 1;
edges++;
}
printf("Minimum Cost: %d\n", min_cost);
}
void main()
{
int i, j;
createGraph();
prim();
printf("Operation Count : %d\n", count);
}