switch from uint64_t to unsigned long

This commit is contained in:
AustrianToast 2024-10-22 23:26:09 +02:00
parent c41d130a45
commit cddbadfd4a
Signed by: AustrianToast
GPG Key ID: 1B4D0AAF6E558816
5 changed files with 336 additions and 273 deletions

281
graph.c
View File

@ -1,16 +1,15 @@
#include "graph.h" #include "graph.h"
#include <stdint.h> #include "matrix.h"
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include <stdlib.h> #include <stdlib.h>
#include "matrix.h"
void random_adjacency(const uint64_t vertex_count, uint64_t matrix[vertex_count][vertex_count]) { void random_adjacency(const ulong vertex_count, ulong matrix[vertex_count][vertex_count]) {
srand(time(NULL)); srand(time(NULL));
for (uint64_t row_index = 0; row_index < vertex_count; row_index++) { for (ulong row_index = 0; row_index < vertex_count; row_index++) {
for (uint64_t column_index = 0; column_index < vertex_count; column_index++) { for (ulong 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; matrix[row_index][column_index] = 0;
} else { } else {
@ -20,30 +19,30 @@ void random_adjacency(const uint64_t vertex_count, uint64_t matrix[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_distance_matrix(const ulong vertex_count, const ulong adjacency_matrix[vertex_count][vertex_count], ulong distance_matrix[vertex_count][vertex_count]) {
uint64_t power_matrix[vertex_count][vertex_count]; ulong power_matrix[vertex_count][vertex_count];
uint64_t temp_power_matrix[vertex_count][vertex_count]; ulong temp_power_matrix[vertex_count][vertex_count];
memcpy(power_matrix, adjacency_matrix, vertex_count * vertex_count * sizeof(uint64_t)); memcpy(power_matrix, adjacency_matrix, vertex_count * vertex_count * sizeof(ulong));
for (uint64_t row_index = 0; row_index < vertex_count; row_index++) { for (ulong row_index = 0; row_index < vertex_count; row_index++) {
for (uint64_t column_index = 0; column_index < vertex_count; column_index++) { for (ulong column_index = 0; column_index < vertex_count; column_index++) {
if (row_index == column_index) { if (row_index == column_index) {
distance_matrix[row_index][column_index] = 0; distance_matrix[row_index][column_index] = 0;
} else if (adjacency_matrix[row_index][column_index] == 1) { } else if (adjacency_matrix[row_index][column_index] == 1) {
distance_matrix[row_index][column_index] = 1; distance_matrix[row_index][column_index] = 1;
} else { } else {
distance_matrix[row_index][column_index] = UINT64_MAX; distance_matrix[row_index][column_index] = -1;
} }
} }
} }
for(uint64_t k = 2; k <= vertex_count; k++) { for(ulong k = 2; k <= vertex_count; k++) {
memcpy(temp_power_matrix, power_matrix, vertex_count * vertex_count * sizeof(uint64_t)); memcpy(temp_power_matrix, power_matrix, vertex_count * vertex_count * sizeof(ulong));
gemm_basic(vertex_count, vertex_count, adjacency_matrix, vertex_count, vertex_count, temp_power_matrix, power_matrix); gemm_basic(vertex_count, vertex_count, adjacency_matrix, vertex_count, vertex_count, temp_power_matrix, power_matrix);
for (uint64_t row_index = 0; row_index < vertex_count; row_index++) { for (ulong row_index = 0; row_index < vertex_count; row_index++) {
for (uint64_t column_index = 0; column_index < vertex_count; column_index++) { for (ulong 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) { if (power_matrix[row_index][column_index] != 0 && distance_matrix[row_index][column_index] == -1) {
distance_matrix[row_index][column_index] = k; distance_matrix[row_index][column_index] = k;
} }
} }
@ -51,18 +50,18 @@ void calculate_distance_matrix(const uint64_t vertex_count, const uint64_t adjac
} }
} }
int get_eccentricities(const uint64_t vertex_count, const uint64_t distance_matrix[vertex_count][vertex_count], uint64_t eccentricities[vertex_count]) { int get_eccentricities(const ulong vertex_count, const ulong distance_matrix[vertex_count][vertex_count], ulong eccentricities[vertex_count]) {
uint64_t eccentricity; ulong eccentricity;
// set all eccentricities to infinity in case this is a disconnected graph // set all eccentricities to infinity in case this is a disconnected graph
for (uint64_t index = 0; index < vertex_count; index++) { for (ulong index = 0; index < vertex_count; index++) {
eccentricities[index] = UINT64_MAX; eccentricities[index] = -1;
} }
for (uint64_t row_index = 0; row_index < vertex_count; row_index++) { for (ulong row_index = 0; row_index < vertex_count; row_index++) {
eccentricity = 0; eccentricity = 0;
for (uint64_t column_index = 0; column_index < vertex_count; column_index++) { for (ulong column_index = 0; column_index < vertex_count; column_index++) {
if (distance_matrix[row_index][column_index] > eccentricity) { if (distance_matrix[row_index][column_index] > eccentricity) {
eccentricity = distance_matrix[row_index][column_index]; eccentricity = distance_matrix[row_index][column_index];
} }
@ -77,10 +76,10 @@ int get_eccentricities(const uint64_t vertex_count, const uint64_t distance_matr
return 0; return 0;
} }
uint64_t get_radius(const uint64_t vertex_count, const uint64_t eccentricities[vertex_count]) { ulong get_radius(const ulong vertex_count, const ulong eccentricities[vertex_count]) {
uint64_t radius = UINT64_MAX; ulong radius = -1;
for (uint64_t index = 0; index < vertex_count; index++) { for (ulong index = 0; index < vertex_count; index++) {
if (eccentricities[index] < radius) { if (eccentricities[index] < radius) {
radius = eccentricities[index]; radius = eccentricities[index];
} }
@ -89,10 +88,10 @@ uint64_t get_radius(const uint64_t vertex_count, const uint64_t eccentricities[v
return radius; return radius;
} }
uint64_t get_diameter(const uint64_t vertex_count, const uint64_t eccentricities[vertex_count]) { ulong get_diameter(const ulong vertex_count, const ulong eccentricities[vertex_count]) {
uint64_t diamter = 0; ulong diamter = 0;
for (uint64_t index = 0; index < vertex_count; index++) { for (ulong index = 0; index < vertex_count; index++) {
if (eccentricities[index] > diamter) { if (eccentricities[index] > diamter) {
diamter = eccentricities[index]; diamter = eccentricities[index];
} }
@ -101,23 +100,23 @@ uint64_t get_diameter(const uint64_t vertex_count, const uint64_t eccentricities
return diamter; 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]) { void get_centre(const ulong vertex_count, const ulong eccentricities[vertex_count], const ulong radius, ulong centre[vertex_count]) {
memset(centre, 0, vertex_count * sizeof(uint64_t)); memset(centre, 0, vertex_count * sizeof(ulong));
for (uint64_t index = 0; index < vertex_count; index++) { for (ulong index = 0; index < vertex_count; index++) {
if (eccentricities[index] == radius) { if (eccentricities[index] == radius) {
centre[index] = 1; centre[index] = 1;
} }
} }
} }
void calculate_path_matrix(const uint64_t vertex_count, const uint64_t adjacency_matrix[vertex_count][vertex_count], uint64_t path_matrix[vertex_count][vertex_count]) { void calculate_path_matrix(const ulong vertex_count, const ulong adjacency_matrix[vertex_count][vertex_count], ulong path_matrix[vertex_count][vertex_count]) {
uint64_t power_matrix[vertex_count][vertex_count]; ulong power_matrix[vertex_count][vertex_count];
uint64_t temp_power_matrix[vertex_count][vertex_count]; ulong temp_power_matrix[vertex_count][vertex_count];
memcpy(power_matrix, adjacency_matrix, vertex_count * vertex_count * sizeof(uint64_t)); memcpy(power_matrix, adjacency_matrix, vertex_count * vertex_count * sizeof(ulong));
for (uint64_t row_index = 0; row_index < vertex_count; row_index++) { for (ulong row_index = 0; row_index < vertex_count; row_index++) {
for (uint64_t column_index = 0; column_index < vertex_count; column_index++) { for (ulong column_index = 0; column_index < vertex_count; column_index++) {
if (row_index == column_index || adjacency_matrix[row_index][column_index] == 1) { if (row_index == column_index || adjacency_matrix[row_index][column_index] == 1) {
path_matrix[row_index][column_index] = 1; path_matrix[row_index][column_index] = 1;
} else { } else {
@ -126,12 +125,12 @@ void calculate_path_matrix(const uint64_t vertex_count, const uint64_t adjacency
} }
} }
for(uint64_t k = 2; k <= vertex_count; k++) { for(ulong k = 2; k <= vertex_count; k++) {
memcpy(temp_power_matrix, power_matrix, vertex_count * vertex_count * sizeof(uint64_t)); memcpy(temp_power_matrix, power_matrix, vertex_count * vertex_count * sizeof(ulong));
gemm_basic(vertex_count, vertex_count, adjacency_matrix, vertex_count, vertex_count, temp_power_matrix, power_matrix); gemm_basic(vertex_count, vertex_count, adjacency_matrix, vertex_count, vertex_count, temp_power_matrix, power_matrix);
for (uint64_t row_index = 0; row_index < vertex_count; row_index++) { for (ulong row_index = 0; row_index < vertex_count; row_index++) {
for (uint64_t column_index = 0; column_index < vertex_count; column_index++) { for (ulong column_index = 0; column_index < vertex_count; column_index++) {
if (power_matrix[row_index][column_index] != 0) { if (power_matrix[row_index][column_index] != 0) {
path_matrix[row_index][column_index] = 1; path_matrix[row_index][column_index] = 1;
} }
@ -140,40 +139,40 @@ void calculate_path_matrix(const uint64_t vertex_count, const uint64_t adjacency
} }
} }
void find_components_basic(const uint64_t vertex_count, const uint64_t path_matrix[vertex_count][vertex_count], uint64_t components[vertex_count][vertex_count]) { void find_components_basic(const ulong vertex_count, const ulong path_matrix[vertex_count][vertex_count], ulong components[vertex_count][vertex_count]) {
uint64_t component[vertex_count]; ulong component[vertex_count];
int contains_component; int contains_component;
memset(components, 0, vertex_count * vertex_count * sizeof(uint64_t)); memset(components, 0, vertex_count * vertex_count * sizeof(ulong));
for (uint64_t row_index = 0; row_index < vertex_count; row_index++) { for (ulong row_index = 0; row_index < vertex_count; row_index++) {
memset(component, 0, vertex_count * sizeof(uint64_t)); memset(component, 0, vertex_count * sizeof(ulong));
contains_component = 0; contains_component = 0;
for (uint64_t column_index = 0; column_index < vertex_count; column_index++) { for (ulong column_index = 0; column_index < vertex_count; column_index++) {
if (path_matrix[row_index][column_index] == 1) { if (path_matrix[row_index][column_index] == 1) {
component[column_index] = column_index + 1; component[column_index] = column_index + 1;
} }
} }
for (uint64_t index = 0; index < vertex_count; index++) { for (ulong index = 0; index < vertex_count; index++) {
if (memcmp(components[index], component, vertex_count * sizeof(uint64_t)) == 0) { if (memcmp(components[index], component, vertex_count * sizeof(ulong)) == 0) {
contains_component = 1; contains_component = 1;
} }
} }
if (!contains_component) { if (!contains_component) {
for (uint64_t index = 0; index < vertex_count; index++) { for (ulong index = 0; index < vertex_count; index++) {
components[row_index][index] = component[index]; components[row_index][index] = component[index];
} }
} }
} }
} }
void dfs(const uint64_t vertex_count, const uint64_t adjacency_matrix[vertex_count][vertex_count], const uint64_t vertex, uint64_t visited[vertex_count]) { void dfs(const ulong vertex_count, const ulong adjacency_matrix[vertex_count][vertex_count], const ulong vertex, ulong visited[vertex_count]) {
visited[vertex] = 1; visited[vertex] = 1;
for (uint64_t neighbor_vertex = 0; neighbor_vertex < vertex_count; neighbor_vertex++) { for (ulong neighbor_vertex = 0; neighbor_vertex < vertex_count; neighbor_vertex++) {
if (adjacency_matrix[vertex][neighbor_vertex] != 1) { if (adjacency_matrix[vertex][neighbor_vertex] != 1) {
continue; continue;
} }
@ -184,37 +183,37 @@ void dfs(const uint64_t vertex_count, const uint64_t adjacency_matrix[vertex_cou
} }
} }
void find_components_dfs(const uint64_t vertex_count, const uint64_t adjacency_matrix[vertex_count][vertex_count], uint64_t components[vertex_count][vertex_count]) { void find_components_dfs(const ulong vertex_count, const ulong adjacency_matrix[vertex_count][vertex_count], ulong components[vertex_count][vertex_count]) {
uint64_t component[vertex_count]; ulong component[vertex_count];
int contains_component = 0; int contains_component = 0;
memset(components, 0, vertex_count * vertex_count * sizeof(uint64_t)); memset(components, 0, vertex_count * vertex_count * sizeof(ulong));
for (uint64_t vertex = 0; vertex < vertex_count; vertex++) { for (ulong vertex = 0; vertex < vertex_count; vertex++) {
memset(component, 0, vertex_count * sizeof(uint64_t)); memset(component, 0, vertex_count * sizeof(ulong));
contains_component = 0; contains_component = 0;
dfs(vertex_count, adjacency_matrix, vertex, component); dfs(vertex_count, adjacency_matrix, vertex, component);
for (uint64_t index = 0; index < vertex_count; index++) { for (ulong index = 0; index < vertex_count; index++) {
if (memcmp(components[index], component, vertex_count * sizeof(uint64_t)) == 0) { if (memcmp(components[index], component, vertex_count * sizeof(ulong)) == 0) {
contains_component = 1; contains_component = 1;
} }
} }
if (!contains_component) { if (!contains_component) {
for (uint64_t index = 0; index < vertex_count; index++) { for (ulong index = 0; index < vertex_count; index++) {
components[vertex][index] = component[index]; components[vertex][index] = component[index];
} }
} }
} }
} }
uint64_t amount_of_components(const uint64_t vertex_count, const uint64_t components[vertex_count][vertex_count]) { ulong amount_of_components(const ulong vertex_count, const ulong components[vertex_count][vertex_count]) {
uint64_t amount_of_components = 0; ulong amount_of_components = 0;
for (uint64_t row_index = 0; row_index < vertex_count; row_index++) { for (ulong row_index = 0; row_index < vertex_count; row_index++) {
for (uint64_t column_index = 0; column_index < vertex_count; column_index++) { for (ulong column_index = 0; column_index < vertex_count; column_index++) {
if (components[row_index][column_index] != 0) { if (components[row_index][column_index] != 0) {
amount_of_components++; amount_of_components++;
break; break;
@ -225,9 +224,9 @@ uint64_t amount_of_components(const uint64_t vertex_count, const uint64_t compon
return amount_of_components; return amount_of_components;
} }
int contains_bridge(const uint64_t vertex_count, const uint64_t bridges[vertex_count][2], const uint64_t bridge[2]) { int contains_bridge(const ulong vertex_count, const ulong bridges[vertex_count][2], const ulong bridge[2]) {
for (uint64_t index = 0; index < vertex_count; index++) { for (ulong index = 0; index < vertex_count; index++) {
if (memcmp(bridges[index], bridge, 2 * sizeof(uint64_t)) == 0) { if (memcmp(bridges[index], bridge, 2 * sizeof(ulong)) == 0) {
return 1; return 1;
} }
} }
@ -235,17 +234,17 @@ int contains_bridge(const uint64_t vertex_count, const uint64_t bridges[vertex_c
return 0; return 0;
} }
void find_bridges_basic(const uint64_t vertex_count, const uint64_t adjacency_matrix[vertex_count][vertex_count], void find_bridges_basic(const ulong vertex_count, const ulong adjacency_matrix[vertex_count][vertex_count],
const uint64_t components[vertex_count][vertex_count], uint64_t bridges[vertex_count][2]) { const ulong components[vertex_count][vertex_count], ulong bridges[vertex_count][2]) {
uint64_t path_matrix[vertex_count][vertex_count]; ulong path_matrix[vertex_count][vertex_count];
uint64_t temp_adjacency_matrix[vertex_count][vertex_count]; ulong temp_adjacency_matrix[vertex_count][vertex_count];
uint64_t temp_components[vertex_count][vertex_count]; ulong temp_components[vertex_count][vertex_count];
uint64_t bridge[2]; ulong bridge[2];
memset(bridges, 0, vertex_count * 2 * sizeof(uint64_t)); memset(bridges, 0, vertex_count * 2 * sizeof(ulong));
for (uint64_t row_index = 0; row_index < vertex_count; row_index++) { for (ulong row_index = 0; row_index < vertex_count; row_index++) {
for (uint64_t column_index = 0; column_index < vertex_count; column_index++) { for (ulong column_index = 0; column_index < vertex_count; column_index++) {
if (row_index == column_index) { if (row_index == column_index) {
continue; continue;
} }
@ -258,7 +257,7 @@ void find_bridges_basic(const uint64_t vertex_count, const uint64_t adjacency_ma
bridge[1] = column_index + 1; bridge[1] = column_index + 1;
} }
memcpy(temp_adjacency_matrix, adjacency_matrix, vertex_count * vertex_count * sizeof(uint64_t)); memcpy(temp_adjacency_matrix, adjacency_matrix, vertex_count * vertex_count * sizeof(ulong));
temp_adjacency_matrix[row_index][column_index] = 0; temp_adjacency_matrix[row_index][column_index] = 0;
temp_adjacency_matrix[column_index][row_index] = 0; temp_adjacency_matrix[column_index][row_index] = 0;
@ -275,16 +274,16 @@ void find_bridges_basic(const uint64_t vertex_count, const uint64_t adjacency_ma
print_matrix(vertex_count, 2, bridges); print_matrix(vertex_count, 2, bridges);
} }
void find_bridges_dfs_v1(const uint64_t vertex_count, const uint64_t adjacency_matrix[vertex_count][vertex_count], void find_bridges_dfs_v1(const ulong vertex_count, const ulong adjacency_matrix[vertex_count][vertex_count],
const uint64_t components[vertex_count][vertex_count], uint64_t bridges[vertex_count][2]) { const ulong components[vertex_count][vertex_count], ulong bridges[vertex_count][2]) {
uint64_t temp_adjacency_matrix[vertex_count][vertex_count]; ulong temp_adjacency_matrix[vertex_count][vertex_count];
uint64_t temp_components[vertex_count][vertex_count]; ulong temp_components[vertex_count][vertex_count];
uint64_t bridge[2]; ulong bridge[2];
memset(bridges, 0, vertex_count * 2 * sizeof(uint64_t)); memset(bridges, 0, vertex_count * 2 * sizeof(ulong));
for (uint64_t row_index = 0; row_index < vertex_count; row_index++) { for (ulong row_index = 0; row_index < vertex_count; row_index++) {
for (uint64_t column_index = 0; column_index < vertex_count; column_index++) { for (ulong column_index = 0; column_index < vertex_count; column_index++) {
if (row_index == column_index) { if (row_index == column_index) {
continue; continue;
} }
@ -297,7 +296,7 @@ void find_bridges_dfs_v1(const uint64_t vertex_count, const uint64_t adjacency_m
bridge[1] = column_index + 1; bridge[1] = column_index + 1;
} }
memcpy(temp_adjacency_matrix, adjacency_matrix, vertex_count * vertex_count * sizeof(uint64_t)); memcpy(temp_adjacency_matrix, adjacency_matrix, vertex_count * vertex_count * sizeof(ulong));
temp_adjacency_matrix[row_index][column_index] = 0; temp_adjacency_matrix[row_index][column_index] = 0;
temp_adjacency_matrix[column_index][row_index] = 0; temp_adjacency_matrix[column_index][row_index] = 0;
@ -312,14 +311,14 @@ void find_bridges_dfs_v1(const uint64_t vertex_count, const uint64_t adjacency_m
} }
} }
void dfs_bridges(const uint64_t vertex_count, const uint64_t adjacency_matrix[vertex_count][vertex_count], const uint64_t vertex, const uint64_t parent_vertex, uint64_t visited[vertex_count], void dfs_bridges(const ulong vertex_count, const ulong adjacency_matrix[vertex_count][vertex_count], const ulong vertex, const ulong parent_vertex, ulong visited[vertex_count],
uint64_t current_time, uint64_t discovery_time[vertex_count], uint64_t lowest_time[vertex_count], uint64_t bridges[vertex_count][2]) { ulong current_time, ulong discovery_time[vertex_count], ulong lowest_time[vertex_count], ulong bridges[vertex_count][2]) {
current_time++; current_time++;
visited[vertex] = 1; visited[vertex] = 1;
discovery_time[vertex] = current_time; discovery_time[vertex] = current_time;
lowest_time[vertex] = current_time; lowest_time[vertex] = current_time;
for (uint64_t neighbor_vertex = 0; neighbor_vertex < vertex_count; neighbor_vertex++) { for (ulong neighbor_vertex = 0; neighbor_vertex < vertex_count; neighbor_vertex++) {
if (adjacency_matrix[vertex][neighbor_vertex] != 1) { if (adjacency_matrix[vertex][neighbor_vertex] != 1) {
continue; continue;
} }
@ -345,38 +344,38 @@ void dfs_bridges(const uint64_t vertex_count, const uint64_t adjacency_matrix[ve
} }
} }
void find_bridges_dfs_v2(const uint64_t vertex_count, const uint64_t adjacency_matrix[vertex_count][vertex_count], void find_bridges_dfs_v2(const ulong vertex_count, const ulong adjacency_matrix[vertex_count][vertex_count],
const uint64_t components[vertex_count][vertex_count], uint64_t bridges[vertex_count][2]) { const ulong components[vertex_count][vertex_count], ulong bridges[vertex_count][2]) {
uint64_t visited[vertex_count]; ulong visited[vertex_count];
uint64_t current_time = 0; ulong current_time = 0;
uint64_t discovery_time[vertex_count]; ulong discovery_time[vertex_count];
uint64_t lowest_time[vertex_count]; ulong lowest_time[vertex_count];
memset(bridges, 0, vertex_count * 2 * sizeof(uint64_t)); memset(bridges, 0, vertex_count * 2 * sizeof(ulong));
memset(visited, 0, vertex_count * sizeof(uint64_t)); memset(visited, 0, vertex_count * sizeof(ulong));
memset(discovery_time, 0, vertex_count * sizeof(uint64_t)); memset(discovery_time, 0, vertex_count * sizeof(ulong));
memset(lowest_time, 0, vertex_count * sizeof(uint64_t)); memset(lowest_time, 0, vertex_count * sizeof(ulong));
for (uint64_t vertex = 0; vertex < vertex_count; vertex++) { for (ulong vertex = 0; vertex < vertex_count; vertex++) {
if (!visited[vertex]) { if (!visited[vertex]) {
dfs_bridges(vertex_count, adjacency_matrix, vertex, UINT64_MAX, visited, current_time, discovery_time, lowest_time, bridges); dfs_bridges(vertex_count, adjacency_matrix, vertex, -1, visited, current_time, discovery_time, lowest_time, bridges);
} }
} }
} }
void find_articulations_basic(const uint64_t vertex_count, const uint64_t adjacency_matrix[vertex_count][vertex_count], void find_articulations_basic(const ulong vertex_count, const ulong adjacency_matrix[vertex_count][vertex_count],
const uint64_t components[vertex_count][vertex_count], uint64_t articulations[vertex_count]) { const ulong components[vertex_count][vertex_count], ulong articulations[vertex_count]) {
uint64_t path_matrix[vertex_count][vertex_count]; ulong path_matrix[vertex_count][vertex_count];
uint64_t temp_adjacency_matrix[vertex_count][vertex_count]; ulong temp_adjacency_matrix[vertex_count][vertex_count];
uint64_t temp_components[vertex_count][vertex_count]; ulong temp_components[vertex_count][vertex_count];
memset(articulations, 0, vertex_count * sizeof(uint64_t)); memset(articulations, 0, vertex_count * sizeof(ulong));
for (uint64_t i = 0; i < vertex_count; i++) { for (ulong i = 0; i < vertex_count; i++) {
memcpy(temp_adjacency_matrix, adjacency_matrix, vertex_count * vertex_count * sizeof(uint64_t)); memcpy(temp_adjacency_matrix, adjacency_matrix, vertex_count * vertex_count * sizeof(ulong));
for (uint64_t row_index = 0; row_index < vertex_count; row_index++) { for (ulong row_index = 0; row_index < vertex_count; row_index++) {
for (uint64_t column_index = 0; column_index < vertex_count; column_index++) { for (ulong column_index = 0; column_index < vertex_count; column_index++) {
temp_adjacency_matrix[row_index][i] = 0; temp_adjacency_matrix[row_index][i] = 0;
temp_adjacency_matrix[i][column_index] = 0; temp_adjacency_matrix[i][column_index] = 0;
} }
@ -393,18 +392,18 @@ void find_articulations_basic(const uint64_t vertex_count, const uint64_t adjace
} }
} }
void find_articulations_dfs_v1(const uint64_t vertex_count, const uint64_t adjacency_matrix[vertex_count][vertex_count], void find_articulations_dfs_v1(const ulong vertex_count, const ulong adjacency_matrix[vertex_count][vertex_count],
const uint64_t components[vertex_count][vertex_count], uint64_t articulations[vertex_count]) { const ulong components[vertex_count][vertex_count], ulong articulations[vertex_count]) {
uint64_t temp_adjacency_matrix[vertex_count][vertex_count]; ulong temp_adjacency_matrix[vertex_count][vertex_count];
uint64_t temp_components[vertex_count][vertex_count]; ulong temp_components[vertex_count][vertex_count];
memset(articulations, 0, vertex_count * sizeof(uint64_t)); memset(articulations, 0, vertex_count * sizeof(ulong));
for (uint64_t i = 0; i < vertex_count; i++) { for (ulong i = 0; i < vertex_count; i++) {
memcpy(temp_adjacency_matrix, adjacency_matrix, vertex_count * vertex_count * sizeof(uint64_t)); memcpy(temp_adjacency_matrix, adjacency_matrix, vertex_count * vertex_count * sizeof(ulong));
for (uint64_t row_index = 0; row_index < vertex_count; row_index++) { for (ulong row_index = 0; row_index < vertex_count; row_index++) {
for (uint64_t column_index = 0; column_index < vertex_count; column_index++) { for (ulong column_index = 0; column_index < vertex_count; column_index++) {
temp_adjacency_matrix[row_index][i] = 0; temp_adjacency_matrix[row_index][i] = 0;
temp_adjacency_matrix[i][column_index] = 0; temp_adjacency_matrix[i][column_index] = 0;
} }
@ -420,17 +419,17 @@ void find_articulations_dfs_v1(const uint64_t vertex_count, const uint64_t adjac
} }
} }
void dfs_articulations(const uint64_t vertex_count, const uint64_t adjacency_matrix[vertex_count][vertex_count], const uint64_t vertex, const uint64_t parent_vertex, void dfs_articulations(const ulong vertex_count, const ulong adjacency_matrix[vertex_count][vertex_count], const ulong vertex, const ulong parent_vertex,
uint64_t visited[vertex_count], uint64_t current_time, uint64_t discovery_time[vertex_count], uint64_t lowest_time[vertex_count], ulong visited[vertex_count], ulong current_time, ulong discovery_time[vertex_count], ulong lowest_time[vertex_count],
uint64_t articulations[vertex_count]) { ulong articulations[vertex_count]) {
current_time++; current_time++;
visited[vertex] = 1; visited[vertex] = 1;
discovery_time[vertex] = current_time; discovery_time[vertex] = current_time;
lowest_time[vertex] = current_time; lowest_time[vertex] = current_time;
uint64_t child_count = 0; ulong child_count = 0;
uint64_t is_articulation = 0; ulong is_articulation = 0;
for (uint64_t neighbor_vertex = 0; neighbor_vertex < vertex_count; neighbor_vertex++) { for (ulong neighbor_vertex = 0; neighbor_vertex < vertex_count; neighbor_vertex++) {
if (adjacency_matrix[vertex][neighbor_vertex] != 1) { if (adjacency_matrix[vertex][neighbor_vertex] != 1) {
continue; continue;
} }
@ -449,11 +448,11 @@ void dfs_articulations(const uint64_t vertex_count, const uint64_t adjacency_mat
lowest_time[vertex] = lowest_time[neighbor_vertex]; lowest_time[vertex] = lowest_time[neighbor_vertex];
} }
if (parent_vertex != UINT64_MAX && discovery_time[vertex] <= lowest_time[neighbor_vertex]) { if (parent_vertex != -1 && discovery_time[vertex] <= lowest_time[neighbor_vertex]) {
is_articulation = 1; is_articulation = 1;
} }
} }
if (parent_vertex == UINT64_MAX && child_count > 1) { if (parent_vertex == -1 && child_count > 1) {
is_articulation = 1; is_articulation = 1;
} }
if (is_articulation) { if (is_articulation) {
@ -461,21 +460,21 @@ void dfs_articulations(const uint64_t vertex_count, const uint64_t adjacency_mat
} }
} }
void find_articulations_dfs_v2(const uint64_t vertex_count, const uint64_t adjacency_matrix[vertex_count][vertex_count], void find_articulations_dfs_v2(const ulong vertex_count, const ulong adjacency_matrix[vertex_count][vertex_count],
const uint64_t components[vertex_count][vertex_count], uint64_t articulations[vertex_count]) { const ulong components[vertex_count][vertex_count], ulong articulations[vertex_count]) {
uint64_t visited[vertex_count]; ulong visited[vertex_count];
uint64_t current_time = 0; ulong current_time = 0;
uint64_t discovery_time[vertex_count]; ulong discovery_time[vertex_count];
uint64_t lowest_time[vertex_count]; ulong lowest_time[vertex_count];
memset(articulations, 0, vertex_count * sizeof(uint64_t)); memset(articulations, 0, vertex_count * sizeof(ulong));
memset(visited, 0, vertex_count * sizeof(uint64_t)); memset(visited, 0, vertex_count * sizeof(ulong));
memset(discovery_time, 0, vertex_count * sizeof(uint64_t)); memset(discovery_time, 0, vertex_count * sizeof(ulong));
memset(lowest_time, 0, vertex_count * sizeof(uint64_t)); memset(lowest_time, 0, vertex_count * sizeof(ulong));
for (uint64_t vertex = 0; vertex < vertex_count; vertex++) { for (ulong vertex = 0; vertex < vertex_count; vertex++) {
if (!visited[vertex]) { if (!visited[vertex]) {
dfs_articulations(vertex_count, adjacency_matrix, vertex, UINT64_MAX, visited, current_time, discovery_time, lowest_time, articulations); dfs_articulations(vertex_count, adjacency_matrix, vertex, -1, visited, current_time, discovery_time, lowest_time, articulations);
} }
} }
} }

100
graph.h
View File

@ -1,71 +1,71 @@
#ifndef GRAPH_H #ifndef GRAPH_H
#define GRAPH_H #define GRAPH_H
#include <stdint.h> typedef unsigned long ulong;
void random_adjacency(const uint64_t vertex_count, void random_adjacency(const ulong vertex_count,
uint64_t matrix[vertex_count][vertex_count]); ulong matrix[vertex_count][vertex_count]);
void calculate_distance_matrix(const uint64_t vertex_count, void calculate_distance_matrix(const ulong vertex_count,
const uint64_t adjacency_matrix[vertex_count][vertex_count], const ulong adjacency_matrix[vertex_count][vertex_count],
uint64_t distance_matrix[vertex_count][vertex_count]); ulong distance_matrix[vertex_count][vertex_count]);
// returns 1 if it is a disconnected graph and sets all values in eccentricities to UINT64_MAX // returns 1 if it is a disconnected graph and sets all values in eccentricities to UINT64_MAX
int get_eccentricities(const uint64_t vertex_count, int get_eccentricities(const ulong vertex_count,
const uint64_t distance_matrix[vertex_count][vertex_count], const ulong distance_matrix[vertex_count][vertex_count],
uint64_t eccentricities[vertex_count]); ulong eccentricities[vertex_count]);
uint64_t get_radius(const uint64_t vertex_count, ulong get_radius(const ulong vertex_count,
const uint64_t eccentricities[vertex_count]); const ulong eccentricities[vertex_count]);
uint64_t get_diameter(const uint64_t vertex_count, ulong get_diameter(const ulong vertex_count,
const uint64_t eccentricities[vertex_count]); const ulong eccentricities[vertex_count]);
void get_centre(const uint64_t vertex_count, void get_centre(const ulong vertex_count,
const uint64_t eccentricities[vertex_count], const ulong eccentricities[vertex_count],
const uint64_t radius, const ulong radius,
uint64_t centre[vertex_count]); ulong centre[vertex_count]);
void calculate_path_matrix(const uint64_t vertex_count, void calculate_path_matrix(const ulong vertex_count,
const uint64_t adjacency_matrix[vertex_count][vertex_count], const ulong adjacency_matrix[vertex_count][vertex_count],
uint64_t path_matrix[vertex_count][vertex_count]); ulong path_matrix[vertex_count][vertex_count]);
void find_components_basic(const uint64_t vertex_count, void find_components_basic(const ulong vertex_count,
const uint64_t path_matrix[vertex_count][vertex_count], const ulong path_matrix[vertex_count][vertex_count],
uint64_t components[vertex_count][vertex_count]); ulong components[vertex_count][vertex_count]);
void find_components_dfs(const uint64_t vertex_count, void find_components_dfs(const ulong vertex_count,
const uint64_t adjacency_matrix[vertex_count][vertex_count], const ulong adjacency_matrix[vertex_count][vertex_count],
uint64_t components[vertex_count][vertex_count]); ulong components[vertex_count][vertex_count]);
void find_bridges_basic(const uint64_t vertex_count, void find_bridges_basic(const ulong vertex_count,
const uint64_t adjacency_matrix[vertex_count][vertex_count], const ulong adjacency_matrix[vertex_count][vertex_count],
const uint64_t components[vertex_count][vertex_count], const ulong components[vertex_count][vertex_count],
uint64_t bridges[vertex_count][2]); ulong bridges[vertex_count][2]);
void find_bridges_dfs_v1(const uint64_t vertex_count, void find_bridges_dfs_v1(const ulong vertex_count,
const uint64_t adjacency_matrix[vertex_count][vertex_count], const ulong adjacency_matrix[vertex_count][vertex_count],
const uint64_t components[vertex_count][vertex_count], const ulong components[vertex_count][vertex_count],
uint64_t bridges[vertex_count][2]); ulong bridges[vertex_count][2]);
void find_bridges_dfs_v2(const uint64_t vertex_count, void find_bridges_dfs_v2(const ulong vertex_count,
const uint64_t adjacency_matrix[vertex_count][vertex_count], const ulong adjacency_matrix[vertex_count][vertex_count],
const uint64_t components[vertex_count][vertex_count], const ulong components[vertex_count][vertex_count],
uint64_t bridges[vertex_count][2]); ulong bridges[vertex_count][2]);
void find_articulations_basic(const uint64_t vertex_count, void find_articulations_basic(const ulong vertex_count,
const uint64_t adjacency_matrix[vertex_count][vertex_count], const ulong adjacency_matrix[vertex_count][vertex_count],
const uint64_t components[vertex_count][vertex_count], const ulong components[vertex_count][vertex_count],
uint64_t articulations[vertex_count]); ulong articulations[vertex_count]);
void find_articulations_dfs_v1(const uint64_t vertex_count, void find_articulations_dfs_v1(const ulong vertex_count,
const uint64_t adjacency_matrix[vertex_count][vertex_count], const ulong adjacency_matrix[vertex_count][vertex_count],
const uint64_t components[vertex_count][vertex_count], const ulong components[vertex_count][vertex_count],
uint64_t articulations[vertex_count]); ulong articulations[vertex_count]);
void find_articulations_dfs_v2(const uint64_t vertex_count, void find_articulations_dfs_v2(const ulong vertex_count,
const uint64_t adjacency_matrix[vertex_count][vertex_count], const ulong adjacency_matrix[vertex_count][vertex_count],
const uint64_t components[vertex_count][vertex_count], const ulong components[vertex_count][vertex_count],
uint64_t articulations[vertex_count]); ulong articulations[vertex_count]);
#endif #endif

179
main.c
View File

@ -1,21 +1,23 @@
#include "graph.h" #include "graph.h"
#include "matrix.h" #include "matrix.h"
#include <limits.h> #include <limits.h>
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <time.h> #include <time.h>
#include <stdlib.h>
typedef struct matrix {ulong row_length; ulong column_length; ulong **values;} Matrix;
void benchmark_gemm() { void benchmark_gemm() {
const uint64_t vertex_count = 100; const ulong vertex_count = 100;
uint64_t adjacency_matrix1[vertex_count][vertex_count]; ulong adjacency_matrix1[vertex_count][vertex_count];
uint64_t adjacency_matrix2[vertex_count][vertex_count]; ulong adjacency_matrix2[vertex_count][vertex_count];
uint64_t dot_product[vertex_count][vertex_count]; ulong dot_product[vertex_count][vertex_count];
double elapsed_time = 0.0; double elapsed_time = 0.0;
const uint64_t iterations = 10; const ulong iterations = 10;
clock_t start_time; clock_t start_time;
for (uint64_t i = 0; i < iterations; i++) { for (ulong i = 0; i < iterations; i++) {
random_adjacency(vertex_count, adjacency_matrix1); random_adjacency(vertex_count, adjacency_matrix1);
random_adjacency(vertex_count, adjacency_matrix2); random_adjacency(vertex_count, adjacency_matrix2);
@ -33,16 +35,16 @@ void benchmark_gemm() {
} }
void benchmark_find_components() { void benchmark_find_components() {
const uint64_t vertex_count = 100; const ulong vertex_count = 100;
uint64_t adjacency_matrix[vertex_count][vertex_count]; ulong adjacency_matrix[vertex_count][vertex_count];
uint64_t components[vertex_count][vertex_count]; ulong components[vertex_count][vertex_count];
uint64_t path_matrix[vertex_count][vertex_count]; ulong path_matrix[vertex_count][vertex_count];
double elapsed_time = 0.0; double elapsed_time = 0.0;
const uint64_t iterations = 100; const ulong iterations = 100;
clock_t start_time; clock_t start_time;
for (uint64_t i = 0; i < iterations; i++) { for (ulong i = 0; i < iterations; i++) {
random_adjacency(vertex_count, adjacency_matrix); random_adjacency(vertex_count, adjacency_matrix);
start_time = clock(); start_time = clock();
@ -58,7 +60,7 @@ void benchmark_find_components() {
elapsed_time = 0.0; elapsed_time = 0.0;
for (uint64_t i = 0; i < iterations; i++) { for (ulong i = 0; i < iterations; i++) {
random_adjacency(vertex_count, adjacency_matrix); random_adjacency(vertex_count, adjacency_matrix);
start_time = clock(); start_time = clock();
@ -73,15 +75,15 @@ void benchmark_find_components() {
} }
void test_with_basic() { void test_with_basic() {
const uint64_t vertex_count = 24; const ulong vertex_count = 24;
uint64_t adjacency_matrix[vertex_count][vertex_count]; ulong adjacency_matrix[vertex_count][vertex_count];
uint64_t distance_matrix[vertex_count][vertex_count]; ulong distance_matrix[vertex_count][vertex_count];
uint64_t path_matrix[vertex_count][vertex_count]; ulong path_matrix[vertex_count][vertex_count];
uint64_t eccentricities[vertex_count]; ulong eccentricities[vertex_count];
uint64_t radius, diameter, centre[vertex_count]; ulong radius, diameter, centre[vertex_count];
uint64_t components[vertex_count][vertex_count]; ulong components[vertex_count][vertex_count];
uint64_t bridges[vertex_count][2]; ulong bridges[vertex_count][2];
uint64_t articulations[vertex_count]; ulong articulations[vertex_count];
if (read_csv("csv/24n.csv", vertex_count, vertex_count, adjacency_matrix) == 1) { if (read_csv("csv/24n.csv", vertex_count, vertex_count, adjacency_matrix) == 1) {
return; return;
@ -104,14 +106,14 @@ void test_with_basic() {
print_matrix(vertex_count, vertex_count, distance_matrix); print_matrix(vertex_count, vertex_count, distance_matrix);
puts("\neccentricities:"); puts("\neccentricities:");
for (uint64_t index = 0; index < vertex_count; index++) { for (ulong index = 0; index < vertex_count; index++) {
printf("\tVertex %lu: %lu\n", index + 1, eccentricities[index]); printf("\tVertex %lu: %lu\n", index + 1, eccentricities[index]);
} }
printf("\nradius: %lu", radius); printf("\nradius: %lu", radius);
printf("\ndiameter: %lu", diameter); printf("\ndiameter: %lu", diameter);
puts("\ncentre:"); puts("\ncentre:");
for (uint64_t index = 0; index < vertex_count; index++) { for (ulong index = 0; index < vertex_count; index++) {
if (centre[index] == 1) { if (centre[index] == 1) {
printf("\tVertex %lu\n", index + 1); printf("\tVertex %lu\n", index + 1);
} }
@ -121,10 +123,10 @@ void test_with_basic() {
print_matrix(vertex_count, vertex_count, path_matrix); print_matrix(vertex_count, vertex_count, path_matrix);
puts("\ncomponents:"); puts("\ncomponents:");
for (uint64_t row_index = 0; row_index < vertex_count; row_index++) { for (ulong row_index = 0; row_index < vertex_count; row_index++) {
int empty = 1; int empty = 1;
for (uint64_t column_index = 0; column_index < vertex_count; column_index++) { for (ulong column_index = 0; column_index < vertex_count; column_index++) {
if (components[row_index][column_index] != 0) { if (components[row_index][column_index] != 0) {
empty = 0; empty = 0;
} }
@ -135,7 +137,7 @@ void test_with_basic() {
} }
printf("\tComponent %lu: {", row_index + 1); printf("\tComponent %lu: {", row_index + 1);
for (uint64_t column_index = 0; column_index < vertex_count; column_index++) { for (ulong column_index = 0; column_index < vertex_count; column_index++) {
if (components[row_index][column_index] != 0) { if (components[row_index][column_index] != 0) {
printf("%lu, ", components[row_index][column_index]); printf("%lu, ", components[row_index][column_index]);
} }
@ -144,14 +146,14 @@ void test_with_basic() {
} }
puts("\nbridges:"); puts("\nbridges:");
for (uint64_t index = 0; index < vertex_count; index++) { for (ulong index = 0; index < vertex_count; index++) {
if (bridges[index][0] != 0) { if (bridges[index][0] != 0) {
printf("\tBridge %lu: {%lu, %lu}\n", index + 1, bridges[index][0], bridges[index][1]); printf("\tBridge %lu: {%lu, %lu}\n", index + 1, bridges[index][0], bridges[index][1]);
} }
} }
puts("\narticulations:"); puts("\narticulations:");
for (uint64_t index = 0; index < vertex_count; index++) { for (ulong index = 0; index < vertex_count; index++) {
if (articulations[index] != 0) { if (articulations[index] != 0) {
printf("\tVertex %lu\n", articulations[index]); printf("\tVertex %lu\n", articulations[index]);
} }
@ -159,30 +161,30 @@ void test_with_basic() {
} }
void test_with_dfs() { void test_with_dfs() {
const uint64_t vertex_count = 24; const ulong vertex_count = 24;
uint64_t adjacency_matrix[vertex_count][vertex_count]; ulong adjacency_matrix[vertex_count][vertex_count];
uint64_t distance_matrix[vertex_count][vertex_count]; ulong distance_matrix[vertex_count][vertex_count];
uint64_t eccentricities[vertex_count]; ulong eccentricities[vertex_count];
uint64_t radius, diameter; ulong radius, diameter;
uint64_t centre[vertex_count]; ulong centre[vertex_count];
uint64_t components[vertex_count][vertex_count]; ulong components[vertex_count][vertex_count];
uint64_t bridges[vertex_count][2]; ulong bridges[vertex_count][2];
uint64_t articulations[vertex_count]; ulong articulations[vertex_count];
if (read_csv("csv/24n.csv", vertex_count, vertex_count, adjacency_matrix) == 1) { if (read_csv("csv/24n.csv", vertex_count, vertex_count, adjacency_matrix) == 1) {
return; return;
} }
/* /*
const uint64_t vertex_count = 1500; const ulong vertex_count = 1500;
uint64_t (*adjacency_matrix)[vertex_count] = malloc(vertex_count * vertex_count * sizeof(uint64_t)); ulong (*adjacency_matrix)[vertex_count] = malloc(vertex_count * vertex_count * sizeof(ulong));
uint64_t (*distance_matrix)[vertex_count] = malloc(vertex_count * vertex_count * sizeof(uint64_t)); ulong (*distance_matrix)[vertex_count] = malloc(vertex_count * vertex_count * sizeof(ulong));
uint64_t *eccentricities = malloc(vertex_count * sizeof(uint64_t)); ulong *eccentricities = malloc(vertex_count * sizeof(ulong));
uint64_t radius, diameter; ulong radius, diameter;
uint64_t *centre = malloc(vertex_count * sizeof(uint64_t)); ulong *centre = malloc(vertex_count * sizeof(ulong));
uint64_t (*components)[vertex_count] = malloc(vertex_count * vertex_count * sizeof(uint64_t)); ulong (*components)[vertex_count] = malloc(vertex_count * vertex_count * sizeof(ulong));
uint64_t (*bridges)[vertex_count] = malloc(vertex_count * 2 * sizeof(uint64_t)); ulong (*bridges)[vertex_count] = malloc(vertex_count * 2 * sizeof(ulong));
uint64_t *articulations = malloc(vertex_count * sizeof(uint64_t)); ulong *articulations = malloc(vertex_count * sizeof(ulong));
random_adjacency(vertex_count, adjacency_matrix); random_adjacency(vertex_count, adjacency_matrix);
*/ */
@ -203,24 +205,24 @@ void test_with_dfs() {
print_matrix(vertex_count, vertex_count, distance_matrix); print_matrix(vertex_count, vertex_count, distance_matrix);
puts("\neccentricities:"); puts("\neccentricities:");
for (uint64_t index = 0; index < vertex_count; index++) { for (ulong index = 0; index < vertex_count; index++) {
printf("\tVertex %lu: %lu\n", index + 1, eccentricities[index]); printf("\tVertex %lu: %lu\n", index + 1, eccentricities[index]);
} }
printf("\nradius: %lu", radius); printf("\nradius: %lu", radius);
printf("\ndiameter: %lu", diameter); printf("\ndiameter: %lu", diameter);
puts("\ncentre:"); puts("\ncentre:");
for (uint64_t index = 0; index < vertex_count; index++) { for (ulong index = 0; index < vertex_count; index++) {
if (centre[index] == 1) { if (centre[index] == 1) {
printf("\tVertex %lu\n", index + 1); printf("\tVertex %lu\n", index + 1);
} }
} }
puts("\ncomponents:"); puts("\ncomponents:");
for (uint64_t row_index = 0; row_index < vertex_count; row_index++) { for (ulong row_index = 0; row_index < vertex_count; row_index++) {
int empty = 1; int empty = 1;
for (uint64_t column_index = 0; column_index < vertex_count; column_index++) { for (ulong column_index = 0; column_index < vertex_count; column_index++) {
if (components[row_index][column_index] != 0) { if (components[row_index][column_index] != 0) {
empty = 0; empty = 0;
} }
@ -231,7 +233,7 @@ void test_with_dfs() {
} }
printf("\tComponent %lu: {", row_index + 1); printf("\tComponent %lu: {", row_index + 1);
for (uint64_t column_index = 0; column_index < vertex_count; column_index++) { for (ulong column_index = 0; column_index < vertex_count; column_index++) {
if (components[row_index][column_index] != 0) { if (components[row_index][column_index] != 0) {
printf("%lu, ", column_index + 1); printf("%lu, ", column_index + 1);
} }
@ -239,9 +241,9 @@ void test_with_dfs() {
puts("}"); puts("}");
} }
uint64_t bridge_number = 1; ulong bridge_number = 1;
puts("\nbridges:"); puts("\nbridges:");
for (uint64_t index = 0; index < vertex_count; index++) { for (ulong index = 0; index < vertex_count; index++) {
if (bridges[index][0] != 0) { if (bridges[index][0] != 0) {
printf("\tBridge %lu: {%lu, %lu}\n", bridge_number, bridges[index][0], bridges[index][1]); printf("\tBridge %lu: {%lu, %lu}\n", bridge_number, bridges[index][0], bridges[index][1]);
bridge_number++; bridge_number++;
@ -249,7 +251,7 @@ void test_with_dfs() {
} }
puts("\narticulations:"); puts("\narticulations:");
for (uint64_t index = 0; index < vertex_count; index++) { for (ulong index = 0; index < vertex_count; index++) {
if (articulations[index] != 0) { if (articulations[index] != 0) {
printf("\tVertex %lu\n", articulations[index]); printf("\tVertex %lu\n", articulations[index]);
} }
@ -264,9 +266,70 @@ void test_with_dfs() {
// free(articulations); // free(articulations);
} }
void print_matrix_struct(const Matrix *matrix) {
for (ulong column_index=0; column_index < matrix->column_length; column_index++) {
for (ulong row_index=0; row_index < matrix->row_length; row_index++) {
printf("%lu ", matrix->values[row_index][column_index]);
}
puts("");
}
}
void gemm_structs(const Matrix *matrix1, const Matrix *matrix2, Matrix *output_matrix) {
ulong sum;
for (ulong i = 0; i < matrix1->row_length; i++) {
for (ulong j = 0; j < matrix2->column_length; j++) {
sum = 0;
for (ulong k = 0; k < matrix1->row_length; k++) {
sum += matrix1->values[i][k] * matrix2->values[k][j];
}
output_matrix->values[i][j] = sum;
}
}
}
void random_adjacency_struct(const Matrix *matrix) {
if (matrix->row_length != matrix->column_length) {
return;
}
srand(time(NULL));
for (ulong row_index = 0; row_index < matrix->row_length; row_index++) {
for (ulong column_index = 0; column_index < matrix->column_length; column_index++) {
if (column_index == row_index) {
matrix->values[row_index][column_index] = 0;
} else {
matrix->values[row_index][column_index] = rand() % 2;
}
}
}
}
void test_with_structs() {
ulong row_length = 5, column_length = 10;
ulong **values = (ulong**)malloc(row_length * sizeof(ulong*));
for (ulong index = 0; index < row_length; index++) {
values[index] = (ulong*)malloc(column_length * sizeof(ulong));
}
Matrix adjacency_matrix = {row_length, column_length, values};
random_adjacency_struct(&adjacency_matrix);
print_matrix_struct(&adjacency_matrix);
for (ulong index = 0; index < row_length; index++) {
free(values[index]);
}
free(values);
}
int main(void) { int main(void) {
// test_with_basic();
test_with_dfs();
// benchmark_gemm(); // benchmark_gemm();
// benchmark_find_components(); // benchmark_find_components();
// test_with_basic();
// test_with_dfs();
test_with_structs();
} }

View File

@ -1,29 +1,29 @@
#include "matrix.h"
#include <stddef.h> #include <stddef.h>
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
void print_matrix(const uint64_t row_length, const uint64_t column_length, const uint64_t matrix[row_length][column_length]) { void print_matrix(const ulong row_length, const ulong column_length, const ulong matrix[row_length][column_length]) {
for (uint64_t column_index=0; column_index < column_length; column_index++) { for (ulong column_index=0; column_index < column_length; column_index++) {
for (uint64_t row_index=0; row_index < row_length; row_index++) { for (ulong row_index=0; row_index < row_length; row_index++) {
printf("%lu ", matrix[row_index][column_index]); printf("%lu ", matrix[row_index][column_index]);
} }
puts(""); puts("");
} }
} }
void gemm_basic(const uint64_t row_length1, const uint64_t column_length1, const uint64_t matrix1[row_length1][column_length1], void gemm_basic(const ulong row_length1, const ulong column_length1, const ulong matrix1[row_length1][column_length1],
const uint64_t row_length2, const uint64_t column_length2, const uint64_t matrix2[row_length2][column_length2], const ulong row_length2, const ulong column_length2, const ulong matrix2[row_length2][column_length2],
uint64_t output_matrix[row_length1][column_length2]) { ulong output_matrix[row_length1][column_length2]) {
uint64_t sum; ulong sum;
for (uint64_t i = 0; i < row_length1; i++) { for (ulong i = 0; i < row_length1; i++) {
for (uint64_t j = 0; j < column_length2; j++) { for (ulong j = 0; j < column_length2; j++) {
sum = 0; sum = 0;
for (uint64_t k = 0; k < row_length1; k++) { for (ulong k = 0; k < row_length1; k++) {
sum += matrix1[i][k] * matrix2[k][j]; sum += matrix1[i][k] * matrix2[k][j];
} }
@ -32,12 +32,12 @@ void gemm_basic(const uint64_t row_length1, const uint64_t column_length1, const
} }
} }
int read_csv(const char *file_name, const uint64_t row_length, const uint64_t column_length, uint64_t output_matrix[row_length][column_length]) { int read_csv(const char *file_name, const ulong row_length, const ulong column_length, ulong output_matrix[row_length][column_length]) {
FILE *file_ptr; FILE *file_ptr;
uint64_t bufsize = row_length*2+1; // have to account for delimiters ulong bufsize = row_length*2+1; // have to account for delimiters
char buffer[bufsize]; char buffer[bufsize];
char *value, *file_line; char *value, *file_line;
uint64_t row_index = 0, column_index = 0; ulong row_index = 0, column_index = 0;
file_ptr = fopen(file_name, "r"); file_ptr = fopen(file_name, "r");
if (file_ptr == NULL) { if (file_ptr == NULL) {

View File

@ -1,23 +1,24 @@
#ifndef MATRIX_H #ifndef MATRIX_H
#define MATRIX_H #define MATRIX_H
#include <stdint.h> typedef unsigned long ulong;
void print_matrix(const uint64_t row_length, const uint64_t column_length, void print_matrix(const ulong row_length, const ulong column_length,
const uint64_t matrix[row_length][column_length]); const ulong matrix[row_length][column_length]);
/* /*
First two matrices will be multiplied and First two matrices will be multiplied and
restult will be written to output_matrix. restult will be written to output_matrix.
Matrix size requirements are as specified in the parameters. Matrix size requirements are as specified in the parameters.
*/ */
void gemm_basic(const uint64_t row_length1, const uint64_t column_length1, void gemm_basic(const ulong row_length1, const ulong column_length1,
const uint64_t matrix1[row_length1][column_length1], const ulong matrix1[row_length1][column_length1],
const uint64_t row_length2, const uint64_t column_length2, const ulong row_length2, const ulong column_length2,
const uint64_t matrix2[row_length2][column_length2], const ulong matrix2[row_length2][column_length2],
uint64_t output_matrix[row_length1][column_length2]); ulong output_matrix[row_length1][column_length2]);
int read_csv(char *file_name, uint64_t row_length, uint64_t column_length, int read_csv(const char *file_name, const ulong row_length,
uint64_t output_matrix[row_length][column_length]); const ulong column_length,
ulong output_matrix[row_length][column_length]);
#endif #endif