From 59fc6988d6aa0ccbbe3d60edc093e833c86ffd9a Mon Sep 17 00:00:00 2001 From: Sam Hadow Date: Fri, 16 May 2025 19:06:12 +0200 Subject: [PATCH] crappy but working version --- src/gauss_seidel.c | 4 ++-- src/main.c | 36 ++++++++++++++++++++++++++++++++++++ src/matrix_operation.c | 8 ++++---- src/read_from_mtx.c | 20 ++++++++++---------- src/read_from_rb.c | 20 ++++++++++---------- src/sparse_matrix.h | 2 +- 6 files changed, 63 insertions(+), 27 deletions(-) diff --git a/src/gauss_seidel.c b/src/gauss_seidel.c index 279dff3..f918b73 100644 --- a/src/gauss_seidel.c +++ b/src/gauss_seidel.c @@ -49,8 +49,8 @@ double* gauss_seidel_pagerank(const SparseMatrix *matrix, double epsilon, double for (int i = N - 1; i >= 0; i--) { double result_mult = 0.0; double M_ii = 0.0; - for (int k = matrix->row_ptr[i]; k < matrix->row_ptr[i + 1]; k++) { - int j = matrix->arcs[k].dest; + for (int k = matrix->col_ptr[i]; k < matrix->col_ptr[i + 1]; k++) { + int j = matrix->arcs[k].origin; double value = matrix->arcs[k].value; if (j == i) { diff --git a/src/main.c b/src/main.c index 088f16c..3b31396 100644 --- a/src/main.c +++ b/src/main.c @@ -10,6 +10,37 @@ #include "time_helper.h" #include #include +#include +#include + +static double *values_for_sort; +int cmp_desc(const void *a, const void *b) { + int ia = *(const int*)a; + int ib = *(const int*)b; + if (values_for_sort[ia] < values_for_sort[ib]) return 1; + if (values_for_sort[ia] > values_for_sort[ib]) return -1; + return 0; +} + +void print_top_k(const char *name, double *pi, int n, int k) { + int *idx = malloc(n * sizeof(int)); + if(!idx) { + fprintf(stderr, "malloc failed\n"); + return; + } + for (int i = 0; i < n; i++) idx[i] = i; + + values_for_sort = pi; + qsort(idx, n, sizeof(int), cmp_desc); + + printf("Top %d entries of %s:\n", k, name); + for (int i = 0; i < k && i < n; i++) { + int node = idx[i]; + printf(" %2d: node %d → %.8f\n", i+1, node, pi[node]); + } + free(idx); +} + void test_stationary_distribution(const char *path, double epsilon, double alpha) { struct timeval tvstart, tvend; @@ -42,6 +73,11 @@ void test_stationary_distribution(const char *path, double epsilon, double alpha double diff = diff_norm_vector(pi_power, pi_new, matrix->num_nodes); printf("Difference norm Pagerank and Gauss-Seidel: %.16f\n", diff); + int n = matrix->num_nodes; + int k = 10; + print_top_k("pagerank (power)", pi_power, n, k); + print_top_k("pagerank (Gauss–Seidel)", pi_new, n, k); + free(pi_power); free(pi_new); free_sparse_matrix(matrix); diff --git a/src/matrix_operation.c b/src/matrix_operation.c index 6cc9a8f..7160b0d 100644 --- a/src/matrix_operation.c +++ b/src/matrix_operation.c @@ -53,14 +53,14 @@ void convert_to_stochastic(SparseMatrix *matrix) { // count non zero values for (int i = 0; i < matrix->num_arcs; i++) { - non_zero[matrix->arcs[i].dest] += matrix->arcs[i].value; + non_zero[matrix->arcs[i].origin] += matrix->arcs[i].value; } // normalize values for (int i = 0; i < matrix->num_arcs; i++) { - int dest = matrix->arcs[i].dest; + int origin = matrix->arcs[i].origin; double old_value = matrix->arcs[i].value; - matrix->arcs[i].value = old_value / (double)non_zero[dest]; + matrix->arcs[i].value = old_value / (double)non_zero[origin]; } free(non_zero); @@ -69,6 +69,6 @@ void convert_to_stochastic(SparseMatrix *matrix) { int compare_arcs(const void *a, const void *b) { Arc *arc_a = (Arc *)a; Arc *arc_b = (Arc *)b; - return arc_a->origin - arc_b->origin; + return arc_a->dest - arc_b->dest; } diff --git a/src/read_from_mtx.c b/src/read_from_mtx.c index 5cc50b6..42d2a2d 100644 --- a/src/read_from_mtx.c +++ b/src/read_from_mtx.c @@ -67,20 +67,20 @@ SparseMatrix* read_sparse_matrix_from_mtx(const char *filename) { parse_arcs(matrix, file); - // Sort arcs by origin + // Sort arcs by destination qsort(matrix->arcs, matrix->num_arcs, sizeof(Arc), compare_arcs); - // Build row_ptr - matrix->row_ptr = (int *)malloc((matrix->num_nodes + 1) * sizeof(int)); - int current_origin = 0; - matrix->row_ptr[0] = 0; + // Build col_ptr + matrix->col_ptr = (int *)malloc((matrix->num_nodes + 1) * sizeof(int)); + int current_dest = 0; + matrix->col_ptr[0] = 0; for (int k = 0; k < matrix->num_arcs; k++) { - while (matrix->arcs[k].origin > current_origin) { - current_origin++; - matrix->row_ptr[current_origin] = k; + while (matrix->arcs[k].dest > current_dest) { + current_dest++; + matrix->col_ptr[current_dest] = k; } } - for (int i = current_origin + 1; i <= matrix->num_nodes; i++) { - matrix->row_ptr[i] = matrix->num_arcs; + for (int i = current_dest + 1; i <= matrix->num_nodes; i++) { + matrix->col_ptr[i] = matrix->num_arcs; } fclose(file); diff --git a/src/read_from_rb.c b/src/read_from_rb.c index 29c578e..b2edf66 100644 --- a/src/read_from_rb.c +++ b/src/read_from_rb.c @@ -99,21 +99,21 @@ SparseMatrix* read_sparse_matrix_from_rb(const char *filename) { } - // Sort arcs by origin + // Sort arcs by destination qsort(matrix->arcs, matrix->num_arcs, sizeof(Arc), compare_arcs); - // Build row_ptr - matrix->row_ptr = (int *)malloc((matrix->num_nodes + 1) * sizeof(int)); - int current_origin = 0; - matrix->row_ptr[0] = 0; + // Build col_ptr + matrix->col_ptr = (int *)malloc((matrix->num_nodes + 1) * sizeof(int)); + int current_destination = 0; + matrix->col_ptr[0] = 0; for (int k = 0; k < matrix->num_arcs; k++) { - while (matrix->arcs[k].origin > current_origin) { - current_origin++; - matrix->row_ptr[current_origin] = k; + while (matrix->arcs[k].dest > current_destination) { + current_destination++; + matrix->col_ptr[current_destination] = k; } } - for (int i = current_origin + 1; i <= matrix->num_nodes; i++) { - matrix->row_ptr[i] = matrix->num_arcs; + for (int i = current_destination + 1; i <= matrix->num_nodes; i++) { + matrix->col_ptr[i] = matrix->num_arcs; } free(col_ptr); diff --git a/src/sparse_matrix.h b/src/sparse_matrix.h index f5d61cf..2bc2660 100644 --- a/src/sparse_matrix.h +++ b/src/sparse_matrix.h @@ -11,7 +11,7 @@ typedef struct { int num_nodes; int num_arcs; Arc *arcs; - int *row_ptr; + int *col_ptr; } SparseMatrix; void free_sparse_matrix(SparseMatrix *matrix);