crappy but working version

This commit is contained in:
Sam Hadow 2025-05-16 19:06:12 +02:00
parent ff4c21dbd9
commit 59fc6988d6
6 changed files with 63 additions and 27 deletions

View File

@ -49,8 +49,8 @@ double* gauss_seidel_pagerank(const SparseMatrix *matrix, double epsilon, double
for (int i = N - 1; i >= 0; i--) { for (int i = N - 1; i >= 0; i--) {
double result_mult = 0.0; double result_mult = 0.0;
double M_ii = 0.0; double M_ii = 0.0;
for (int k = matrix->row_ptr[i]; k < matrix->row_ptr[i + 1]; k++) { for (int k = matrix->col_ptr[i]; k < matrix->col_ptr[i + 1]; k++) {
int j = matrix->arcs[k].dest; int j = matrix->arcs[k].origin;
double value = matrix->arcs[k].value; double value = matrix->arcs[k].value;
if (j == i) { if (j == i) {

View File

@ -10,6 +10,37 @@
#include "time_helper.h" #include "time_helper.h"
#include <sys/time.h> #include <sys/time.h>
#include <string.h> #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);
}
void test_stationary_distribution(const char *path, double epsilon, double alpha) { void test_stationary_distribution(const char *path, double epsilon, double alpha) {
struct timeval tvstart, tvend; 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); 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 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 (GaussSeidel)", pi_new, n, k);
free(pi_power); free(pi_power);
free(pi_new); free(pi_new);
free_sparse_matrix(matrix); free_sparse_matrix(matrix);

View File

@ -53,14 +53,14 @@ void convert_to_stochastic(SparseMatrix *matrix) {
// count non zero values // count non zero values
for (int i = 0; i < matrix->num_arcs; i++) { 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 // normalize values
for (int i = 0; i < matrix->num_arcs; i++) { 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; 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); free(non_zero);
@ -69,6 +69,6 @@ void convert_to_stochastic(SparseMatrix *matrix) {
int compare_arcs(const void *a, const void *b) { int compare_arcs(const void *a, const void *b) {
Arc *arc_a = (Arc *)a; Arc *arc_a = (Arc *)a;
Arc *arc_b = (Arc *)b; Arc *arc_b = (Arc *)b;
return arc_a->origin - arc_b->origin; return arc_a->dest - arc_b->dest;
} }

View File

@ -67,20 +67,20 @@ SparseMatrix* read_sparse_matrix_from_mtx(const char *filename) {
parse_arcs(matrix, file); parse_arcs(matrix, file);
// Sort arcs by origin // Sort arcs by destination
qsort(matrix->arcs, matrix->num_arcs, sizeof(Arc), compare_arcs); qsort(matrix->arcs, matrix->num_arcs, sizeof(Arc), compare_arcs);
// Build row_ptr // Build col_ptr
matrix->row_ptr = (int *)malloc((matrix->num_nodes + 1) * sizeof(int)); matrix->col_ptr = (int *)malloc((matrix->num_nodes + 1) * sizeof(int));
int current_origin = 0; int current_dest = 0;
matrix->row_ptr[0] = 0; matrix->col_ptr[0] = 0;
for (int k = 0; k < matrix->num_arcs; k++) { for (int k = 0; k < matrix->num_arcs; k++) {
while (matrix->arcs[k].origin > current_origin) { while (matrix->arcs[k].dest > current_dest) {
current_origin++; current_dest++;
matrix->row_ptr[current_origin] = k; matrix->col_ptr[current_dest] = k;
} }
} }
for (int i = current_origin + 1; i <= matrix->num_nodes; i++) { for (int i = current_dest + 1; i <= matrix->num_nodes; i++) {
matrix->row_ptr[i] = matrix->num_arcs; matrix->col_ptr[i] = matrix->num_arcs;
} }
fclose(file); fclose(file);

View File

@ -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); qsort(matrix->arcs, matrix->num_arcs, sizeof(Arc), compare_arcs);
// Build row_ptr // Build col_ptr
matrix->row_ptr = (int *)malloc((matrix->num_nodes + 1) * sizeof(int)); matrix->col_ptr = (int *)malloc((matrix->num_nodes + 1) * sizeof(int));
int current_origin = 0; int current_destination = 0;
matrix->row_ptr[0] = 0; matrix->col_ptr[0] = 0;
for (int k = 0; k < matrix->num_arcs; k++) { for (int k = 0; k < matrix->num_arcs; k++) {
while (matrix->arcs[k].origin > current_origin) { while (matrix->arcs[k].dest > current_destination) {
current_origin++; current_destination++;
matrix->row_ptr[current_origin] = k; matrix->col_ptr[current_destination] = k;
} }
} }
for (int i = current_origin + 1; i <= matrix->num_nodes; i++) { for (int i = current_destination + 1; i <= matrix->num_nodes; i++) {
matrix->row_ptr[i] = matrix->num_arcs; matrix->col_ptr[i] = matrix->num_arcs;
} }
free(col_ptr); free(col_ptr);

View File

@ -11,7 +11,7 @@ typedef struct {
int num_nodes; int num_nodes;
int num_arcs; int num_arcs;
Arc *arcs; Arc *arcs;
int *row_ptr; int *col_ptr;
} SparseMatrix; } SparseMatrix;
void free_sparse_matrix(SparseMatrix *matrix); void free_sparse_matrix(SparseMatrix *matrix);