-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtw6Prims.cpp
More file actions
75 lines (66 loc) · 1.56 KB
/
tw6Prims.cpp
File metadata and controls
75 lines (66 loc) · 1.56 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
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n , cost[20][20] , ne = 1 , min , mincost=0 , a , b , u , v , visited[20] = {0};
cout << "Enter the Number of Vertices : ";
cin >> n;
cout << "Enter the costs in cost matrix\n";
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cin >> cost[i][j];
if(cost[i][j] == 0)
{
cost[i][j] = 999;
}
}
}
visited[1] = 1;
while(ne < n)
{
min = 999;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(cost[i][j] < min && visited[i]!=0)
{
min = cost[i][j];
a = i;
b = j;
}
}
}
if(visited[a] == 0 || visited[b] == 0)
{
mincost+=cost[a][b];
printf("%d Edge : (%d , %d) ----> %d\n" , ne++ , a, b , min);
visited[b] = 1;
}
cost[a][b] = 999;
cost[b][a] = 999;
}
cout << "The Minimum cost is : " << mincost << endl;
}
/* Let us create the following graph
2 3
(0)--(1)--(2)
| / \ |
6| 8/ \5 |7
| / \ |
(3)-------(4)
9 */
// Enter the Number of Vertices : 5
// Enter the costs in cost matrix
// 0 2 0 6 0
// 2 0 3 8 5
// 0 3 0 0 7
// 6 8 0 0 9
// 0 5 7 9 0
// 1 Edge : (1 , 2) ----> 2
// 2 Edge : (2 , 3) ----> 3
// 3 Edge : (2 , 5) ----> 5
// 4 Edge : (1 , 4) ----> 6
// The Minimum cost is : 16