-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphEccentricityMeasures.c
229 lines (179 loc) · 6.47 KB
/
GraphEccentricityMeasures.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
//
// Algoritmos e Estruturas de Dados --- 2024/2025
//
// Joaquim Madeira - Dec 2024
//
// GraphEccentricityMeasures
//
// Student Name :
// Student Number :
// Student Name : Duarte Branco
// Student Number : 119253
/*** COMPLETE THE GraphEccentricityMeasuresCompute FUNCTION ***/
/*** COMPLETE THE GraphGetCentralVertices FUNCTION ***/
/*** COMPLETE THE GraphEccentricityMeasuresPrint FUNCTION ***/
#include "GraphEccentricityMeasures.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
// INT_MAX == INF
#include <limits.h>
#include "Graph.h"
#include "GraphAllPairsShortestDistances.h"
struct _GraphEccentricityMeasures {
unsigned int*
centralVertices; // centralVertices[0] = number of central vertices
// array size is (number of central vertices + 1)
int* eccentricity; // the eccentricity value of each vertex
Graph* graph; // the graph
int graphRadius; // the graph radius
int graphDiameter; // the graph diameter
};
// Auxiliary static functions
static int GraphComputeRadius(const GraphEccentricityMeasures* p) {
assert(p != NULL);
int radius = INT_MAX;
unsigned int numVertices = GraphGetNumVertices(p->graph);
for (unsigned int v = 0; v < numVertices; v++) {
if (p->eccentricity[v] != -1 && p->eccentricity[v] < radius) {
radius = p->eccentricity[v];
}
}
radius = (radius == INT_MAX) ? -1 : radius;
return radius;
}
static int GraphComputeDiameter(const GraphEccentricityMeasures* p) {
assert(p != NULL);
int diameter = -1;
unsigned int numVertices = GraphGetNumVertices(p->graph);
for (unsigned int v = 0; v < numVertices; v++) {
if (p->eccentricity[v] != -1 && p->eccentricity[v] > diameter) {
diameter = p->eccentricity[v];
}
}
return diameter;
}
// Allocate memory
// Compute the vertex eccentricity values
// Compute graph radius and graph diameter
// Compute the set of central vertices
GraphEccentricityMeasures* GraphEccentricityMeasuresCompute(Graph* g) {
assert(g != NULL);
// COMPLETE THE CODE
// CREATE AUXILIARY (static) FUNCTIONS, IF USEFUL
// Graph radius --- the smallest vertex eccentricity value
// Graph diameter --- the largest vertex eccentricity value
// Do not forget that -1 represents an IDEFINITE value
// Computing the set of central vertices
// Allocate the central vertices array : number of central vertices + 1
// Fill in the central vertices array
unsigned int numVertices = GraphGetNumVertices(g);
// Allocate memory for result
GraphEccentricityMeasures* result = (GraphEccentricityMeasures*)malloc(sizeof(struct _GraphEccentricityMeasures));
assert(result != NULL);
result->graph = g;
result->eccentricity = (int*)malloc(numVertices * sizeof(int));
assert(result->eccentricity != NULL);
// All-pairs shortest distances
GraphAllPairsShortestDistances* allPairsDistances = GraphAllPairsShortestDistancesExecute(g);
assert(allPairsDistances != NULL);
// Eccentricity for each vertex
for (unsigned int v = 0; v < numVertices; v++) {
int maxDistance = -1;
int hasPath = 0;
for (unsigned int w = 0; w < numVertices; w++) {
if (v == w) continue; // Skip self
int distance = GraphGetDistanceVW(allPairsDistances, v, w);
if (distance != -1) {
hasPath = 1;
if (distance > maxDistance) {
maxDistance = distance;
}
}
}
// If vertex has no paths to other vertices, set eccentricity to -1
result->eccentricity[v] = hasPath ? maxDistance : -1;
}
// Compute radius and diameter
result->graphRadius = GraphComputeRadius(result);
result->graphDiameter = GraphComputeDiameter(result);
// Compute the set of central vertices
unsigned int centralCount = 0;
for (unsigned int v = 0; v < numVertices; v++) {
if (result->eccentricity[v] == result->graphRadius) {
centralCount++;
}
}
// Allocate memory for the central vertices array
result->centralVertices = (unsigned int*)malloc((centralCount + 1) * sizeof(unsigned int));
assert(result->centralVertices != NULL);
result->centralVertices[0] = centralCount;
unsigned int index = 1;
for (unsigned int v = 0; v < numVertices; v++) {
if (result->eccentricity[v] == result->graphRadius) {
result->centralVertices[index++] = v;
}
}
GraphAllPairsShortestDistancesDestroy(&allPairsDistances);
return result;
}
void GraphEccentricityMeasuresDestroy(GraphEccentricityMeasures** p) {
assert(*p != NULL);
GraphEccentricityMeasures* aux = *p;
free(aux->centralVertices);
free(aux->eccentricity);
free(*p);
*p = NULL;
}
// Getting the computed measures
int GraphGetRadius(const GraphEccentricityMeasures* p) {
assert(p != NULL);
return p->graphRadius;
}
int GraphGetDiameter(const GraphEccentricityMeasures* p) {
assert(p != NULL);
return p->graphDiameter;
}
int GraphGetVertexEccentricity(const GraphEccentricityMeasures* p,
unsigned int v) {
assert(p != NULL);
assert(v < GraphGetNumVertices(p->graph));
assert(p->eccentricity != NULL);
return p->eccentricity[v];
}
// Getting a copy of the set of central vertices
// centralVertices[0] = number of central vertices in the set
unsigned int* GraphGetCentralVertices(const GraphEccentricityMeasures* p) {
assert(p != NULL);
assert(p->centralVertices != NULL);
// COMPLETE THE CODE
unsigned int centralCount = p->centralVertices[0];
unsigned int* centralVerticesCopy = (unsigned int*)malloc((centralCount + 1) * sizeof(unsigned int));
assert(centralVerticesCopy != NULL);
for (unsigned int i = 0; i <= centralCount; i++) {
centralVerticesCopy[i] = p->centralVertices[i];
}
return centralVerticesCopy;
}
// Print the graph radius and diameter
// Print the vertex eccentricity values
// Print the set of central vertices
void GraphEccentricityMeasuresPrint(const GraphEccentricityMeasures* p) {
assert(p != NULL);
// COMPLETE THE CODE
// Print the vertex eccentricity values
printf("Graph Radius: %d\n", p->graphRadius);
printf("Graph Diameter: %d\n", p->graphDiameter);
// Print the vertex eccentricity values
unsigned int numVertices = GraphGetNumVertices(p->graph);
printf("Vertex Eccentricities:\n");
for (unsigned int v = 0; v < numVertices; v++) {
printf("Vertex %u: %d\n", v, p->eccentricity[v]);
}
// Print the set of central vertices
printf("Central Vertices:\n");
unsigned int centralCount = p->centralVertices[0];
for (unsigned int i = 1; i <= centralCount; i++) {
printf("Vertex %u\n", p->centralVertices[i]);
}
}