@@ -11,31 +11,19 @@ typedef struct {
11
11
int a , b ;
12
12
} Pair ;
13
13
14
- /*
15
- * returns cost of edge formed by vertices `i` and `j` in
16
- * graph represented by adjacency matrix `matrix`
17
- */
18
14
double
19
15
cost (Matrix * matrix , int i , int j )
20
16
{
21
17
return matrix -> arr [i * matrix -> n_vertices + j ];
22
18
}
23
19
24
- /*
25
- * sets cost to `cost` between vertices `i` and `j` of
26
- * graph `matrix`
27
- */
28
20
void
29
21
set_cost (Matrix * matrix , int i , int j , double cost )
30
22
{
31
23
matrix -> arr [i * matrix -> n_vertices + j ] =
32
24
matrix -> arr [j * matrix -> n_vertices + i ] = cost ;
33
25
}
34
26
35
- /*
36
- * creates matrix with `n` rows and columns to represent graph with
37
- * `n` nodes and initializes all edges to infinity i.e. not edges
38
- */
39
27
Matrix *
40
28
create_matrix (int n_vertices )
41
29
{
@@ -48,15 +36,13 @@ create_matrix(int n_vertices)
48
36
return matrix ;
49
37
}
50
38
51
- /* destructor for matrix created with `create_matrix` */
52
39
void
53
40
destroy_matrix (Matrix * matrix )
54
41
{
55
42
free (matrix -> arr );
56
43
free (matrix );
57
44
}
58
45
59
- /* gets edge with minimum cost from the graph */
60
46
Pair
61
47
get_min_cost_edge (Matrix * matrix )
62
48
{
@@ -73,7 +59,6 @@ get_min_cost_edge(Matrix * matrix)
73
59
return min ;
74
60
}
75
61
76
- /* function to print square matrix */
77
62
void
78
63
print_matrix (Matrix * matrix )
79
64
{
@@ -85,10 +70,6 @@ print_matrix(Matrix *matrix)
85
70
}
86
71
}
87
72
88
- /*
89
- * function accepts a graph represented by adjacency matrix `matrix`
90
- * and returns its MST's adjacency matrix
91
- */
92
73
Matrix *
93
74
prims_algorithm (Matrix * matrix )
94
75
{
@@ -105,12 +86,7 @@ prims_algorithm(Matrix *matrix)
105
86
near [i ] = k ;
106
87
}
107
88
printf ("\n" );
108
- /* we represent including `k` and `l` in MST by
109
- * setting their keys in `near` to -1 */
110
89
near [k ] = near [l ] = -1 ;
111
- /* adjaceny matrix will have `n_vertices - 1` edges
112
- * we have filled one edge so loop has to run
113
- * `n_vertices - 2` times */
114
90
for (int _ = 1 ; _ <= matrix -> n_vertices - 2 ; ++ _ ) {
115
91
int index ;
116
92
double min_cost = INFINITY ;
@@ -136,14 +112,19 @@ int
136
112
main ()
137
113
{
138
114
Matrix * matrix = create_matrix (6 );
139
- set_cost (matrix , 0 , 1 , 8 );
115
+ set_cost (matrix , 0 , 1 , 3 );
116
+ set_cost (matrix , 0 , 3 , 4 );
140
117
set_cost (matrix , 0 , 2 , 1 );
141
- set_cost (matrix , 1 , 2 , 5 );
142
- set_cost (matrix , 1 , 3 , 2 );
143
- set_cost (matrix , 2 , 4 , 7 );
144
- set_cost (matrix , 3 , 4 , 2 );
145
- set_cost (matrix , 3 , 5 , 3 );
146
- set_cost (matrix , 4 , 5 , 15 );
118
+
119
+ set_cost (matrix , 1 , 3 , 5 );
120
+ set_cost (matrix , 1 , 4 , 3 );
121
+
122
+ set_cost (matrix , 2 , 3 , 5 );
123
+ set_cost (matrix , 2 , 5 , 3 );
124
+
125
+ set_cost (matrix , 5 , 3 , 6 );
126
+ set_cost (matrix , 5 , 4 , 2 );
127
+
147
128
Matrix * MST = prims_algorithm (matrix );
148
129
print_matrix (matrix );
149
130
printf ("\n" );
0 commit comments