int main() {

  igraph_t g1, g2;
  igraph_vector_t v1, v2;

  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(&g1, &v1, 0, 0);
  igraph_copy(&g2, &g1);

  igraph_vector_init(&v2, 0);
  igraph_get_edgelist(&g2, &v2, 0);
  if (!igraph_vector_is_equal(&v1, &v2)) {
    return 1;
  }

  igraph_vector_destroy(&v1);
  igraph_vector_destroy(&v2);
  igraph_destroy(&g1);
  igraph_destroy(&g2);

  return 0;
}
Example #2
0
/* Document-method: initialize_copy
 *
 * Internal method for copying IGraph objects.
 */
VALUE cIGraph_init_copy(VALUE copy, VALUE orig){

  igraph_t *orig_graph;
  igraph_t *copy_graph;

  if (copy == orig)
    return copy;

  if(TYPE(orig) != T_DATA || RDATA(orig)->dfree != (RUBY_DATA_FUNC)cIGraph_free){
    rb_raise(rb_eTypeError, "Wrong argument type.");
  }
  
  Data_Get_Struct(copy, igraph_t, copy_graph); 
  Data_Get_Struct(orig, igraph_t, orig_graph);

  IGRAPH_CHECK(igraph_copy(copy_graph,orig_graph));

  return copy;

}
Example #3
0
int igraph_cohesive_blocks(const igraph_t *graph,
			   igraph_vector_ptr_t *blocks,
			   igraph_vector_t *cohesion,
			   igraph_vector_t *parent,
			   igraph_t *block_tree) {

  /* Some implementation comments. Everything is relatively
     straightforward, except, that we need to follow the vertex ids
     of the various subgraphs, without having to store two-way
     mappings at each level. The subgraphs can overlap, this
     complicates things a bit.

     The 'Q' vector is used as a double ended queue and it contains
     the subgraphs to work on in the future. Some other vectors are
     associated with it. 'Qparent' gives the parent graph of a graph
     in Q. Qmapping gives the mapping of the vertices from the graph
     to the parent graph. Qcohesion is the vertex connectivity of the 
     graph. 

     Qptr is an integer and points to the next graph to work on.
  */
  
  igraph_vector_ptr_t Q;
  igraph_vector_ptr_t Qmapping;
  igraph_vector_long_t Qparent;
  igraph_vector_long_t Qcohesion;
  igraph_vector_bool_t Qcheck;
  long int Qptr=0;
  igraph_integer_t conn;
  igraph_bool_t is_simple;

  igraph_t *graph_copy;
  
  igraph_vector_ptr_t separators;
  igraph_vector_t compvertices;
  igraph_vector_long_t components;
  igraph_vector_bool_t marked;

  igraph_vector_long_t compid;
  igraph_dqueue_t bfsQ;
  igraph_vector_t neis;

  if (igraph_is_directed(graph)) {
    IGRAPH_ERROR("Cohesive blocking only works on undirected graphs",
		 IGRAPH_EINVAL);
  }

  IGRAPH_CHECK(igraph_is_simple(graph, &is_simple));
  if (!is_simple) {
    IGRAPH_ERROR("Cohesive blocking only works on simple graphs",
		 IGRAPH_EINVAL);
  }

  IGRAPH_STATUS("Starting cohesive block calculation.\n", 0);

  if (blocks)   { igraph_vector_ptr_clear(blocks); }
  if (cohesion) { igraph_vector_clear(cohesion);   }
  if (parent)   { igraph_vector_clear(parent);     }

  IGRAPH_CHECK(igraph_vector_ptr_init(&Q, 1));
  IGRAPH_FINALLY(igraph_vector_ptr_destroy, &Q);
  IGRAPH_FINALLY(igraph_i_cohesive_blocks_free, &Q);

  IGRAPH_CHECK(igraph_vector_ptr_init(&Qmapping, 1));
  IGRAPH_FINALLY(igraph_vector_ptr_destroy, &Qmapping);
  IGRAPH_FINALLY(igraph_i_cohesive_blocks_free2, &Qmapping);

  IGRAPH_CHECK(igraph_vector_long_init(&Qparent, 1));
  IGRAPH_FINALLY(igraph_vector_long_destroy, &Qparent);

  IGRAPH_CHECK(igraph_vector_long_init(&Qcohesion, 1));
  IGRAPH_FINALLY(igraph_vector_long_destroy, &Qcohesion);

  IGRAPH_CHECK(igraph_vector_bool_init(&Qcheck, 1));
  IGRAPH_FINALLY(igraph_vector_bool_destroy, &Qcheck);
  
  IGRAPH_CHECK(igraph_vector_ptr_init(&separators, 0));
  IGRAPH_FINALLY(igraph_vector_ptr_destroy, &separators);

  IGRAPH_VECTOR_INIT_FINALLY(&compvertices, 0);
  IGRAPH_CHECK(igraph_vector_bool_init(&marked, 0));
  IGRAPH_FINALLY(igraph_vector_bool_destroy, &marked);
  IGRAPH_VECTOR_INIT_FINALLY(&neis, 0);
  IGRAPH_CHECK(igraph_dqueue_init(&bfsQ, 100));
  IGRAPH_FINALLY(igraph_dqueue_destroy, &bfsQ);
  IGRAPH_CHECK(igraph_vector_long_init(&compid, 0));
  IGRAPH_FINALLY(igraph_vector_long_destroy, &compid);
  IGRAPH_CHECK(igraph_vector_long_init(&components, 0));
  IGRAPH_FINALLY(igraph_vector_long_destroy, &components);
  
  /* Put the input graph in the queue */
  graph_copy=igraph_Calloc(1, igraph_t);
  if (!graph_copy) { 
    IGRAPH_ERROR("Cannot do cohesive blocking", IGRAPH_ENOMEM);
  }
  IGRAPH_CHECK(igraph_copy(graph_copy, graph));
  VECTOR(Q)[0] = graph_copy;
  VECTOR(Qmapping)[0] = 0;	/* Identity mapping */
  VECTOR(Qparent)[0] = -1;	/* Has no parent */
  IGRAPH_CHECK(igraph_vertex_connectivity(graph, &conn, /*checks=*/ 1));
  VECTOR(Qcohesion)[0] = conn;
  VECTOR(Qcheck)[0] = 0;  

  /* Then work until the queue is empty */
  while (Qptr < igraph_vector_ptr_size(&Q)) {
    igraph_t *mygraph=VECTOR(Q)[Qptr];
    igraph_bool_t mycheck=VECTOR(Qcheck)[Qptr];
    long int mynodes=igraph_vcount(mygraph);
    long int i, nsep;
    long int no, kept=0;
    long int cptr=0;
    long int nsepv=0;
    igraph_bool_t addedsep=0;

    IGRAPH_STATUSF(("Candidate %li: %li vertices,", 
		    0, Qptr, mynodes));
    IGRAPH_ALLOW_INTERRUPTION();

    /* Get the separators */
    IGRAPH_CHECK(igraph_minimum_size_separators(mygraph, &separators));
    IGRAPH_FINALLY(igraph_i_cohesive_blocks_free3, &separators);
    nsep=igraph_vector_ptr_size(&separators);
    
    IGRAPH_STATUSF((" %li separators,", 0, nsep));

    /* Remove them from the graph, also mark them */    
    IGRAPH_CHECK(igraph_vector_bool_resize(&marked, mynodes));
    igraph_vector_bool_null(&marked);
    for (i=0; i<nsep; i++) {
      igraph_vector_t *v=VECTOR(separators)[i];
      long int j, n=igraph_vector_size(v);
      for (j=0; j<n; j++) {
	long int vv=(long int) VECTOR(*v)[j];
	if (!VECTOR(marked)[vv]) {
	  nsepv++;
	  VECTOR(marked)[vv] = 1;
	}
      }
    }
    
    /* Find the connected components, omitting the separator vertices, 
       but including the neighboring separator vertices
     */
    IGRAPH_CHECK(igraph_i_cb_components(mygraph, &marked, 
					&components, &no,
					&compid, &bfsQ, &neis));

    /* Add the separator vertices themselves, as another component,
       but only if there is at least one vertex not included in any 
       separator. */
    if (nsepv != mynodes) {
      addedsep=1;
      for (i=0; i<mynodes; i++) {
	if (VECTOR(marked)[i]) {
	  IGRAPH_CHECK(igraph_vector_long_push_back(&components, i));
	}
      }
      IGRAPH_CHECK(igraph_vector_long_push_back(&components, -1));
      no++;
    }

    IGRAPH_STATUSF((" %li new candidates,", 0, no));

    for (i=0; i<no; i++) {
      igraph_vector_t *newmapping;
      igraph_t *newgraph;
      igraph_integer_t maxdeg;

      igraph_vector_clear(&compvertices);
      
      while (1) {
	long int v=VECTOR(components)[cptr++];
	if (v < 0) { break; }
	IGRAPH_CHECK(igraph_vector_push_back(&compvertices, v));
      }
      
      newmapping=igraph_Calloc(1, igraph_vector_t);
      if (!newmapping) {
	IGRAPH_ERROR("Cannot do cohesive blocking", IGRAPH_ENOMEM);
      }
      IGRAPH_FINALLY(igraph_free, newmapping);
      IGRAPH_VECTOR_INIT_FINALLY(newmapping, 0);
      newgraph=igraph_Calloc(1, igraph_t);
      if (!newgraph) { 
	IGRAPH_ERROR("Cannot do cohesive blocking", IGRAPH_ENOMEM);
      }
      IGRAPH_FINALLY(igraph_free, newgraph);
      IGRAPH_CHECK(igraph_induced_subgraph_map(mygraph, newgraph, 
					igraph_vss_vector(&compvertices),
					IGRAPH_SUBGRAPH_AUTO,
					/*map=*/ 0,
					/*invmap=*/ newmapping));
      IGRAPH_FINALLY(igraph_destroy, newgraph);

      IGRAPH_CHECK(igraph_maxdegree(newgraph, &maxdeg, igraph_vss_all(),
				    IGRAPH_ALL, IGRAPH_LOOPS));
      if (maxdeg > VECTOR(Qcohesion)[Qptr]) {
	igraph_integer_t newconn;
	kept++;
	IGRAPH_CHECK(igraph_vector_ptr_push_back(&Q, newgraph));
	IGRAPH_FINALLY_CLEAN(2);
	IGRAPH_CHECK(igraph_vector_ptr_push_back(&Qmapping, newmapping));
	IGRAPH_FINALLY_CLEAN(2);
	IGRAPH_CHECK(igraph_vertex_connectivity(newgraph, &newconn, 
						/*checks=*/ 1));
	IGRAPH_CHECK(igraph_vector_long_push_back(&Qcohesion, newconn));
	IGRAPH_CHECK(igraph_vector_long_push_back(&Qparent, Qptr));
	IGRAPH_CHECK(igraph_vector_bool_push_back(&Qcheck, 
						  mycheck || addedsep));
      } else {
	igraph_destroy(newgraph);
	igraph_free(newgraph);
	igraph_vector_destroy(newmapping);
	igraph_free(newmapping);
	IGRAPH_FINALLY_CLEAN(4);
      }
    }

    IGRAPH_STATUSF((" keeping %li.\n", 0, kept));

    igraph_destroy(mygraph);
    igraph_free(mygraph);
    VECTOR(Q)[Qptr] = 0;
    igraph_i_cohesive_blocks_free3(&separators);
    IGRAPH_FINALLY_CLEAN(1);

    Qptr++;
  }

  igraph_vector_long_destroy(&components);
  igraph_vector_long_destroy(&compid);
  igraph_dqueue_destroy(&bfsQ);
  igraph_vector_destroy(&neis);
  igraph_vector_bool_destroy(&marked);
  igraph_vector_destroy(&compvertices);
  igraph_vector_ptr_destroy(&separators);
  IGRAPH_FINALLY_CLEAN(7);

  if (blocks || cohesion || parent || block_tree) {
    igraph_integer_t noblocks=(igraph_integer_t) Qptr, badblocks=0;
    igraph_vector_bool_t removed;
    long int i, resptr=0;
    igraph_vector_long_t rewritemap;
    
    IGRAPH_CHECK(igraph_vector_bool_init(&removed, noblocks));
    IGRAPH_FINALLY(igraph_vector_bool_destroy, &removed);
    IGRAPH_CHECK(igraph_vector_long_init(&rewritemap, noblocks));
    IGRAPH_FINALLY(igraph_vector_long_destroy, &rewritemap);

    for (i=1; i<noblocks; i++) {
      long int p=VECTOR(Qparent)[i];
      while (VECTOR(removed)[p]) { p=VECTOR(Qparent)[p]; }
      if (VECTOR(Qcohesion)[p] >= VECTOR(Qcohesion)[i]) {
	VECTOR(removed)[i]=1;
	badblocks++;
      }
    }

    /* Rewrite the mappings */
    for (i=1; i<Qptr; i++) {
      long int p=VECTOR(Qparent)[i];
      igraph_vector_t *mapping=VECTOR(Qmapping)[i];
      igraph_vector_t *pmapping=VECTOR(Qmapping)[p];
      long int j, n=igraph_vector_size(mapping);

      if (!pmapping) { continue; }
      for (j=0; j<n; j++) {
	long int v=(long int) VECTOR(*mapping)[j];
	VECTOR(*mapping)[j] = VECTOR(*pmapping)[v];
      }
    }

    /* Because we also put the separator vertices in the queue, it is 
       not ensured that the found blocks are not subsets of each other.
       We check this now. */
    for (i=1; i<noblocks; i++) {
      long int j, ic;
      igraph_vector_t *ivec;
      if (!VECTOR(Qcheck)[i] || VECTOR(removed)[i]) { continue; }
      ivec=VECTOR(Qmapping)[i];
      ic=VECTOR(Qcohesion)[i];
      for (j=1; j<noblocks; j++) {
	igraph_vector_t *jvec;
	long int jc;
	if (j==i || !VECTOR(Qcheck)[j] || VECTOR(removed)[j]) { continue; }
	jvec=VECTOR(Qmapping)[j];
	jc=VECTOR(Qcohesion)[j];
	if (igraph_i_cb_isin(ivec, jvec) && jc >= ic) { 
	  badblocks++; 
	  VECTOR(removed)[i]=1;
	  break;
	}
      }
    }
	  
    noblocks -= badblocks;

    if (blocks) { IGRAPH_CHECK(igraph_vector_ptr_resize(blocks, noblocks)); }
    if (cohesion) { IGRAPH_CHECK(igraph_vector_resize(cohesion, noblocks)); }
    if (parent) { IGRAPH_CHECK(igraph_vector_resize(parent, noblocks)); }

    for (i=0; i<Qptr; i++) {
      if (VECTOR(removed)[i]) { 
	IGRAPH_STATUSF(("Candidate %li ignored.\n", 0, i));
	continue; 
      } else {
	IGRAPH_STATUSF(("Candidate %li is a cohesive (sub)block\n", 0, i));
      }
      VECTOR(rewritemap)[i] = resptr;
      if (cohesion) { VECTOR(*cohesion)[resptr]=VECTOR(Qcohesion)[i]; }
      if (parent || block_tree) {
	long int p=VECTOR(Qparent)[i];
	while (p>=0 && VECTOR(removed)[p]) { p=VECTOR(Qparent)[p]; }
	if (p>=0) { p=VECTOR(rewritemap)[p]; }
	VECTOR(Qparent)[i]=p;
	if (parent) { VECTOR(*parent)[resptr]=p; }
      }
      if (blocks) {
	VECTOR(*blocks)[resptr]=VECTOR(Qmapping)[i];
	VECTOR(Qmapping)[i]=0;
      }
      resptr++;
    }

    /* Plus the original graph */
    if (blocks) {
      igraph_vector_t *orig=igraph_Calloc(1, igraph_vector_t);
      if (!orig) { 
	IGRAPH_ERROR("Cannot do cohesive blocking", IGRAPH_ENOMEM); 
      }
      IGRAPH_FINALLY(igraph_free, orig);
      IGRAPH_CHECK(igraph_vector_init_seq(orig, 0, igraph_vcount(graph)-1));
      VECTOR(*blocks)[0]=orig;
      IGRAPH_FINALLY_CLEAN(1);
    }

    if (block_tree) {
      igraph_vector_t edges;
      long int eptr=0;
      IGRAPH_VECTOR_INIT_FINALLY(&edges, noblocks*2-2);
      for (i=1; i<Qptr; i++) {
	if (VECTOR(removed)[i]) { continue; }
	VECTOR(edges)[eptr++] = VECTOR(Qparent)[i];
	VECTOR(edges)[eptr++] = VECTOR(rewritemap)[i];
      }
      
      IGRAPH_CHECK(igraph_create(block_tree, &edges, noblocks, 
				 IGRAPH_DIRECTED));
      igraph_vector_destroy(&edges);
      IGRAPH_FINALLY_CLEAN(1);
    }

    igraph_vector_long_destroy(&rewritemap);
    igraph_vector_bool_destroy(&removed);
    IGRAPH_FINALLY_CLEAN(2);

  }

  igraph_vector_bool_destroy(&Qcheck);
  igraph_vector_long_destroy(&Qcohesion);
  igraph_vector_long_destroy(&Qparent);
  igraph_i_cohesive_blocks_free2(&Qmapping);
  IGRAPH_FINALLY_CLEAN(4);
  
  igraph_vector_ptr_destroy(&Qmapping);
  igraph_vector_ptr_destroy(&Q);
  IGRAPH_FINALLY_CLEAN(3);  	/* + the elements of Q, they were
				   already destroyed */

  IGRAPH_STATUS("Cohesive blocking done.\n", 0);

  return 0;
}
Example #4
0
igraph_vector_t * ggen_analyze_longest_antichain(igraph_t *g)
{
	/* The following steps are implemented :
	 *  - Convert our DAG to a specific bipartite graph B
	 *  - solve maximum matching on B
	 *  - conver maximum matching to min vectex cover
	 *  - convert min vertex cover to antichain on G
	 */
	int err;
	unsigned long i,vg,found,added;
	igraph_t b,gstar;
	igraph_vector_t edges,*res = NULL;
	igraph_vector_t c,s,t,todo,n,next,l,r;
	igraph_eit_t eit;
	igraph_es_t es;
	igraph_integer_t from,to;
	igraph_vit_t vit;
	igraph_vs_t vs;
	igraph_real_t value;

	if(g == NULL)
		return NULL;

	/* before creating the bipartite graph, we need all relations
	 * between any two vertices : the transitive closure of g */
	err = igraph_copy(&gstar,g);
	if(err) return NULL;

	err = ggen_transform_transitive_closure(&gstar);
	if(err) goto error;

	/* Bipartite convertion : let G = (S,C),
	 * we build B = (U,V,E) with
	 *	- U = V = S (each vertex is present twice)
	 *	- (u,v) \in E iff :
	 *		- u \in U
	 *		- v \in V
	 *		- u < v in C (warning, this means that we take
	 *		transitive closure into account, not just the
	 *		original edges)
	 * We will also need two additional nodes further in the code.
	 */
	vg = igraph_vcount(g);
	err = igraph_empty(&b,vg*2,1);
	if(err) goto error;

	/* id and id+vg will be a vertex in U and its copy in V,
	 * iterate over gstar edges to create edges in b
	 */
	err = igraph_vector_init(&edges,igraph_ecount(&gstar));
	if(err) goto d_b;
	igraph_vector_clear(&edges);

	err = igraph_eit_create(&gstar,igraph_ess_all(IGRAPH_EDGEORDER_ID),&eit);
	if(err) goto d_edges;

	for(IGRAPH_EIT_RESET(eit); !IGRAPH_EIT_END(eit); IGRAPH_EIT_NEXT(eit))
	{
		err = igraph_edge(&gstar,IGRAPH_EIT_GET(eit),&from,&to);
		if(err)
		{
			igraph_eit_destroy(&eit);
			goto d_edges;
		}
		to += vg;
		igraph_vector_push_back(&edges,(igraph_real_t)from);
		igraph_vector_push_back(&edges,(igraph_real_t)to);
	}
	igraph_eit_destroy(&eit);
	err = igraph_add_edges(&b,&edges,NULL);
	if(err) goto d_edges;

	/* maximum matching on b */
	igraph_vector_clear(&edges);
	err = bipartite_maximum_matching(&b,&edges);
	if(err) goto d_edges;

	/* Let M be the max matching, and N be E - M
	 * Define T as all unmatched vectices from U as well as all vertices
	 * reachable from those by going left-to-right along N and right-to-left along
	 * M.
	 * Define L = U - T, R = V \inter T
	 * C:= L + R
	 * C is a minimum vertex cover
	 */
	err = igraph_vector_init_seq(&n,0,igraph_ecount(&b)-1);
	if(err) goto d_edges;

	err = vector_diff(&n,&edges);
	if(err) goto d_n;

	err = igraph_vector_init(&c,vg);
	if(err) goto d_n;
	igraph_vector_clear(&c);

	/* matched vertices : S */
	err = igraph_vector_init(&s,vg);
	if(err) goto d_c;
	igraph_vector_clear(&s);

	for(i = 0; i < igraph_vector_size(&edges); i++)
	{
		err = igraph_edge(&b,VECTOR(edges)[i],&from,&to);
		if(err) goto d_s;

		igraph_vector_push_back(&s,from);
	}
	/* we may have inserted the same vertex multiple times */
	err = vector_uniq(&s);
	if(err) goto d_s;

	/* unmatched */
	err = igraph_vector_init_seq(&t,0,vg-1);
	if(err) goto d_s;

	err = vector_diff(&t,&s);
	if(err) goto d_t;

	/* alternating paths
	 */
	err = igraph_vector_copy(&todo,&t);
	if(err) goto d_t;

	err = igraph_vector_init(&next,vg);
	if(err) goto d_todo;
	igraph_vector_clear(&next);
	do {
		vector_uniq(&todo);
		added = 0;
		for(i = 0; i < igraph_vector_size(&todo); i++)
		{
			if(VECTOR(todo)[i] < vg)
			{
				/* scan edges */
				err = igraph_es_adj(&es,VECTOR(todo)[i],IGRAPH_OUT);
				if(err) goto d_next;
				err = igraph_eit_create(&b,es,&eit);
				if(err)
				{
					igraph_es_destroy(&es);
					goto d_next;
				}
				for(IGRAPH_EIT_RESET(eit); !IGRAPH_EIT_END(eit); IGRAPH_EIT_NEXT(eit))
				{
					if(igraph_vector_binsearch(&n,IGRAPH_EIT_GET(eit),NULL))
					{
						err = igraph_edge(&b,IGRAPH_EIT_GET(eit),&from,&to);
						if(err)
						{
							igraph_eit_destroy(&eit);
							igraph_es_destroy(&es);
							goto d_next;
						}
						if(!igraph_vector_binsearch(&t,to,NULL))
						{
							igraph_vector_push_back(&next,to);
							added = 1;
						}
					}
				}
			}
			else
			{
				/* scan edges */
				err = igraph_es_adj(&es,VECTOR(todo)[i],IGRAPH_IN);
				if(err) goto d_next;
				err = igraph_eit_create(&b,es,&eit);
				if(err)
				{
					igraph_es_destroy(&es);
					goto d_next;
				}
				for(IGRAPH_EIT_RESET(eit); !IGRAPH_EIT_END(eit); IGRAPH_EIT_NEXT(eit))
				{
					if(igraph_vector_binsearch(&edges,IGRAPH_EIT_GET(eit),NULL))
					{
						err = igraph_edge(&b,IGRAPH_EIT_GET(eit),&from,&to);
						if(err)
						{
							igraph_eit_destroy(&eit);
							igraph_es_destroy(&es);
							goto d_next;
						}
						if(!igraph_vector_binsearch(&t,to,NULL))
						{
							igraph_vector_push_back(&next,from);
							added = 1;
						}
					}
				}
			}
			igraph_es_destroy(&es);
			igraph_eit_destroy(&eit);
		}
		igraph_vector_append(&t,&todo);
		igraph_vector_clear(&todo);
		igraph_vector_append(&todo,&next);
		igraph_vector_clear(&next);
	} while(added);

	err = igraph_vector_init_seq(&l,0,vg-1);
	if(err) goto d_t;

	err = vector_diff(&l,&t);
	if(err) goto d_l;

	err = igraph_vector_update(&c,&l);
	if(err) goto d_l;

	err = igraph_vector_init(&r,vg);
	if(err) goto d_l;
	igraph_vector_clear(&r);

	/* compute V \inter T */
	for(i = 0; i < igraph_vector_size(&t); i++)
	{
		if(VECTOR(t)[i] >= vg)
			igraph_vector_push_back(&r,VECTOR(t)[i]);
	}

	igraph_vector_add_constant(&r,(igraph_real_t)-vg);
	err = vector_union(&c,&r);
	if(err) goto d_r;

	/* our antichain is U - C */
	res = malloc(sizeof(igraph_vector_t));
	if(res == NULL) goto d_r;

	err = igraph_vector_init_seq(res,0,vg-1);
	if(err) goto f_res;

	err = vector_diff(res,&c);
	if(err) goto d_res;

	goto ret;
d_res:
	igraph_vector_destroy(res);
f_res:
	free(res);
	res = NULL;
ret:
d_r:
	igraph_vector_destroy(&r);
d_l:
	igraph_vector_destroy(&l);
d_next:
	igraph_vector_destroy(&next);
d_todo:
	igraph_vector_destroy(&todo);
d_t:
	igraph_vector_destroy(&t);
d_s:
	igraph_vector_destroy(&s);
d_c:
	igraph_vector_destroy(&c);
d_n:
	igraph_vector_destroy(&n);
d_edges:
	igraph_vector_destroy(&edges);
d_b:
	igraph_destroy(&b);
error:
	igraph_destroy(&gstar);
	return res;
}
Example #5
0
/**
 * Loops through the graph and spreads the infection
 * from sick individuals to their neighbors
 */
void spread_infection(igraph_t *graph, igraph_t *new_graph) {
	igraph_vector_t neighbors;

	double T;
	float random; // Random number to compare against transmission probability T to see if the infection is passed on
	int num_neighbors;

	// Loop through all vertices looking for infected individuals
	for(int i = 0; i < NETWORK_SIZE; i++) {
		// LATENT
		if(state_get(graph, i) == LATENT) {
			// If it is after its first day in latent state, become infectious
			if(state_counter_get(graph, i) == DAYS_LATENT) {
				state_set(&new_graph, i, INFECTIOUS);
				state_counter_set(&new_graph, i, 0);
			}
		}
		// INFECTIOUS
		else if(state_get(graph, i) == INFECTIOUS) {
			// If it has been infectious for the specified time, move to recovered class
			if(state_counter_get(graph, i) == DAYS_INFECTIOUS) {
				state_set(&new_graph, i, RECOVERED);
				state_counter_set(&new_graph, i, 0);
			}
			else {
				igraph_neighbors(graph, &neighbors, i, IGRAPH_ALL);
				num_neighbors = igraph_vector_size(&neighbors);
				
				 // The probability of the disease spreading to a given neighbor
				 // is a function of the infected vertex's degree.
				 //
				 // The function: T = 1 - (1 - R0/k) * (1/d)
				 //
				 // Where T is the transmission probability
				 // R0 is a fixed constant representing the R0 of the disease
				 // k is the degree
				 // and d is the number of days spent in the infectious class

				T = 1 - (1 - ((R0/num_neighbors) / (double)DAYS_INFECTIOUS));

				// Loop through the neighbors of the infected individual
				
				for(int n = 0; n < num_neighbors; n++) {

					// Only infect the neighbor if they are susceptible
					if(state_get(graph, (int)VECTOR(neighbors)[n]) == SUSCEPTIBLE) {

						// Generates float from 0.0-1.0 inclusive
						random = (float)rand()/(float)RAND_MAX; 

						// Pass on the transmission with probability T
						if(random < T) {
							state_set(&new_graph, (int)VECTOR(neighbors)[n], LATENT);
							state_counter_set(&new_graph, (int)VECTOR(neighbors)[n], 0);
						}

					}
				}

			}
		}
		// RECOVERED
		else if(state_get(graph, i) == RECOVERED) {
			if(state_counter_get(graph, i) == DAYS_RECOVERED) {
				state_set(&new_graph, i, SUSCEPTIBLE);
				state_counter_set(&new_graph, i, 0);
			}
		}
	}

	igraph_vector_destroy(&neighbors);

	state_counter_increment(&new_graph);

	igraph_copy(graph, &new_graph);

	igraph_destroy(&new_graph);
}
Example #6
0
int igraph_community_multilevel(const igraph_t *graph,
  const igraph_vector_t *weights, igraph_vector_t *membership,
  igraph_matrix_t *memberships, igraph_vector_t *modularity) {
 
  igraph_t g;
  igraph_vector_t w, m, level_membership;
  igraph_real_t prev_q = -1, q = -1;
  int i, level = 1;
  long int vcount = igraph_vcount(graph);

  /* Make a copy of the original graph, we will do the merges on the copy */
  IGRAPH_CHECK(igraph_copy(&g, graph));
  IGRAPH_FINALLY(igraph_destroy, &g);

  if (weights) {
    IGRAPH_CHECK(igraph_vector_copy(&w, weights));   
    IGRAPH_FINALLY(igraph_vector_destroy, &w);  
  } else {
    IGRAPH_VECTOR_INIT_FINALLY(&w, igraph_ecount(&g));
    igraph_vector_fill(&w, 1);
  }

  IGRAPH_VECTOR_INIT_FINALLY(&m, vcount);
  IGRAPH_VECTOR_INIT_FINALLY(&level_membership, vcount);

  if (memberships || membership) {
    /* Put each vertex in its own community */
    for (i = 0; i < vcount; i++) {
      VECTOR(level_membership)[i] = i;
    }
  }
  if (memberships) {
    /* Resize the membership matrix to have vcount columns and no rows */
    IGRAPH_CHECK(igraph_matrix_resize(memberships, 0, vcount));
  }
  if (modularity) {
    /* Clear the modularity vector */
    igraph_vector_clear(modularity);
  }
  
  while (1) {
    /* Remember the previous modularity and vertex count, do a single step */
    igraph_integer_t step_vcount = igraph_vcount(&g);

    prev_q = q;
    IGRAPH_CHECK(igraph_i_community_multilevel_step(&g, &w, &m, &q));
 
    /* Were there any merges? If not, we have to stop the process */
    if (igraph_vcount(&g) == step_vcount || q < prev_q)
      break;

    if (memberships || membership) {
      for (i = 0; i < vcount; i++) {
        /* Readjust the membership vector */
        VECTOR(level_membership)[i] = VECTOR(m)[(long int) VECTOR(level_membership)[i]];
      }
        
    }

    if (modularity) {
      /* If we have to return the modularity scores, add it to the modularity vector */
      IGRAPH_CHECK(igraph_vector_push_back(modularity, q));
    }

    if (memberships) {
      /* If we have to return the membership vectors at each level, store the new
       * membership vector */
      IGRAPH_CHECK(igraph_matrix_add_rows(memberships, 1));
      IGRAPH_CHECK(igraph_matrix_set_row(memberships, &level_membership, level - 1));
    }

    /* debug("Level: %d Communities: %ld Modularity: %f\n", level, (long int) igraph_vcount(&g),
      (double) q); */

    /* Increase the level counter */
    level++;
  }

  /* It might happen that there are no merges, so every vertex is in its 
     own community. We still might want the modularity score for that. */
  if (modularity && igraph_vector_size(modularity) == 0) {
    igraph_vector_t tmp;
    igraph_real_t mod;
    int i;
    IGRAPH_VECTOR_INIT_FINALLY(&tmp, vcount);
    for (i=0; i<vcount; i++) { VECTOR(tmp)[i]=i; }
    IGRAPH_CHECK(igraph_modularity(graph, &tmp, &mod, weights));
    igraph_vector_destroy(&tmp);
    IGRAPH_FINALLY_CLEAN(1);
    IGRAPH_CHECK(igraph_vector_resize(modularity, 1));
    VECTOR(*modularity)[0]=mod;
  }

  /* If we need the final membership vector, copy it to the output */
  if (membership) {
    IGRAPH_CHECK(igraph_vector_resize(membership, vcount));   
    for (i = 0; i < vcount; i++) {
      VECTOR(*membership)[i] = VECTOR(level_membership)[i];
    }
  }

  /* Destroy the copy of the graph */
  igraph_destroy(&g);

  /* Destroy the temporary vectors */
  igraph_vector_destroy(&m);
  igraph_vector_destroy(&w);
  igraph_vector_destroy(&level_membership);
  IGRAPH_FINALLY_CLEAN(4);

  return 0;
}
Example #7
0
int main() {
  
  igraph_t g, g2;
  FILE *ifile;
  igraph_vector_t gtypes, vtypes, etypes;
  igraph_strvector_t gnames, vnames, enames;
  long int i;
  igraph_vector_t y;
  igraph_strvector_t id;
  char str[20];

  /* turn on attribute handling */
  igraph_i_set_attribute_table(&igraph_cattribute_table);
  
  ifile=fopen("LINKS.NET", "r");
  if (ifile==0) {
    return 10;
  }
  igraph_read_graph_pajek(&g, ifile);
  fclose(ifile);

  igraph_vector_init(&gtypes, 0);
  igraph_vector_init(&vtypes, 0);
  igraph_vector_init(&etypes, 0);
  igraph_strvector_init(&gnames, 0);
  igraph_strvector_init(&vnames, 0);
  igraph_strvector_init(&enames, 0);
  
  igraph_cattribute_list(&g, &gnames, &gtypes, &vnames, &vtypes, 
			 &enames, &etypes);
  
  /* List attribute names and types */
  printf("Graph attributes: ");
  for (i=0; i<igraph_strvector_size(&gnames); i++) {
    printf("%s (%i) ", STR(gnames, i), (int)VECTOR(gtypes)[i]);
  }
  printf("\n");
  printf("Vertex attributes: ");
  for (i=0; i<igraph_strvector_size(&vnames); i++) {
    printf("%s (%i) ", STR(vnames, i), (int)VECTOR(vtypes)[i]);
  }
  printf("\n");
  printf("Edge attributes: ");
  for (i=0; i<igraph_strvector_size(&enames); i++) {
    printf("%s (%i) ", STR(enames, i), (int)VECTOR(etypes)[i]);
  }
  printf("\n");

  print_attributes(&g);

  /* Copying a graph */
  igraph_copy(&g2, &g);
  print_attributes(&g2);
  igraph_destroy(&g2);
  
  /* Adding vertices */
  igraph_add_vertices(&g, 3, 0);
  print_attributes(&g);

  /* Adding edges */
  igraph_add_edge(&g, 1, 1);
  igraph_add_edge(&g, 2, 5);
  igraph_add_edge(&g, 3, 6);
  print_attributes(&g);

  /* Deleting vertices */
  igraph_delete_vertices(&g, igraph_vss_1(1));
  igraph_delete_vertices(&g, igraph_vss_1(4));
  print_attributes(&g);

  /* Deleting edges */
  igraph_delete_edges(&g, igraph_ess_1(igraph_ecount(&g)-1));
  igraph_delete_edges(&g, igraph_ess_1(0));
  print_attributes(&g);

  /* Set graph attributes */
  SETGAN(&g, "id", 10);
  if (GAN(&g, "id") != 10) {
    return 11;
  }
  SETGAS(&g, "name", "toy");
  if (strcmp(GAS(&g, "name"), "toy")) {
    return 12;
  }
  
  /* Delete graph attributes */
  DELGA(&g, "id");
  DELGA(&g, "name");
  igraph_cattribute_list(&g, &gnames, 0,0,0,0,0);
  if (igraph_strvector_size(&gnames) != 0) {
    return 14;
  }  

  /* Delete vertex attributes */
  DELVA(&g, "x");
  DELVA(&g, "shape");
  DELVA(&g, "xfact");
  DELVA(&g, "yfact");
  igraph_cattribute_list(&g, 0,0, &vnames, 0,0,0);  
  if (igraph_strvector_size(&vnames) != 2) {
    return 15;
  }
  
  /* Delete edge attributes */
  igraph_cattribute_list(&g, 0,0,0,0,&enames,0);
  i=igraph_strvector_size(&enames);
  DELEA(&g, "hook1");
  DELEA(&g, "hook2"); 
  DELEA(&g, "label");
  igraph_cattribute_list(&g, 0,0,0,0,&enames,0);
  if (igraph_strvector_size(&enames) != i-3) {
    return 16;
  }
  
  /* Set vertex attributes */
  SETVAN(&g, "y", 0, -1);
  SETVAN(&g, "y", 1, 2.1);
  if (VAN(&g, "y", 0) != -1 || 
      VAN(&g, "y", 1) != 2.1) {
    return 17;
  }
  SETVAS(&g, "id", 0, "foo");
  SETVAS(&g, "id", 1, "bar");
  if (strcmp(VAS(&g, "id", 0), "foo") ||
      strcmp(VAS(&g, "id", 1), "bar")) {
    return 18;
  }

  /* Set edge attributes */
  SETEAN(&g, "weight", 2, 100.0);
  SETEAN(&g, "weight", 0, -100.1);
  if (EAN(&g, "weight", 2) != 100.0 ||
      EAN(&g, "weight", 0) != -100.1) {
    return 19;
  }
  SETEAS(&g, "color", 2, "RED");
  SETEAS(&g, "color", 0, "Blue");
  if (strcmp(EAS(&g, "color", 2), "RED") ||
      strcmp(EAS(&g, "color", 0), "Blue")) {
    return 20;
  }      

  /* Set vector attributes as vector */
  igraph_vector_init(&y, igraph_vcount(&g));
  igraph_vector_fill(&y, 1.23);
  SETVANV(&g, "y", &y);
  igraph_vector_destroy(&y);
  for (i=0; i<igraph_vcount(&g); i++) {    
    if (VAN(&g, "y", i) != 1.23) {
      return 21;
    }
  }
  igraph_vector_init_seq(&y, 0, igraph_vcount(&g)-1);
  SETVANV(&g, "foobar", &y);
  igraph_vector_destroy(&y);
  for (i=0; i<igraph_vcount(&g); i++) {
    if (VAN(&g, "foobar", i) != i) {
      return 22;
    }
  }  
  
  igraph_strvector_init(&id, igraph_vcount(&g));
  for (i=0; i<igraph_vcount(&g); i++) {
    snprintf(str, sizeof(str)-1, "%li", i);
    igraph_strvector_set(&id, i, str);
  }
  SETVASV(&g, "foo", &id);
  igraph_strvector_destroy(&id);
  for (i=0; i<igraph_vcount(&g); i++) {
    printf("%s ", VAS(&g, "foo", i));
  }
  printf("\n");
  igraph_strvector_init(&id, igraph_vcount(&g));
  for (i=0; i<igraph_vcount(&g); i++) {
    snprintf(str, sizeof(str)-1, "%li", i);
    igraph_strvector_set(&id, i, str);
  }
  SETVASV(&g, "id", &id);
  igraph_strvector_destroy(&id);
  for (i=0; i<igraph_vcount(&g); i++) {
    printf("%s ", VAS(&g, "id", i));
  }
  printf("\n");  
  
  /* Set edge attributes as vector */
  igraph_vector_init(&y, igraph_ecount(&g));
  igraph_vector_fill(&y, 12.3);
  SETEANV(&g, "weight", &y);
  igraph_vector_destroy(&y);
  for (i=0; i<igraph_ecount(&g); i++) {    
    if (EAN(&g, "weight", i) != 12.3) {
      return 23;
    }
  }
  igraph_vector_init_seq(&y, 0, igraph_ecount(&g)-1);
  SETEANV(&g, "foobar", &y);
  igraph_vector_destroy(&y);
  for (i=0; i<igraph_ecount(&g); i++) {
    if (VAN(&g, "foobar", i) != i) {
      return 24;
    }
  }  
  
  igraph_strvector_init(&id, igraph_ecount(&g));
  for (i=0; i<igraph_ecount(&g); i++) {
    snprintf(str, sizeof(str)-1, "%li", i);
    igraph_strvector_set(&id, i, str);
  }
  SETEASV(&g, "foo", &id);
  igraph_strvector_destroy(&id);
  for (i=0; i<igraph_ecount(&g); i++) {
    printf("%s ", EAS(&g, "foo", i));
  }
  printf("\n");
  igraph_strvector_init(&id, igraph_ecount(&g));
  for (i=0; i<igraph_ecount(&g); i++) {
    snprintf(str, sizeof(str)-1, "%li", i);
    igraph_strvector_set(&id, i, str);
  }
  SETEASV(&g, "color", &id);
  igraph_strvector_destroy(&id);
  for (i=0; i<igraph_ecount(&g); i++) {
    printf("%s ", EAS(&g, "color", i));
  }
  printf("\n");    

  /* Delete all remaining attributes */
  DELALL(&g);
  igraph_cattribute_list(&g, &gnames, &gtypes, &vnames, &vtypes, &enames, &etypes);
  if (igraph_strvector_size(&gnames) != 0 ||
      igraph_strvector_size(&vnames) != 0 ||
      igraph_strvector_size(&enames) != 0) {
    return 25;
  }

  /* Destroy */
  igraph_vector_destroy(&gtypes);
  igraph_vector_destroy(&vtypes);
  igraph_vector_destroy(&etypes);  
  igraph_strvector_destroy(&gnames);
  igraph_strvector_destroy(&vnames);
  igraph_strvector_destroy(&enames);

  igraph_destroy(&g);

  return 0;
}
Example #8
0
Graph::Graph(Graph const& other) : size(other.size), graph(new igraph_t) {
  igraph_copy(graph, other.graph);
}  
Example #9
0
int main() {
  
  igraph_t g, g2;
  igraph_vector_t weight;
  igraph_attribute_combination_t comb;
  
  igraph_i_set_attribute_table(&igraph_cattribute_table);
  
  igraph_small(&g, 4, IGRAPH_DIRECTED, 
	       0, 1, 0, 1, 0, 1,
	       1, 2, 2, 3, 
	       -1);
  
  igraph_vector_init_seq(&weight, 1, igraph_ecount(&g));
  SETEANV(&g, "weight", &weight);
  igraph_vector_destroy(&weight);

  /* ****************************************************** */
  igraph_copy(&g2, &g);
  igraph_attribute_combination(&comb, 
			       "weight", IGRAPH_ATTRIBUTE_COMBINE_SUM,
			       "",       IGRAPH_ATTRIBUTE_COMBINE_IGNORE, 
			       IGRAPH_NO_MORE_ATTRIBUTES);
  igraph_simplify(&g2, /*multiple=*/ 1, /*loops=*/ 1, &comb);
  igraph_attribute_combination_destroy(&comb);
  igraph_write_graph_graphml(&g2, stdout);
  igraph_destroy(&g2);
  /* ****************************************************** */

  /* ****************************************************** */
  igraph_copy(&g2, &g);
  igraph_attribute_combination(&comb, 
			       "weight", IGRAPH_ATTRIBUTE_COMBINE_PROD,
			       "",       IGRAPH_ATTRIBUTE_COMBINE_IGNORE, 
			       IGRAPH_NO_MORE_ATTRIBUTES);
  igraph_simplify(&g2, /*multiple=*/ 1, /*loops=*/ 1, &comb);
  igraph_attribute_combination_destroy(&comb);
  igraph_write_graph_graphml(&g2, stdout);
  igraph_destroy(&g2);
  /* ****************************************************** */

  /* ****************************************************** */
  igraph_copy(&g2, &g);
  igraph_attribute_combination(&comb, 
			       "weight", IGRAPH_ATTRIBUTE_COMBINE_MIN,
			       "",       IGRAPH_ATTRIBUTE_COMBINE_IGNORE, 
			       IGRAPH_NO_MORE_ATTRIBUTES);
  igraph_simplify(&g2, /*multiple=*/ 1, /*loops=*/ 1, &comb);
  igraph_attribute_combination_destroy(&comb);
  igraph_write_graph_graphml(&g2, stdout);
  igraph_destroy(&g2);
  /* ****************************************************** */

  /* ****************************************************** */
  igraph_copy(&g2, &g);
  igraph_attribute_combination(&comb, 
			       "weight", IGRAPH_ATTRIBUTE_COMBINE_MAX,
			       "",       IGRAPH_ATTRIBUTE_COMBINE_IGNORE, 
			       IGRAPH_NO_MORE_ATTRIBUTES);
  igraph_simplify(&g2, /*multiple=*/ 1, /*loops=*/ 1, &comb);
  igraph_attribute_combination_destroy(&comb);
  igraph_write_graph_graphml(&g2, stdout);
  igraph_destroy(&g2);
  /* ****************************************************** */

  /* ****************************************************** */
  igraph_copy(&g2, &g);
  igraph_attribute_combination(&comb, 
			       "weight", IGRAPH_ATTRIBUTE_COMBINE_FIRST,
			       "",       IGRAPH_ATTRIBUTE_COMBINE_IGNORE, 
			       IGRAPH_NO_MORE_ATTRIBUTES);
  igraph_simplify(&g2, /*multiple=*/ 1, /*loops=*/ 1, &comb);
  igraph_attribute_combination_destroy(&comb);
  igraph_write_graph_graphml(&g2, stdout);
  igraph_destroy(&g2);
  /* ****************************************************** */

  /* ****************************************************** */
  igraph_copy(&g2, &g);
  igraph_attribute_combination(&comb, 
			       "weight", IGRAPH_ATTRIBUTE_COMBINE_LAST,
			       "",       IGRAPH_ATTRIBUTE_COMBINE_IGNORE, 
			       IGRAPH_NO_MORE_ATTRIBUTES);
  igraph_simplify(&g2, /*multiple=*/ 1, /*loops=*/ 1, &comb);
  igraph_attribute_combination_destroy(&comb);
  igraph_write_graph_graphml(&g2, stdout);
  igraph_destroy(&g2);
  /* ****************************************************** */

  /* ****************************************************** */
  igraph_copy(&g2, &g);
  igraph_attribute_combination(&comb, 
			       "weight", IGRAPH_ATTRIBUTE_COMBINE_MEAN,
			       "",       IGRAPH_ATTRIBUTE_COMBINE_IGNORE, 
			       IGRAPH_NO_MORE_ATTRIBUTES);
  igraph_simplify(&g2, /*multiple=*/ 1, /*loops=*/ 1, &comb);
  igraph_attribute_combination_destroy(&comb);
  igraph_write_graph_graphml(&g2, stdout);
  igraph_destroy(&g2);
  /* ****************************************************** */  

  /* ****************************************************** */
  igraph_copy(&g2, &g);
  igraph_attribute_combination(&comb, 
			       "weight", IGRAPH_ATTRIBUTE_COMBINE_FUNCTION, mf,
			       "",       IGRAPH_ATTRIBUTE_COMBINE_IGNORE, 
			       IGRAPH_NO_MORE_ATTRIBUTES);
  igraph_simplify(&g2, /*multiple=*/ 1, /*loops=*/ 1, &comb);
  igraph_attribute_combination_destroy(&comb);
  igraph_write_graph_graphml(&g2, stdout);
  igraph_destroy(&g2);
  /* ****************************************************** */  

  /* ****************************************************** */
  igraph_copy(&g2, &g);
  igraph_attribute_combination(&comb, 
			       "",       IGRAPH_ATTRIBUTE_COMBINE_MEAN,
			       IGRAPH_NO_MORE_ATTRIBUTES);
  igraph_simplify(&g2, /*multiple=*/ 1, /*loops=*/ 1, &comb);
  igraph_attribute_combination_destroy(&comb);
  igraph_write_graph_graphml(&g2, stdout);
  igraph_destroy(&g2);
  /* ****************************************************** */  

  igraph_destroy(&g);

  return 0;
}
Example #10
0
int main() {
  
  igraph_t g, g2;
  igraph_attribute_combination_t comb;
  
  igraph_i_set_attribute_table(&igraph_cattribute_table);
  
  igraph_small(&g, 4, IGRAPH_DIRECTED, 
	       0, 1, 0, 1, 0, 1,
	       1, 2, 2, 3, 
	       -1);
    
  SETEAB(&g, "type", 0, 1);
  SETEAB(&g, "type", 1, 1);
  SETEAB(&g, "type", 2, 0);
  SETEAB(&g, "type", 3, 0);
  SETEAB(&g, "type", 4, 1);
  
  /* ****************************************************** */
  igraph_copy(&g2, &g);
  igraph_attribute_combination(&comb, 
			       "weight", IGRAPH_ATTRIBUTE_COMBINE_SUM,
			       "type",   IGRAPH_ATTRIBUTE_COMBINE_FIRST,
			       "",       IGRAPH_ATTRIBUTE_COMBINE_IGNORE, 
			       IGRAPH_NO_MORE_ATTRIBUTES);
  igraph_simplify(&g2, /*multiple=*/ 1, /*loops=*/ 1, &comb);
  igraph_attribute_combination_destroy(&comb);
  igraph_write_graph_graphml(&g2, stdout, /*prefixattr=*/ 1);
  igraph_destroy(&g2);
  /* ****************************************************** */

  /* ****************************************************** */
  igraph_copy(&g2, &g);
  igraph_attribute_combination(&comb, 
			       "",       IGRAPH_ATTRIBUTE_COMBINE_LAST,
			       IGRAPH_NO_MORE_ATTRIBUTES);
  igraph_simplify(&g2, /*multiple=*/ 1, /*loops=*/ 1, &comb);
  igraph_attribute_combination_destroy(&comb);
  igraph_write_graph_graphml(&g2, stdout, /*prefixattr=*/ 1);
  igraph_destroy(&g2);
  /* ****************************************************** */

  /* ****************************************************** */
  igraph_copy(&g2, &g);
  igraph_attribute_combination(&comb, 
			       "",       IGRAPH_ATTRIBUTE_COMBINE_IGNORE, 
			       "type",   IGRAPH_ATTRIBUTE_COMBINE_LAST,
			       IGRAPH_NO_MORE_ATTRIBUTES);
  igraph_simplify(&g2, /*multiple=*/ 1, /*loops=*/ 1, &comb);
  igraph_attribute_combination_destroy(&comb);
  igraph_write_graph_graphml(&g2, stdout, /*prefixattr=*/ 1);
  igraph_destroy(&g2);
  /* ****************************************************** */
  
  /* ****************************************************** */
  igraph_copy(&g2, &g);
  igraph_attribute_combination(&comb, 
			       "",       IGRAPH_ATTRIBUTE_COMBINE_IGNORE, 
			       "type",   IGRAPH_ATTRIBUTE_COMBINE_SUM,
			       IGRAPH_NO_MORE_ATTRIBUTES);
  igraph_simplify(&g2, /*multiple=*/ 1, /*loops=*/ 1, &comb);
  igraph_attribute_combination_destroy(&comb);
  igraph_write_graph_graphml(&g2, stdout, /*prefixattr=*/ 1);
  igraph_destroy(&g2);
  /* ****************************************************** */
  
  /* ****************************************************** */
  igraph_copy(&g2, &g);
  igraph_attribute_combination(&comb, 
			       "",       IGRAPH_ATTRIBUTE_COMBINE_IGNORE, 
			       "type",   IGRAPH_ATTRIBUTE_COMBINE_PROD,
			       IGRAPH_NO_MORE_ATTRIBUTES);
  igraph_simplify(&g2, /*multiple=*/ 1, /*loops=*/ 1, &comb);
  igraph_attribute_combination_destroy(&comb);
  igraph_write_graph_graphml(&g2, stdout, /*prefixattr=*/ 1);
  igraph_destroy(&g2);
  /* ****************************************************** */
  
  /* ****************************************************** */
  igraph_copy(&g2, &g);
  igraph_attribute_combination(&comb, 
			       "",       IGRAPH_ATTRIBUTE_COMBINE_IGNORE, 
			       "type",   IGRAPH_ATTRIBUTE_COMBINE_MIN,
			       IGRAPH_NO_MORE_ATTRIBUTES);
  igraph_simplify(&g2, /*multiple=*/ 1, /*loops=*/ 1, &comb);
  igraph_attribute_combination_destroy(&comb);
  igraph_write_graph_graphml(&g2, stdout, /*prefixattr=*/ 1);
  igraph_destroy(&g2);
  /* ****************************************************** */
  
  /* ****************************************************** */
  igraph_copy(&g2, &g);
  igraph_attribute_combination(&comb, 
			       "",       IGRAPH_ATTRIBUTE_COMBINE_IGNORE, 
			       "type",   IGRAPH_ATTRIBUTE_COMBINE_MAX,
			       IGRAPH_NO_MORE_ATTRIBUTES);
  igraph_simplify(&g2, /*multiple=*/ 1, /*loops=*/ 1, &comb);
  igraph_attribute_combination_destroy(&comb);
  igraph_write_graph_graphml(&g2, stdout, /*prefixattr=*/ 1);
  igraph_destroy(&g2);
  /* ****************************************************** */
  
  /* ****************************************************** */
  igraph_copy(&g2, &g);
  igraph_attribute_combination(&comb, 
			       "",       IGRAPH_ATTRIBUTE_COMBINE_IGNORE, 
			       "type",   IGRAPH_ATTRIBUTE_COMBINE_MEAN,
			       IGRAPH_NO_MORE_ATTRIBUTES);
  igraph_simplify(&g2, /*multiple=*/ 1, /*loops=*/ 1, &comb);
  igraph_attribute_combination_destroy(&comb);
  igraph_write_graph_graphml(&g2, stdout, /*prefixattr=*/ 1);
  igraph_destroy(&g2);
  /* ****************************************************** */
  
  /* ****************************************************** */
  igraph_copy(&g2, &g);
  igraph_attribute_combination(&comb, 
			       "",       IGRAPH_ATTRIBUTE_COMBINE_IGNORE, 
			       "type",   IGRAPH_ATTRIBUTE_COMBINE_MEDIAN,
			       IGRAPH_NO_MORE_ATTRIBUTES);
  igraph_simplify(&g2, /*multiple=*/ 1, /*loops=*/ 1, &comb);
  igraph_attribute_combination_destroy(&comb);
  igraph_write_graph_graphml(&g2, stdout, /*prefixattr=*/ 1);
  igraph_destroy(&g2);
  /* ****************************************************** */
  
  igraph_destroy(&g);

  return 0;
}