Ejemplo n.º 1
0
int main() {
  
  igraph_t g;
  igraph_vector_t y;  

  /* turn on attribute handling */
  igraph_i_set_attribute_table(&igraph_cattribute_table);

  /* Create a graph, add some attributes and save it as a GraphML file */
  igraph_famous(&g, "Petersen");
  SETGAS(&g, "name", "Petersen's graph");
  SETGAN(&g, "vertices", igraph_vcount(&g));
  SETGAN(&g, "edges", igraph_ecount(&g));

  igraph_vector_init_seq(&y, 1, igraph_vcount(&g));
  SETVANV(&g, "id", &y);
  igraph_vector_destroy(&y);

  SETVAS(&g, "name", 0, "foo");
  SETVAS(&g, "name", 1, "foobar");
  
  igraph_vector_init_seq(&y, 1, igraph_ecount(&g));
  SETEANV(&g, "id", &y);
  igraph_vector_destroy(&y);

  SETEAS(&g, "name", 0, "FOO");
  SETEAS(&g, "name", 1, "FOOBAR");

  igraph_write_graph_gml(&g, stdout, 0, "");
  igraph_write_graph_graphml(&g, stdout);
   
  igraph_destroy(&g);
  
  return 0;
}
Ejemplo n.º 2
0
igraph_bool_t check_laplacian(igraph_t* graph, igraph_matrix_t* matrix, igraph_vector_t* w) {
  igraph_vector_t vec, res;
  long int i, j;

  igraph_vector_init(&vec, 0);
  igraph_vector_init(&res, igraph_vcount(graph));

  if (w)
    igraph_strength(graph, &vec, igraph_vss_all(), IGRAPH_OUT, IGRAPH_NO_LOOPS, w);
  else
    igraph_degree(graph, &vec, igraph_vss_all(), IGRAPH_OUT, IGRAPH_NO_LOOPS);

  for (i = 0; i < igraph_vcount(graph); i++) {
    VECTOR(vec)[i] = sqrt(VECTOR(vec)[i]);
  }

  for (i = 0; i < igraph_vcount(graph); i++) {
    for (j = 0; j < igraph_vcount(graph); j++) {
      VECTOR(res)[i] += MATRIX(*matrix, i, j) * VECTOR(vec)[j];
    }
  }

  if (igraph_vector_min(&res) > 1e-7) {
    printf("Invalid Laplacian matrix:\n");
    igraph_matrix_print(matrix);
    return 0;
  }

  igraph_vector_destroy(&vec);
  igraph_vector_destroy(&res);

  return 1;
}
Ejemplo n.º 3
0
int check_ring_properties(const igraph_t *ring, igraph_bool_t directed,
			  igraph_bool_t mutual, igraph_bool_t circular) {

  igraph_bool_t res;

  /* Connected */
  igraph_is_connected(ring, &res, IGRAPH_WEAK); 
  if (!res) {
    printf("Not connected\n");
    return 1;
  }

  /* Simple */
  igraph_is_simple(ring, &res); 
  if (!res) {
    printf("Not simple\n");
    return 2;
  }
  
  /* Girth, for big enough circular graphs */
  if (circular && igraph_vcount(ring) > 2) { 
    igraph_integer_t girth;
    igraph_girth(ring, &girth, NULL);
    if (girth != igraph_vcount(ring)) { 
      printf("Wrong girth\n");
      return 3; 
    }
  }
   
  return 0;
}
Ejemplo n.º 4
0
int TMIgraph::readTopology(char *file_name) {
    int ret;
    Bitvector *lid;
    Bitvector *ilid;
    ifstream infile;
    string str;
    size_t found, first, second;
    FILE *instream;
    infile.open(file_name, ifstream::in);
    /*first the Global graph attributes - c igraph does not do it!!*/
    while (infile.good()) {
        getline(infile, str);
        found = str.find("<data key=\"FID_LEN\">");
        if (found != string::npos) {
            first = str.find(">");
            second = str.find("<", first);
            sscanf(str.substr(first + 1, second - first - 1).c_str(), "%d", &fid_len);
        }
        found = str.find("<data key=\"TM\">");
        if (found != string::npos) {
            first = str.find(">");
            second = str.find("<", first);
            nodeID = str.substr(first + 1, second - first - 1);
        }

        found = str.find("<data key=\"TM_MODE\">");
        if (found != string::npos) {
            first = str.find(">");
            second = str.find("<", first);
            mode = str.substr(first + 1, second - first - 1);
        }
    }
    infile.close();
    instream = fopen(file_name, "r");
    ret = igraph_read_graph_graphml(&graph, instream, 0);
    fclose(instream);
    if (ret < 0) {
        return ret;
    }
    cout << "TM: " << igraph_vcount(&graph) << " nodes" << endl;
    cout << "TM: " << igraph_ecount(&graph) << " edges" << endl;
    for (int i = 0; i < igraph_vcount(&graph); i++) {
        string nID = string(igraph_cattribute_VAS(&graph, "NODEID", i));
        string iLID = string(igraph_cattribute_VAS(&graph, "iLID", i));
        reverse_node_index.insert(pair<string, int>(nID, i));
        ilid = new Bitvector(iLID);
        nodeID_iLID.insert(pair<string, Bitvector *>(nID, ilid));
        vertex_iLID.insert(pair<int, Bitvector *>(i, ilid));
        //cout << "node " << i << " has NODEID " << nID << endl;
        //cout << "node " << i << " has ILID " << ilid->to_string() << endl;
    }
    for (int i = 0; i < igraph_ecount(&graph); i++) {
        string LID = string(igraph_cattribute_EAS(&graph, "LID", i));
        reverse_edge_index.insert(pair<string, int>(LID, i));
        lid = new Bitvector(LID);
        edge_LID.insert(pair<int, Bitvector *>(i, lid));
        //cout << "edge " << i << " has LID  " << lid->to_string() << endl;
    }
    return ret;
}
Ejemplo n.º 5
0
Archivo: scan.c Proyecto: abeham/igraph
int igraph_local_scan_0_them(const igraph_t *us, const igraph_t *them,
			     igraph_vector_t *res,
			     const igraph_vector_t *weights_them,
			     igraph_neimode_t mode) {

  igraph_t is;

  if (igraph_vcount(us) != igraph_vcount(them)) {
    IGRAPH_ERROR("Number of vertices don't match in scan-0", IGRAPH_EINVAL);
  }
  if (igraph_is_directed(us) != igraph_is_directed(them)) {
    IGRAPH_ERROR("Directedness don't match in scan-0", IGRAPH_EINVAL);
  }

  if (weights_them) {
    return igraph_i_local_scan_0_them_w(us, them, res, weights_them, mode);
  }

  igraph_intersection(&is, us, them, /*edgemap1=*/ 0, /*edgemap2=*/ 0);
  IGRAPH_FINALLY(igraph_destroy, &is);

  igraph_degree(&is, res, igraph_vss_all(), mode, IGRAPH_LOOPS);

  igraph_destroy(&is);
  IGRAPH_FINALLY_CLEAN(1);

  return 0;
}
Ejemplo n.º 6
0
int main() {
  
  igraph_t g;
  igraph_vector_t v1, v2;
  int ret;
  
  /* simple use */
  igraph_vector_init(&v1, 8);
  VECTOR(v1)[0]=0; VECTOR(v1)[1]=1;
  VECTOR(v1)[2]=1; VECTOR(v1)[3]=2;
  VECTOR(v1)[4]=2; VECTOR(v1)[5]=3;
  VECTOR(v1)[6]=2; VECTOR(v1)[7]=2;
  igraph_create(&g, &v1, 0, 0);
  if (igraph_vcount(&g) != 4) {
    return 1;
  }
  igraph_vector_init(&v2, 0);
  igraph_get_edgelist(&g, &v2, 0);
  igraph_vector_sort(&v1);
  igraph_vector_sort(&v2);
  if (!igraph_vector_all_e(&v1, &v2)) {
    return 2;
  }
  igraph_destroy(&g);
  
  /* higher number of vertices */
  igraph_create(&g, &v1, 10, 0);
  if (igraph_vcount(&g) != 10) {
    return 1;
  }
  igraph_get_edgelist(&g, &v2, 0);
  igraph_vector_sort(&v1);
  igraph_vector_sort(&v2);
  if (!igraph_vector_all_e(&v1, &v2)) {
    return 3;
  }
  igraph_destroy(&g);

  /* error: IGRAPH_EINVEVECTOR */
  igraph_set_error_handler(igraph_error_handler_ignore);
  igraph_vector_resize(&v1, 9);
  VECTOR(v1)[8]=0;
  ret=igraph_create(&g, &v1, 0, 0);
  if (ret != IGRAPH_EINVEVECTOR) {
    return 4;
  }
  
  /* error: IGRAPH_EINVVID */
  igraph_vector_resize(&v1, 8);
  VECTOR(v1)[7]=-1;
  ret=igraph_create(&g, &v1, 10, 1);
  if (ret != IGRAPH_EINVVID) {
    return 5;
  }
  igraph_vector_destroy(&v1);
  igraph_vector_destroy(&v2);

  return 0;
}
Ejemplo n.º 7
0
int main() {
  
  igraph_t g;
  int ret;

  /* empty directed graph, zero vertices */
  igraph_empty(&g, 0, 1);
  if (igraph_vcount(&g) != 0) {
    return 1;
  }
  if (igraph_ecount(&g) != 0) {
    return 2;
  }
  igraph_destroy(&g);
  
  /* empty undirected graph, zero vertices */
  igraph_empty(&g, 0, 0);
  if (igraph_vcount(&g) != 0) {
    return 3;
  }
  if (igraph_ecount(&g) != 0) {
    return 4;
  }
  igraph_destroy(&g);

  /* empty directed graph, 20 vertices */
  igraph_empty(&g, 20, 1);
  if (igraph_vcount(&g) != 20) {
    return 5;
  }
  if (igraph_ecount(&g) != 0) {
    return 6;
  }
  igraph_destroy(&g);
  
  /* empty undirected graph, 30 vertices */
  igraph_empty(&g, 30, 0);
  if (igraph_vcount(&g) != 30) {
    return 7;
  }
  if (igraph_ecount(&g) != 0) {
    return 8;
  }
  igraph_destroy(&g);

  /* error: negative number of vertices */
  igraph_set_error_handler(igraph_error_handler_ignore);
  ret=igraph_empty(&g, -1, 0);
  if (ret != IGRAPH_EINVAL) {
    return 9;
  }

  return 0;
}
int main() {

  igraph_t g;
  igraph_vector_t v;
  int ret;

  /* without edges */
  igraph_empty(&g, 5, IGRAPH_DIRECTED);
  igraph_add_vertices(&g, 2, 0);
  igraph_add_vertices(&g, 3, 0);
  igraph_add_vertices(&g, 1, 0);
  igraph_add_vertices(&g, 4, 0);
  if (igraph_vcount(&g) != 15)  {
    return 1;
  }
  igraph_delete_vertices(&g, igraph_vss_1(2));
  if (igraph_vcount(&g) != 14)  {
    return 2;
  }
  igraph_destroy(&g);
   
  igraph_vector_init(&v, 8);
  VECTOR(v)[0]=0; VECTOR(v)[1]=1;
  VECTOR(v)[2]=1; VECTOR(v)[3]=2;
  VECTOR(v)[4]=2; VECTOR(v)[5]=3;
  VECTOR(v)[6]=2; VECTOR(v)[7]=2;
  igraph_create(&g, &v, 0, 0);
  igraph_vector_destroy(&v);

  /* resize vector */
  igraph_delete_vertices(&g, igraph_vss_1(2));
  if (igraph_vcount(&g) != 3) {
    return 3;
  }
  if (igraph_ecount(&g) != 1) {
    return 4;
  }

  /* error test */
  igraph_set_error_handler(igraph_error_handler_ignore);
  ret=igraph_delete_vertices(&g, igraph_vss_1(3));
  if (ret != IGRAPH_EINVVID) {
    return 5;
  }
  
  igraph_destroy(&g);

  return 0;
}
Ejemplo n.º 9
0
int igraph_adjlist_init(const igraph_t *graph, igraph_adjlist_t *al, 
			  igraph_neimode_t mode) {
  long int i;

  if (mode != IGRAPH_IN && mode != IGRAPH_OUT && mode != IGRAPH_ALL) {
    IGRAPH_ERROR("Cannot create adjlist view", IGRAPH_EINVMODE);
  }

  if (!igraph_is_directed(graph)) { mode=IGRAPH_ALL; }

  al->length=igraph_vcount(graph);
  al->adjs=igraph_Calloc(al->length, igraph_vector_t);
  if (al->adjs == 0) {
    IGRAPH_ERROR("Cannot create adjlist view", IGRAPH_ENOMEM);
  }

  IGRAPH_FINALLY(igraph_adjlist_destroy, al);
  for (i=0; i<al->length; i++) {
    IGRAPH_ALLOW_INTERRUPTION();
    IGRAPH_CHECK(igraph_vector_init(&al->adjs[i], 0));
    IGRAPH_CHECK(igraph_neighbors(graph, &al->adjs[i], i, mode));
  }

  IGRAPH_FINALLY_CLEAN(1);
  return 0;
}
Ejemplo n.º 10
0
VALUE cIGraph_modularity(VALUE self, VALUE groups){

  igraph_t *graph;
  igraph_real_t value;
  igraph_vector_t membership;

  VALUE group;

  int i,j;

  Data_Get_Struct(self, igraph_t, graph);

  igraph_vector_init(&membership,igraph_vcount(graph));

  for(i=0;i<RARRAY_LEN(groups);i++){
    group = RARRAY_PTR(groups)[i];
    for(j=0;j<RARRAY_LEN(group);j++){
      igraph_vector_set(&membership,
			cIGraph_get_vertex_id(self,RARRAY_PTR(group)[j]),i);
    }
  }

  igraph_modularity(graph,&membership,&value,NULL);
  
  igraph_vector_destroy(&membership);

  return rb_float_new(value);

}
Ejemplo n.º 11
0
int igraph_i_cliquer_callback(const igraph_t *graph,
                    igraph_integer_t min_size, igraph_integer_t max_size,
                    igraph_clique_handler_t *cliquehandler_fn, void *arg)
{
    graph_t *g;
    struct callback_data cd;
    igraph_integer_t vcount = igraph_vcount(graph);

    if (vcount == 0)
        return IGRAPH_SUCCESS;

    if (min_size <= 0) min_size = 1;
    if (max_size <= 0) max_size = 0;

    if (max_size > 0 && max_size < min_size)
        IGRAPH_ERROR("max_size must not be smaller than min_size", IGRAPH_EINVAL);

    igraph_to_cliquer(graph, &g);
    IGRAPH_FINALLY(graph_free, g);

    cd.handler = cliquehandler_fn;
    cd.arg = arg;
    igraph_cliquer_opt.user_data = &cd;
    igraph_cliquer_opt.user_function = &callback_callback;

    CLIQUER_INTERRUPTABLE(clique_unweighted_find_all(g, min_size, max_size, /* maximal= */ FALSE, &igraph_cliquer_opt));

    graph_free(g);
    IGRAPH_FINALLY_CLEAN(1);

    return IGRAPH_SUCCESS;
}
Ejemplo n.º 12
0
int igraph_i_cliquer_cliques(const igraph_t *graph, igraph_vector_ptr_t *res,
                    igraph_integer_t min_size, igraph_integer_t max_size)
{
    graph_t *g;
    igraph_integer_t vcount = igraph_vcount(graph);

    if (vcount == 0) {
        igraph_vector_ptr_clear(res);
        return IGRAPH_SUCCESS;
    }

    if (min_size <= 0) min_size = 1;
    if (max_size <= 0) max_size = 0;

    if (max_size > 0 && max_size < min_size)
        IGRAPH_ERROR("max_size must not be smaller than min_size", IGRAPH_EINVAL);

    igraph_to_cliquer(graph, &g);
    IGRAPH_FINALLY(graph_free, g);

    igraph_vector_ptr_clear(res);
    igraph_cliquer_opt.user_data = res;
    igraph_cliquer_opt.user_function = &collect_cliques_callback;

    IGRAPH_FINALLY(free_clique_list, res);
    CLIQUER_INTERRUPTABLE(clique_unweighted_find_all(g, min_size, max_size, /* maximal= */ FALSE, &igraph_cliquer_opt));
    IGRAPH_FINALLY_CLEAN(1);

    graph_free(g);
    IGRAPH_FINALLY_CLEAN(1);

    return IGRAPH_SUCCESS;
}
Ejemplo n.º 13
0
/* call-seq:
 *   graph.get_adjacency(type) -> Array
 *
 * Returns the adjacency matrix of a graph
 *
 */
VALUE cIGraph_get_adjacency(VALUE self, VALUE mode){

  igraph_t *graph;
  igraph_get_adjacency_t pmode = NUM2INT(mode);
  igraph_matrix_t res;
  int i;
  int j;
  VALUE row;
  VALUE path_length;
  VALUE matrix = rb_ary_new();
  int n;

  Data_Get_Struct(self, igraph_t, graph);

  n = igraph_vcount(graph);

  //matrix to hold the results of the calculations
  igraph_matrix_init(&res,n,n);

  igraph_get_adjacency(graph,&res,pmode);

  for(i=0; i<igraph_matrix_nrow(&res); i++){
    row = rb_ary_new();
    rb_ary_push(matrix,row);
    for(j=0; j<igraph_matrix_ncol(&res); j++){
      path_length = INT2NUM(MATRIX(res,i,j));
      rb_ary_push(row,path_length);
    }
  }

  igraph_matrix_destroy(&res);

  return matrix;

}
Ejemplo n.º 14
0
static inline void
rvine_tree_cleanup(igraph_t *tree)
{
    igraph_integer_t e, a;

    for (a = 0; a < igraph_vcount(tree); a++) {
        if (VAP(tree, "h", a) != VAP(tree, "hrev", a)) {
            gsl_vector_free(VAP(tree, "h", a));
            gsl_vector_free(VAP(tree, "hrev", a));
            gsl_permutation_free(VAP(tree, "hrank", a));
            gsl_permutation_free(VAP(tree, "hrevrank", a));
        } else if (VAP(tree, "h", a) != NULL) {
            gsl_vector_free(VAP(tree, "h", a));
            gsl_permutation_free(VAP(tree, "hrank", a));
        } else {
            gsl_vector_free(VAP(tree, "hrev", a));
            gsl_permutation_free(VAP(tree, "hrevrank", a));
        }
    }
    DELVAS(tree);

    for (e = 0; e < igraph_ecount(tree); e++) {
        gsl_vector_short_free(EAP(tree, "Ue", e));
        dml_measure_free(EAP(tree, "measure", e));
    }
    DELEA(tree, "Ue");
    DELEA(tree, "weight");
    DELEA(tree, "measure");
}
Ejemplo n.º 15
0
void dump_graph(const char* header, const igraph_t* g) {
  fputs(header, stdout);
  printf("Vertices: %li\n", (long int) igraph_vcount(g));
  printf("Edges: %li\n", (long int) igraph_ecount(g));
  printf("Directed: %i\n", (int) igraph_is_directed(g));
  igraph_write_graph_edgelist(g, stdout);
}
Ejemplo n.º 16
0
int igraph_is_separator(const igraph_t *graph, 
			const igraph_vs_t candidate,
			igraph_bool_t *res) {

  long int no_of_nodes=igraph_vcount(graph);
  igraph_vector_bool_t removed;
  igraph_dqueue_t Q;
  igraph_vector_t neis;
  igraph_vit_t vit;

  IGRAPH_CHECK(igraph_vit_create(graph, candidate, &vit));
  IGRAPH_FINALLY(igraph_vit_destroy, &vit);
  IGRAPH_CHECK(igraph_vector_bool_init(&removed, no_of_nodes));
  IGRAPH_FINALLY(igraph_vector_bool_destroy, &removed);
  IGRAPH_CHECK(igraph_dqueue_init(&Q, 100));
  IGRAPH_FINALLY(igraph_dqueue_destroy, &Q);
  IGRAPH_VECTOR_INIT_FINALLY(&neis, 0);

  IGRAPH_CHECK(igraph_i_is_separator(graph, &vit, -1, res, &removed, 
				     &Q, &neis, no_of_nodes));

  igraph_vector_destroy(&neis);
  igraph_dqueue_destroy(&Q);
  igraph_vector_bool_destroy(&removed);
  igraph_vit_destroy(&vit);
  IGRAPH_FINALLY_CLEAN(4);

  return 0;
}
Ejemplo n.º 17
0
int igraph_i_largest_weighted_cliques(const igraph_t *graph,
                    const igraph_vector_t *vertex_weights, igraph_vector_ptr_t *res)
{
    graph_t *g;
    igraph_integer_t vcount = igraph_vcount(graph);

    if (vcount == 0) {
        igraph_vector_ptr_clear(res);
        return IGRAPH_SUCCESS;
    }

    igraph_to_cliquer(graph, &g);
    IGRAPH_FINALLY(graph_free, g);

    IGRAPH_CHECK(set_weights(vertex_weights, g));

    igraph_vector_ptr_clear(res);
    igraph_cliquer_opt.user_data = res;
    igraph_cliquer_opt.user_function = &collect_cliques_callback;

    IGRAPH_FINALLY(free_clique_list, res);
    CLIQUER_INTERRUPTABLE(clique_find_all(g, 0, 0, FALSE, &igraph_cliquer_opt));
    IGRAPH_FINALLY_CLEAN(1);

    graph_free(g);
    IGRAPH_FINALLY_CLEAN(1);

    return IGRAPH_SUCCESS;
}
Ejemplo n.º 18
0
int igraph_inclist_init(const igraph_t *graph, 
			      igraph_inclist_t *il, 
			      igraph_neimode_t mode) {
  long int i;

  if (mode != IGRAPH_IN && mode != IGRAPH_OUT && mode != IGRAPH_ALL) {
    IGRAPH_ERROR("Cannot create incidence list view", IGRAPH_EINVMODE);
  }

  if (!igraph_is_directed(graph)) { mode=IGRAPH_ALL; }

  il->length=igraph_vcount(graph);
  il->incs=igraph_Calloc(il->length, igraph_vector_t);
  if (il->incs == 0) {
    IGRAPH_ERROR("Cannot create incidence list view", IGRAPH_ENOMEM);
  }

  IGRAPH_FINALLY(igraph_inclist_destroy, il);  
  for (i=0; i<il->length; i++) {
    IGRAPH_ALLOW_INTERRUPTION();
    IGRAPH_CHECK(igraph_vector_init(&il->incs[i], 0));
    IGRAPH_CHECK(igraph_incident(graph, &il->incs[i], i, mode));
  }
  
  IGRAPH_FINALLY_CLEAN(1);
  return 0;
}
Ejemplo n.º 19
0
int igraph_i_weighted_clique_number(const igraph_t *graph,
                    const igraph_vector_t *vertex_weights, igraph_real_t *res)
{
    graph_t *g;
    igraph_integer_t vcount = igraph_vcount(graph);

    if (vcount == 0) {
        *res = 0;
        return IGRAPH_SUCCESS;
    }

    igraph_to_cliquer(graph, &g);
    IGRAPH_FINALLY(graph_free, g);

    IGRAPH_CHECK(set_weights(vertex_weights, g));

    igraph_cliquer_opt.user_function = NULL;

    /* we are not using a callback function, thus this is not interruptable */
    *res = clique_max_weight(g, &igraph_cliquer_opt);

    graph_free(g);
    IGRAPH_FINALLY_CLEAN(1);

    return IGRAPH_SUCCESS;
}
Ejemplo n.º 20
0
int main(void) {
 igraph_real_t avg_path;
 igraph_t graph;
 igraph_vector_t dimvector;
 igraph_vector_t edges;
 int i;

 igraph_vector_init(&dimvector, 2);
 VECTOR(dimvector)[0]=30;
 VECTOR(dimvector)[1]=30;
 igraph_lattice(&graph, &dimvector, 0, IGRAPH_UNDIRECTED, 0, 1);
 igraph_rng_seed(igraph_rng_default(), 42);
 igraph_vector_init(&edges, 20);
 for (i=0; i<igraph_vector_size(&edges); i++) {
 VECTOR(edges)[i] = rand() % (int)igraph_vcount(&graph);
 }
 igraph_average_path_length(&graph, &avg_path, IGRAPH_UNDIRECTED, 1);
 printf("Average path length (lattice): %f\n", (double) avg_path);
 igraph_add_edges(&graph, &edges, 0);
 igraph_average_path_length(&graph, &avg_path, IGRAPH_UNDIRECTED, 1);
 printf("Average path length (randomized lattice): %f\n", (double) avg_path);

 igraph_vector_destroy(&dimvector);
 igraph_vector_destroy(&edges);
 igraph_destroy(&graph);
 return 0;
}
Ejemplo n.º 21
0
/**
 * \function igraph_maximum_bipartite_matching
 * Calculates a maximum matching in a bipartite graph.
 *
 * A matching in a bipartite graph is a partial assignment of vertices
 * of the first kind to vertices of the second kind such that each vertex of
 * the first kind is matched to at most one vertex of the second kind and
 * vice versa, and matched vertices must be connected by an edge in the graph.
 * The size (or cardinality) of a matching is the number of edges.
 * A matching is a maximum matching if there exists no other matching with
 * larger cardinality. For weighted graphs, a maximum matching is a matching
 * whose edges have the largest possible total weight among all possible
 * matchings.
 *
 * </para><para>
 * Maximum matchings in bipartite graphs are found by the push-relabel algorithm
 * with greedy initialization and a global relabeling after every n/2 steps where
 * n is the number of vertices in the graph.
 *
 * </para><para>
 * References: Cherkassky BV, Goldberg AV, Martin P, Setubal JC and Stolfi J:
 * Augment or push: A computational study of bipartite matching and
 * unit-capacity flow algorithms. ACM Journal of Experimental Algorithmics 3,
 * 1998.
 *
 * </para><para>
 * Kaya K, Langguth J, Manne F and Ucar B: Experiments on push-relabel-based
 * maximum cardinality matching algorithms for bipartite graphs. Technical
 * Report TR/PA/11/33 of the Centre Europeen de Recherche et de Formation
 * Avancee en Calcul Scientifique, 2011.
 *
 * \param graph The input graph. It can be directed but the edge directions
 *              will be ignored.
 * \param types Boolean vector giving the vertex types of the graph.
 * \param matching_size The size of the matching (i.e. the number of matched
 *                      vertex pairs will be returned here). It may be \c NULL
 *                      if you don't need this.
 * \param matching_weight The weight of the matching if the edges are weighted,
 *                        or the size of the matching again if the edges are
 *                        unweighted. It may be \c NULL if you don't need this.
 * \param matching The matching itself. It must be a vector where element i
 *                 contains the ID of the vertex that vertex i is matched to,
 *                 or -1 if vertex i is unmatched.
 * \param weights A null pointer (=no edge weights), or a vector giving the
 *                weights of the edges. Note that the algorithm is stable
 *                only for integer weights.
 * \param eps A small real number used in equality tests in the weighted
 *            bipartite matching algorithm. Two real numbers are considered
 *            equal in the algorithm if their difference is smaller than
 *            \c eps. This is required to avoid the accumulation of numerical
 *            errors. It is advised to pass a value derived from the
 *            \c DBL_EPSILON constant in \c float.h here. If you are
 *            running the algorithm with no \c weights vector, this argument
 *            is ignored.
 * \return Error code.
 *
 * Time complexity: O(sqrt(|V|) |E|) for unweighted graphs (according to the
 * technical report referenced above), O(|V||E|) for weighted graphs.
 * 
 * \example examples/simple/igraph_maximum_bipartite_matching.c
 */
int igraph_maximum_bipartite_matching(const igraph_t* graph,
    const igraph_vector_bool_t* types, igraph_integer_t* matching_size,
    igraph_real_t* matching_weight, igraph_vector_long_t* matching,
    const igraph_vector_t* weights, igraph_real_t eps) {

  /* Sanity checks */
  if (igraph_vector_bool_size(types) < igraph_vcount(graph)) {
    IGRAPH_ERROR("types vector too short", IGRAPH_EINVAL);
  }
  if (weights && igraph_vector_size(weights) < igraph_ecount(graph)) {
    IGRAPH_ERROR("weights vector too short", IGRAPH_EINVAL);
  }

  if (weights == 0) {
    IGRAPH_CHECK(igraph_i_maximum_bipartite_matching_unweighted(graph, types,
        matching_size, matching));
    if (matching_weight != 0) {
      *matching_weight = *matching_size;
    }
    return IGRAPH_SUCCESS;
  } else {
    return igraph_i_maximum_bipartite_matching_weighted(graph, types,
        matching_size, matching_weight, matching, weights, eps);
  }
}
Ejemplo n.º 22
0
int igraph_similarity_inverse_log_weighted(const igraph_t *graph,
  igraph_matrix_t *res, const igraph_vs_t vids, igraph_neimode_t mode) {
  igraph_vector_t weights;
  igraph_neimode_t mode0;
  long int i, no_of_nodes;

  switch (mode) {
    case IGRAPH_OUT: mode0 = IGRAPH_IN; break;
    case IGRAPH_IN: mode0 = IGRAPH_OUT; break;
    default: mode0 = IGRAPH_ALL;
  }

  no_of_nodes = igraph_vcount(graph);

  IGRAPH_VECTOR_INIT_FINALLY(&weights, no_of_nodes);
  IGRAPH_CHECK(igraph_degree(graph, &weights, igraph_vss_all(), mode0, 1));
  for (i=0; i < no_of_nodes; i++) {
    if (VECTOR(weights)[i] > 1)
      VECTOR(weights)[i] = 1.0 / log(VECTOR(weights)[i]);
  }

  IGRAPH_CHECK(igraph_cocitation_real(graph, res, vids, mode0, &weights));
  igraph_vector_destroy(&weights);
  IGRAPH_FINALLY_CLEAN(1);
  return 0;
}
Ejemplo n.º 23
0
Archivo: scan.c Proyecto: abeham/igraph
int igraph_i_local_scan_1_directed(const igraph_t *graph,
				   igraph_vector_t *res,
				   const igraph_vector_t *weights,
				   igraph_neimode_t mode) {

  int no_of_nodes=igraph_vcount(graph);
  igraph_inclist_t incs;
  int i, node;

  igraph_vector_int_t neis;

  IGRAPH_CHECK(igraph_inclist_init(graph, &incs, mode));
  IGRAPH_FINALLY(igraph_inclist_destroy, &incs);

  igraph_vector_int_init(&neis, no_of_nodes);
  IGRAPH_FINALLY(igraph_vector_int_destroy, &neis);

  igraph_vector_resize(res, no_of_nodes);
  igraph_vector_null(res);

  for (node=0; node < no_of_nodes; node++) {
    igraph_vector_int_t *edges1=igraph_inclist_get(&incs, node);
    int edgeslen1=igraph_vector_int_size(edges1);

    IGRAPH_ALLOW_INTERRUPTION();

    /* Mark neighbors and self*/
    VECTOR(neis)[node] = node+1;
    for (i=0; i<edgeslen1; i++) {
      int e=VECTOR(*edges1)[i];
      int nei=IGRAPH_OTHER(graph, e, node);
      igraph_real_t w= weights ? VECTOR(*weights)[e] : 1;
      VECTOR(neis)[nei] = node+1;
      VECTOR(*res)[node] += w;
    }

    /* Crawl neighbors */
    for (i=0; i<edgeslen1; i++) {
      int e2=VECTOR(*edges1)[i];
      int nei=IGRAPH_OTHER(graph, e2, node);
      igraph_vector_int_t *edges2=igraph_inclist_get(&incs, nei);
      int j, edgeslen2=igraph_vector_int_size(edges2);
      for (j=0; j<edgeslen2; j++) {
	int e2=VECTOR(*edges2)[j];
	int nei2=IGRAPH_OTHER(graph, e2, nei);
	igraph_real_t w2= weights ? VECTOR(*weights)[e2] : 1;
	if (VECTOR(neis)[nei2] == node+1) {
	  VECTOR(*res)[node] += w2;
	}
      }
    }

  } /* node < no_of_nodes */

  igraph_vector_int_destroy(&neis);
  igraph_inclist_destroy(&incs);
  IGRAPH_FINALLY_CLEAN(2);

  return 0;
}
int igraph_get_eid2(const igraph_t *graph, igraph_integer_t *eid,
		   igraph_integer_t pfrom, igraph_integer_t pto,
		   igraph_bool_t directed) {

  long int from=pfrom, to=pto;
  long int nov=igraph_vcount(graph);

  if (from < 0 || to < 0 || from > nov-1 || to > nov-1) {
    IGRAPH_ERROR("cannot get edge id", IGRAPH_EINVVID);
  }

  *eid=-1;
  if (igraph_is_directed(graph)) {

    /* Directed graph */
    FIND_DIRECTED_EDGE(graph,from,to,eid);
    if (!directed && *eid < 0) {
      FIND_DIRECTED_EDGE(graph,to,from,eid);
    }
    
  } else {

    /* Undirected graph, they only have one mode */
    FIND_UNDIRECTED_EDGE(graph,from,to,eid);

  }

  return IGRAPH_SUCCESS;  
}
Ejemplo n.º 25
0
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;
}
Ejemplo n.º 26
0
int GraphRepresentation::buildIGraphTopology() {
    int source_vertex_id, destination_vertex_id;
    igraph_i_set_attribute_table(&igraph_cattribute_table);
    igraph_empty(&igraph, 0, true);
    //cout << "iGraph: number of nodes: " << dm->number_of_nodes << endl;
    //cout << "iGraph: number number_of_connections nodes: " << dm->number_of_connections << endl;
    for (int i = 0; i < dm->network_nodes.size(); i++) {
        NetworkNode *nn = dm->network_nodes[i];
        for (int j = 0; j < nn->connections.size(); j++) {
            NetworkConnection *nc = nn->connections[j];
            map <string, int>::iterator index_it;
            /*find that node - if exists*/
            index_it = reverse_node_index.find(nc->src_label);
            /*check if the source node exists in the reverse index*/
            if (index_it == reverse_node_index.end()) {
                /*it does not exist...add it*/
                source_vertex_id = igraph_vcount(&igraph);
                igraph_add_vertices(&igraph, 1, 0);
                reverse_node_index.insert(pair<string, int>(nc->src_label, source_vertex_id));
                igraph_cattribute_VAS_set(&igraph, "NODEID", source_vertex_id, nc->src_label.c_str());
                //cout << "added node " << nc->src_label << " in the igraph" << endl;
            } else {
                source_vertex_id = (*index_it).second;
            }
            index_it = reverse_node_index.find(nc->dst_label);
            /*check if the destination node exists in the reverse index*/
            if (index_it == reverse_node_index.end()) {
                /*it does not exist...add it*/
                destination_vertex_id = igraph_vcount(&igraph);
                igraph_add_vertices(&igraph, 1, 0);
                reverse_node_index.insert(pair<string, int>(nc->dst_label, destination_vertex_id));
                igraph_cattribute_VAS_set(&igraph, "NODEID", destination_vertex_id, nc->dst_label.c_str());
                //cout << "added node " << nc->dst_label << " in the igraph" << endl;
            } else {
                destination_vertex_id = (*index_it).second;
            }
            /*add an edge in the graph*/
            igraph_add_edge(&igraph, source_vertex_id, destination_vertex_id);
            igraph_cattribute_EAS_set(&igraph, "LID", igraph_ecount(&igraph) - 1, nc->LID.to_string().c_str());
            reverse_edge_index.insert(pair<string, int>(nc->LID.to_string(), igraph_ecount(&igraph) - 1));
        }
    }
    for (int i = 0; i < dm->network_nodes.size(); i++) {
        NetworkNode *nn = dm->network_nodes[i];
        igraph_cattribute_VAS_set(&igraph, "iLID", i, nn->iLid.to_string().c_str());
    }
}
Ejemplo n.º 27
0
int main() {
  
  igraph_t g;
  /* long int i; */
  /* struct tms time; */
  /* clock_t current_time,start_time; */
  /* long int runs=100, n=10000; */
  /* igraph_real_t r=0.01; */
  
  /* Empty graph */
  igraph_grg_game(&g, 100, 0, 0, 0, 0);
  if (igraph_ecount(&g) != 0) {
    return 1;
  }
  igraph_destroy(&g);
  
  /* Full graph */
  igraph_grg_game(&g, 10, sqrt(2.0)/2, 1, 0, 0);
  if (igraph_ecount(&g) != igraph_vcount(&g) * (igraph_vcount(&g)-1)/2) {
    return 2;
  }
  igraph_destroy(&g);

  /* Measure running time */
/*   tps=sysconf(_SC_CLK_TCK); // clock ticks per second  */
/*   times(&time); start_time=time.tms_utime; */
/*   for (i=0; i<runs; i++) { */
/*     igraph_grg_game2(&g, n, r, 1);  */
/*     igraph_destroy(&g); */
/*   } */
/*   times(&time); current_time=time.tms_utime; */
/*   fprintf(stdout,"    sorted: time=%.3fs\n",(current_time-start_time)/(double)tps); */

/*   tps=sysconf(_SC_CLK_TCK); // clock ticks per second  */
/*   times(&time); start_time=time.tms_utime; */
/*   for (i=0; i<runs; i++) { */
/*     igraph_grg_game(&g, n, r, 1); */
/*     igraph_destroy(&g); */
/*   } */
/*   times(&time); current_time=time.tms_utime; */
/*   fprintf(stdout,"non-sorted: time=%.3fs\n", */
/* 	  (current_time-start_time)/(double)tps); */


  return 0;
}
Ejemplo n.º 28
0
/**
 * \ingroup python_interface_vertexseq
 * \brief Initialize a new vertex sequence object for a given graph
 * \return the initialized PyObject
 */
int igraphmodule_VertexSeq_init(igraphmodule_VertexSeqObject *self,
  PyObject *args, PyObject *kwds) {
  static char *kwlist[] = { "graph", "vertices", NULL };
  PyObject *g, *vsobj=Py_None;
  igraph_vs_t vs;

  if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!|O", kwlist,
    &igraphmodule_GraphType, &g, &vsobj))
      return -1;

  if (vsobj == Py_None) {
    /* If vs is None, we are selecting all the vertices */
    igraph_vs_all(&vs);
  } else if (PyInt_Check(vsobj)) {
    /* We selected a single vertex */
    long int idx = PyInt_AsLong(vsobj);
    if (idx < 0 || idx >= igraph_vcount(&((igraphmodule_GraphObject*)g)->g)) {
      PyErr_SetString(PyExc_ValueError, "vertex index out of range");
      return -1;
    }
    igraph_vs_1(&vs, (igraph_integer_t)idx);
  } else {
    igraph_vector_t v;
    igraph_integer_t n = igraph_vcount(&((igraphmodule_GraphObject*)g)->g);
    if (igraphmodule_PyObject_to_vector_t(vsobj, &v, 1))
      return -1;
    if (!igraph_vector_isininterval(&v, 0, n-1)) {
      igraph_vector_destroy(&v);
      PyErr_SetString(PyExc_ValueError, "vertex index out of range");
      return -1;
    }
    if (igraph_vs_vector_copy(&vs, &v)) {
      igraphmodule_handle_igraph_error();
      igraph_vector_destroy(&v);
      return -1;
    }
    igraph_vector_destroy(&v);
  }

  self->vs = vs;
  Py_INCREF(g);
  self->gref = (igraphmodule_GraphObject*)g;

  return 0;
}
Ejemplo n.º 29
0
int igraph_i_cb_components(igraph_t *graph, 
			   const igraph_vector_bool_t *excluded,
			   igraph_vector_long_t *components,
			   long int *no,
			   /* working area follows */
			   igraph_vector_long_t *compid, 
			   igraph_dqueue_t *Q,
			   igraph_vector_t *neis) {

  long int no_of_nodes=igraph_vcount(graph);
  long int i;
  long int cno=0;

  igraph_vector_long_clear(components);
  igraph_dqueue_clear(Q);
  IGRAPH_CHECK(igraph_vector_long_resize(compid, no_of_nodes));
  igraph_vector_long_null(compid);
  
  for (i=0; i<no_of_nodes; i++) {    

    if (VECTOR(*compid)[i])   { continue; }
    if (VECTOR(*excluded)[i]) { continue; }
    
    IGRAPH_CHECK(igraph_dqueue_push(Q, i));
    IGRAPH_CHECK(igraph_vector_long_push_back(components, i));
    VECTOR(*compid)[i] = ++cno;
    
    while (!igraph_dqueue_empty(Q)) {
      igraph_integer_t node=(igraph_integer_t) igraph_dqueue_pop(Q);
      long int j, n;
      IGRAPH_CHECK(igraph_neighbors(graph, neis, node, IGRAPH_ALL));
      n=igraph_vector_size(neis);
      for (j=0; j<n; j++) {
	long int v=(long int) VECTOR(*neis)[j];
	if (VECTOR(*excluded)[v]) {
	  if (VECTOR(*compid)[v] != cno) {
	    VECTOR(*compid)[v] = cno;
	    IGRAPH_CHECK(igraph_vector_long_push_back(components, v));
	  }
	} else {
	  if (!VECTOR(*compid)[v]) {
	    VECTOR(*compid)[v] = cno; /* could be anything positive */
	    IGRAPH_CHECK(igraph_vector_long_push_back(components, v));
	    IGRAPH_CHECK(igraph_dqueue_push(Q, v));
	  }
	}
      }
    } /* while !igraph_dqueue_empty */

    IGRAPH_CHECK(igraph_vector_long_push_back(components, -1));
    
  } /* for i<no_of_nodes */
  
  *no=cno;

  return 0;
}
Ejemplo n.º 30
0
void dump_vertex_attribute_string(const char* name, const igraph_t* g) {
  long int i, n = igraph_vcount(g);

  printf("Vertex attribute '%s':", name);
  for (i = 0; i < n; i++) {
    printf(" %s", VAS(g, name, i));
  }
  printf("\n");
}