Exemplo n.º 1
0
/* call-seq:
 *   graph.edge_betweenness(mode) -> Array
 *
 * Returns an Array of betweenness centrality measures for the edges 
 * in the graph. mode defines whether directed paths or considered for 
 * directed graphs.
 */
VALUE cIGraph_edge_betweenness(VALUE self, VALUE directed){

  igraph_t *graph;
  igraph_bool_t dir = 0;
  igraph_vector_t res;
  int i;
  VALUE betweenness = rb_ary_new();

  if(directed == Qtrue)
    dir = 1;

  //vector to hold the results of the degree calculations
  igraph_vector_init_int(&res,0);

  Data_Get_Struct(self, igraph_t, graph);

  igraph_edge_betweenness(graph,&res,dir);

  for(i=0;i<igraph_vector_size(&res);i++){
    rb_ary_push(betweenness,INT2NUM((int)VECTOR(res)[i]));
  }

  igraph_vector_destroy(&res);

  return betweenness;

}
int main() {

  igraph_t g;
  igraph_vector_t eb; 

  igraph_small(&g, 0, IGRAPH_UNDIRECTED, 
	       0,  1,  0,  2,  0,  3,  0,  4,  0,  5,
	       0,  6,  0,  7,  0,  8,  0, 10,  0, 11,
	       0, 12,  0, 13,  0, 17,  0, 19,  0, 21,
	       0, 31,  1,  2,  1,  3,  1,  7,  1, 13,
	       1, 17,  1, 19,  1, 21,  1, 30,  2,  3,
	       2,  7,  2,  8,  2,  9,  2, 13,  2, 27,
	       2, 28,  2, 32,  3,  7,  3, 12,  3, 13,
	       4,  6,  4, 10,  5,  6,  5, 10,  5, 16,
	       6, 16,  8, 30,  8, 32,  8, 33,  9, 33,
	       13, 33, 14, 32, 14, 33, 15, 32, 15, 33,
	       18, 32, 18, 33, 19, 33, 20, 32, 20, 33,
	       22, 32, 22, 33, 23, 25, 23, 27, 23, 29,
	       23, 32, 23, 33, 24, 25, 24, 27, 24, 31,
	       25, 31, 26, 29, 26, 33, 27, 33, 28, 31,
	       28, 33, 29, 32, 29, 33, 30, 32, 30, 33,
	       31, 32, 31, 33, 32, 33,
	       -1);
  
  igraph_vector_init(&eb, igraph_ecount(&g));
  igraph_edge_betweenness(&g, &eb, IGRAPH_UNDIRECTED);
  print_vector(&eb, stdout);
  igraph_vector_destroy(&eb);
  igraph_destroy(&g);

  igraph_small(&g, 0, IGRAPH_UNDIRECTED,
    0, 1, 0, 2, 0, 3, 1, 4, -1);
  igraph_vector_init(&eb, igraph_ecount(&g));
  igraph_edge_betweenness_estimate(&g, &eb, IGRAPH_UNDIRECTED, 2);
  print_vector(&eb, stdout);
  igraph_vector_destroy(&eb);
  igraph_destroy(&g);

  igraph_small(&g, 0, IGRAPH_UNDIRECTED,
    0, 1, 0, 3, 1, 2, 1, 4, 2, 5, 3, 4, 3, 6, 4, 5, 4, 7, 5, 8,
    6, 7, 7, 8, -1);
  igraph_vector_init(&eb, igraph_ecount(&g));
  igraph_edge_betweenness_estimate(&g, &eb, IGRAPH_UNDIRECTED, 2);
  print_vector(&eb, stdout);
  igraph_vector_destroy(&eb);
  igraph_destroy(&g);

  return 0;
}
int main() {
  
  igraph_t g, tree;
  igraph_vector_t eb, edges;
  long int i;
  
  igraph_small(&g, 0, IGRAPH_UNDIRECTED, 
	       0,  1,  0,  2,  0,  3,  0,  4,  0,  5,
	       0,  6,  0,  7,  0,  8,  0, 10,  0, 11,
	       0, 12,  0, 13,  0, 17,  0, 19,  0, 21,
	       0, 31,  1,  2,  1,  3,  1,  7,  1, 13,
	       1, 17,  1, 19,  1, 21,  1, 30,  2,  3,
	       2,  7,  2,  8,  2,  9,  2, 13,  2, 27,
	       2, 28,  2, 32,  3,  7,  3, 12,  3, 13,
	       4,  6,  4, 10,  5,  6,  5, 10,  5, 16,
	       6, 16,  8, 30,  8, 32,  8, 33,  9, 33,
	       13, 33, 14, 32, 14, 33, 15, 32, 15, 33,
	       18, 32, 18, 33, 19, 33, 20, 32, 20, 33,
	       22, 32, 22, 33, 23, 25, 23, 27, 23, 29,
	       23, 32, 23, 33, 24, 25, 24, 27, 24, 31,
	       25, 31, 26, 29, 26, 33, 27, 33, 28, 31,
	       28, 33, 29, 32, 29, 33, 30, 32, 30, 33,
	       31, 32, 31, 33, 32, 33,
	       -1);
  
  igraph_vector_init(&eb, igraph_ecount(&g));
  igraph_edge_betweenness(&g, &eb, IGRAPH_UNDIRECTED, /*weights=*/ 0);
  for (i=0; i<igraph_vector_size(&eb); i++) {
    VECTOR(eb)[i] = -VECTOR(eb)[i];
  }

  igraph_minimum_spanning_tree_prim(&g, &tree, &eb);
  igraph_write_graph_edgelist(&tree, stdout);

  igraph_vector_init(&edges, 0);
  igraph_minimum_spanning_tree(&g, &edges, &eb);
  igraph_vector_print(&edges);
  igraph_vector_destroy(&edges);

  igraph_destroy(&tree);
  igraph_destroy(&g);
  igraph_vector_destroy(&eb);
  
  return 0;
}