-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimAlgorithm.c
More file actions
167 lines (133 loc) · 3.96 KB
/
PrimAlgorithm.c
File metadata and controls
167 lines (133 loc) · 3.96 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/************************************************************************
In this project is realized the algorithm of Prim, which find a Minimum
Spanning Tree (MST). It's received a txt file with the numbers of vertex,
edges and the edges with their costs. The result is printed in a txt file.
*************************************************************************/
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#define INFINITY 1000000000000
typedef struct edge {
int destination_vertex;
double cost;
struct edge *next;
} TypeEdge, *PEDGE;
typedef struct {
PEDGE *adjacentList;
int numVertex;
int numEdge;
int *predecessor; //using in prim
double *cost; //using in prim
double totalCost;
} TypeGraphs, *PGRAPHS;
//initializes graphs
void initializesGraphs(TypeGraphs* g, int nv, int na) {
g->adjacentList = (PEDGE*) malloc(nv*sizeof(PEDGE));
g->predecessor = (int*) malloc (nv*sizeof(int));
g->cost = (double*) malloc (nv*sizeof(double));
g->numVertex = nv;
g->numEdge = na;
g->totalCost = 0;
int i;
for (i = 0; i < g->numVertex; i++)
g->adjacentList[i] = NULL;
}
//insert edges (v1, v2) and (v2, v1) with cost c in the graph g, because the graph is directed
void insertEdge(int v1, int v2, double c, TypeGraphs* g) {
if (v1 >= 0 && v1 < g->numVertex && v2 >= 0 && v2 < g->numVertex && c < INFINITY) {
PEDGE new1 = (PEDGE) malloc(sizeof(TypeEdge));
new1->destination_vertex = v2;
new1->cost = c;
new1->next = g->adjacentList[v1];
g->adjacentList[v1] = new1;
PEDGE new2 = (PEDGE) malloc(sizeof(TypeEdge));
new2->destination_vertex = v1;
new2->cost = c;
new2->next = g->adjacentList[v2];
g->adjacentList[v2] = new2;
}
else {
printf("Could not insert edge (%d, %d).", v1, v2);
if (c >= INFINITY) printf(" %g >= INFINITY.\n", c);
else printf(" Invalid vertex.\n");
}
}
//executes the prim's algorithm
void executesPrim(TypeGraphs* g) {
//declaration of variables
int i;
int minVertex = 0;
int count = g->numVertex;
double minCost;
bool available = true;
bool vertexAdded[g->numVertex];
//initialization
for (i = 0; i < g->numVertex; i++) {
g->cost[i] = INFINITY;
g->predecessor[i] = -1;
vertexAdded[i] = false;
}
g->cost[0] = 0;
while (count > 0) {
available = true;
//find the available vertex that have the smaller cost
for (i = 0; i < g->numVertex; i++) {
if (!vertexAdded[i]) {
if (available || g->cost[i] < minCost) {
minVertex = i;
minCost = g->cost[i];
available = false;
}
}
}
//adds the vertex that have the smaller cost in vertexAdded
vertexAdded[minVertex] = true;
//visits every adjacent vertex of selected vertex
PEDGE nextU = g->adjacentList[minVertex];
while (nextU != NULL) {
if (!vertexAdded[nextU->destination_vertex] && nextU->cost < g->cost[nextU->destination_vertex]) {
g->predecessor[nextU->destination_vertex] = minVertex;
g->cost[nextU->destination_vertex] = nextU->cost;
}
nextU = nextU->next;
}
count--;
}
//calculates the total cost
for(i = 0; i < g->numVertex; i++)
g->totalCost += g->cost[i];
}
int main(int argc, char* argv[]) {
//argv[0] = prim.exe
//argv[1] = inputArchive.txt
//argv[2] = outputArchive.txt
TypeGraphs* g = (TypeGraphs*) malloc(sizeof(TypeGraphs));
//declaration of variables
FILE *fileIn, *fileOut;
int nv, na, i, u, v;
double cost;
//input of datas
fileIn = fopen(argv[1], "r");
if (fileIn) {
fscanf(fileIn, "%i %i", &nv, &na);
initializesGraphs(g, nv, na);
i = 0;
while (i < na) {
fscanf(fileIn, "%i %i %lf", &u, &v, &cost);
insertEdge(u, v, cost, g);
i++;
}
fclose(fileIn);
//executes the prim's algorithm
executesPrim(g);
//output of datas
fileOut = fopen(argv[2], "w");
fprintf(fileOut, "%g\n", g->totalCost);
for(i = 1; i < g->numVertex; i++)
fprintf(fileOut, "%d %d\n", g->predecessor[i], i);
fclose(fileOut);
}
else
printf("Make sure the files actually exist or are in the right directory.\n");
return 0;
}