From bd4e30cdfd8f4c29d814ed23d336755989d465fc Mon Sep 17 00:00:00 2001 From: Sam Hadow Date: Fri, 16 May 2025 22:08:07 +0200 Subject: [PATCH] clean up code --- main.py | 10 +++++----- src/gauss_seidel.c | 5 +---- src/main.c | 37 +++++-------------------------------- src/power_algorithm.c | 5 +---- src/ranking_utils.c | 40 ++++++++++++++++++++++++++++++++++++++++ src/ranking_utils.h | 6 ++++++ 6 files changed, 58 insertions(+), 45 deletions(-) create mode 100644 src/ranking_utils.c create mode 100644 src/ranking_utils.h diff --git a/main.py b/main.py index 9ab7de4..f7e05b5 100644 --- a/main.py +++ b/main.py @@ -25,19 +25,19 @@ def run_pagerank(matrix_path, epsilon, alpha): match = re.search(r"Read and convert matrix: (\d+\.\d+) seconds", output) data["read_time"] = float(match.group(1)) if match else None - match = re.search(r"Pagerank: (\d+) Iterations", output) + match = re.search(r"Pagerank \(power\): (\d+) Iterations", output) data["pagerank_iterations"] = int(match.group(1)) if match else None - match = re.search(r"Pagerank: (\d+\.\d+) seconds", output) + match = re.search(r"Pagerank \(power\): (\d+\.\d+) seconds", output) data["pagerank_time"] = float(match.group(1)) if match else None - match = re.search(r"Gauss-Seidel: (\d+) Iterations", output) + match = re.search(r"Gauß-Seidel: (\d+) Iterations", output) data["gauss_seidel_iterations"] = int(match.group(1)) if match else None - match = re.search(r"Gauss-Seidel: (\d+\.\d+) seconds", output) + match = re.search(r"Gauß-Seidel: (\d+\.\d+) seconds", output) data["gauss_seidel_time"] = float(match.group(1)) if match else None - match = re.search(r"Difference norm Pagerank and Gauss-Seidel: (\d+\.\d+)", output) + match = re.search(r"Difference norm Pagerank power and Gauß-Seidel: (\d+\.\d+)", output) data["diff_norm"] = float(match.group(1)) if match else None return data diff --git a/src/gauss_seidel.c b/src/gauss_seidel.c index f918b73..c6512bb 100644 --- a/src/gauss_seidel.c +++ b/src/gauss_seidel.c @@ -72,13 +72,10 @@ double* gauss_seidel_pagerank(const SparseMatrix *matrix, double epsilon, double // 5. convergence diff = diff_norm_vector(x, x_old, N); - // if ((++iter) % 10 == 0) { - // printf("Gauss-Seidel Iteration %d: diff = %.16f\n", iter, diff); - // } ++iter; } while (diff > epsilon); - printf("Gauss-Seidel: %d Iterations\n", iter); + printf("Gauß-Seidel: %d Iterations\n", iter); free(x_old); free(f); diff --git a/src/main.c b/src/main.c index 3b31396..587c6ad 100644 --- a/src/main.c +++ b/src/main.c @@ -12,34 +12,7 @@ #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); -} +#include "ranking_utils.h" void test_stationary_distribution(const char *path, double epsilon, double alpha) { @@ -62,21 +35,21 @@ void test_stationary_distribution(const char *path, double epsilon, double alpha gettimeofday(&tvstart, NULL); double *pi_power = pagerank(matrix, epsilon, alpha); gettimeofday(&tvend, NULL); - print_time_diff("Pagerank", &tvstart, &tvend); + print_time_diff("Pagerank (power)", &tvstart, &tvend); gettimeofday(&tvstart, NULL); double *pi_new = gauss_seidel_pagerank(matrix, epsilon, alpha); gettimeofday(&tvend, NULL); - print_time_diff("Gauss-Seidel", &tvstart, &tvend); + print_time_diff("Gauß-Seidel", &tvstart, &tvend); double diff = diff_norm_vector(pi_power, pi_new, matrix->num_nodes); - printf("Difference norm Pagerank and Gauss-Seidel: %.16f\n", diff); + printf("Difference norm Pagerank power and Gauß-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); + print_top_k("pagerank (Gauß–Seidel)", pi_new, n, k); free(pi_power); free(pi_new); diff --git a/src/power_algorithm.c b/src/power_algorithm.c index c74f177..f9dc261 100644 --- a/src/power_algorithm.c +++ b/src/power_algorithm.c @@ -64,14 +64,11 @@ double* pagerank(const SparseMatrix *matrix, double epsilon, double alpha) { pi = pi_new; pi_new = malloc(vec_size); - // if ((++iter)%1 == 0) { - // printf("Iteration %d: diff = %.16f\n", iter, diff); - // } ++iter; free(temp); } while (diff > epsilon); - printf("Pagerank: %d Iterations\n", iter); + printf("Pagerank (power): %d Iterations\n", iter); free(pi_new); free(f); diff --git a/src/ranking_utils.c b/src/ranking_utils.c new file mode 100644 index 0000000..10ba39e --- /dev/null +++ b/src/ranking_utils.c @@ -0,0 +1,40 @@ +#include +#include +#include "ranking_utils.h" + +typedef struct { + int index; + double value; +} IndexedValue; + +// descending order sort for indexed stationary distribution values +static int cmp_desc_struct(const void *a, const void *b) { + const IndexedValue *ia = (const IndexedValue *)a; + const IndexedValue *ib = (const IndexedValue *)b; + if (ia->value < ib->value) return 1; + if (ia->value > ib->value) return -1; + return 0; +} + +void print_top_k(const char *name, double *pi, int n, int k) { + + IndexedValue *ivals = malloc(n * sizeof(IndexedValue)); + if (!ivals) { + fprintf(stderr, "malloc failed\n"); + return; + } + + for (int i = 0; i < n; i++) { + ivals[i].index = i; + ivals[i].value = pi[i]; + } + + qsort(ivals, n, sizeof(IndexedValue), cmp_desc_struct); + + printf("Top %d entries of %s:\n", k, name); + for (int i = 0; i < k && i < n; i++) { + printf(" %2d: node %d -> %.8f\n", i + 1, ivals[i].index, ivals[i].value); + } + + free(ivals); +} diff --git a/src/ranking_utils.h b/src/ranking_utils.h new file mode 100644 index 0000000..7a85530 --- /dev/null +++ b/src/ranking_utils.h @@ -0,0 +1,6 @@ +#ifndef RANKING_UTILS_H +#define RANKING_UTILS_H + +void print_top_k(const char *name, double *pi, int n, int k); + +#endif