60 lines
1.5 KiB
C
60 lines
1.5 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include "matrix_operation.h"
|
|
#include "sparse_matrix.h"
|
|
#include "read_from_mtx.h"
|
|
#include "vector.h"
|
|
#include "power_algorithm.h"
|
|
#include "gauss_seidel.h"
|
|
#include "time_helper.h"
|
|
#include <sys/time.h>
|
|
|
|
void test_stationary_distribution(const char *path) {
|
|
struct timeval tvstart, tvend;
|
|
|
|
// Read and prepare the matrix
|
|
gettimeofday(&tvstart, NULL);
|
|
SparseMatrix *matrix = read_sparse_matrix_from_mtx(path);
|
|
convert_to_stochastic(matrix);
|
|
gettimeofday(&tvend, NULL);
|
|
print_time_diff("Read and convert matrix", &tvstart, &tvend);
|
|
|
|
double epsilon = 1e-10;
|
|
double alpha = 0.8;
|
|
|
|
// pagerank
|
|
gettimeofday(&tvstart, NULL);
|
|
double *pi_power = pagerank(matrix, epsilon, alpha);
|
|
gettimeofday(&tvend, NULL);
|
|
print_time_diff("Pagerank", &tvstart, &tvend);
|
|
|
|
// nable delta
|
|
gettimeofday(&tvstart, NULL);
|
|
double *pi_new = gauss_seidel_pagerank(matrix, epsilon, alpha);
|
|
|
|
gettimeofday(&tvend, NULL);
|
|
print_time_diff("Gauss Seidel algorithm", &tvstart, &tvend);
|
|
|
|
// Compare results
|
|
double diff = diff_norm_vector(pi_power, pi_new, matrix->num_nodes);
|
|
printf("Difference between power method and new algorithm: %.16f\n", diff);
|
|
|
|
|
|
for (int i = 0; i < 10; ++i) {
|
|
printf("pi_old: %.16f\n", pi_power[i]);
|
|
printf("pi_new: %.16f\n", pi_new[i]);
|
|
printf("\n");
|
|
}
|
|
|
|
|
|
// Clean up
|
|
free(pi_power);
|
|
free(pi_new);
|
|
free_sparse_matrix(matrix);
|
|
}
|
|
|
|
int main() {
|
|
test_stationary_distribution("./out/input.mtx");
|
|
return 0;
|
|
}
|