-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
126 lines (104 loc) · 3.3 KB
/
Copy pathmain.c
File metadata and controls
126 lines (104 loc) · 3.3 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
#include <stdio.h>
#include <stdlib.h>
#include "graph.h"
#include "PQ.h"
#include "vertex.h"
#include <time.h>
#include "RTT.h"
/**
* @brief Lê o arquivo de entrada, construindo o grafo a partir dos dados lidos
*
* @param inFile
* @return Graph*
*/
static Graph* LeArquivo_ConstroiGrafo (FILE* inFile);
/**
* @brief Calcula o caminho mínimo de todos os vértices servidores e clientes para
* todos os outros vértices, usando o algoritmo de Dijkstra
*
* @param G
* @return long**
*/
static long double** dijkstra_all_S_and_C_pairs (Graph* G);
/**
* @brief Libera memória alocada por uma matriz de 'size' linhas
*
* @param mat
* @param size
*/
void liberaMatriz (long double ** mat, int size);
int main (int argc, char** argv) {
if (argc != 3) {
puts("Use: ./exe <entrada.txt> <saida.txt>");
}
FILE* inFile= fopen(argv[1], "r");
if (!inFile) {
puts("File not Found");
exit(1);
}
Graph* G = LeArquivo_ConstroiGrafo(inFile);
long double** shortestDistance_All = dijkstra_all_S_and_C_pairs(G);
//a quantidade de RTTs a serem calculados é o produto entre a quantidade de servidores
//e clientes
int size = returnNumServers(G) * returnNumClients(G);
//inicia um vetor de ponteiros para um RTT
RTT** rttvec = RTTvec_create(size);
calculate_RTTs_Reais(G, shortestDistance_All, rttvec);
calculate_RTTs_Aproximados(G, shortestDistance_All ,rttvec);
RTT_sort(rttvec, size);
FILE* outFile = fopen(argv[2], "w");
RTTvec_fprint(outFile, rttvec, size);
//Area de liberação de memória
liberaMatriz(shortestDistance_All, returnNumVertices(G));
RTTvec_destroy(rttvec, size);
destroyGraph(G);
fclose(inFile);
fclose(outFile);
return 0;
}
static Graph* LeArquivo_ConstroiGrafo (FILE* inFile) {
int V, E;
int numServidores, numClientes, numMonitores;
//lê o número de vértices e de arestas
fscanf(inFile, "%d %d\n", &V, &E);
int aux;
//lê o número de servidores, clientes e monitores
fscanf(inFile, "%d %d %d\n", &numServidores, &numClientes, &numMonitores);
Graph* G = initGraph(V, numServidores, numClientes, numMonitores);
for (int i=0; i<numServidores; i++) {
fscanf(inFile, "%d\n", &aux);
addServidor(G, aux);
}
for (int i=0; i<numClientes; i++) {
fscanf(inFile, "%d\n", &aux);
addCliente(G, aux);
}
for (int i=0; i<numMonitores; i++) {
fscanf(inFile, "%d\n", &aux);
addMonitor(G, aux);
}
int u, v;
long double weight;
//lê as arestas e adiciona elas no grafo
for (int i=0; i<E; i++) {
fscanf(inFile, "%d %d %LF\n", &u, &v, &weight);
addEdge(G, u, v, weight);
}
return G;
}
static long double** dijkstra_all_S_and_C_pairs (Graph* G) {
int numVertices = returnNumVertices(G);
long double** dist = malloc(sizeof(long double*)*numVertices);
for (int i=0; i<numVertices; i++) {
dist[i] = calloc(numVertices, sizeof(long double));
if (!ehVerticePadrao(G, i))
dijkstraAlgorithm(G, i, dist[i]);
}
return dist;
}
void liberaMatriz (long double ** mat, int size) {
for (int i=0; i<size; i++) {
free(mat[i]);
}
free(mat);
}