1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
/* -*- mode: C -*- */
/* IGraph library.
Copyright (C) 2007-2021 The igraph development team <igraph@igraph.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <igraph.h>
#include <string.h>
#include <stdlib.h>
/* Prints graph, vertex and edge attributes stored in a graph. */
void print_attributes(const igraph_t *g) {
igraph_vector_int_t gtypes, vtypes, etypes;
igraph_strvector_t gnames, vnames, enames;
igraph_integer_t i, j;
igraph_vector_int_init(>ypes, 0);
igraph_vector_int_init(&vtypes, 0);
igraph_vector_int_init(&etypes, 0);
igraph_strvector_init(&gnames, 0);
igraph_strvector_init(&vnames, 0);
igraph_strvector_init(&enames, 0);
igraph_cattribute_list(g,
&gnames, >ypes,
&vnames, &vtypes,
&enames, &etypes);
/* graph attributes */
for (i = 0; i < igraph_strvector_size(&gnames); i++) {
printf("%s=", STR(gnames, i));
if (VECTOR(gtypes)[i] == IGRAPH_ATTRIBUTE_NUMERIC) {
igraph_real_printf(GAN(g, STR(gnames, i)));
putchar(' ');
} else {
printf("\"%s\" ", GAS(g, STR(gnames, i)));
}
}
printf("\n");
/* vertex attributes */
for (i = 0; i < igraph_vcount(g); i++) {
printf("Vertex %" IGRAPH_PRId ": ", i);
for (j = 0; j < igraph_strvector_size(&vnames); j++) {
printf("%s=", STR(vnames, j));
if (VECTOR(vtypes)[j] == IGRAPH_ATTRIBUTE_NUMERIC) {
igraph_real_printf(VAN(g, STR(vnames, j), i));
putchar(' ');
} else {
printf("\"%s\" ", VAS(g, STR(vnames, j), i));
}
}
printf("\n");
}
/* edge attributes */
for (i = 0; i < igraph_ecount(g); i++) {
printf("Edge %" IGRAPH_PRId " (%" IGRAPH_PRId "-%" IGRAPH_PRId "): ", i, IGRAPH_FROM(g, i), IGRAPH_TO(g, i));
for (j = 0; j < igraph_strvector_size(&enames); j++) {
printf("%s=", STR(enames, j));
if (VECTOR(etypes)[j] == IGRAPH_ATTRIBUTE_NUMERIC) {
igraph_real_printf(EAN(g, STR(enames, j), i));
putchar(' ');
} else {
printf("\"%s\" ", EAS(g, STR(enames, j), i));
}
}
printf("\n");
}
igraph_strvector_destroy(&enames);
igraph_strvector_destroy(&vnames);
igraph_strvector_destroy(&gnames);
igraph_vector_int_destroy(&etypes);
igraph_vector_int_destroy(&vtypes);
igraph_vector_int_destroy(>ypes);
}
int main(void) {
igraph_t graph;
igraph_vector_t y;
/* Turn on attribute handling. */
igraph_set_attribute_table(&igraph_cattribute_table);
igraph_small(&graph, 3, IGRAPH_DIRECTED, 0,1, 1,2, -1);
/* Set graph attributes. */
/* numeric */
SETGAN(&graph, "id", 10);
/* string */
SETGAS(&graph, "name", "toy");
/* boolean */
SETGAB(&graph, "is_regular", false);
/* Set edge string attribute. */
SETEAS(&graph, "color", 1, "RED");
/* Set vertex attributes as vector. */
igraph_vector_init(&y, igraph_vcount(&graph));
igraph_vector_fill(&y, 1.23);
SETVANV(&graph, "y", &y);
igraph_vector_destroy(&y);
/* Set single vertex numeric attribute. */
SETVAN(&graph, "y", 0, -1);
/* Delete graph attribute. */
DELGA(&graph, "is_regular");
/* Print the final result. */
print_attributes(&graph);
/* Delete all remaining attributes. */
DELALL(&graph);
/* Destroy the graph. */
igraph_destroy(&graph);
return 0;
}
|