-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.c
288 lines (264 loc) · 7.48 KB
/
graph.c
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include "def.h"
extern FEDGE **edges;
double *f_i, *f_j, max_f_i, max_f_j;
// local methods
int compare_edges_desc(const void *a, const void *b);
int compare_edges_asc(const void *a, const void *b);
double calculate_node_ratio(int i, int len, int **t, int **w);
void get_flow_usage(FEDGE **edges, int len);
FEDGE **initialize_edge_matrix(int **t, int **w, int len)
{
// initialization
FEDGE **edges = (FEDGE **)calloc(len, sizeof(FEDGE));
if (edges == NULL)
{
printf("\nError: Insuficient memory \n");
exit(8);
}
for (int i = 0; i < len; i++)
{
edges[i] = (FEDGE *)calloc(len, sizeof(FEDGE));
if (edges[i] == NULL)
{
printf("\nError: Insuficient memory \n");
exit(8);
}
for (int j = 0; j < len; j++)
{
edges[i][j].i = i;
edges[i][j].j = j;
edges[i][j].t = t[i][j];
edges[i][j].sp = edges[i][j].t;
edges[i][j].w = w[i][j];
edges[i][j].is_access_edge = false;
edges[i][j].is_exit_edge = false;
edges[i][j].is_direct = false;
edges[i][j].is_hub_edge = false;
edges[i][j].has_saving = false;
}
}
return edges;
}
void restart_edge_matrix(FEDGE **edges, int len, int **t, int **w)
{
for (int i = 0; i < len; i++)
{
for (int j = 0; j < len; j++)
{
edges[i][j].i = i;
edges[i][j].j = j;
edges[i][j].t = t[i][j];
edges[i][j].sp = edges[i][j].t;
edges[i][j].w = w[i][j];
edges[i][j].is_access_edge = false;
edges[i][j].is_exit_edge = false;
edges[i][j].is_direct = false;
edges[i][j].is_hub_edge = false;
edges[i][j].has_saving = false;
}
}
}
int *generate_edge_vector(int *z, int len, int p)
{
int *h = create_int_vector(p);
int h_len = 0;
for (int i = 0; i < len; i++)
{
if (z[i] == 1)
h[h_len++] = i;
}
return h;
}
void print_edges_analysis(FEDGE **edges, int len)
{
// int **direct_edges = create_int_matrix(len, len);
// int **access_edges = create_int_matrix(len, len);
// int **hub_edges = create_int_matrix(len, len);
// for (int i = 0; i < len; i++)
// {
// for (int j = 0; j < len; j++)
// {
// direct_edges[i][j] = edges[i][j].is_direct ? 1 : 0;
// access_edges[i][j] = edges[i][j].is_access_edge ? 1 : 0;
// hub_edges[i][j] = edges[i][j].is_hub_edge ? 1 : 0;
// }
// }
// double direct_pct = 0.0;
// double hub_pct = 0;
// int access = 0;
// int hubs = 0;
// int directs = 0;
get_flow_usage(edges, len);
// printMatrix(direct_edges, len, len, "Direct edges: ");
// printMatrix(access_edges, len, len, "Access edges: ");
// printMatrix(hub_edges, len, len, "Hub edges: ");
// free_int_matrix(direct_edges, len);
// free_int_matrix(access_edges, len);
// free_int_matrix(hub_edges, len);
}
void get_flow_usage(FEDGE **edges, int len)
{
int access_edges = 0;
int hub_edges = 0;
int direct_edges = 0;
int hub_flow = 0;
int direct_flow = 0;
double hub_pct = 0.0;
double direct_pct = 0.0;
for (int i = 0; i < len; i++)
{
for (int j = 0; j < len; j++)
{
if(edges[i][j].is_hub_edge)
{
hub_edges++;
hub_flow += edges[i][j].w;
}
else if (edges[i][j].is_access_edge)
{
access_edges++;
hub_flow += edges[i][j].w;
}
else if (edges[i][j].is_direct)
{
direct_edges++;
direct_flow += edges[i][j].w;
}
}
}
hub_pct = hub_flow * 1.0 / (hub_flow + direct_flow);
direct_pct = 1 - hub_pct;
printf("Direct_flow: %5.2f, hub_flow: %5.2f, Direct Edges: %d, Access Edges: %d\n", direct_pct, hub_pct, direct_edges, access_edges);
}
void clone_edge_matrix(FEDGE **origin, FEDGE **target, int len)
{
for (int i = 0; i < len; i++)
{
for (int j = 0; j < len; j++)
{
target[i][j].i = origin[i][j].i;
target[i][j].j = origin[i][j].j;
target[i][j].t = origin[i][j].t;
target[i][j].sp = origin[i][j].sp;
target[i][j].w = origin[i][j].w;
target[i][j].is_access_edge = origin[i][j].is_access_edge;
target[i][j].is_exit_edge = origin[i][j].is_exit_edge;
target[i][j].is_direct = origin[i][j].is_direct;
target[i][j].is_hub_edge = origin[i][j].is_hub_edge;
target[i][j].has_saving = origin[i][j].has_saving;
}
}
}
void destroy_edges(FEDGE **edges, int len)
{
for (int i = 0; i < len; i++)
free(edges[i]);
free(edges);
}
FNODE *create_node_list(int len)
{
return (FNODE *)calloc(len, sizeof(FNODE));
}
void populate_node_list(FNODE *node_list, int len, int **t, int **w)
{
for (int i = 0; i < len; i++)
{
node_list[i].ratio = calculate_node_ratio(i, len, t, w);
node_list[i].n = i;
}
}
double calculate_node_ratio(int i, int len, int **t, int **w)
{
int acum = 0;
for (int j = 0; j < len; j++)
{
acum += t[i][j] * w[i][j];
}
return acum/len;
}
int compare_nodes_desc(const void *a, const void *b)
{
if (((FNODE *)a)->ratio > ((FNODE *)b)->ratio)
return -1;
if (((FNODE *)a)->ratio < ((FNODE *)b)->ratio)
return 1;
return 0;
}
int compare_nodes_asc(const void *a, const void *b)
{
if (((FNODE *)a)->ratio > ((FNODE *)b)->ratio)
return 1;
if (((FNODE *)a)->ratio < ((FNODE *)b)->ratio)
return -1;
return 0;
}
void sort_nodes(FNODE *node_list, int len, int order)
{
if (order)
qsort(node_list, len, sizeof(FNODE), compare_edges_asc);
else
{
qsort(node_list, len, sizeof(FNODE), compare_edges_desc);
}
}
double get_max_node_ratio(FNODE *node_list, int len)
{
double max_ratio = 0;
for (int i = 0; i < len; i++)
max_ratio = node_list[i].ratio > max_ratio ? node_list[i].ratio : max_ratio;
return max_ratio;
}
double get_min_node_ratio(FNODE *node_list, int len)
{
double min_ratio = 0;
for (int i = 0; i < len; i++)
min_ratio = node_list[i].ratio > min_ratio ? node_list[i].ratio : min_ratio;
return min_ratio;
}
int compare_edges_desc(const void *a, const void *b)
{
if (((FEDGE *)a)->ratio > ((FEDGE *)b)->ratio)
return -1;
if (((FEDGE *)a)->ratio < ((FEDGE *)b)->ratio)
return 1;
return 0;
}
int compare_edges_asc(const void *a, const void *b)
{
if (((FEDGE *)a)->ratio > ((FEDGE *)b)->ratio)
return 1;
if (((FEDGE *)a)->ratio < ((FEDGE *)b)->ratio)
return -1;
return 0;
}
void sort_edges(FEDGE *edge_list, int len, int order)
{
if (order)
qsort(edge_list, len * len, sizeof(FEDGE), compare_edges_asc);
else
{
qsort(edge_list, len * len, sizeof(FEDGE), compare_edges_desc);
}
}
double calculate_edge_ratio(int i, int j)
{
return f_i[i] / max_f_i;
}
FEDGE *create_edge_list(int len)
{
return (FEDGE *)calloc(len * len, sizeof(FEDGE));
}
void populate_edge_list(FEDGE *edge_list, int len)
{
int k = 0;
for (int i = 0; i < len; i++)
{
for (int j = 0; j < len; j++)
{
edge_list[k].ratio = calculate_edge_ratio(i, j);
edge_list[k].i = i;
edge_list[k].j = j;
k++;
}
}
}