2024-09-26 14:32:20 +02:00
|
|
|
#include "graph.h"
|
|
|
|
#include "matrix.h"
|
2024-10-11 00:09:26 +02:00
|
|
|
#include <limits.h>
|
2024-09-26 14:32:20 +02:00
|
|
|
#include <stdint.h>
|
2024-10-04 01:03:39 +02:00
|
|
|
#include <stdio.h>
|
2024-10-08 23:04:09 +02:00
|
|
|
#include <stdlib.h>
|
2024-10-04 01:03:39 +02:00
|
|
|
#include <time.h>
|
2024-09-26 14:32:20 +02:00
|
|
|
|
2024-10-11 00:09:26 +02:00
|
|
|
void benchmark_gemm() {
|
2024-10-08 23:04:09 +02:00
|
|
|
const uint64_t vertex_count1 = 1024;
|
|
|
|
uint64_t (*matrix1)[vertex_count1] = malloc(vertex_count1 * vertex_count1 * sizeof(uint64_t));
|
2024-09-26 14:32:20 +02:00
|
|
|
|
2024-10-08 23:04:09 +02:00
|
|
|
const uint64_t vertex_count2 = 1024;
|
|
|
|
uint64_t (*matrix2)[vertex_count2] = malloc(vertex_count2 * vertex_count2 * sizeof(uint64_t));
|
2024-10-04 01:03:39 +02:00
|
|
|
|
2024-10-08 23:04:09 +02:00
|
|
|
uint64_t (*new_matrix)[vertex_count2] = malloc(vertex_count1 * vertex_count2 * sizeof(uint64_t));
|
2024-10-04 01:03:39 +02:00
|
|
|
|
|
|
|
double elapsed_time = 0.0;
|
2024-10-08 23:29:02 +02:00
|
|
|
const uint64_t iterations = 10;
|
2024-10-04 01:03:39 +02:00
|
|
|
clock_t start_time;
|
|
|
|
|
|
|
|
for (uint64_t i = 0; i < iterations; i++) {
|
2024-10-08 23:04:09 +02:00
|
|
|
random_adjacency(vertex_count1, matrix1);
|
|
|
|
random_adjacency(vertex_count2, matrix2);
|
2024-10-04 01:03:39 +02:00
|
|
|
|
|
|
|
start_time = clock();
|
|
|
|
|
2024-10-08 23:29:02 +02:00
|
|
|
gemm_basic(vertex_count1, vertex_count1, matrix1,
|
2024-10-08 23:04:09 +02:00
|
|
|
vertex_count2, vertex_count2, matrix2,
|
2024-10-04 01:03:39 +02:00
|
|
|
new_matrix);
|
|
|
|
|
|
|
|
elapsed_time += (double)(clock() - start_time) / CLOCKS_PER_SEC;
|
|
|
|
}
|
2024-09-26 14:32:20 +02:00
|
|
|
|
2024-10-08 23:29:02 +02:00
|
|
|
printf("%lu iterations of gemm_basic took roughly %f seconds\n", iterations, elapsed_time);
|
|
|
|
printf("An iteration of gemm_basic took on average roughly %f seconds\n", elapsed_time/iterations);
|
2024-10-08 23:04:09 +02:00
|
|
|
|
|
|
|
free(matrix1);
|
|
|
|
free(matrix2);
|
|
|
|
free(new_matrix);
|
2024-10-04 01:03:39 +02:00
|
|
|
}
|
|
|
|
|
2024-10-11 00:09:26 +02:00
|
|
|
void benchmark_find_components() {
|
|
|
|
const uint64_t vertex_count = 100;
|
|
|
|
uint64_t adjacency_matrix[vertex_count][vertex_count];
|
|
|
|
uint64_t components[vertex_count][vertex_count];
|
|
|
|
uint64_t path_matrix[vertex_count][vertex_count];
|
|
|
|
|
|
|
|
double elapsed_time = 0.0;
|
|
|
|
const uint64_t iterations = 100;
|
|
|
|
clock_t start_time;
|
|
|
|
|
|
|
|
for (uint64_t i = 0; i < iterations; i++) {
|
|
|
|
random_adjacency(vertex_count, adjacency_matrix);
|
|
|
|
|
|
|
|
start_time = clock();
|
|
|
|
|
|
|
|
calculate_path_matrix(vertex_count, adjacency_matrix, path_matrix);
|
|
|
|
find_components_basic(vertex_count, path_matrix, components);
|
|
|
|
|
|
|
|
elapsed_time += (double)(clock() - start_time) / CLOCKS_PER_SEC;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("%lu iterations of find_components_basic took roughly %f seconds\n", iterations, elapsed_time);
|
|
|
|
printf("An iteration of find_components_basic took on average roughly %f seconds\n", elapsed_time/iterations);
|
|
|
|
|
|
|
|
elapsed_time = 0.0;
|
|
|
|
|
|
|
|
for (uint64_t i = 0; i < iterations; i++) {
|
|
|
|
random_adjacency(vertex_count, adjacency_matrix);
|
|
|
|
|
|
|
|
start_time = clock();
|
|
|
|
|
|
|
|
find_components_dfs(vertex_count, adjacency_matrix, components);
|
|
|
|
|
|
|
|
elapsed_time += (double)(clock() - start_time) / CLOCKS_PER_SEC;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("%lu iterations of find_components_dfs took roughly %f seconds\n", iterations, elapsed_time);
|
|
|
|
printf("An iteration of find_components_dfs took on average roughly %f seconds\n", elapsed_time/iterations);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_with_basic() {
|
2024-10-10 00:44:00 +02:00
|
|
|
const uint64_t vertex_count = 24;
|
2024-10-04 17:36:40 +02:00
|
|
|
uint64_t adjacency_matrix[vertex_count][vertex_count];
|
2024-10-09 01:42:55 +02:00
|
|
|
uint64_t distance_matrix[vertex_count][vertex_count];
|
2024-10-09 15:35:17 +02:00
|
|
|
uint64_t path_matrix[vertex_count][vertex_count];
|
2024-10-09 01:42:55 +02:00
|
|
|
uint64_t eccentricities[vertex_count];
|
|
|
|
uint64_t radius, diameter, centre[vertex_count];
|
2024-10-09 15:35:17 +02:00
|
|
|
uint64_t components[vertex_count][vertex_count];
|
|
|
|
uint64_t bridges[vertex_count][2];
|
2024-10-10 00:44:00 +02:00
|
|
|
uint64_t articulations[vertex_count];
|
2024-10-04 01:03:39 +02:00
|
|
|
|
2024-10-10 00:44:00 +02:00
|
|
|
if (read_csv("csv/24n.csv", vertex_count, vertex_count, adjacency_matrix) == 1) {
|
|
|
|
return;
|
|
|
|
}
|
2024-10-11 00:09:26 +02:00
|
|
|
|
2024-10-10 00:44:00 +02:00
|
|
|
calculate_distance_matrix(vertex_count, adjacency_matrix, distance_matrix);
|
|
|
|
get_eccentricities(vertex_count, distance_matrix, eccentricities);
|
|
|
|
radius = get_radius(vertex_count, eccentricities);
|
|
|
|
diameter = get_diameter(vertex_count, eccentricities);
|
|
|
|
get_centre(vertex_count, eccentricities, radius, centre);
|
|
|
|
calculate_path_matrix(vertex_count, adjacency_matrix, path_matrix);
|
|
|
|
find_components_basic(vertex_count, path_matrix, components);
|
|
|
|
find_bridges_basic(vertex_count, adjacency_matrix, components, bridges);
|
|
|
|
find_articulations_basic(vertex_count, adjacency_matrix, components, articulations);
|
2024-10-04 01:03:39 +02:00
|
|
|
|
2024-10-10 00:44:00 +02:00
|
|
|
puts("adjacency_matrix:");
|
2024-10-04 17:36:40 +02:00
|
|
|
print_matrix(vertex_count, vertex_count, adjacency_matrix);
|
2024-09-26 14:32:20 +02:00
|
|
|
|
2024-10-10 00:44:00 +02:00
|
|
|
puts("\ndistance_matrix:");
|
2024-10-04 17:36:40 +02:00
|
|
|
print_matrix(vertex_count, vertex_count, distance_matrix);
|
2024-10-09 01:42:55 +02:00
|
|
|
|
2024-10-10 00:44:00 +02:00
|
|
|
puts("\neccentricities:");
|
2024-10-09 01:42:55 +02:00
|
|
|
for (uint64_t index = 0; index < vertex_count; index++) {
|
2024-10-09 15:35:17 +02:00
|
|
|
printf("\tVertex %lu: %lu\n", index + 1, eccentricities[index]);
|
2024-10-09 01:42:55 +02:00
|
|
|
}
|
|
|
|
|
2024-10-10 00:44:00 +02:00
|
|
|
printf("\nradius: %lu", radius);
|
|
|
|
printf("\ndiameter: %lu", diameter);
|
|
|
|
puts("\ncentre:");
|
2024-10-09 01:42:55 +02:00
|
|
|
for (uint64_t index = 0; index < vertex_count; index++) {
|
|
|
|
if (centre[index] == 1) {
|
2024-10-09 15:35:17 +02:00
|
|
|
printf("\tVertex %lu\n", index + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-10 00:44:00 +02:00
|
|
|
puts("\npath_matrix:");
|
2024-10-09 15:35:17 +02:00
|
|
|
print_matrix(vertex_count, vertex_count, path_matrix);
|
|
|
|
|
2024-10-10 00:44:00 +02:00
|
|
|
puts("\ncomponents:");
|
2024-10-09 15:35:17 +02:00
|
|
|
for (uint64_t row_index = 0; row_index < vertex_count; row_index++) {
|
2024-10-10 00:44:00 +02:00
|
|
|
int empty = 1;
|
|
|
|
|
|
|
|
for (uint64_t column_index = 0; column_index < vertex_count; column_index++) {
|
2024-10-11 00:09:26 +02:00
|
|
|
if (components[row_index][column_index] != 0) {
|
2024-10-10 00:44:00 +02:00
|
|
|
empty = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-10-09 15:35:17 +02:00
|
|
|
printf("\tComponent %lu: {", row_index + 1);
|
|
|
|
for (uint64_t column_index = 0; column_index < vertex_count; column_index++) {
|
2024-10-11 00:09:26 +02:00
|
|
|
if (components[row_index][column_index] != 0) {
|
2024-10-09 15:35:17 +02:00
|
|
|
printf("%lu, ", components[row_index][column_index]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
puts("}");
|
|
|
|
}
|
|
|
|
|
2024-10-10 00:44:00 +02:00
|
|
|
puts("\nbridges:");
|
2024-10-09 15:35:17 +02:00
|
|
|
for (uint64_t index = 0; index < vertex_count; index++) {
|
2024-10-11 00:09:26 +02:00
|
|
|
if (bridges[index][0] != 0) {
|
2024-10-10 00:44:00 +02:00
|
|
|
printf("\tBridge %lu: {%lu, %lu}\n", index + 1, bridges[index][0], bridges[index][1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
puts("\narticulations:");
|
|
|
|
for (uint64_t index = 0; index < vertex_count; index++) {
|
2024-10-11 00:09:26 +02:00
|
|
|
if (articulations[index] != 0) {
|
2024-10-10 00:44:00 +02:00
|
|
|
printf("\tVertex %lu\n", articulations[index]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-11 00:09:26 +02:00
|
|
|
void test_with_dfs() {
|
|
|
|
const uint64_t vertex_count = 24;
|
2024-10-10 00:44:00 +02:00
|
|
|
uint64_t adjacency_matrix[vertex_count][vertex_count];
|
2024-10-11 00:09:26 +02:00
|
|
|
uint64_t distance_matrix[vertex_count][vertex_count];
|
|
|
|
uint64_t eccentricities[vertex_count];
|
|
|
|
uint64_t radius, diameter;
|
|
|
|
uint64_t centre[vertex_count];
|
|
|
|
uint64_t components[vertex_count][vertex_count];
|
|
|
|
uint64_t bridges[vertex_count][2];
|
|
|
|
uint64_t articulations[vertex_count];
|
2024-10-10 00:44:00 +02:00
|
|
|
|
2024-10-11 00:09:26 +02:00
|
|
|
if (read_csv("csv/24n.csv", vertex_count, vertex_count, adjacency_matrix) == 1) {
|
2024-10-10 00:44:00 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-10-11 00:09:26 +02:00
|
|
|
/*
|
|
|
|
const uint64_t vertex_count = 1500;
|
|
|
|
uint64_t (*adjacency_matrix)[vertex_count] = malloc(vertex_count * vertex_count * sizeof(uint64_t));
|
|
|
|
uint64_t (*distance_matrix)[vertex_count] = malloc(vertex_count * vertex_count * sizeof(uint64_t));
|
|
|
|
uint64_t *eccentricities = malloc(vertex_count * sizeof(uint64_t));
|
|
|
|
uint64_t radius, diameter;
|
|
|
|
uint64_t *centre = malloc(vertex_count * sizeof(uint64_t));
|
|
|
|
uint64_t (*components)[vertex_count] = malloc(vertex_count * vertex_count * sizeof(uint64_t));
|
|
|
|
uint64_t (*bridges)[vertex_count] = malloc(vertex_count * 2 * sizeof(uint64_t));
|
|
|
|
uint64_t *articulations = malloc(vertex_count * sizeof(uint64_t));
|
|
|
|
|
|
|
|
random_adjacency(vertex_count, adjacency_matrix);
|
|
|
|
*/
|
|
|
|
|
|
|
|
calculate_distance_matrix(vertex_count, adjacency_matrix, distance_matrix);
|
|
|
|
get_eccentricities(vertex_count, distance_matrix, eccentricities);
|
|
|
|
radius = get_radius(vertex_count, eccentricities);
|
|
|
|
diameter = get_diameter(vertex_count, eccentricities);
|
|
|
|
get_centre(vertex_count, eccentricities, radius, centre);
|
|
|
|
find_components_dfs(vertex_count, adjacency_matrix, components);
|
|
|
|
find_bridges_dfs_v2(vertex_count, adjacency_matrix, components, bridges);
|
|
|
|
find_articulations_dfs_v2(vertex_count, adjacency_matrix, components, articulations);
|
|
|
|
|
|
|
|
puts("\nadjacency_matrix:");
|
|
|
|
print_matrix(vertex_count, vertex_count, adjacency_matrix);
|
|
|
|
|
|
|
|
puts("\ndistance_matrix:");
|
|
|
|
print_matrix(vertex_count, vertex_count, distance_matrix);
|
|
|
|
|
|
|
|
puts("\neccentricities:");
|
|
|
|
for (uint64_t index = 0; index < vertex_count; index++) {
|
|
|
|
printf("\tVertex %lu: %lu\n", index + 1, eccentricities[index]);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\nradius: %lu", radius);
|
|
|
|
printf("\ndiameter: %lu", diameter);
|
|
|
|
puts("\ncentre:");
|
|
|
|
for (uint64_t index = 0; index < vertex_count; index++) {
|
|
|
|
if (centre[index] == 1) {
|
|
|
|
printf("\tVertex %lu\n", index + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
puts("\ncomponents:");
|
|
|
|
for (uint64_t row_index = 0; row_index < vertex_count; row_index++) {
|
|
|
|
int empty = 1;
|
|
|
|
|
|
|
|
for (uint64_t column_index = 0; column_index < vertex_count; column_index++) {
|
|
|
|
if (components[row_index][column_index] != 0) {
|
|
|
|
empty = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\tComponent %lu: {", row_index + 1);
|
|
|
|
for (uint64_t column_index = 0; column_index < vertex_count; column_index++) {
|
|
|
|
if (components[row_index][column_index] != 0) {
|
|
|
|
printf("%lu, ", column_index + 1);
|
|
|
|
}
|
2024-10-10 00:44:00 +02:00
|
|
|
}
|
2024-10-11 00:09:26 +02:00
|
|
|
puts("}");
|
|
|
|
}
|
2024-10-10 00:44:00 +02:00
|
|
|
|
2024-10-11 00:09:26 +02:00
|
|
|
uint64_t bridge_number = 1;
|
|
|
|
puts("\nbridges:");
|
|
|
|
for (uint64_t index = 0; index < vertex_count; index++) {
|
|
|
|
if (bridges[index][0] != 0) {
|
|
|
|
printf("\tBridge %lu: {%lu, %lu}\n", bridge_number, bridges[index][0], bridges[index][1]);
|
|
|
|
bridge_number++;
|
|
|
|
}
|
|
|
|
}
|
2024-10-10 00:44:00 +02:00
|
|
|
|
2024-10-11 00:09:26 +02:00
|
|
|
puts("\narticulations:");
|
|
|
|
for (uint64_t index = 0; index < vertex_count; index++) {
|
|
|
|
if (articulations[index] != 0) {
|
|
|
|
printf("\tVertex %lu\n", articulations[index]);
|
2024-10-09 01:42:55 +02:00
|
|
|
}
|
|
|
|
}
|
2024-10-11 00:09:26 +02:00
|
|
|
|
|
|
|
// free(adjacency_matrix);
|
|
|
|
// free(distance_matrix);
|
|
|
|
// free(eccentricities);
|
|
|
|
// free(centre);
|
|
|
|
// free(components);
|
|
|
|
// free(bridges);
|
|
|
|
// free(articulations);
|
2024-10-04 01:03:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(void) {
|
2024-10-11 00:09:26 +02:00
|
|
|
// test_with_basic();
|
|
|
|
test_with_dfs();
|
|
|
|
// benchmark_gemm();
|
|
|
|
// benchmark_find_components();
|
2024-09-26 14:32:20 +02:00
|
|
|
}
|