int main() {
  
  igraph_t g;
  igraph_integer_t result;
  igraph_vector_t edges, res;
  igraph_vs_t vids;
  long i, n;
  
  igraph_tree(&g, 10, 3, IGRAPH_TREE_OUT);
  igraph_rewire(&g, 1000, IGRAPH_REWIRING_SIMPLE);
  
  n=igraph_vcount(&g);
  igraph_vector_init(&res, 0);
  igraph_degree(&g, &res, igraph_vss_all(), IGRAPH_IN, 0);
  for (i=0; i<n; i++)
    printf("%ld ", (long)igraph_vector_e(&res, i));
  printf("\n");
  
  igraph_degree(&g, &res, igraph_vss_all(), IGRAPH_OUT, 0);
  for (i=0; i<n; i++)
    printf("%ld ", (long)igraph_vector_e(&res, i));
  printf("\n");
    
  igraph_destroy(&g);
  igraph_vector_destroy(&res);
  
  return 0;
}
예제 #2
0
int main() {

  igraph_t g;
  igraph_vector_t vids, layers, parents;

  igraph_ring(&g, 10, IGRAPH_UNDIRECTED, 0, 0);
  igraph_vector_init(&vids, 0);
  igraph_vector_init(&layers, 0);
  igraph_vector_init(&parents, 0);
  igraph_i_bfs(&g, 0, IGRAPH_ALL, &vids, &layers, &parents);
  vector_print(&vids);
  vector_print(&layers);
  vector_print(&parents);
  igraph_destroy(&g);  

  igraph_tree(&g, 20, 2, IGRAPH_TREE_UNDIRECTED);
  igraph_i_bfs(&g, 0, IGRAPH_ALL, &vids, &layers, &parents);
  vector_print(&vids);
  vector_print(&layers);
  vector_print(&parents);
  igraph_destroy(&g);  
  
  igraph_vector_destroy(&vids);
  igraph_vector_destroy(&layers);
  igraph_vector_destroy(&parents);

  return 0;
}
예제 #3
0
int main() {
  
  igraph_t g;
  igraph_vector_ptr_t result;
  igraph_es_t es;
  igraph_integer_t omega;
  long int i, j, n;
  const int params[] = {4, -1, 2, 2, 0, 0, -1, -1};
 
  igraph_set_warning_handler(warning_handler_ignore);

  igraph_vector_ptr_init(&result, 0);
  igraph_full(&g, 6, 0, 0);
  igraph_es_pairs_small(&es, 0, 0, 1, 0, 2, 3, 5, -1);
  igraph_delete_edges(&g, es);
  igraph_es_destroy(&es);
  
  for (j=0; j<sizeof(params)/(2*sizeof(params[0])); j++) {
    if (params[2*j+1] != 0) {
      igraph_cliques(&g, &result, params[2*j], params[2*j+1]);  
    } else {
      igraph_largest_cliques(&g, &result);
    }
    n = igraph_vector_ptr_size(&result);
    printf("%ld cliques found\n", (long)n);
    canonicalize_list(&result);
    for (i=0; i<n; i++) {
      igraph_vector_t* v = (igraph_vector_t*) igraph_vector_ptr_e(&result,i);
      print_vector(v);
      igraph_vector_destroy(v);
      free(v);
    }
  }
   
  igraph_clique_number(&g, &omega);
  printf("omega=%ld\n", (long)omega);

  test_callback(&g);

  igraph_destroy(&g);

  igraph_tree(&g, 5, 2, IGRAPH_TREE_OUT);
  igraph_cliques(&g, &result, 5, 5);
  if (igraph_vector_ptr_size(&result) != 0) return 1;

  igraph_destroy(&g);
  igraph_vector_ptr_destroy(&result);

  return 0;
}
예제 #4
0
int main() {
  igraph_t graph;
  igraph_t full, tree;
  igraph_hrg_t hrg;
  igraph_t dendrogram;
  // int i, j;
  // igraph_vector_t neis;

  igraph_rng_seed(igraph_rng_default(), 42);

  // We need attributes
  igraph_i_set_attribute_table(&igraph_cattribute_table);
  
  igraph_full(&full, 10, /*directed=*/ 0, /*loops=*/ 0);
  igraph_tree(&tree, 15, /*children=*/ 2, /*type=*/ IGRAPH_TREE_UNDIRECTED);
  igraph_disjoint_union(&graph, &full, &tree);
  igraph_add_edge(&graph, 0, 10);
  
  igraph_destroy(&full);
  igraph_destroy(&tree);

  // Fit
  igraph_hrg_init(&hrg, igraph_vcount(&graph));
  igraph_hrg_fit(&graph, &hrg, /*start=*/ 0, /*steps=*/ 0);

  // Create a graph from it
  igraph_hrg_dendrogram(&dendrogram, &hrg);

  // Print the tree, with labels
  // igraph_vector_init(&neis, 0);
  // for (i=0; i<igraph_vcount(&graph)-1; i++) {
  //   printf("Vertex # %2i, ", (int) (i+igraph_vcount(&graph)));
  //   igraph_neighbors(&dendrogram, &neis, i+igraph_vcount(&graph), IGRAPH_OUT);
  //   printf("left: # %2i, right: # %2i, ", (int) VECTOR(neis)[0], 
  // 	   (int) VECTOR(neis)[1]);
  //   printf("prob: %6.2g\n", 
  // 	   VAN(&dendrogram, "probability", i+igraph_vcount(&graph)));
  // }
  // igraph_vector_destroy(&neis);

  igraph_destroy(&dendrogram);
  igraph_hrg_destroy(&hrg);
  igraph_destroy(&graph);

  return 0;
}
예제 #5
0
int main() {
  
  igraph_t graph;
  igraph_vector_t order, order_out, father, dist;
  long int i,n,c;
  scanf("%li %li",&n,&c);
  fp=fopen("qwe.txt","w+");
  fclose(fp);
  igraph_tree(&graph,(igraph_integer_t) n,(igraph_integer_t) 3, IGRAPH_TREE_OUT);
  fp=fopen("qwe.txt","a+");
  igraph_vector_init(&order, 0);
  igraph_vector_init(&order_out, 0);
  igraph_vector_init(&father, 0);
  igraph_vector_init(&dist, 0);

 
  /* Test the callback */

  for(i=0;i<10;i++)
  {	printf("Iteration starts here\n");
  	dfs_d(&graph, /*root=*/ 0, /*neimode=*/ IGRAPH_ALL, 
	     /*unreachable=*/ 1,
	     &order, 0, 0, &dist, &dfs_callback, &dfs_callback, 0);
  	fprintf(fp,"\n");
	igraph_vector_print(&order);
	igraph_vector_print(&dist);
  }
  
  //printf("\n");
  igraph_destroy(&graph);
   igraph_vector_destroy(&order);
  igraph_vector_destroy(&order_out);
  igraph_vector_destroy(&father);
  igraph_vector_destroy(&dist);
  fclose(fp);
  
  return 0;
}
int main() {

  igraph_t g;
  igraph_matrix_t L, R;
  igraph_sparsemat_t Lsparse, Rsparse;
  igraph_matrix_t adj, V;
  igraph_vector_t groups;
  igraph_eigen_which_t which;

  igraph_matrix_init(&L, 0, 0);
  igraph_matrix_init(&R, 0, 0);
  igraph_matrix_init(&adj, 0, 0);
  igraph_matrix_init(&V, 0, 0);
  igraph_vector_init(&groups, 0);
    
  igraph_rng_seed(igraph_rng_default(), 42);
  
  igraph_tree(&g, 10, /* children= */ 3, IGRAPH_TREE_UNDIRECTED);
  
  igraph_get_adjacency(&g, &adj, IGRAPH_GET_ADJACENCY_BOTH, /*eids=*/ 0);

  which.pos=IGRAPH_EIGEN_LM;
  which.howmany=1;
  igraph_eigen_matrix_symmetric(&adj, /*sparsemat=*/ 0, /*fun=*/ 0,
				igraph_vcount(&g), /*extra=*/ 0, 
				/*algorithm=*/ IGRAPH_EIGEN_LAPACK,
				&which, /*options=*/ 0, /*storage=*/ 0, 
				/*values=*/ 0, &V);

#define SEMI()								\
  do {									\
    igraph_scg_semiprojectors(&groups, IGRAPH_SCG_SYMMETRIC, &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");				\
  } while (0)

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

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

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

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

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

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

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

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

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

  igraph_vector_destroy(&groups);
  igraph_matrix_destroy(&V);
  igraph_matrix_destroy(&adj);
  igraph_destroy(&g);
  
  return 0;
}
예제 #7
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;
}
예제 #8
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
}
예제 #9
0
int main() {
  
  igraph_t g;
  igraph_vector_t bet, bet2, weights, edges;
  igraph_vector_t bbet, bbet2;
  
  igraph_real_t nontriv[] = { 0, 19, 0, 16, 0, 20, 1, 19, 2, 5, 3, 7, 3, 8, 
			      4, 15, 4, 11, 5, 8, 5, 19, 6, 7, 6, 10, 6, 8, 
			      6, 9, 7, 20, 9, 10, 9, 20, 10, 19, 
			      11, 12, 11, 20, 12, 15, 13, 15, 
			      14, 18, 14, 16, 14, 17, 15, 16, 17, 18 };
  
  igraph_real_t nontriv_weights[] = { 0.5249, 1, 0.1934, 0.6274, 0.5249, 
				      0.0029, 0.3831, 0.05, 0.6274, 0.3831, 
				      0.5249, 0.0587, 0.0579, 0.0562, 0.0562, 
				      0.1934, 0.6274, 0.6274, 0.6274, 0.0418, 
				      0.6274, 0.3511, 0.3511, 0.1486, 1, 1, 
				      0.0711, 0.2409 };

  igraph_real_t nontriv_res[] = { 20, 0, 0, 0, 0, 19, 80, 85, 32, 0, 10, 
				  75, 70, 0, 36, 81, 60, 0, 19, 19, 86 };

  /*******************************************************/

  igraph_barabasi_game(/* graph= */    &g,
		       /* n= */        1000,
		       /* power= */    1,
		       /* m= */        3,
		       /* outseq= */   0,
		       /* outpref= */  0,
		       /* A= */        1,
		       /* directed= */ 0, 
		       /* algo= */     IGRAPH_BARABASI_BAG,
		       /* start_from= */ 0);
  
  igraph_simplify(&g, /* multiple= */ 1, /* loops= */ 1, /*edge_comb=*/ 0);
  
  igraph_vector_init(&bet, 0);
  igraph_vector_init(&bbet, 0);
  
  igraph_betweenness_estimate(/* graph=     */ &g,
			      /* res=       */ &bet,
			      /* vids=      */ igraph_vss_all(),
			      /* directed = */ 0,
			      /* cutoff=    */ 2,
			      /* weights=   */ 0, 
			      /* nobigint=  */ 1);

  igraph_betweenness_estimate(/* graph=     */ &g,
			      /* res=       */ &bbet,
			      /* vids=      */ igraph_vss_all(),
			      /* directed = */ 0,
			      /* cutoff=    */ 2,
			      /* weights=   */ 0, 
			      /* nobigint=  */ 0);  

  check(&bet, &bbet, 10);

  igraph_vector_destroy(&bet);
  igraph_vector_destroy(&bbet);
  igraph_destroy(&g);

  /*******************************************************/

  igraph_tree(&g, 20000, 10, IGRAPH_TREE_UNDIRECTED);
  
  igraph_vector_init(&bet, 0);
  igraph_vector_init(&bbet, 0);
  
  igraph_betweenness_estimate(/* graph=     */ &g,
			      /* res=       */ &bet,
			      /* vids=      */ igraph_vss_all(),
			      /* directed = */ 0,
			      /* cutoff=    */ 3,
			      /* weights=   */ 0, 
			      /* nobigint=  */ 1);

  igraph_betweenness_estimate(/* graph=     */ &g,
			      /* res=       */ &bbet,
			      /* vids=      */ igraph_vss_all(),
			      /* directed = */ 0,
			      /* cutoff=    */ 3,
			      /* weights=   */ 0, 
			      /* nobigint=  */ 0);

  check(&bet, &bbet, 20);

  igraph_vector_init(&bet2, 0);
  igraph_vector_init(&bbet2, 0);
  igraph_vector_init(&weights, igraph_ecount(&g));
  igraph_vector_fill(&weights, 1.0);
  
  igraph_betweenness_estimate(/* graph=     */ &g,
			      /* res=       */ &bet2,
			      /* vids=      */ igraph_vss_all(),
			      /* directed = */ 0,
			      /* cutoff=    */ 3,
			      /* weights=   */ &weights, 
			      /* nobigint=  */ 1);

  igraph_betweenness_estimate(/* graph=     */ &g,
			      /* res=       */ &bbet2,
			      /* vids=      */ igraph_vss_all(),
			      /* directed = */ 0,
			      /* cutoff=    */ 3,
			      /* weights=   */ &weights, 
			      /* nobigint=  */ 0);

  if (!igraph_vector_all_e(&bet, &bet2)) {
    return 1;
  }

/*   if (!igraph_vector_all_e(&bbet, &bbet2)) { */
/*     return 2; */
/*   } */

  check(&bet, &bbet, 30);
  check(&bet2, &bbet2, 40);

  igraph_vector_destroy(&bet);
  igraph_vector_destroy(&bet2);
  igraph_vector_destroy(&bbet);
  igraph_vector_destroy(&bbet2);
  igraph_vector_destroy(&weights);
  igraph_destroy(&g);

  /* Non-trivial weighted graph */
  igraph_vector_view(&edges, nontriv, sizeof(nontriv)/sizeof(igraph_real_t));
  igraph_create(&g, &edges, 0, /* directed= */ 0);
  igraph_vector_view(&weights, nontriv_weights, 
		     sizeof(nontriv_weights)/sizeof(igraph_real_t));
  igraph_vector_init(&bet, 0);
  igraph_vector_init(&bbet, 0);

  igraph_betweenness(/*graph=*/ &g, /*res=*/ &bet, /*vids=*/ igraph_vss_all(), 
		     /*directed=*/0, /*weights=*/ &weights, /*nobigint=*/ 1);

  igraph_betweenness(/*graph=*/ &g, /*res=*/ &bbet, /*vids=*/ igraph_vss_all(), 
		     /*directed=*/0, /*weights=*/ &weights, /*nobigint=*/ 0);

  igraph_vector_view(&bet2, nontriv_res, 
		     sizeof(nontriv_res)/sizeof(igraph_real_t));

  if (!igraph_vector_all_e(&bet, &bet2)) {
    return 2;
  }

  check(&bet, &bbet, 50);
  
  igraph_vector_destroy(&bet);
  igraph_vector_destroy(&bbet);
  igraph_destroy(&g);

  if (IGRAPH_FINALLY_STACK_SIZE() != 0) return 3;

  return 0;
}
int main() {

  int nodes=10;
  igraph_t g;
  igraph_matrix_t L, R;
  igraph_sparsemat_t Lsparse, Rsparse;
  igraph_matrix_t V, V3;
  igraph_matrix_complex_t V2;
  igraph_sparsemat_t stochastic, stochasticT;
  igraph_vector_t groups;
  igraph_eigen_which_t which;
  igraph_vector_t p, selcol;

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

  igraph_get_stochastic_sparsemat(&g, &stochastic, /*column-wise=*/ 0);
  igraph_sparsemat_transpose(&stochastic, &stochasticT, /*values=*/ 1);

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

  igraph_eigen_matrix(/*matrix=*/ 0, &stochasticT, /*fun=*/ 0, 10,
		      /*extra=*/ 0, /*algorithm=*/ IGRAPH_EIGEN_LAPACK,
		      &which, /*options=*/ 0, /*storage=*/ 0, 
		      /*values=*/ 0, &V2);
  igraph_matrix_complex_real(&V2, &V);
  /* `p' is always the eigenvector corresponding to the 1-eigenvalue */
  igraph_matrix_get_col(&V, &p, 0);

  which.howmany=3;
  igraph_eigen_matrix(/*matrix=*/ 0, &stochastic, /*fun=*/ 0, 10,
		      /*extra=*/ 0, /*algorithm=*/ IGRAPH_EIGEN_LAPACK,
		      &which, /*options=*/ 0, /*storage=*/ 0, 
		      /*values=*/ 0, &V2);
  igraph_matrix_complex_real(&V2, &V3);
  VECTOR(selcol)[0]=2;
  igraph_matrix_select_cols(&V3, &V, &selcol);

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

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

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

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

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

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

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

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

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

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

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

  igraph_vector_destroy(&p);
  igraph_vector_destroy(&selcol);
  igraph_vector_destroy(&groups);
  igraph_matrix_destroy(&L);
  igraph_matrix_destroy(&R);
  igraph_matrix_destroy(&V);
  igraph_matrix_destroy(&V3);
  igraph_matrix_complex_destroy(&V2);
  igraph_sparsemat_destroy(&stochasticT);
  igraph_sparsemat_destroy(&stochastic);
  igraph_destroy(&g);
  
#ifdef __APPLE__
  return 0;
#else
  return 77;
#endif
}
예제 #11
0
파일: scg3.c 프로젝트: igraph/igraph
int main() {

  igraph_t g;
  igraph_vector_t ev;
  igraph_t scg_graph;
  igraph_matrix_t scg_matrix;
  igraph_sparsemat_t scg_sparsemat;
  igraph_matrix_t L, R;
  igraph_sparsemat_t Lsparse, Rsparse;
  igraph_vector_t groups;
  igraph_vector_complex_t eval;
  igraph_matrix_complex_t evec;
  
  igraph_tree(&g, 10, /* children= */ 3, IGRAPH_TREE_UNDIRECTED);
  
  igraph_vector_init(&ev, 1);
  igraph_matrix_init(&L, 0, 0);
  igraph_matrix_init(&R, 0, 0);
  igraph_matrix_init(&scg_matrix, 0, 0);
  igraph_vector_init(&groups, 0);
  igraph_vector_complex_init(&eval, 0);
  igraph_matrix_complex_init(&evec, 0, 0);

#define CALLLAP() do {							 \
    igraph_vector_resize(&groups, 0);					 \
    igraph_vector_complex_resize(&eval, 0);				 \
    igraph_matrix_complex_resize(&evec, 0, 0);				 \
    igraph_scg_laplacian(&g, /*matrix=*/ 0, /*sparsemat=*/ 0, &ev,	 \
			 /* intervals= */ 2, /* intervals_vector= */ 0, \
			 /* algorithm= */ IGRAPH_SCG_EXACT,		\
			 IGRAPH_SCG_NORM_ROW,				\
			 IGRAPH_SCG_DIRECTION_DEFAULT, &eval, &evec,	\
			 &groups, /* use_arpack= */ 0,			\
			 /* maxiter= */ 0, &scg_graph, &scg_matrix,	\
			 &scg_sparsemat, &L, &R,			\
			 &Lsparse, &Rsparse);				\
} while (0)

#define PRINTRES()						\
  do {								\
    printf("--------------------------------\n");		\
    igraph_vector_print(&groups);				\
    printf("---\n");						\
    igraph_vector_complex_print(&eval);				\
    igraph_matrix_complex_print(&evec);				\
    printf("---\n");						\
    igraph_write_graph_edgelist(&scg_graph, stdout);		\
    printf("---\n");						\
    igraph_sparsemat_print(&scg_sparsemat, stdout);		\
    printf("---\n");						\
    igraph_sparsemat_print(&Lsparse, stdout);			\
    printf("---\n");						\
    igraph_sparsemat_print(&Rsparse, stdout);			\
    printf("---\n");						\
  } while (0)

  VECTOR(ev)[0] = 1;
  CALLLAP();
  PRINTRES();
  igraph_destroy(&scg_graph);
  igraph_sparsemat_destroy(&scg_sparsemat);
  igraph_sparsemat_destroy(&Lsparse);
  igraph_sparsemat_destroy(&Rsparse);

  VECTOR(ev)[0] = 3;
  CALLLAP();
  PRINTRES();
  igraph_destroy(&scg_graph);
  igraph_sparsemat_destroy(&scg_sparsemat);
  igraph_sparsemat_destroy(&Lsparse);
  igraph_sparsemat_destroy(&Rsparse);

  igraph_vector_resize(&ev, 2);
  VECTOR(ev)[0] = 1; VECTOR(ev)[1] = 3;
  CALLLAP();
  PRINTRES();
  igraph_destroy(&scg_graph);
  igraph_sparsemat_destroy(&scg_sparsemat);
  igraph_sparsemat_destroy(&Lsparse);
  igraph_sparsemat_destroy(&Rsparse);

  igraph_matrix_complex_destroy(&evec);
  igraph_vector_complex_destroy(&eval);
  igraph_vector_destroy(&groups);
  igraph_matrix_destroy(&scg_matrix);
  igraph_matrix_destroy(&L);
  igraph_matrix_destroy(&R);
  igraph_vector_destroy(&ev);
  igraph_destroy(&g);

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

  return 0;
}
예제 #12
0
int main() {
  float dist[8][8] = {
	  {0.00, 4.69, 6.79, 3.50, 3.11, 4.46, 5.57, 3.00},
	  {4.69, 0.00, 2.10, 2.27, 2.65, 2.36, 1.99, 1.74},
	  {6.79, 2.10, 0.00, 3.78, 4.53, 2.83, 2.44, 3.79},
	  {3.50, 2.27, 3.78, 0.00, 1.98, 4.35, 2.07, 0.53},
	  {3.11, 2.65, 4.53, 1.98, 0.00, 3.80, 3.31, 1.47},
	  {4.46, 2.36, 2.83, 4.35, 3.80, 0.00, 4.35, 3.82},
	  {5.57, 1.99, 2.44, 2.07, 3.31, 4.35, 0.00, 2.57},
	  {3.00, 1.74, 3.79, 0.53, 1.47, 3.82, 2.57, 0.00},
  };
  igraph_t g;
  igraph_matrix_t coords, dist_mat;
  igraph_real_t vc;
  igraph_arpack_options_t options;
  int i, j;
  srand(time(0));

  igraph_arpack_options_init(&options);

  igraph_tree(&g, 10, 2, IGRAPH_TREE_UNDIRECTED);
  igraph_matrix_init(&coords, 0, 0);
  igraph_layout_mds(&g, &coords, 0, 2, &options);
  if (MATRIX(coords, 0, 0) > 0) {
    for (i = 0; i < igraph_matrix_nrow(&coords); i++)
      MATRIX(coords, i, 0) *= -1;
  }
  if (MATRIX(coords, 0, 1) < 0) {
    for (i = 0; i < igraph_matrix_nrow(&coords); i++)
      MATRIX(coords, i, 1) *= -1;
  }
  igraph_matrix_print(&coords);
  igraph_matrix_destroy(&coords);
  igraph_destroy(&g);

  igraph_full(&g, 8, IGRAPH_UNDIRECTED, 0);
  igraph_matrix_init(&coords, 8, 2);
  igraph_matrix_init(&dist_mat, 8, 8);
  for (i = 0; i < 8; i++)
    for (j = 0; j < 2; j++)
      MATRIX(coords, i, j) = rand() % 1000;
  for (i = 0; i < 8; i++)
    for (j = i+1; j < 8; j++) {
      double dist_sq = 0.0;
      dist_sq += sqr(MATRIX(coords, i, 0)-MATRIX(coords, j, 0));
      dist_sq += sqr(MATRIX(coords, i, 1)-MATRIX(coords, j, 1));
      MATRIX(dist_mat, i, j) = sqrt(dist_sq);
      MATRIX(dist_mat, j, i) = sqrt(dist_sq);
	}
  igraph_layout_mds(&g, &coords, &dist_mat, 2, &options);
  for (i = 0; i < 8; i++)
    for (j = i+1; j < 8; j++) {
      double dist_sq = 0.0;
      dist_sq += sqr(MATRIX(coords, i, 0)-MATRIX(coords, j, 0));
      dist_sq += sqr(MATRIX(coords, i, 1)-MATRIX(coords, j, 1));
      if (fabs(sqrt(dist_sq) - MATRIX(dist_mat, i, j)) > 1e-2) {
        printf("dist(%d,%d) should be %.4f, but it is %.4f\n",
				i, j, MATRIX(dist_mat, i, j), sqrt(dist_sq));
        return 1;
      }
    }
  igraph_matrix_destroy(&dist_mat);
  igraph_matrix_destroy(&coords);
  igraph_destroy(&g);

  return 0;
}
예제 #13
0
int main() {
  
  igraph_t graph ;
  igraph_vector_t dist,order;
   long int i,j,n,c,x,p,y,a,b,d[10];
   printf("Enter total number of nodes.");
   scanf("%li",&n);
   printf("Enter number of children per node.");
   scanf("%li",&c);
  igraph_vector_init(&order, 0);
  igraph_vector_init(&dist, 0);   

  igraph_tree(&graph, (igraph_integer_t)n, (igraph_integer_t)c,IGRAPH_TREE_OUT);
  FILE *fp=fopen("abc.gml","w");
  igraph_write_graph_gml(&graph,fp,0,0);
 long int mat[n][n];
for(i=0;i<n;i++)
{
igraph_bfsr(&graph, /*root=*/(igraph_integer_t)i, /*roots=*/ 0, /*neimode=*/ IGRAPH_ALL, 
       /*unreachable=*/ 1, /*restricted=*/ 0,
       0, 0, 0, 0, 0, &dist, 
       /*callback=*/ 0, /*extra=*/ 0);
for(j=0;j<n;j++)
mat[i][j]=(long int)VECTOR(dist)[j];
}

  flag=1;
  /* Test the callback */
printf("The distance matrix: \n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%li ",mat[i][j]);
printf("\n");
}
printf("\nEnter the node for which average is to be computed: ");
scanf("%li",&x);
printf("The order for every call is :\n"); 
y=0;
  for(i=0;i<10;i++)
{
 igraph_bfsr(&graph, /*root=*/0, /*roots=*/ 0, /*neimode=*/ IGRAPH_ALL, 
       /*unreachable=*/ 1, /*restricted=*/ 0,
       &order, 0, 0, 0, 0, 0, 
       /*callback=*/ 0, /*extra=*/ 0);
j=0;p=0;
igraph_vector_print(&order);
while(j<n && (long int)VECTOR(order)[j]!=x)
{
p=p+(mat[(long int)VECTOR(order)[j]][(long int)VECTOR(order)[j+1]]);
j++;
}
d[i]=p;
y=y+p;
}
y=y/10;

printf("The distances for every call are :\n");
for(i=0;i<10;i++)
printf("%li ",d[i]);
printf("\nThe average path length is: %li\n",y);

   igraph_vector_destroy(&dist);
   igraph_destroy(&graph);
  
  return 0;
}