Skip to content

Commit 7617202

Browse files
committed
first
1 parent 4b89e2d commit 7617202

File tree

1 file changed

+12
-31
lines changed

1 file changed

+12
-31
lines changed

algorithms/prims-algorithm/prims.c

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,19 @@ typedef struct {
1111
int a, b;
1212
} Pair;
1313

14-
/*
15-
* returns cost of edge formed by vertices `i` and `j` in
16-
* graph represented by adjacency matrix `matrix`
17-
*/
1814
double
1915
cost(Matrix * matrix, int i, int j)
2016
{
2117
return matrix->arr[i * matrix->n_vertices + j];
2218
}
2319

24-
/*
25-
* sets cost to `cost` between vertices `i` and `j` of
26-
* graph `matrix`
27-
*/
2820
void
2921
set_cost(Matrix *matrix, int i, int j, double cost)
3022
{
3123
matrix->arr[i * matrix->n_vertices + j] =
3224
matrix->arr[j * matrix->n_vertices + i] = cost;
3325
}
3426

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-
*/
3927
Matrix *
4028
create_matrix(int n_vertices)
4129
{
@@ -48,15 +36,13 @@ create_matrix(int n_vertices)
4836
return matrix;
4937
}
5038

51-
/* destructor for matrix created with `create_matrix` */
5239
void
5340
destroy_matrix(Matrix *matrix)
5441
{
5542
free(matrix->arr);
5643
free(matrix);
5744
}
5845

59-
/* gets edge with minimum cost from the graph */
6046
Pair
6147
get_min_cost_edge(Matrix * matrix)
6248
{
@@ -73,7 +59,6 @@ get_min_cost_edge(Matrix * matrix)
7359
return min;
7460
}
7561

76-
/* function to print square matrix */
7762
void
7863
print_matrix(Matrix *matrix)
7964
{
@@ -85,10 +70,6 @@ print_matrix(Matrix *matrix)
8570
}
8671
}
8772

88-
/*
89-
* function accepts a graph represented by adjacency matrix `matrix`
90-
* and returns its MST's adjacency matrix
91-
*/
9273
Matrix *
9374
prims_algorithm(Matrix *matrix)
9475
{
@@ -105,12 +86,7 @@ prims_algorithm(Matrix *matrix)
10586
near[i] = k;
10687
}
10788
printf("\n");
108-
/* we represent including `k` and `l` in MST by
109-
* setting their keys in `near` to -1 */
11089
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 */
11490
for (int _ = 1; _ <= matrix->n_vertices - 2; ++_) {
11591
int index;
11692
double min_cost = INFINITY;
@@ -136,14 +112,19 @@ int
136112
main()
137113
{
138114
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);
140117
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+
147128
Matrix *MST = prims_algorithm(matrix);
148129
print_matrix(matrix);
149130
printf("\n");

0 commit comments

Comments
 (0)