int test_unnormalized_laplacian(igraph_bool_t dir) {
  igraph_t g;
  igraph_matrix_t m;
  igraph_vector_t vec;
  igraph_matrix_init(&m, 1, 1);

  /* No loop or multiple edges */
  igraph_ring(&g, 5, dir, 0, 1);
  igraph_laplacian(&g, &m, 0);
  print_matrix(&m);
  printf("===\n");

  /* Add some loop edges */
  igraph_vector_init_real(&vec, 4, 1.0, 1.0, 2.0, 2.0);
  igraph_add_edges(&g, &vec, 0);
  igraph_vector_destroy(&vec);

  igraph_laplacian(&g, &m, 0);
  print_matrix(&m);
  printf("===\n");

  /* Duplicate some edges */
  igraph_vector_init_real(&vec, 4, 1.0, 2.0, 3.0, 4.0);
  igraph_add_edges(&g, &vec, 0);
  igraph_vector_destroy(&vec);

  igraph_laplacian(&g, &m, 0);
  print_matrix(&m);

  igraph_destroy(&g);

  igraph_matrix_destroy(&m);

  return 0;
}
int test_normalized_laplacian(igraph_bool_t dir) {
  igraph_t g;
  igraph_matrix_t m;
  igraph_vector_t vec;
  igraph_matrix_init(&m, 1, 1);
  igraph_bool_t ok = 1;

  /* Undirected graph, no loop or multiple edges */
  igraph_ring(&g, 5, dir, 0, 1);
  igraph_laplacian(&g, &m, 1);
  ok = ok && check_laplacian(&g, &m);

  /* Add some loop edges */
  igraph_vector_init_real(&vec, 4, 1.0, 1.0, 2.0, 2.0);
  igraph_add_edges(&g, &vec, 0);
  igraph_vector_destroy(&vec);

  igraph_laplacian(&g, &m, 1);
  ok = ok && check_laplacian(&g, &m);

  /* Duplicate some edges */
  igraph_vector_init_real(&vec, 4, 1.0, 2.0, 3.0, 4.0);
  igraph_add_edges(&g, &vec, 0);
  igraph_vector_destroy(&vec);

  igraph_laplacian(&g, &m, 1);
  ok = ok && check_laplacian(&g, &m);

  igraph_destroy(&g);

  igraph_matrix_destroy(&m);

  if (ok)
    printf("OK\n");

  return !ok;
}
Exemplo n.º 3
0
int main() {

  int nodes=10;
  igraph_t g;
  igraph_matrix_t L, R;
  igraph_sparsemat_t Lsparse, Rsparse;
  igraph_matrix_t V;
  igraph_matrix_complex_t V2;
  igraph_sparsemat_t laplacian;
  igraph_vector_t groups;
  igraph_eigen_which_t which;

  igraph_matrix_init(&L, 0, 0);
  igraph_matrix_init(&R, 0, 0);
  igraph_matrix_init(&V, 0, 0);
  igraph_matrix_complex_init(&V2, 0, 0);
  igraph_vector_init(&groups, 0);
    
  igraph_rng_seed(igraph_rng_default(), 42);
  
  igraph_tree(&g, 10, /* children= */ 3, IGRAPH_TREE_UNDIRECTED);
  
  igraph_sparsemat_init(&laplacian, nodes, nodes, igraph_ecount(&g)*2);
  
  igraph_rng_seed(igraph_rng_default(), 42);

  igraph_laplacian(&g, /*res=*/ 0, /*sparseres=*/ &laplacian, 
		   /*normalized=*/ 0, /*weights=*/ 0);

  which.pos=IGRAPH_EIGEN_LM;
  which.howmany=1;

  igraph_eigen_matrix(/*matrix=*/ 0, &laplacian, /*fun=*/ 0, 10,
		      /*extra=*/ 0, /*algorithm=*/ IGRAPH_EIGEN_LAPACK,
		      &which, /*options=*/ 0, /*storage=*/ 0, 
		      /*values=*/ 0, &V2);
  igraph_matrix_complex_real(&V2, &V);

#define SEMI()								\
  do {									\
    igraph_scg_semiprojectors(&groups, IGRAPH_SCG_LAPLACIAN, &L, &R,	\
			      &Lsparse, &Rsparse, /*p=*/ 0,		\
			      IGRAPH_SCG_NORM_ROW);			\
  } while(0)

#define PRINTRES()				\
  do {						\
    printf("----------------------\n");		\
    igraph_matrix_print(&L);			\
    printf("---\n");				\
    igraph_matrix_print(&R);			\
    printf("---\n");				\
    igraph_sparsemat_destroy(&Lsparse);         \
    igraph_sparsemat_destroy(&Rsparse);         \
  } while (0)

  /* -------------- */

  igraph_scg_grouping(&V, &groups, /*intervals=*/ 3, 
		      /*intervals_vector=*/ 0, IGRAPH_SCG_LAPLACIAN,
		      IGRAPH_SCG_OPTIMUM, /*p=*/ 0, /*maxiter=*/ 10000);
  SEMI();
  PRINTRES();

  /* -------------- */

  igraph_scg_grouping(&V, &groups, /*intervals=*/ 2, 
		      /*intervals_vector=*/ 0, IGRAPH_SCG_LAPLACIAN,
		      IGRAPH_SCG_INTERV_KM, /*p=*/ 0, /*maxiter=*/ 10000);
  SEMI();
  PRINTRES();

  /* -------------- */

  igraph_scg_grouping(&V, &groups, /*intervals=*/ 2, 
		      /*intervals_vector=*/ 0, IGRAPH_SCG_LAPLACIAN,
		      IGRAPH_SCG_INTERV, /*p=*/ 0, /*maxiter=*/ 10000);
  SEMI();
  PRINTRES();

  /* -------------- */

  igraph_scg_grouping(&V, &groups, /*(ignored) intervals=*/ 0, 
		      /*intervals_vector=*/ 0, IGRAPH_SCG_LAPLACIAN,
		      IGRAPH_SCG_EXACT, /*p=*/ 0, /*maxiter=*/ 10000);
  SEMI();
  PRINTRES();

  /* -------------- */

  igraph_matrix_destroy(&L);
  igraph_matrix_destroy(&R);
  igraph_matrix_destroy(&V);
  igraph_matrix_complex_destroy(&V2);
  igraph_vector_destroy(&groups);
  igraph_sparsemat_destroy(&laplacian);
  igraph_destroy(&g);
  
  return 0;
}
Exemplo n.º 4
0
int main() {

  const int nodes=10;
  igraph_t g;
  igraph_matrix_t V;
  igraph_matrix_complex_t V2;
  igraph_sparsemat_t laplacian;
  igraph_vector_t groups;
  igraph_eigen_which_t which;

  igraph_tree(&g, nodes, /* children= */ 3, IGRAPH_TREE_UNDIRECTED);

  igraph_sparsemat_init(&laplacian, nodes, nodes, igraph_ecount(&g)*2);
  igraph_matrix_complex_init(&V2, 0, 0);
  igraph_matrix_init(&V, 0, 0);
  igraph_vector_init(&groups, 0);
  
  igraph_rng_seed(igraph_rng_default(), 42);

  igraph_sparsemat_init(&laplacian, 0, 0, 0);
  igraph_laplacian(&g, /*res=*/ 0, /*sparseres=*/ &laplacian, 
		   /*normalized=*/ 0, /*weights=*/ 0);

  which.pos=IGRAPH_EIGEN_LR;
  which.howmany=1;

  igraph_eigen_matrix(/*matrix=*/ 0, &laplacian, /*fun=*/ 0, nodes,
		      /*extra=*/ 0, /*algorithm=*/ IGRAPH_EIGEN_LAPACK,
		      &which, /*options=*/ 0, /*storage=*/ 0, 
		      /*values=*/ 0, &V2);
  igraph_matrix_complex_real(&V2, &V);

  /* ------------ */
  
  igraph_scg_grouping(&V, &groups, /*intervals=*/ 3, 
		      /*intervals_vector=*/ 0, IGRAPH_SCG_LAPLACIAN,
		      IGRAPH_SCG_OPTIMUM, /*p=*/ 0, /*maxiter=*/ 10000);
  igraph_vector_print(&groups);

  /* ------------ */

  igraph_scg_grouping(&V, &groups, /*intervals=*/ 3, 
		      /*intervals_vector=*/ 0, IGRAPH_SCG_LAPLACIAN,
		      IGRAPH_SCG_INTERV_KM, /*p=*/ 0, /*maxiter=*/ 10000);
  igraph_vector_print(&groups);

  /* ------------ */

  igraph_scg_grouping(&V, &groups, /*intervals=*/ 3, 
		      /*intervals_vector=*/ 0, IGRAPH_SCG_LAPLACIAN,
		      IGRAPH_SCG_INTERV, /*p=*/ 0, /*maxiter=*/ 10000);
  igraph_vector_print(&groups);

  /* ------------ */

  igraph_scg_grouping(&V, &groups, /*(ignored) intervals=*/ 0, 
		      /*intervals_vector=*/ 0, IGRAPH_SCG_LAPLACIAN,
		      IGRAPH_SCG_EXACT, /*p=*/ 0, /*maxiter=*/ 10000);
  igraph_vector_print(&groups);

  /* ------------ */  

  igraph_vector_destroy(&groups);
  igraph_matrix_destroy(&V);
  igraph_matrix_complex_destroy(&V2);
  igraph_sparsemat_destroy(&laplacian);
  igraph_destroy(&g);

#ifdef __APPLE__
  return 0;
#else
  return 77;
#endif
}
Exemplo n.º 5
0
int test_unnormalized_laplacian(igraph_vector_t* w, igraph_bool_t dir) {
  igraph_t g;
  igraph_matrix_t m, m2;
  igraph_sparsemat_t sm;
  igraph_vector_t vec, *weights = 0;
  igraph_matrix_init(&m, 1, 1);
  igraph_sparsemat_init(&sm, 0, 0, 0);

  if (w) {
    weights = (igraph_vector_t*)calloc(1, sizeof(igraph_vector_t));
    igraph_vector_copy(weights, w);
  }

  /* No loop or multiple edges */
  igraph_ring(&g, 5, dir, 0, 1);
  igraph_laplacian(&g, &m, &sm, 0, weights);
  igraph_matrix_init(&m2, 0, 0);
  igraph_sparsemat_as_matrix(&m2, &sm);
  if (!igraph_matrix_all_e_tol(&m, &m2, 0)) { 
    return 41;
  }
  igraph_matrix_destroy(&m2);
  igraph_matrix_print(&m);
  printf("===\n");

  /* Add some loop edges */
  igraph_vector_init_real(&vec, 4, 1.0, 1.0, 2.0, 2.0);
  igraph_add_edges(&g, &vec, 0);
  igraph_vector_destroy(&vec);
  if (weights) {
    igraph_vector_push_back(weights, 2);
    igraph_vector_push_back(weights, 2);
  }

  igraph_laplacian(&g, &m, &sm, 0, weights);
  igraph_matrix_init(&m2, 0, 0);
  igraph_sparsemat_as_matrix(&m2, &sm);
  if (!igraph_matrix_all_e_tol(&m, &m2, 0)) { 
    return 42;
  }
  igraph_matrix_destroy(&m2);
  igraph_matrix_print(&m);
  printf("===\n");

  /* Duplicate some edges */
  igraph_vector_init_real(&vec, 4, 1.0, 2.0, 3.0, 4.0);
  igraph_add_edges(&g, &vec, 0);
  igraph_vector_destroy(&vec);
  if (weights) {
    igraph_vector_push_back(weights, 3);
    igraph_vector_push_back(weights, 3);
  }

  igraph_laplacian(&g, &m, &sm, 0, weights);
  igraph_matrix_init(&m2, 0, 0);
  igraph_sparsemat_as_matrix(&m2, &sm);
  if (!igraph_matrix_all_e_tol(&m, &m2, 0)) { 
    return 43;
  }
  igraph_matrix_destroy(&m2);
  igraph_matrix_print(&m);

  igraph_destroy(&g);

  igraph_matrix_destroy(&m);
  if (weights) {
    igraph_vector_destroy(weights); free(weights);
  }

  return 0;
}
Exemplo n.º 6
0
int test_normalized_laplacian(igraph_vector_t *w, igraph_bool_t dir) {
  igraph_t g;
  igraph_matrix_t m, m2;
  igraph_sparsemat_t sm;
  igraph_vector_t vec, *weights = 0;
  igraph_bool_t ok = 1;
  igraph_matrix_init(&m, 1, 1);
  igraph_sparsemat_init(&sm, 0, 0, 0);

  if (w) {
    weights = (igraph_vector_t*)calloc(1, sizeof(igraph_vector_t));
    igraph_vector_copy(weights, w);
  }

  /* Undirected graph, no loop or multiple edges */
  igraph_ring(&g, 5, dir, 0, 1);
  igraph_laplacian(&g, &m, &sm, 1, weights);
  igraph_matrix_init(&m2, 0, 0);
  igraph_sparsemat_as_matrix(&m2, &sm);
  if (!igraph_matrix_all_e_tol(&m, &m2, 0)) { 
    return 44;
  }
  igraph_matrix_destroy(&m2);
  ok = ok && check_laplacian(&g, &m, weights);

  /* Add some loop edges */
  igraph_vector_init_real(&vec, 4, 1.0, 1.0, 2.0, 2.0);
  igraph_add_edges(&g, &vec, 0);
  igraph_vector_destroy(&vec);
  if (weights) {
    igraph_vector_push_back(weights, 2);
    igraph_vector_push_back(weights, 2);
  }

  igraph_laplacian(&g, &m, &sm, 1, weights);
  igraph_matrix_init(&m2, 0, 0);
  igraph_sparsemat_as_matrix(&m2, &sm);
  if (!igraph_matrix_all_e_tol(&m, &m2, 0)) { 
    return 45;
  }
  igraph_matrix_destroy(&m2);
  ok = ok && check_laplacian(&g, &m, weights);

  /* Duplicate some edges */
  igraph_vector_init_real(&vec, 4, 1.0, 2.0, 3.0, 4.0);
  igraph_add_edges(&g, &vec, 0);
  igraph_vector_destroy(&vec);
  if (weights) {
    igraph_vector_push_back(weights, 3);
    igraph_vector_push_back(weights, 3);
  }

  igraph_laplacian(&g, &m, &sm, 1, weights);
  igraph_matrix_init(&m2, 0, 0);
  igraph_sparsemat_as_matrix(&m2, &sm);
  if (!igraph_matrix_all_e_tol(&m, &m2, 0)) { 
    return 46;
  }
  igraph_matrix_destroy(&m2);
  ok = ok && check_laplacian(&g, &m, weights);

  igraph_destroy(&g);

  igraph_matrix_destroy(&m);
  if (weights) {
    igraph_vector_destroy(weights); free(weights);
  }

  if (ok)
    printf("OK\n");

  return !ok;
}