diff --git a/graph.c b/graph.c index d88d9ad..306c220 100644 --- a/graph.c +++ b/graph.c @@ -2,13 +2,14 @@ #include #include #include +#include "matrix.h" void random_adjacency(const uint64_t vertex_count, uint64_t matrix[vertex_count][vertex_count]) { srand(time(NULL)); for (uint64_t row_index=0; row_index < vertex_count; row_index++) { for (uint64_t column_index=0; column_index < vertex_count; column_index++) { - if(column_index == row_index) { + if (column_index == row_index) { matrix[row_index][column_index] = 0; continue; } @@ -17,6 +18,34 @@ void random_adjacency(const uint64_t vertex_count, uint64_t matrix[vertex_count] } } -int calculate_eccentricities() { - return 0; +void calculate_distance_matrix(const uint64_t vertex_count, const uint64_t adjacency_matrix[vertex_count][vertex_count], uint64_t distance_matrix[vertex_count][vertex_count]) { + uint64_t power_matrix[vertex_count][vertex_count]; + copy(vertex_count, vertex_count, adjacency_matrix, power_matrix); + + for (uint64_t row_index=0; row_index < vertex_count; row_index++) { + for (uint64_t column_index=0; column_index < vertex_count; column_index++) { + if (column_index == row_index) { + distance_matrix[row_index][column_index] = 0; + } else if (adjacency_matrix[row_index][column_index] == 1) { + distance_matrix[row_index][column_index] = 1; + } else { + distance_matrix[row_index][column_index] = UINT64_MAX; + } + } + } + + for(uint64_t k = 2; k < vertex_count; k++) { + matrix_multiply_basic(vertex_count, vertex_count, adjacency_matrix, vertex_count, vertex_count, power_matrix, power_matrix); + + for (uint64_t row_index=0; row_index < vertex_count; row_index++) { + for (uint64_t column_index=0; column_index < vertex_count; column_index++) { + if (power_matrix[row_index][column_index] != 0 && distance_matrix[row_index][column_index] == UINT64_MAX) { + distance_matrix[row_index][column_index] = k; + } + } + } + } +} + +void calculate_eccentricities(const uint64_t vertex_count, const uint64_t output_matrix[vertex_count][vertex_count]) { } diff --git a/graph.h b/graph.h index 563e491..4b3883f 100644 --- a/graph.h +++ b/graph.h @@ -6,4 +6,11 @@ void random_adjacency(const uint64_t vertex_count, uint64_t matrix[vertex_count][vertex_count]); +void calculate_distance_matrix(const uint64_t vertex_count, + const uint64_t adjacency_matrix[vertex_count][vertex_count], + uint64_t distance_matrix[vertex_count][vertex_count]); + +void calculate_eccentricities(const uint64_t vertex_count, + const uint64_t output_matrix[vertex_count][vertex_count]); + #endif diff --git a/main.c b/main.c index fe7fcea..378ed85 100644 --- a/main.c +++ b/main.c @@ -35,28 +35,35 @@ void benchmark() { } void test() { - const uint64_t row_length1 = 5; - const uint64_t column_length1 = 5; - uint64_t matrix1[row_length1][column_length1]; + const uint64_t vertex_count = 5; + uint64_t adjacency_matrix[vertex_count][vertex_count]; - read_csv("csv/graph.csv", row_length1, column_length1, matrix1); + read_csv("csv/graph.csv", vertex_count, vertex_count, adjacency_matrix); printf("G:\n"); - print_matrix(row_length1, column_length1, matrix1); + print_matrix(vertex_count, vertex_count, adjacency_matrix); printf("\n"); - uint64_t new_matrix[row_length1][column_length1]; + // uint64_t new_matrix[row_length1][column_length1]; + // + // matrix_multiply_basic(row_length1, column_length1, matrix1, + // row_length1, column_length1, matrix1, + // new_matrix); + // + // printf("G²:\n"); + // print_matrix(row_length1, column_length1, new_matrix); + // printf("\n"); - matrix_multiply_basic(row_length1, column_length1, matrix1, - row_length1, column_length1, matrix1, - new_matrix); + uint64_t distance_matrix[vertex_count][vertex_count]; - printf("G²:\n"); - print_matrix(row_length1, column_length1, new_matrix); + calculate_distance_matrix(vertex_count, adjacency_matrix, distance_matrix); + + printf("distance_matrix:\n"); + print_matrix(vertex_count, vertex_count, distance_matrix); printf("\n"); } int main(void) { - // test(); - benchmark(); + test(); + // benchmark(); } diff --git a/matrix.c b/matrix.c index 8a14a89..43e52ba 100644 --- a/matrix.c +++ b/matrix.c @@ -25,7 +25,7 @@ int matrix_multiply_basic(const uint64_t row_length1, const uint64_t column_leng uint64_t sum; for (uint64_t i = 0; i < row_length1; i++) { - for(uint64_t j = 0; j < column_length2; j++) { + for (uint64_t j = 0; j < column_length2; j++) { sum = 0; for (uint64_t k = 0; k