clean up code
This commit is contained in:
parent
59fc6988d6
commit
bd4e30cdfd
10
main.py
10
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
|
||||
|
@ -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);
|
||||
|
37
src/main.c
37
src/main.c
@ -12,34 +12,7 @@
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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);
|
||||
|
@ -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);
|
||||
|
40
src/ranking_utils.c
Normal file
40
src/ranking_utils.c
Normal file
@ -0,0 +1,40 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#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);
|
||||
}
|
6
src/ranking_utils.h
Normal file
6
src/ranking_utils.h
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user