crappy but working version
This commit is contained in:
parent
ff4c21dbd9
commit
59fc6988d6
@ -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) {
|
||||
|
36
src/main.c
36
src/main.c
@ -10,6 +10,37 @@
|
||||
#include "time_helper.h"
|
||||
#include <sys/time.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) {
|
||||
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);
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user