예제 #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;
}
예제 #2
0
void graph_save(igraph_t *graph, char *name) {
	FILE *file;
	file = fopen(name, "wb");
	igraph_write_graph_graphml(graph, file);
	fclose(file);
}
예제 #3
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;
}
예제 #4
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;
}
예제 #5
0
int main(int argc, char **argv) {
  igraph_t g;
  igraph_error_handler_t* oldhandler;
  igraph_warning_handler_t* oldwarnhandler;
  int result;
  FILE *ifile, *ofile;

  igraph_i_set_attribute_table(&igraph_cattribute_table);

  /* GraphML */
  ifile=fopen("test.gxl", "r");
  if (ifile==0) {
    return 10;
  }
  
  oldhandler=igraph_set_error_handler(igraph_error_handler_ignore);
  oldwarnhandler=igraph_set_warning_handler(custom_warning_handler);
  if ((result=igraph_read_graph_graphml(&g, ifile, 0))) {
    /* maybe it is simply disabled at compile-time */
    if (result == IGRAPH_UNIMPLEMENTED) return 77;
    return 1;
  }
  igraph_set_error_handler(oldhandler);

  fclose(ifile);

  /* Write it back */
  ofile=fopen("test2.gxl", "w");
  /* If we can't create the test file, just skip the test */
  if (ofile) {
    if ((result=igraph_write_graph_graphml(&g, ofile, /*prefixattr=*/ 1))) {
      return 1;
    }
    fclose(ofile);
    unlink("test2.gxl");
  }
  dump_graph("The directed graph:\n", &g);
  igraph_destroy(&g);
 
  /* The same with undirected graph */
  ifile=fopen("test.gxl", "r");
  if ((result=igraph_read_graph_graphml(&g, ifile, 0))) {
    return 1;
  }
  fclose(ifile);
  dump_graph("The undirected graph:\n", &g);
  igraph_destroy(&g);

  /* Test a GraphML file with default attributes */
  ifile=fopen("graphml-default-attrs.xml", "r");
  if ((result=igraph_read_graph_graphml(&g, ifile, 0))) {
    return 1;
  }
  fclose(ifile);
  dump_graph("The directed graph:\n", &g);
  dump_vertex_attribute_bool("type", &g);
  dump_vertex_attribute_string("gender", &g);
  dump_vertex_attribute_numeric("age", &g);
  dump_vertex_attribute_bool("retired", &g);
  igraph_destroy(&g);

  /* Test a GraphML file with namespaces */
  ifile=fopen("graphml-namespace.xml", "r");
  if ((result=igraph_read_graph_graphml(&g, ifile, 0))) {
    return 1;
  }
  fclose(ifile);
  dump_graph("The undirected graph:\n", &g);
  igraph_destroy(&g);

  /* Restore the old warning handler */
  igraph_set_warning_handler(oldwarnhandler);

  /* There were sometimes problems with this file */
  /* Only if called from R though, and only on random occasions, once in every 
     ten reads. Do testing here doesn't make much sense, but if we have the file 
     then let's do it anyway. */
  ifile=fopen("graphml-hsa05010.xml", "r");  
  igraph_read_graph_graphml(&g, ifile, 0);
  fclose(ifile);
  igraph_destroy(&g);
  
  return 0;
}