new functionality

This commit is contained in:
AustrianToast 2024-10-09 01:42:55 +02:00
parent 02bd70cedd
commit a94122eeb2
Signed by: AustrianToast
GPG Key ID: 1B4D0AAF6E558816
3 changed files with 114 additions and 7 deletions

67
graph.c
View File

@ -49,5 +49,70 @@ void calculate_distance_matrix(const uint64_t vertex_count, const uint64_t adjac
} }
} }
void calculate_eccentricities(const uint64_t vertex_count, const uint64_t output_arraay[vertex_count]) { int get_eccentricities(const uint64_t vertex_count, const uint64_t distance_matrix[vertex_count][vertex_count], uint64_t eccentricities[vertex_count]) {
uint64_t eccentricity;
// set all eccentricities to infinity in case this is a disconnected graph
for (uint64_t index = 0; index < vertex_count; index++) {
eccentricities[index] = UINT64_MAX;
}
for (uint64_t row_index = 0; row_index < vertex_count; row_index++) {
eccentricity = 0;
for (uint64_t column_index = 0; column_index < vertex_count; column_index++) {
if (distance_matrix[row_index][column_index] > eccentricity) {
eccentricity = distance_matrix[row_index][column_index];
}
}
// in case of a disconnected graph
if (eccentricity == 0) {
return 1;
}
eccentricities[row_index] = eccentricity;
}
return 0;
} }
uint64_t get_radius(const uint64_t vertex_count, const uint64_t eccentricities[vertex_count]) {
uint64_t radius = UINT64_MAX;
for (uint64_t index = 0; index < vertex_count; index++) {
if (eccentricities[index] < radius) {
radius = eccentricities[index];
}
}
return radius;
}
uint64_t get_diameter(const uint64_t vertex_count, const uint64_t eccentricities[vertex_count]) {
uint64_t diamter = 0;
for (uint64_t index = 0; index < vertex_count; index++) {
// in case of a disconnected graph
if (eccentricities[index] == UINT64_MAX) {
return UINT64_MAX;
}
if (eccentricities[index] > diamter) {
diamter = eccentricities[index];
}
}
return diamter;
}
void get_centre(const uint64_t vertex_count, const uint64_t eccentricities[vertex_count], const uint64_t radius, uint64_t centre[vertex_count]) {
for (uint64_t index = 0; index < vertex_count; index++) {
centre[index] = 0;
}
for (uint64_t index = 0; index < vertex_count; index++) {
if (eccentricities[index] == radius) {
centre[index] = 1;
}
}
}

19
graph.h
View File

@ -10,4 +10,23 @@ void calculate_distance_matrix(const uint64_t vertex_count,
const uint64_t adjacency_matrix[vertex_count][vertex_count], const uint64_t adjacency_matrix[vertex_count][vertex_count],
uint64_t distance_matrix[vertex_count][vertex_count]); uint64_t distance_matrix[vertex_count][vertex_count]);
// returns 1 if it is a disconnected graph and just in case it was on purpose,
// all values in eccentricities will be set to UINT64_MAX
int get_eccentricities(const uint64_t vertex_count,
const uint64_t distance_matrix[vertex_count][vertex_count],
uint64_t eccentricities[vertex_count]);
// returns the radius
uint64_t get_radius(const uint64_t vertex_count,
const uint64_t eccentricities[vertex_count]);
// returns the diameter
uint64_t get_diameter(const uint64_t vertex_count,
const uint64_t eccentricities[vertex_count]);
void get_centre(const uint64_t vertex_count,
const uint64_t eccentricities[vertex_count],
const uint64_t radius,
uint64_t centre[vertex_count]);
#endif #endif

35
main.c
View File

@ -40,25 +40,48 @@ void benchmark() {
} }
void test() { void test() {
const uint64_t vertex_count = 5; const uint64_t vertex_count = 7;
uint64_t adjacency_matrix[vertex_count][vertex_count]; uint64_t adjacency_matrix[vertex_count][vertex_count];
uint64_t distance_matrix[vertex_count][vertex_count];
uint64_t eccentricities[vertex_count];
uint64_t radius, diameter, centre[vertex_count];
read_csv("csv/graph.csv", vertex_count, vertex_count, adjacency_matrix); read_csv("csv/7n.csv", vertex_count, vertex_count, adjacency_matrix);
puts("G:"); puts("G:");
print_matrix(vertex_count, vertex_count, adjacency_matrix); print_matrix(vertex_count, vertex_count, adjacency_matrix);
puts(""); puts("");
uint64_t distance_matrix[vertex_count][vertex_count];
calculate_distance_matrix(vertex_count, adjacency_matrix, distance_matrix); calculate_distance_matrix(vertex_count, adjacency_matrix, distance_matrix);
puts("distance_matrix:"); puts("distance_matrix:");
print_matrix(vertex_count, vertex_count, distance_matrix); print_matrix(vertex_count, vertex_count, distance_matrix);
puts(""); puts("");
get_eccentricities(vertex_count, distance_matrix, eccentricities);
puts("eccentricities:");
for (uint64_t index = 0; index < vertex_count; index++) {
printf("Vertex %lu: %lu, ", index + 1, eccentricities[index]);
}
puts("");
radius = get_radius(vertex_count, eccentricities);
diameter = get_diameter(vertex_count, eccentricities);
get_centre(vertex_count, eccentricities, radius, centre);
printf("radius: %lu\n", radius);
printf("diameter: %lu\n", diameter);
printf("centre: ");
for (uint64_t index = 0; index < vertex_count; index++) {
if (centre[index] == 1) {
printf("Vertex: %lu, ", index);
}
}
puts("");
} }
int main(void) { int main(void) {
// test(); test();
benchmark(); // benchmark();
} }