switch from uint64_t to unsigned long
This commit is contained in:
parent
c41d130a45
commit
cddbadfd4a
281
graph.c
281
graph.c
@ -1,16 +1,15 @@
|
||||
#include "graph.h"
|
||||
#include <stdint.h>
|
||||
#include "matrix.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <time.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));
|
||||
|
||||
for (uint64_t row_index = 0; row_index < vertex_count; row_index++) {
|
||||
for (uint64_t column_index = 0; column_index < vertex_count; column_index++) {
|
||||
for (ulong row_index = 0; row_index < vertex_count; row_index++) {
|
||||
for (ulong column_index = 0; column_index < vertex_count; column_index++) {
|
||||
if (column_index == row_index) {
|
||||
matrix[row_index][column_index] = 0;
|
||||
} 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]) {
|
||||
uint64_t power_matrix[vertex_count][vertex_count];
|
||||
uint64_t temp_power_matrix[vertex_count][vertex_count];
|
||||
memcpy(power_matrix, adjacency_matrix, vertex_count * vertex_count * sizeof(uint64_t));
|
||||
void calculate_distance_matrix(const ulong vertex_count, const ulong adjacency_matrix[vertex_count][vertex_count], ulong distance_matrix[vertex_count][vertex_count]) {
|
||||
ulong power_matrix[vertex_count][vertex_count];
|
||||
ulong temp_power_matrix[vertex_count][vertex_count];
|
||||
memcpy(power_matrix, adjacency_matrix, vertex_count * vertex_count * sizeof(ulong));
|
||||
|
||||
for (uint64_t row_index = 0; row_index < vertex_count; row_index++) {
|
||||
for (uint64_t column_index = 0; column_index < vertex_count; column_index++) {
|
||||
for (ulong row_index = 0; row_index < vertex_count; row_index++) {
|
||||
for (ulong column_index = 0; column_index < vertex_count; column_index++) {
|
||||
if (row_index == column_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;
|
||||
distance_matrix[row_index][column_index] = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for(uint64_t k = 2; k <= vertex_count; k++) {
|
||||
memcpy(temp_power_matrix, power_matrix, vertex_count * vertex_count * sizeof(uint64_t));
|
||||
for(ulong k = 2; k <= vertex_count; k++) {
|
||||
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);
|
||||
|
||||
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) {
|
||||
for (ulong row_index = 0; row_index < vertex_count; row_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] == -1) {
|
||||
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]) {
|
||||
uint64_t eccentricity;
|
||||
int get_eccentricities(const ulong vertex_count, const ulong distance_matrix[vertex_count][vertex_count], ulong eccentricities[vertex_count]) {
|
||||
ulong 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 (ulong index = 0; index < vertex_count; index++) {
|
||||
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;
|
||||
|
||||
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) {
|
||||
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;
|
||||
}
|
||||
|
||||
uint64_t get_radius(const uint64_t vertex_count, const uint64_t eccentricities[vertex_count]) {
|
||||
uint64_t radius = UINT64_MAX;
|
||||
ulong get_radius(const ulong vertex_count, const ulong eccentricities[vertex_count]) {
|
||||
ulong radius = -1;
|
||||
|
||||
for (uint64_t index = 0; index < vertex_count; index++) {
|
||||
for (ulong index = 0; index < vertex_count; index++) {
|
||||
if (eccentricities[index] < radius) {
|
||||
radius = eccentricities[index];
|
||||
}
|
||||
@ -89,10 +88,10 @@ uint64_t get_radius(const uint64_t vertex_count, const uint64_t eccentricities[v
|
||||
return radius;
|
||||
}
|
||||
|
||||
uint64_t get_diameter(const uint64_t vertex_count, const uint64_t eccentricities[vertex_count]) {
|
||||
uint64_t diamter = 0;
|
||||
ulong get_diameter(const ulong vertex_count, const ulong eccentricities[vertex_count]) {
|
||||
ulong diamter = 0;
|
||||
|
||||
for (uint64_t index = 0; index < vertex_count; index++) {
|
||||
for (ulong index = 0; index < vertex_count; index++) {
|
||||
if (eccentricities[index] > diamter) {
|
||||
diamter = eccentricities[index];
|
||||
}
|
||||
@ -101,23 +100,23 @@ uint64_t get_diameter(const uint64_t vertex_count, const uint64_t eccentricities
|
||||
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]) {
|
||||
memset(centre, 0, vertex_count * sizeof(uint64_t));
|
||||
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(ulong));
|
||||
|
||||
for (uint64_t index = 0; index < vertex_count; index++) {
|
||||
for (ulong index = 0; index < vertex_count; index++) {
|
||||
if (eccentricities[index] == radius) {
|
||||
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]) {
|
||||
uint64_t power_matrix[vertex_count][vertex_count];
|
||||
uint64_t temp_power_matrix[vertex_count][vertex_count];
|
||||
memcpy(power_matrix, adjacency_matrix, vertex_count * vertex_count * sizeof(uint64_t));
|
||||
void calculate_path_matrix(const ulong vertex_count, const ulong adjacency_matrix[vertex_count][vertex_count], ulong path_matrix[vertex_count][vertex_count]) {
|
||||
ulong power_matrix[vertex_count][vertex_count];
|
||||
ulong temp_power_matrix[vertex_count][vertex_count];
|
||||
memcpy(power_matrix, adjacency_matrix, vertex_count * vertex_count * sizeof(ulong));
|
||||
|
||||
for (uint64_t row_index = 0; row_index < vertex_count; row_index++) {
|
||||
for (uint64_t column_index = 0; column_index < vertex_count; column_index++) {
|
||||
for (ulong row_index = 0; row_index < vertex_count; row_index++) {
|
||||
for (ulong column_index = 0; column_index < vertex_count; column_index++) {
|
||||
if (row_index == column_index || adjacency_matrix[row_index][column_index] == 1) {
|
||||
path_matrix[row_index][column_index] = 1;
|
||||
} 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++) {
|
||||
memcpy(temp_power_matrix, power_matrix, vertex_count * vertex_count * sizeof(uint64_t));
|
||||
for(ulong k = 2; k <= vertex_count; k++) {
|
||||
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);
|
||||
|
||||
for (uint64_t row_index = 0; row_index < vertex_count; row_index++) {
|
||||
for (uint64_t column_index = 0; column_index < vertex_count; column_index++) {
|
||||
for (ulong row_index = 0; row_index < vertex_count; row_index++) {
|
||||
for (ulong column_index = 0; column_index < vertex_count; column_index++) {
|
||||
if (power_matrix[row_index][column_index] != 0) {
|
||||
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]) {
|
||||
uint64_t component[vertex_count];
|
||||
void find_components_basic(const ulong vertex_count, const ulong path_matrix[vertex_count][vertex_count], ulong components[vertex_count][vertex_count]) {
|
||||
ulong component[vertex_count];
|
||||
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++) {
|
||||
memset(component, 0, vertex_count * sizeof(uint64_t));
|
||||
for (ulong row_index = 0; row_index < vertex_count; row_index++) {
|
||||
memset(component, 0, vertex_count * sizeof(ulong));
|
||||
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) {
|
||||
component[column_index] = column_index + 1;
|
||||
}
|
||||
}
|
||||
|
||||
for (uint64_t index = 0; index < vertex_count; index++) {
|
||||
if (memcmp(components[index], component, vertex_count * sizeof(uint64_t)) == 0) {
|
||||
for (ulong index = 0; index < vertex_count; index++) {
|
||||
if (memcmp(components[index], component, vertex_count * sizeof(ulong)) == 0) {
|
||||
contains_component = 1;
|
||||
}
|
||||
}
|
||||
|
||||
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];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
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) {
|
||||
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]) {
|
||||
uint64_t component[vertex_count];
|
||||
void find_components_dfs(const ulong vertex_count, const ulong adjacency_matrix[vertex_count][vertex_count], ulong components[vertex_count][vertex_count]) {
|
||||
ulong component[vertex_count];
|
||||
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++) {
|
||||
memset(component, 0, vertex_count * sizeof(uint64_t));
|
||||
for (ulong vertex = 0; vertex < vertex_count; vertex++) {
|
||||
memset(component, 0, vertex_count * sizeof(ulong));
|
||||
contains_component = 0;
|
||||
|
||||
dfs(vertex_count, adjacency_matrix, vertex, component);
|
||||
|
||||
for (uint64_t index = 0; index < vertex_count; index++) {
|
||||
if (memcmp(components[index], component, vertex_count * sizeof(uint64_t)) == 0) {
|
||||
for (ulong index = 0; index < vertex_count; index++) {
|
||||
if (memcmp(components[index], component, vertex_count * sizeof(ulong)) == 0) {
|
||||
contains_component = 1;
|
||||
}
|
||||
}
|
||||
|
||||
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];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t amount_of_components(const uint64_t vertex_count, const uint64_t components[vertex_count][vertex_count]) {
|
||||
uint64_t amount_of_components = 0;
|
||||
ulong amount_of_components(const ulong vertex_count, const ulong components[vertex_count][vertex_count]) {
|
||||
ulong amount_of_components = 0;
|
||||
|
||||
for (uint64_t row_index = 0; row_index < vertex_count; row_index++) {
|
||||
for (uint64_t column_index = 0; column_index < vertex_count; column_index++) {
|
||||
for (ulong row_index = 0; row_index < vertex_count; row_index++) {
|
||||
for (ulong column_index = 0; column_index < vertex_count; column_index++) {
|
||||
if (components[row_index][column_index] != 0) {
|
||||
amount_of_components++;
|
||||
break;
|
||||
@ -225,9 +224,9 @@ uint64_t amount_of_components(const uint64_t vertex_count, const uint64_t compon
|
||||
return amount_of_components;
|
||||
}
|
||||
|
||||
int contains_bridge(const uint64_t vertex_count, const uint64_t bridges[vertex_count][2], const uint64_t bridge[2]) {
|
||||
for (uint64_t index = 0; index < vertex_count; index++) {
|
||||
if (memcmp(bridges[index], bridge, 2 * sizeof(uint64_t)) == 0) {
|
||||
int contains_bridge(const ulong vertex_count, const ulong bridges[vertex_count][2], const ulong bridge[2]) {
|
||||
for (ulong index = 0; index < vertex_count; index++) {
|
||||
if (memcmp(bridges[index], bridge, 2 * sizeof(ulong)) == 0) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@ -235,17 +234,17 @@ int contains_bridge(const uint64_t vertex_count, const uint64_t bridges[vertex_c
|
||||
return 0;
|
||||
}
|
||||
|
||||
void find_bridges_basic(const uint64_t vertex_count, const uint64_t adjacency_matrix[vertex_count][vertex_count],
|
||||
const uint64_t components[vertex_count][vertex_count], uint64_t bridges[vertex_count][2]) {
|
||||
uint64_t path_matrix[vertex_count][vertex_count];
|
||||
uint64_t temp_adjacency_matrix[vertex_count][vertex_count];
|
||||
uint64_t temp_components[vertex_count][vertex_count];
|
||||
uint64_t bridge[2];
|
||||
void find_bridges_basic(const ulong vertex_count, const ulong adjacency_matrix[vertex_count][vertex_count],
|
||||
const ulong components[vertex_count][vertex_count], ulong bridges[vertex_count][2]) {
|
||||
ulong path_matrix[vertex_count][vertex_count];
|
||||
ulong temp_adjacency_matrix[vertex_count][vertex_count];
|
||||
ulong temp_components[vertex_count][vertex_count];
|
||||
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 (uint64_t column_index = 0; column_index < vertex_count; column_index++) {
|
||||
for (ulong row_index = 0; row_index < vertex_count; row_index++) {
|
||||
for (ulong column_index = 0; column_index < vertex_count; column_index++) {
|
||||
if (row_index == column_index) {
|
||||
continue;
|
||||
}
|
||||
@ -258,7 +257,7 @@ void find_bridges_basic(const uint64_t vertex_count, const uint64_t adjacency_ma
|
||||
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[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);
|
||||
}
|
||||
|
||||
void find_bridges_dfs_v1(const uint64_t vertex_count, const uint64_t adjacency_matrix[vertex_count][vertex_count],
|
||||
const uint64_t components[vertex_count][vertex_count], uint64_t bridges[vertex_count][2]) {
|
||||
uint64_t temp_adjacency_matrix[vertex_count][vertex_count];
|
||||
uint64_t temp_components[vertex_count][vertex_count];
|
||||
uint64_t bridge[2];
|
||||
void find_bridges_dfs_v1(const ulong vertex_count, const ulong adjacency_matrix[vertex_count][vertex_count],
|
||||
const ulong components[vertex_count][vertex_count], ulong bridges[vertex_count][2]) {
|
||||
ulong temp_adjacency_matrix[vertex_count][vertex_count];
|
||||
ulong temp_components[vertex_count][vertex_count];
|
||||
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 (uint64_t column_index = 0; column_index < vertex_count; column_index++) {
|
||||
for (ulong row_index = 0; row_index < vertex_count; row_index++) {
|
||||
for (ulong column_index = 0; column_index < vertex_count; column_index++) {
|
||||
if (row_index == column_index) {
|
||||
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;
|
||||
}
|
||||
|
||||
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[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],
|
||||
uint64_t current_time, uint64_t discovery_time[vertex_count], uint64_t lowest_time[vertex_count], uint64_t bridges[vertex_count][2]) {
|
||||
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],
|
||||
ulong current_time, ulong discovery_time[vertex_count], ulong lowest_time[vertex_count], ulong bridges[vertex_count][2]) {
|
||||
current_time++;
|
||||
visited[vertex] = 1;
|
||||
discovery_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) {
|
||||
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],
|
||||
const uint64_t components[vertex_count][vertex_count], uint64_t bridges[vertex_count][2]) {
|
||||
uint64_t visited[vertex_count];
|
||||
uint64_t current_time = 0;
|
||||
uint64_t discovery_time[vertex_count];
|
||||
uint64_t lowest_time[vertex_count];
|
||||
void find_bridges_dfs_v2(const ulong vertex_count, const ulong adjacency_matrix[vertex_count][vertex_count],
|
||||
const ulong components[vertex_count][vertex_count], ulong bridges[vertex_count][2]) {
|
||||
ulong visited[vertex_count];
|
||||
ulong current_time = 0;
|
||||
ulong discovery_time[vertex_count];
|
||||
ulong lowest_time[vertex_count];
|
||||
|
||||
memset(bridges, 0, vertex_count * 2 * sizeof(uint64_t));
|
||||
memset(visited, 0, vertex_count * sizeof(uint64_t));
|
||||
memset(discovery_time, 0, vertex_count * sizeof(uint64_t));
|
||||
memset(lowest_time, 0, vertex_count * sizeof(uint64_t));
|
||||
memset(bridges, 0, vertex_count * 2 * sizeof(ulong));
|
||||
memset(visited, 0, vertex_count * sizeof(ulong));
|
||||
memset(discovery_time, 0, vertex_count * sizeof(ulong));
|
||||
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]) {
|
||||
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],
|
||||
const uint64_t components[vertex_count][vertex_count], uint64_t articulations[vertex_count]) {
|
||||
uint64_t path_matrix[vertex_count][vertex_count];
|
||||
uint64_t temp_adjacency_matrix[vertex_count][vertex_count];
|
||||
uint64_t temp_components[vertex_count][vertex_count];
|
||||
void find_articulations_basic(const ulong vertex_count, const ulong adjacency_matrix[vertex_count][vertex_count],
|
||||
const ulong components[vertex_count][vertex_count], ulong articulations[vertex_count]) {
|
||||
ulong path_matrix[vertex_count][vertex_count];
|
||||
ulong temp_adjacency_matrix[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++) {
|
||||
memcpy(temp_adjacency_matrix, adjacency_matrix, vertex_count * vertex_count * sizeof(uint64_t));
|
||||
for (ulong i = 0; i < vertex_count; i++) {
|
||||
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 (uint64_t column_index = 0; column_index < vertex_count; column_index++) {
|
||||
for (ulong row_index = 0; row_index < vertex_count; row_index++) {
|
||||
for (ulong column_index = 0; column_index < vertex_count; column_index++) {
|
||||
temp_adjacency_matrix[row_index][i] = 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],
|
||||
const uint64_t components[vertex_count][vertex_count], uint64_t articulations[vertex_count]) {
|
||||
uint64_t temp_adjacency_matrix[vertex_count][vertex_count];
|
||||
uint64_t temp_components[vertex_count][vertex_count];
|
||||
void find_articulations_dfs_v1(const ulong vertex_count, const ulong adjacency_matrix[vertex_count][vertex_count],
|
||||
const ulong components[vertex_count][vertex_count], ulong articulations[vertex_count]) {
|
||||
ulong temp_adjacency_matrix[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++) {
|
||||
memcpy(temp_adjacency_matrix, adjacency_matrix, vertex_count * vertex_count * sizeof(uint64_t));
|
||||
for (ulong i = 0; i < vertex_count; i++) {
|
||||
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 (uint64_t column_index = 0; column_index < vertex_count; column_index++) {
|
||||
for (ulong row_index = 0; row_index < vertex_count; row_index++) {
|
||||
for (ulong column_index = 0; column_index < vertex_count; column_index++) {
|
||||
temp_adjacency_matrix[row_index][i] = 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,
|
||||
uint64_t visited[vertex_count], uint64_t current_time, uint64_t discovery_time[vertex_count], uint64_t lowest_time[vertex_count],
|
||||
uint64_t articulations[vertex_count]) {
|
||||
void dfs_articulations(const ulong vertex_count, const ulong adjacency_matrix[vertex_count][vertex_count], const ulong vertex, const ulong parent_vertex,
|
||||
ulong visited[vertex_count], ulong current_time, ulong discovery_time[vertex_count], ulong lowest_time[vertex_count],
|
||||
ulong articulations[vertex_count]) {
|
||||
current_time++;
|
||||
visited[vertex] = 1;
|
||||
discovery_time[vertex] = current_time;
|
||||
lowest_time[vertex] = current_time;
|
||||
uint64_t child_count = 0;
|
||||
uint64_t is_articulation = 0;
|
||||
ulong child_count = 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) {
|
||||
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];
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
if (parent_vertex == UINT64_MAX && child_count > 1) {
|
||||
if (parent_vertex == -1 && child_count > 1) {
|
||||
is_articulation = 1;
|
||||
}
|
||||
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],
|
||||
const uint64_t components[vertex_count][vertex_count], uint64_t articulations[vertex_count]) {
|
||||
uint64_t visited[vertex_count];
|
||||
uint64_t current_time = 0;
|
||||
uint64_t discovery_time[vertex_count];
|
||||
uint64_t lowest_time[vertex_count];
|
||||
void find_articulations_dfs_v2(const ulong vertex_count, const ulong adjacency_matrix[vertex_count][vertex_count],
|
||||
const ulong components[vertex_count][vertex_count], ulong articulations[vertex_count]) {
|
||||
ulong visited[vertex_count];
|
||||
ulong current_time = 0;
|
||||
ulong discovery_time[vertex_count];
|
||||
ulong lowest_time[vertex_count];
|
||||
|
||||
memset(articulations, 0, vertex_count * sizeof(uint64_t));
|
||||
memset(visited, 0, vertex_count * sizeof(uint64_t));
|
||||
memset(discovery_time, 0, vertex_count * sizeof(uint64_t));
|
||||
memset(lowest_time, 0, vertex_count * sizeof(uint64_t));
|
||||
memset(articulations, 0, vertex_count * sizeof(ulong));
|
||||
memset(visited, 0, vertex_count * sizeof(ulong));
|
||||
memset(discovery_time, 0, vertex_count * sizeof(ulong));
|
||||
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]) {
|
||||
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
100
graph.h
@ -1,71 +1,71 @@
|
||||
#ifndef GRAPH_H
|
||||
#define GRAPH_H
|
||||
|
||||
#include <stdint.h>
|
||||
typedef unsigned long ulong;
|
||||
|
||||
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]);
|
||||
|
||||
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]);
|
||||
|
||||
// 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,
|
||||
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 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 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]);
|
||||
|
||||
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]);
|
||||
|
||||
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]);
|
||||
|
||||
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]);
|
||||
|
||||
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]);
|
||||
|
||||
void find_bridges_basic(const uint64_t vertex_count,
|
||||
const uint64_t adjacency_matrix[vertex_count][vertex_count],
|
||||
const uint64_t components[vertex_count][vertex_count],
|
||||
uint64_t bridges[vertex_count][2]);
|
||||
void find_bridges_basic(const ulong vertex_count,
|
||||
const ulong adjacency_matrix[vertex_count][vertex_count],
|
||||
const ulong components[vertex_count][vertex_count],
|
||||
ulong bridges[vertex_count][2]);
|
||||
|
||||
void find_bridges_dfs_v1(const uint64_t vertex_count,
|
||||
const uint64_t adjacency_matrix[vertex_count][vertex_count],
|
||||
const uint64_t components[vertex_count][vertex_count],
|
||||
uint64_t bridges[vertex_count][2]);
|
||||
void find_bridges_dfs_v1(const ulong vertex_count,
|
||||
const ulong adjacency_matrix[vertex_count][vertex_count],
|
||||
const ulong components[vertex_count][vertex_count],
|
||||
ulong bridges[vertex_count][2]);
|
||||
|
||||
void find_bridges_dfs_v2(const uint64_t vertex_count,
|
||||
const uint64_t adjacency_matrix[vertex_count][vertex_count],
|
||||
const uint64_t components[vertex_count][vertex_count],
|
||||
uint64_t bridges[vertex_count][2]);
|
||||
void find_bridges_dfs_v2(const ulong vertex_count,
|
||||
const ulong adjacency_matrix[vertex_count][vertex_count],
|
||||
const ulong components[vertex_count][vertex_count],
|
||||
ulong bridges[vertex_count][2]);
|
||||
|
||||
void find_articulations_basic(const uint64_t vertex_count,
|
||||
const uint64_t adjacency_matrix[vertex_count][vertex_count],
|
||||
const uint64_t components[vertex_count][vertex_count],
|
||||
uint64_t articulations[vertex_count]);
|
||||
void find_articulations_basic(const ulong vertex_count,
|
||||
const ulong adjacency_matrix[vertex_count][vertex_count],
|
||||
const ulong components[vertex_count][vertex_count],
|
||||
ulong articulations[vertex_count]);
|
||||
|
||||
void find_articulations_dfs_v1(const uint64_t vertex_count,
|
||||
const uint64_t adjacency_matrix[vertex_count][vertex_count],
|
||||
const uint64_t components[vertex_count][vertex_count],
|
||||
uint64_t articulations[vertex_count]);
|
||||
void find_articulations_dfs_v1(const ulong vertex_count,
|
||||
const ulong adjacency_matrix[vertex_count][vertex_count],
|
||||
const ulong components[vertex_count][vertex_count],
|
||||
ulong articulations[vertex_count]);
|
||||
|
||||
void find_articulations_dfs_v2(const uint64_t vertex_count,
|
||||
const uint64_t adjacency_matrix[vertex_count][vertex_count],
|
||||
const uint64_t components[vertex_count][vertex_count],
|
||||
uint64_t articulations[vertex_count]);
|
||||
void find_articulations_dfs_v2(const ulong vertex_count,
|
||||
const ulong adjacency_matrix[vertex_count][vertex_count],
|
||||
const ulong components[vertex_count][vertex_count],
|
||||
ulong articulations[vertex_count]);
|
||||
|
||||
#endif
|
||||
|
179
main.c
179
main.c
@ -1,21 +1,23 @@
|
||||
#include "graph.h"
|
||||
#include "matrix.h"
|
||||
#include <limits.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct matrix {ulong row_length; ulong column_length; ulong **values;} Matrix;
|
||||
|
||||
void benchmark_gemm() {
|
||||
const uint64_t vertex_count = 100;
|
||||
uint64_t adjacency_matrix1[vertex_count][vertex_count];
|
||||
uint64_t adjacency_matrix2[vertex_count][vertex_count];
|
||||
uint64_t dot_product[vertex_count][vertex_count];
|
||||
const ulong vertex_count = 100;
|
||||
ulong adjacency_matrix1[vertex_count][vertex_count];
|
||||
ulong adjacency_matrix2[vertex_count][vertex_count];
|
||||
ulong dot_product[vertex_count][vertex_count];
|
||||
|
||||
double elapsed_time = 0.0;
|
||||
const uint64_t iterations = 10;
|
||||
const ulong iterations = 10;
|
||||
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_matrix2);
|
||||
|
||||
@ -33,16 +35,16 @@ void benchmark_gemm() {
|
||||
}
|
||||
|
||||
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];
|
||||
const ulong vertex_count = 100;
|
||||
ulong adjacency_matrix[vertex_count][vertex_count];
|
||||
ulong components[vertex_count][vertex_count];
|
||||
ulong path_matrix[vertex_count][vertex_count];
|
||||
|
||||
double elapsed_time = 0.0;
|
||||
const uint64_t iterations = 100;
|
||||
const ulong iterations = 100;
|
||||
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);
|
||||
|
||||
start_time = clock();
|
||||
@ -58,7 +60,7 @@ void benchmark_find_components() {
|
||||
|
||||
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);
|
||||
|
||||
start_time = clock();
|
||||
@ -73,15 +75,15 @@ void benchmark_find_components() {
|
||||
}
|
||||
|
||||
void test_with_basic() {
|
||||
const uint64_t vertex_count = 24;
|
||||
uint64_t adjacency_matrix[vertex_count][vertex_count];
|
||||
uint64_t distance_matrix[vertex_count][vertex_count];
|
||||
uint64_t path_matrix[vertex_count][vertex_count];
|
||||
uint64_t eccentricities[vertex_count];
|
||||
uint64_t radius, diameter, centre[vertex_count];
|
||||
uint64_t components[vertex_count][vertex_count];
|
||||
uint64_t bridges[vertex_count][2];
|
||||
uint64_t articulations[vertex_count];
|
||||
const ulong vertex_count = 24;
|
||||
ulong adjacency_matrix[vertex_count][vertex_count];
|
||||
ulong distance_matrix[vertex_count][vertex_count];
|
||||
ulong path_matrix[vertex_count][vertex_count];
|
||||
ulong eccentricities[vertex_count];
|
||||
ulong radius, diameter, centre[vertex_count];
|
||||
ulong components[vertex_count][vertex_count];
|
||||
ulong bridges[vertex_count][2];
|
||||
ulong articulations[vertex_count];
|
||||
|
||||
if (read_csv("csv/24n.csv", vertex_count, vertex_count, adjacency_matrix) == 1) {
|
||||
return;
|
||||
@ -104,14 +106,14 @@ void test_with_basic() {
|
||||
print_matrix(vertex_count, vertex_count, distance_matrix);
|
||||
|
||||
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("\nradius: %lu", radius);
|
||||
printf("\ndiameter: %lu", diameter);
|
||||
puts("\ncentre:");
|
||||
for (uint64_t index = 0; index < vertex_count; index++) {
|
||||
for (ulong index = 0; index < vertex_count; index++) {
|
||||
if (centre[index] == 1) {
|
||||
printf("\tVertex %lu\n", index + 1);
|
||||
}
|
||||
@ -121,10 +123,10 @@ void test_with_basic() {
|
||||
print_matrix(vertex_count, vertex_count, path_matrix);
|
||||
|
||||
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;
|
||||
|
||||
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) {
|
||||
empty = 0;
|
||||
}
|
||||
@ -135,7 +137,7 @@ void test_with_basic() {
|
||||
}
|
||||
|
||||
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) {
|
||||
printf("%lu, ", components[row_index][column_index]);
|
||||
}
|
||||
@ -144,14 +146,14 @@ void test_with_basic() {
|
||||
}
|
||||
|
||||
puts("\nbridges:");
|
||||
for (uint64_t index = 0; index < vertex_count; index++) {
|
||||
for (ulong index = 0; index < vertex_count; index++) {
|
||||
if (bridges[index][0] != 0) {
|
||||
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++) {
|
||||
for (ulong index = 0; index < vertex_count; index++) {
|
||||
if (articulations[index] != 0) {
|
||||
printf("\tVertex %lu\n", articulations[index]);
|
||||
}
|
||||
@ -159,30 +161,30 @@ void test_with_basic() {
|
||||
}
|
||||
|
||||
void test_with_dfs() {
|
||||
const uint64_t vertex_count = 24;
|
||||
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;
|
||||
uint64_t centre[vertex_count];
|
||||
uint64_t components[vertex_count][vertex_count];
|
||||
uint64_t bridges[vertex_count][2];
|
||||
uint64_t articulations[vertex_count];
|
||||
const ulong vertex_count = 24;
|
||||
ulong adjacency_matrix[vertex_count][vertex_count];
|
||||
ulong distance_matrix[vertex_count][vertex_count];
|
||||
ulong eccentricities[vertex_count];
|
||||
ulong radius, diameter;
|
||||
ulong centre[vertex_count];
|
||||
ulong components[vertex_count][vertex_count];
|
||||
ulong bridges[vertex_count][2];
|
||||
ulong articulations[vertex_count];
|
||||
|
||||
if (read_csv("csv/24n.csv", vertex_count, vertex_count, adjacency_matrix) == 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
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));
|
||||
const ulong vertex_count = 1500;
|
||||
ulong (*adjacency_matrix)[vertex_count] = malloc(vertex_count * vertex_count * sizeof(ulong));
|
||||
ulong (*distance_matrix)[vertex_count] = malloc(vertex_count * vertex_count * sizeof(ulong));
|
||||
ulong *eccentricities = malloc(vertex_count * sizeof(ulong));
|
||||
ulong radius, diameter;
|
||||
ulong *centre = malloc(vertex_count * sizeof(ulong));
|
||||
ulong (*components)[vertex_count] = malloc(vertex_count * vertex_count * sizeof(ulong));
|
||||
ulong (*bridges)[vertex_count] = malloc(vertex_count * 2 * sizeof(ulong));
|
||||
ulong *articulations = malloc(vertex_count * sizeof(ulong));
|
||||
|
||||
random_adjacency(vertex_count, adjacency_matrix);
|
||||
*/
|
||||
@ -203,24 +205,24 @@ void test_with_dfs() {
|
||||
print_matrix(vertex_count, vertex_count, distance_matrix);
|
||||
|
||||
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("\nradius: %lu", radius);
|
||||
printf("\ndiameter: %lu", diameter);
|
||||
puts("\ncentre:");
|
||||
for (uint64_t index = 0; index < vertex_count; index++) {
|
||||
for (ulong 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++) {
|
||||
for (ulong row_index = 0; row_index < vertex_count; row_index++) {
|
||||
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) {
|
||||
empty = 0;
|
||||
}
|
||||
@ -231,7 +233,7 @@ void test_with_dfs() {
|
||||
}
|
||||
|
||||
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) {
|
||||
printf("%lu, ", column_index + 1);
|
||||
}
|
||||
@ -239,9 +241,9 @@ void test_with_dfs() {
|
||||
puts("}");
|
||||
}
|
||||
|
||||
uint64_t bridge_number = 1;
|
||||
ulong bridge_number = 1;
|
||||
puts("\nbridges:");
|
||||
for (uint64_t index = 0; index < vertex_count; index++) {
|
||||
for (ulong 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++;
|
||||
@ -249,7 +251,7 @@ void test_with_dfs() {
|
||||
}
|
||||
|
||||
puts("\narticulations:");
|
||||
for (uint64_t index = 0; index < vertex_count; index++) {
|
||||
for (ulong index = 0; index < vertex_count; index++) {
|
||||
if (articulations[index] != 0) {
|
||||
printf("\tVertex %lu\n", articulations[index]);
|
||||
}
|
||||
@ -264,9 +266,70 @@ void test_with_dfs() {
|
||||
// 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) {
|
||||
// test_with_basic();
|
||||
test_with_dfs();
|
||||
// benchmark_gemm();
|
||||
// benchmark_find_components();
|
||||
// test_with_basic();
|
||||
// test_with_dfs();
|
||||
test_with_structs();
|
||||
}
|
||||
|
28
matrix.c
28
matrix.c
@ -1,29 +1,29 @@
|
||||
#include "matrix.h"
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.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]) {
|
||||
for (uint64_t column_index=0; column_index < column_length; column_index++) {
|
||||
for (uint64_t row_index=0; row_index < row_length; row_index++) {
|
||||
void print_matrix(const ulong row_length, const ulong column_length, const ulong matrix[row_length][column_length]) {
|
||||
for (ulong column_index=0; column_index < column_length; column_index++) {
|
||||
for (ulong row_index=0; row_index < row_length; row_index++) {
|
||||
printf("%lu ", matrix[row_index][column_index]);
|
||||
}
|
||||
puts("");
|
||||
}
|
||||
}
|
||||
|
||||
void gemm_basic(const uint64_t row_length1, const uint64_t column_length1, const uint64_t matrix1[row_length1][column_length1],
|
||||
const uint64_t row_length2, const uint64_t column_length2, const uint64_t matrix2[row_length2][column_length2],
|
||||
uint64_t output_matrix[row_length1][column_length2]) {
|
||||
uint64_t sum;
|
||||
void gemm_basic(const ulong row_length1, const ulong column_length1, const ulong matrix1[row_length1][column_length1],
|
||||
const ulong row_length2, const ulong column_length2, const ulong matrix2[row_length2][column_length2],
|
||||
ulong output_matrix[row_length1][column_length2]) {
|
||||
ulong sum;
|
||||
|
||||
for (uint64_t i = 0; i < row_length1; i++) {
|
||||
for (uint64_t j = 0; j < column_length2; j++) {
|
||||
for (ulong i = 0; i < row_length1; i++) {
|
||||
for (ulong j = 0; j < column_length2; j++) {
|
||||
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];
|
||||
}
|
||||
|
||||
@ -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;
|
||||
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 *value, *file_line;
|
||||
uint64_t row_index = 0, column_index = 0;
|
||||
ulong row_index = 0, column_index = 0;
|
||||
|
||||
file_ptr = fopen(file_name, "r");
|
||||
if (file_ptr == NULL) {
|
||||
|
21
matrix.h
21
matrix.h
@ -1,23 +1,24 @@
|
||||
#ifndef 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,
|
||||
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]);
|
||||
|
||||
/*
|
||||
First two matrices will be multiplied and
|
||||
restult will be written to output_matrix.
|
||||
Matrix size requirements are as specified in the parameters.
|
||||
*/
|
||||
void gemm_basic(const uint64_t row_length1, const uint64_t column_length1,
|
||||
const uint64_t matrix1[row_length1][column_length1],
|
||||
const uint64_t row_length2, const uint64_t column_length2,
|
||||
const uint64_t matrix2[row_length2][column_length2],
|
||||
uint64_t output_matrix[row_length1][column_length2]);
|
||||
void gemm_basic(const ulong row_length1, const ulong column_length1,
|
||||
const ulong matrix1[row_length1][column_length1],
|
||||
const ulong row_length2, const ulong column_length2,
|
||||
const ulong matrix2[row_length2][column_length2],
|
||||
ulong output_matrix[row_length1][column_length2]);
|
||||
|
||||
int read_csv(char *file_name, uint64_t row_length, 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]);
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user