For using the igraph C library
These functions can write a graph to a file, or read a graphfrom a file.
They assume that the current locale uses a decimal point and nota decimal comma. Seeigraph_enter_safelocale() andigraph_exit_safelocale() for more information.
Note that asigraph uses the traditional C streams, it ispossible to read/write files from/to memory, at least on GNUoperating systems supporting“non-standard” streams.
igraph_read_graph_edgelist — Reads an edge list from a file and creates a graph.igraph_write_graph_edgelist — Writes the edge list of a graph to a file.igraph_read_graph_ncol — Reads an.ncol file used by LGL.igraph_write_graph_ncol — Writes the graph to a file in.ncol format.igraph_read_graph_lgl — Reads a graph from an.lgl file.igraph_write_graph_lgl — Writes the graph to a file in.lgl format.igraph_read_graph_dimacs_flow — Read a graph in DIMACS format.igraph_write_graph_dimacs_flow — Write a graph in DIMACS format.igraph_error_t igraph_read_graph_edgelist(igraph_t *graph, FILE *instream, igraph_integer_t n, igraph_bool_t directed);
This format is simply a series of an even number of non-negative integers separated bywhitespace. The integers represent vertex IDs. Placing each edge (i.e. pair of integers)on a separate line is not required, but it is recommended for readability.Edges of directed graphs are assumed to be in "from, to" order.
The largest vertex ID plus one, or the parametern determines the vertex count,whichever is larger. Seeigraph_read_graph_ncol() for reading files wherevertices are specified by name instead of by a numerical vertex ID.
Arguments:
| Pointer to an uninitialized graph object. |
| Pointer to a stream, it should be readable. |
| The number of vertices in the graph. If smaller than the largest integer in the file it will be ignored. It is thus safe to supply zero here. |
| If true the graph is directed, if false it will be undirected. |
Returns:
Error code: |
Time complexity: O(|V|+|E|), thenumber of vertices plus the number of edges. It is assumed thatreading an integer requires O(1) time.
igraph_error_t igraph_write_graph_edgelist(const igraph_t *graph, FILE *outstream);
Edges are represented as pairs of 0-based vertex indices.One edge is written per line, separated by a single space.For directed graphs edges are written in from, to order.
Arguments:
| The graph object to write. |
| Pointer to a stream, it should be writable. |
Returns:
Error code: |
Time complexity: O(|E|), thenumber of edges in the graph. It is assumed that writing aninteger to the file requires O(1)time.
igraph_error_t igraph_read_graph_ncol(igraph_t *graph, FILE *instream, const igraph_strvector_t *predefnames, igraph_bool_t names, igraph_add_weights_t weights, igraph_bool_t directed);
Also useful for creating graphs from“named” (andoptionally weighted) edge lists.
This format is used by the Large Graph Layout program(https://lgl.sourceforge.net), and it is simply asymbolic weighted edge list. It is a simple text file with one edgeper line. An edge is defined by two symbolic vertex names separatedby whitespace. The vertex names themselves cannot containwhitespace. They may be followed by an optional number,the weight of the edge; the number can be negative and can be inscientific notation. If there is no weight specified to an edge itis assumed to be zero.
The resulting graph is always undirected.LGL cannot deal with files which contain multiple or loop edges,this is however not checked here, asigraph is happy withthese.
Arguments:
| Pointer to an uninitialized graph object. |
| Pointer to a stream, it should be readable. |
| Pointer to the symbolic names of the vertices in the file. If |
| Boolean value. If |
| Whether to add the weights of the edges to the graph as an edge attribute called“weight”. |
| Whether to create a directed graph. As this format was originally used only for undirected graphs there is no information in the file about the directedness of the graph. Set this parameter to |
Returns:
Error code: |
Time complexity:O(|V|+|E|log(|V|)) if we neglectthe time required by the parsing. As usual|V| is the number of vertices,while |E| is the number of edges.
See also:
igraph_error_t igraph_write_graph_ncol(const igraph_t *graph, FILE *outstream, const char *names, const char *weights);
.ncol is a format used by LGL, seeigraph_read_graph_ncol() for details.
Note that having multiple or loop edges in an.ncol file breaks the LGL software butigraph does not check for this condition.
This format cannot represent zero-degree vertices.
Arguments:
| The graph to write. |
| The stream object to write to, it should be writable. |
| The name of a string vertex attribute, if symbolic names are to be written to the file. Supply |
| The name of a numerical edge attribute, which will be written as weights to the file. Supply |
Returns:
Error code: |
Time complexity: O(|E|), thenumber of edges. All file operations are expected to have timecomplexity O(1).
See also:
igraph_error_t igraph_read_graph_lgl(igraph_t *graph, FILE *instream, igraph_bool_t names, igraph_add_weights_t weights, igraph_bool_t directed);
The.lgl format is used by the Large GraphLayout visualization software(https://lgl.sourceforge.net), it candescribe undirected optionally weighted graphs. From the LGLmanual:
The second format is the LGL file format(
.lglfilesuffix). This is yet another graph file format that tries to be asstingy as possible with space, yet keeping the edge file in a humanreadable (not binary) format. The format itself is like thefollowing:# vertex1namevertex2name [optionalWeight]vertex3name [optionalWeight]Here, the first vertex of an edge is preceded with a pound sign'#'. Then each vertex that shares an edge with that vertex islisted one per line on subsequent lines.
LGL cannot handle loop and multiple edges or directed graphs, butinigraph it is not an error to have multiple and loop edges.
Arguments:
| Pointer to an uninitialized graph object. |
| A stream, it should be readable. |
| Boolean value, if |
| Whether to add the weights of the edges to the graph as an edge attribute called“weight”. |
| Whether to create a directed graph. As this format was originally used only for undirected graphs there is no information in the file about the directedness of the graph. Set this parameter to |
Returns:
Error code: |
Time complexity:O(|V|+|E|log(|V|)) if we neglectthe time required by the parsing. As usual|V| is the number of vertices,while |E| is the number of edges.
See also:
Example 21.1. Fileexamples/simple/igraph_read_graph_lgl.c
#include <igraph.h>intmain(void) { igraph_t g; FILE *input;/* Turn on attribute handling. */igraph_set_attribute_table(&igraph_cattribute_table);/* Without names and weights */ input =fopen("igraph_read_graph_lgl-1.lgl", "r");if (!input) {return 1; }igraph_read_graph_lgl(&g, input, 0, IGRAPH_ADD_WEIGHTS_NO, 1);fclose(input);if (!igraph_is_directed(&g)) {return 2; }igraph_write_graph_edgelist(&g, stdout);igraph_destroy(&g);/* With names and weights */ input =fopen("igraph_read_graph_lgl-2.lgl", "r");if (!input) {return 3; }igraph_read_graph_lgl(&g, input, 0, IGRAPH_ADD_WEIGHTS_NO, 1);fclose(input);if (!igraph_is_directed(&g)) {return 4; }igraph_write_graph_ncol(&g, stdout, 0, 0);igraph_destroy(&g);/* Same graph, but forcing undirected mode */ input =fopen("igraph_read_graph_lgl-2.lgl", "r");igraph_read_graph_lgl(&g, input, 0, IGRAPH_ADD_WEIGHTS_NO, 0);fclose(input);if (igraph_is_directed(&g)) {return 5; }igraph_write_graph_ncol(&g, stdout, 0, 0);igraph_destroy(&g);/* Erroneous LGL file (empty vertex name) */ input =fopen("igraph_read_graph_lgl-3.lgl", "r");if (!input) {return 6; }igraph_set_error_handler(igraph_error_handler_ignore);if (igraph_read_graph_lgl(&g, input, 0, IGRAPH_ADD_WEIGHTS_NO, 1) != IGRAPH_PARSEERROR) {return 7; }fclose(input);return 0;}
igraph_error_t igraph_write_graph_lgl(const igraph_t *graph, FILE *outstream, const char *names, const char *weights, igraph_bool_t isolates);
.lgl is a format used by LGL, seeigraph_read_graph_lgl() for details.
Note that having multiple or loop edges in an.lgl file breaks the LGL software butigraphdoes not check for this condition.
Arguments:
| The graph to write. |
| The stream object to write to, it should be writable. |
| The name of a string vertex attribute, if symbolic names are to be written to the file. Supply |
| The name of a numerical edge attribute, which will be written as weights to the file. Supply |
| If |
Returns:
Error code: |
Time complexity: O(|E|), the number of edges ifisolates isfalse,O(|V|+|E|) otherwise. All file operations are expected to havetime complexity O(1).
See also:
Example 21.2. Fileexamples/simple/igraph_write_graph_lgl.c
#include <igraph.h>intmain(void) { igraph_t graph; igraph_strvector_t names;igraph_vector_t weights; igraph_integer_t i; igraph_integer_t vcount, ecount;igraph_set_attribute_table(&igraph_cattribute_table);igraph_small(&graph, 7, IGRAPH_UNDIRECTED, 0,1, 1,3, 1,2, 2,0, 4,2, 3,4, -1); vcount =igraph_vcount(&graph); ecount =igraph_ecount(&graph);printf("Output without isolates:\n");igraph_write_graph_lgl(&graph, stdout,/*names*/ NULL,/*weights*/ NULL,/*isolates*/ 0);printf("\nOutput with isolates:\n");igraph_write_graph_lgl(&graph, stdout,/*names*/ NULL,/*weights*/ NULL,/*isolates*/ 1);printf("\nOutput vertex and edge labels:\n");igraph_strvector_init(&names, vcount);for (i = 0; i < vcount; i++) { char str[2] = " ";/* initialize to ensure presence of null terminator */ str[0] = 'A' + i;igraph_strvector_set(&names, i, str); }SETVASV(&graph, "names", &names);igraph_vector_init_range(&weights, 1, ecount + 1);SETEANV(&graph, "weights", &weights);igraph_write_graph_lgl(&graph, stdout, "names", "weights",/*isolates*/ 0);igraph_strvector_destroy(&names);igraph_vector_destroy(&weights);igraph_destroy(&graph);return 0;}
igraph_error_t igraph_read_graph_dimacs_flow( igraph_t *graph, FILE *instream, igraph_strvector_t *problem, igraph_vector_int_t *label, igraph_integer_t *source, igraph_integer_t *target, igraph_vector_t *capacity, igraph_bool_t directed);
This function reads the DIMACS file format, more specifically theversion for network flow problems, see the files athttp://archive.dimacs.rutgers.edu/pub/netflow/general-info/
This is a line-oriented text file (ASCII) format. The firstcharacter of each line defines the type of the line. If the firstcharacter isc the line is a comment line and it isignored. There is one problem line (p in the file), itmust appear before any node and arc descriptor lines. The problemline has three fields separated by spaces: the problem type(max oredge), the number of vertices,and number of edges in the graph. In MAX problems,exactly two node identification lines are expected(n), one for the source, and one for the target vertex.These have two fields: the ID of the vertex and the type of thevertex, eithers ( = source) ort ( = target).Arc lines start witha and have three fields: the source vertex,the target vertex and the edge capacity. In EDGE problems,there may be a node line (n) for each node. It specifies thenode index and an integer node label. Nodes for which no explicitlabel was specified will use their index as label. In EDGE problems,each edge is specified as an edge line (e).
Within DIMACS files, vertex IDs are numbered from 1.
Arguments:
| Pointer to an uninitialized graph object. |
| The file to read from. |
| If not |
| If not |
| Pointer to an integer, the ID of the source node will be stored here. (The igraph vertex ID, which is one less than the actual number in the file.) It is ignored if |
| Pointer to an integer, the (igraph) ID of the target node will be stored here. It is ignored if |
| Pointer to an initialized vector, the capacity of the edges will be stored here if not \ NULL. |
| Boolean, whether to create a directed graph. |
Returns:
Error code. |
Time complexity: O(|V|+|E|+c), the number of vertices plus thenumber of edges, plus the size of the file in characters.
See also:
igraph_error_t igraph_write_graph_dimacs_flow(const igraph_t *graph, FILE *outstream, igraph_integer_t source, igraph_integer_t target, const igraph_vector_t *capacity);
This function writes a graph to an output stream in DIMACS format,describing a maximum flow problem.See ftp://dimacs.rutgers.edu/pub/netflow/general-info/
This file format is discussed in the documentation ofigraph_read_graph_dimacs_flow(), see that for more information.
Arguments:
| The graph to write to the stream. |
| The stream. |
| Integer, the id of the source vertex for the maximum flow. |
| Integer, the id of the target vertex. |
| Pointer to an initialized vector containing the edge capacity values. |
Returns:
Error code. |
Time complexity: O(|E|), the number of edges in the graph.
See also:
igraph_error_t igraph_read_graph_graphdb(igraph_t *graph, FILE *instream, igraph_bool_t directed);
This is a binary format, used in the ARG Graph Databasefor isomorphism testing. For more information, seehttps://mivia.unisa.it/datasets/graph-database/arg-database/
From the graph database homepage:
The graphs are stored in a compact binary format, one graph perfile. The file is composed of 16 bit words, which are representedusing the so-called little-endian convention, i.e. the leastsignificant byte of the word is stored first.
Then, for each node, the file contains the list of edges comingout of the node itself. The list is represented by a word encodingits length, followed by a word for each edge, representing thedestination node of the edge. Node numeration is 0-based, so thefirst node of the graph has index 0.
As of igraph 0.10, only unlabelled graphs are implemented.
References:
M. De Santo, P. Foggia, C. Sansone, and M. Vento:A large database of graphs and its use for benchmarking graph isomorphism algorithms.Pattern Recognition Letters, 24(8), 1067-1079 (2003).https://doi.org/10.1016/S0167-8655(02)00253-2
MIVIA ARG Dataset,https://zenodo.org/records/11204020,https://mivia.unisa.it/datasets/graph-database/arg-database/
Arguments:
| Pointer to an uninitialized graph object. |
| The stream to read from. It should be opened in binary mode. |
| Whether to create a directed graph. |
Returns:
Error code. |
Time complexity: O(|V|+|E|), the number of vertices plus thenumber of edges.
Example 21.3. Fileexamples/simple/igraph_read_graph_graphdb.c
#include <igraph.h>intmain(void) { igraph_t g; FILE *input; input =fopen("iso_b03_m1000.A00", "rb");if (!input) {return 1; }igraph_read_graph_graphdb(&g, input, IGRAPH_DIRECTED);fclose(input);igraph_write_graph_edgelist(&g, stdout);igraph_destroy(&g);return 0;}
igraph_error_t igraph_read_graph_graphml(igraph_t *graph, FILE *instream, igraph_integer_t index);
GraphML is an XML-based file format for representing various types ofgraphs. Currently only the most basic import functionality is implementedin igraph: it can read GraphML files without nested graphs and hyperedges.Attributes of the graph are loaded only if an attribute interfaceis attached, seeigraph_set_attribute_table(). String attrribute valuesare returned in UTF-8 encoding.
Graph attribute names are taken from theattr.name attributes of thekey tags in the GraphML file. Sinceattr.name is not mandatory,igraph will fall back to theid attribute of thekey tag ifattr.name is missing.
Arguments:
| Pointer to an uninitialized graph object. |
| A stream, it should be readable. |
| If the GraphML file contains more than one graph, the one specified by this index will be loaded. Indices start from zero, so supply zero here if your GraphML file contains only a single graph. |
Returns:
Error code: |
Example 21.4. Fileexamples/simple/graphml.c
#include <igraph.h>#include <stdio.h>#include <unistd.h>/* unlink */intmain(void) { igraph_t graph;const char *infilename = "test.graphml";const char *outfilename = "test2.graphml";/* Set up attribute handling, so graph attributes can be imported * from the GraphML file. */igraph_set_attribute_table(&igraph_cattribute_table);/* Problems in the GraphML file may cause igraph to print warnings. * If this is not desired, set a silent warning handler: */igraph_set_warning_handler(&igraph_warning_handler_ignore);/* Read the contents of a GraphML file. *//* GraphML */ FILE *infile =fopen("test.graphml", "r");if (! infile) {fprintf(stderr, "Could not open input file '%s'.", infilename);exit(1); }/* GraphML support is an optional feature in igraph. If igraph was compiled * without GraphML support, igraph_read_graph_graphml() returns IGRAPH_UNIMPLEMENTED. * We temporarily disable the default error handler so we can test for this condition. */igraph_error_handler_t *oldhandler =igraph_set_error_handler(igraph_error_handler_ignore);igraph_error_t ret =igraph_read_graph_graphml(&graph, infile, 0);if (ret == IGRAPH_UNIMPLEMENTED) {fprintf(stderr, "igraph was compiled without GraphML support.");exit(77); }if (ret != IGRAPH_SUCCESS) {fprintf(stderr, "Unexpected error while reading GraphML.");exit(1); }igraph_set_error_handler(oldhandler);fclose(infile);/* Write it back into another file. */ FILE *outfile =fopen(outfilename, "w");if (outfile) {igraph_write_graph_graphml(&graph, outfile, true);fclose(outfile);/* Clean up after ourselves */unlink(outfilename); }else {fprintf(stderr, "Could not write output file '%s'.", outfilename); }/* Destroy the graph */igraph_destroy(&graph);return 0;}
igraph_error_t igraph_write_graph_graphml(const igraph_t *graph, FILE *outstream, igraph_bool_t prefixattr);
GraphML is an XML-based file format for representing various types ofgraphs. See the GraphML Primer (http://graphml.graphdrawing.org/primer/graphml-primer.html)for the detailed format description.
When a numerical attribute value is NaN, it will be omitted from the file.
This function assumes that non-ASCII characters in attribute names and stringattribute values are UTF-8 encoded. If this is not the case, the resultingXML file will be invalid. Control characters, i.e. character codes up to andincluding 31 (with the exception of tab, cr and lf), are not allowed.
Arguments:
| The graph to write. |
| The stream object to write to, it should be writable. |
| Boolean value. Whether to put a prefix in front of the attribute names to ensure uniqueness if the graph has vertex and edge (or graph) attributes with the same name. |
Returns:
Error code: |
Time complexity: O(|V|+|E|) otherwise. Allfile operations are expected to have time complexityO(1).
Example 21.5. Fileexamples/simple/graphml.c
#include <igraph.h>#include <stdio.h>#include <unistd.h>/* unlink */intmain(void) { igraph_t graph;const char *infilename = "test.graphml";const char *outfilename = "test2.graphml";/* Set up attribute handling, so graph attributes can be imported * from the GraphML file. */igraph_set_attribute_table(&igraph_cattribute_table);/* Problems in the GraphML file may cause igraph to print warnings. * If this is not desired, set a silent warning handler: */igraph_set_warning_handler(&igraph_warning_handler_ignore);/* Read the contents of a GraphML file. *//* GraphML */ FILE *infile =fopen("test.graphml", "r");if (! infile) {fprintf(stderr, "Could not open input file '%s'.", infilename);exit(1); }/* GraphML support is an optional feature in igraph. If igraph was compiled * without GraphML support, igraph_read_graph_graphml() returns IGRAPH_UNIMPLEMENTED. * We temporarily disable the default error handler so we can test for this condition. */igraph_error_handler_t *oldhandler =igraph_set_error_handler(igraph_error_handler_ignore);igraph_error_t ret =igraph_read_graph_graphml(&graph, infile, 0);if (ret == IGRAPH_UNIMPLEMENTED) {fprintf(stderr, "igraph was compiled without GraphML support.");exit(77); }if (ret != IGRAPH_SUCCESS) {fprintf(stderr, "Unexpected error while reading GraphML.");exit(1); }igraph_set_error_handler(oldhandler);fclose(infile);/* Write it back into another file. */ FILE *outfile =fopen(outfilename, "w");if (outfile) {igraph_write_graph_graphml(&graph, outfile, true);fclose(outfile);/* Clean up after ourselves */unlink(outfilename); }else {fprintf(stderr, "Could not write output file '%s'.", outfilename); }/* Destroy the graph */igraph_destroy(&graph);return 0;}
igraph_error_t igraph_read_graph_gml(igraph_t *graph, FILE *instream);
GML is a simple textual format, seehttps://web.archive.org/web/20190207140002/http://www.fim.uni-passau.de/index.php?id=17297%26L=1for details.
Although all syntactically correct GML can be parsed,we implement only a subset of this format. Some attributes might beignored. Here is a list of all the differences:
Only attributes with a simple type are used: integer, real or string. If an attribute is composite, i.e. an array or a record, then it is ignored. When some values of the attribute are simple and some compound, the composite ones are replaced with a default value (NaN for numeric,"" for string).
comment fields are not ignored. They are treated as any other field and converted to attributes.
Top level attributes except forVersion and the firstgraph attribute are completely ignored.
There is no maximum line length or maximum keyword length.
Only thequot,amp,apos,lt andgt character entities are supported. Any other entity is passed through unchanged by the reader after issuing a warning, and is expected to be decoded by the user.
We allowinf,-inf andnan (not a number) as a real number. This is case insensitive, sonan,NaN andNAN are equivalent.
Please contact us if you cannot live with theselimitations of the GML parser.
Arguments:
| Pointer to an uninitialized graph object. |
| The stream to read the GML file from. |
Returns:
Error code. |
Time complexity: should be proportional to the length of the file.
See also:
|
Example 21.6. Fileexamples/simple/gml.c
#include <igraph.h>#include <stdio.h>intmain(void) { igraph_t graph; FILE *ifile; ifile =fopen("karate.gml", "r");if (ifile == 0) {return 1; }igraph_read_graph_gml(&graph, ifile);fclose(ifile);printf("The graph is %s.\n",igraph_is_directed(&graph) ? "directed" : "undirected");/* Output as edge list */printf("\n-----------------\n");igraph_write_graph_edgelist(&graph, stdout);/* Output as GML */printf("\n-----------------\n");igraph_write_graph_gml(&graph, stdout,IGRAPH_WRITE_GML_DEFAULT_SW, 0, "");igraph_destroy(&graph);return 0;}
igraph_error_t igraph_write_graph_gml(const igraph_t *graph, FILE *outstream, igraph_write_gml_sw_t options, const igraph_vector_t *id, const char *creator);
GML is a quite general textual format, seehttps://web.archive.org/web/20190207140002/http://www.fim.uni-passau.de/index.php?id=17297%26L=1for details.
The graph, vertex and edges attributes are written to thefile as well, if they are numeric or string. Boolean attributes are convertedto numeric, with 0 and 1 used for false and true, respectively.NaN values of numeric attributes are skipped, as NaN is not part of the GMLspecification and other software may not be able to read files containing them.This is consistent withigraph_read_graph_gml(), which produces NaNwhen an attribute value is missing. In contrast with NaN, infinite valuesare retained. Ensure that none of the numeric attributes values are infiniteto produce a conformant GML file that can be read by other software.
As igraph is more forgiving about attribute names, it mightbe necessary to simplify the them before writing to the GML file.This way we'll have a syntactically correct GML file. The followingsimple procedure is performed on each attribute name: first the alphanumericcharacters are extracted, the others are ignored. Then if the first characteris not a letter then the attribute name is prefixed with“igraph”.Note that this might result identical names for two attributes, igraphdoes not check this.
The“id” vertex attribute is treated specially.If theid argument is notNULL then it should be a numericvector with the vertex IDs and the“id” vertex attribute isignored (if there is one). Ifid isNULL and there is anumeric“id” vertex attribute, it will be used instead. If idsare not specified in either way then the regular igraph vertex IDs are used.If some of the supplied id values are invalid (non-integer or NaN), all suppliedid are ignored and igraph vertex IDs are used instead.
Note that whichever way vertex IDs are specified, their uniqueness is not checked.
If the graph has edge attributes that become“source”or“target” after encoding, or the graph has an attribute that becomes“directed”, they will be ignored with a warning. GML uses these attributesto specify the edge endpoints, and the graph directedness, so we cannot write themto the file. Rename them before calling this function if you want to preserve them.
Arguments:
| The graph to write to the stream. | ||||||
| The stream to write the file to. | ||||||
| Set of
| ||||||
| Either | ||||||
| An optional string to write to the stream in the creator line. If |
Returns:
Error code. |
Time complexity: should be proportional to the number of characters writtento the file.
See also:
|
Example 21.7. Fileexamples/simple/gml.c
#include <igraph.h>#include <stdio.h>intmain(void) { igraph_t graph; FILE *ifile; ifile =fopen("karate.gml", "r");if (ifile == 0) {return 1; }igraph_read_graph_gml(&graph, ifile);fclose(ifile);printf("The graph is %s.\n",igraph_is_directed(&graph) ? "directed" : "undirected");/* Output as edge list */printf("\n-----------------\n");igraph_write_graph_edgelist(&graph, stdout);/* Output as GML */printf("\n-----------------\n");igraph_write_graph_gml(&graph, stdout,IGRAPH_WRITE_GML_DEFAULT_SW, 0, "");igraph_destroy(&graph);return 0;}
igraph_error_t igraph_read_graph_pajek(igraph_t *graph, FILE *instream);
Only a subset of the Pajek format is implemented. This is partiallybecause there is no formal specification for this format, but also becauseigraph does not support some Pajek features, likemixed graphs.
Starting from version 0.6.1 igraph reads bipartite (two-mode)graphs from Pajek files and adds thetype Boolean vertex attribute forthem. Warnings are given for invalid edges, i.e. edges connectingvertices of the same type.
The list of the current limitations:
Only.net files are supported, Pajekproject files (.paj) are not.
Temporal networks (i.e. with time events) are not supported.
Graphs with both directed and non-directed edges are notsupported, as they cannot be represented inigraph.
Only Pajek networks are supported; permutations, hierarchies,clusters and vectors are not.
Multi-relational networks (i.e. networks with multiple edgetypes) are not supported.
Unicode characters encoded as&#dddd;, or newlinesencoded as\n will not be decoded.
If an attribute handler is installed,igraph also reads the vertex and edge attributesfrom the file. Most attributes are renamed to be more informative:color instead ofc,xfact instead ofx_fact,yfact instead of y_fact,labeldist instead oflr,labeldegree2 instead oflphi,framewidth instead ofbw,fontsize instead offos,rotation instead ofphi,radius instead ofr,diamondratio instead ofq,labeldegree instead ofla,color instead ofic,framecolor instead ofbc,labelcolor instead oflc; these belong to vertices.
Edge attributes are also renamed,s toarrowsize,w toedgewidth,h1 tohook1,h2 tohook2,a1 toangle1,a2 toangle2,k1 tovelocity1,k2 tovelocity2,ap toarrowpos,lp tolabelpos,lr tolabelangle,lphi tolabelangle2,la tolabeldegree,fos tofontsize,a toarrowtype,p tolinepattern,l tolabel,lc tolabelcolor,c tocolor.
Unknown vertex or edge parameters are read as string vertexor edge attributes. If the parameter name conflicts with onethe standard attribute names mentioned above, a_character is appended to it to avoid conflict.
In addition the following vertex attributes might be added:idandname are added (with the same value) if there are vertex IDs in thefile.id is deprecated in favour ofname and will no longer be usedby future versions of igraph.x andy, and potentiallyz are alsoadded if there are vertex coordinates in the file.
Theweight edge attribute will be added if there are edge weights present.
See the Pajek homepage:http://vlado.fmf.uni-lj.si/pub/networks/pajek/ for more info onPajek. The Pajek manual,http://vlado.fmf.uni-lj.si/pub/networks/pajek/doc/pajekman.pdf,andhttp://mrvar.fdv.uni-lj.si/pajek/DrawEPS.htmhave information on the Pajek file format. There is additionaluseful information and sample files athttp://mrvar.fdv.uni-lj.si/pajek/history.htm
Arguments:
| Pointer to an uninitialized graph object. |
| An already opened file handler. |
Returns:
Error code. |
Time complexity: O(|V|+|E|+|A|), |V| is the number of vertices, |E|the number of edges, |A| the number of attributes (vertex + edge)in the graph if there are attribute handlers installed.
See also:
|
Example 21.8. Fileexamples/simple/foreign.c
#include <igraph.h>#include <stdio.h>intmain(void) { igraph_t g; FILE *ifile;/* Turn on attribute handling. */igraph_set_attribute_table(&igraph_cattribute_table);/* Read a Pajek file. */ ifile =fopen("links.net", "r");if (ifile == 0) {return 10; }igraph_read_graph_pajek(&g, ifile);fclose(ifile);/* Write it in edgelist format. */printf("The graph:\n");printf("Vertices: %" IGRAPH_PRId "\n",igraph_vcount(&g));printf("Edges: %" IGRAPH_PRId "\n",igraph_ecount(&g));printf("Directed: %i\n",igraph_is_directed(&g) ? 1 : 0);igraph_write_graph_edgelist(&g, stdout);igraph_destroy(&g);return 0;}
igraph_error_t igraph_write_graph_pajek(const igraph_t *graph, FILE *outstream);
Writes files in the native format of the Pajek software. This formatis not recommended for data exchange or archival. It is meant solelyfor interoperability with Pajek.
The Pajek vertex and edge parameters (like color) are determined bythe attributes of the vertices and edges. Of course this requiresan attribute handler to be installed. The names of thecorresponding vertex and edge attributes are listed atigraph_read_graph_pajek(), e.g. thecolor vertex attributesdetermines the color (c in Pajek) parameter.
Vertex and edge attributes that do not correspond to any documentedPajek parameter are discarded.
As of version 0.6.1 igraph writes bipartite graphs into Pajek filescorrectly, i.e. they will be also bipartite when read into Pajek.As Pajek is less flexible for bipartite graphs (the numeric IDs ofthe vertices must be sorted according to vertex type), igraph mightneed to reorder the vertices when writing a bipartite Pajek file.This effectively means that numeric vertex IDs usually change whena bipartite graph is written to a Pajek file, and then read backinto igraph.
Early versions of Pajek supported only Windows-style line endingsin Pajek files, but recent versions support both Windows and Unixline endings. igraph therefore uses the platform-native line endingswhen the input file is opened in text mode, and uses Unix-styleline endings when the input file is opened in binary mode. If youare using an old version of Pajek, you are on Unix and you are havingproblems reading files written by igraph on a Windows machine, convert theline endings manually with a text editor or withunix2dos oriconvfrom the command line).
Pajek will only interpret UTF-8 encoded files if they contain a byte-ordermark (BOM) at the beginning. igraph is agnostic of string attribute encodingsand therefore it will never write a BOM. You need to add this manuallyif/when necessary.
Arguments:
| The graph object to write. |
| The file to write to. It should be opened and writable. |
Returns:
Error code. |
Time complexity: O(|V|+|E|+|A|), |V| is the number of vertices, |E|is the number of edges, |A| the number of attributes (vertex +edge) in the graph if there are attribute handlers installed.
See also:
|
Example 21.9. Fileexamples/simple/igraph_write_graph_pajek.c
#include <igraph.h>intmain(void) { igraph_t g; igraph_strvector_t names;igraph_set_attribute_table(&igraph_cattribute_table);/* save a simple ring graph */igraph_ring(&g, 10, IGRAPH_DIRECTED, false/* mutual */, true/* circular */);igraph_write_graph_pajek(&g, stdout);/* add some vertex attributes */igraph_strvector_init(&names, 0);igraph_strvector_push_back(&names, "A");igraph_strvector_push_back(&names, "B");igraph_strvector_push_back(&names, "C");igraph_strvector_push_back(&names, "D");igraph_strvector_push_back(&names, "E");igraph_strvector_push_back(&names, "F");igraph_strvector_push_back(&names, "G");igraph_strvector_push_back(&names, "H");igraph_strvector_push_back(&names, "I");igraph_strvector_push_back(&names, "J");SETVASV(&g, "id", &names);igraph_strvector_destroy(&names);/* save the graph with vertex names */igraph_write_graph_pajek(&g, stdout);igraph_strvector_init(&names, 0);igraph_strvector_push_back(&names, "square");igraph_strvector_push_back(&names, "square");igraph_strvector_push_back(&names, "square");igraph_strvector_push_back(&names, "square");igraph_strvector_push_back(&names, "escaping spaces");igraph_strvector_push_back(&names, "square");igraph_strvector_push_back(&names, "square");igraph_strvector_push_back(&names, "escaping\nnewline");igraph_strvector_push_back(&names, "square");igraph_strvector_push_back(&names, "encoding \"quotes\"");SETVASV(&g, "shape", &names);igraph_strvector_destroy(&names);/* save the graph with escaped shapes */igraph_write_graph_pajek(&g, stdout);/* destroy the graph */igraph_destroy(&g);return 0;}
igraph_error_t igraph_read_graph_dl(igraph_t *graph, FILE *instream, igraph_bool_t directed);
This is a simple textual file format used by UCINET. Seehttp://www.analytictech.com/networks/dataentry.htm forexamples. All the forms described here are supported byigraph. Vertex names and edge weights are also supported and theyare added as attributes. (If an attribute handler is attached.)
Note the specification does not mention whether theformat is case sensitive or not. For igraph DL files are casesensitive, i.e.Larry andlarry are not the same.
Arguments:
| Pointer to an uninitialized graph object. |
| The stream to read the DL file from. |
| Boolean, whether to create a directed file. |
Returns:
Error code. |
Time complexity: linear in terms of the number of edges andvertices, except for the matrix format, which is quadratic in thenumber of vertices.
Example 21.10. Fileexamples/simple/igraph_read_graph_dl.c
#include <igraph.h>#include <stdio.h>#include <stdlib.h>intmain(void) {const char *files[] = { "fullmatrix1.dl", "fullmatrix2.dl", "fullmatrix3.dl", "fullmatrix4.dl", "edgelist1.dl", "edgelist2.dl", "edgelist3.dl", "edgelist4.dl", "edgelist5.dl", "edgelist6.dl", "edgelist7.dl", "nodelist1.dl", "nodelist2.dl" }; igraph_t graph; FILE *infile;/* Turn on attribute handling. */igraph_set_attribute_table(&igraph_cattribute_table);for (size_t i = 0; i <sizeof(files) /sizeof(files[0]); i++) {printf("Doing %s\n", files[i]); infile =fopen(files[i], "r");if (!infile) {printf("Cannot open file: %s\n", files[i]);abort(); }igraph_read_graph_dl(&graph, infile, IGRAPH_DIRECTED);fclose(infile);igraph_write_graph_edgelist(&graph, stdout);igraph_destroy(&graph); }if (IGRAPH_FINALLY_STACK_SIZE() != 0) {return 1; }return 0;}
igraph_error_t igraph_write_graph_dot(const igraph_t *graph, FILE* outstream);
DOT is the format used by the widely known GraphViz software, seehttp://www.graphviz.org for details. The grammar of the DOT formatcan be found here:http://www.graphviz.org/doc/info/lang.html
This is only a preliminary implementation, no visualizationinformation is written.
This format is meant solely for interoperability with Graphviz.It is not recommended for data exchange or archival.
Arguments:
| The graph to write to the stream. |
| The stream to write the file to. |
Returns:
Error code. |
Time complexity: should be proportional to the number of characters writtento the file.
See also:
|
Example 21.11. Fileexamples/simple/dot.c
#include <igraph.h>#include <stdio.h>intmain(void) { igraph_t g; FILE *ifile; ifile =fopen("karate.gml", "r");if (ifile == 0) {return 10; }igraph_read_graph_gml(&g, ifile);fclose(ifile);if (igraph_is_directed(&g)) {printf("directed\n"); }else {printf("undirected\n"); }igraph_write_graph_edgelist(&g, stdout);printf("-----------------\n");igraph_write_graph_dot(&g, stdout);igraph_destroy(&g);return 0;}
igraph_error_t igraph_write_graph_leda(const igraph_t *graph, FILE *outstream, const char *vertex_attr_name, const char *edge_attr_name);
This function writes a graph to an output stream in LEDA format.Seehttp://www.algorithmic-solutions.info/leda_guide/graphs/leda_native_graph_fileformat.html
The support for the LEDA format is very basic at the moment; igraphwrites only the LEDA graph section which supports one selected vertexand edge attribute and no layout information or visual attributes.
Arguments:
| The graph to write to the stream. |
| The stream. |
| The name of the vertex attribute whose values are to be stored in the output, or |
| The name of the edge attribute whose values are to be stored in the output, or |
Returns:
Error code. |
Time complexity: O(|V|+|E|), the number of vertices and edges in thegraph.
igraph_error_t igraph_enter_safelocale(igraph_safelocale_t *loc);
This function is experimental and its signature is not considered final yet.We reserve the right to change the function signature without changing themajor version of igraph. Use it at your own risk.
igraph's foreign format readers and writers require a locale that uses adecimal point instead of a decimal comma. This is a convenience functionthat temporarily sets the C locale so that readers and writers would workcorrectly. Itmust be paired with a call toigraph_exit_safelocale(),otherwise a memory leak will occur.
This function tries to set the locale for the current thread only on abest-effort basis. Restricting the locale change to a single thread is notsupported on all platforms. In these cases, this function falls back to usingthe standardsetlocale() function, which affects the entire processand is not safe to use from concurrent threads.
It is generally recommended to run igraph within a thread that has beenpermanently set to the C locale using system-specific means. This is a conveniencefunction for situations when this is not easily possible because the programmeris not in control of the process, such as when developing plugins/extensions.Note that processes start up in the C locale by default, thus nothing needs tobe done unless the locale has been changed away from the default.
Arguments:
| Pointer to a variable of type |
Returns:
Error code. |
Example 21.12. Fileexamples/simple/safelocale.c
#include <igraph.h>#include <locale.h>#include <stdio.h>#include <string.h>intmain(void) {const char *filename = "weighted.gml"; igraph_t graph; igraph_safelocale_t loc;/* Attempt to set a locale that uses a decimal comma. Locale names * differ between platforms, and not all locales are available, * so the locale change may not be successful. */const char *locname =setlocale(LC_ALL, "de_DE");struct lconv *lc =localeconv();if (strcmp(lc->decimal_point, ",")) {/* If decimal point is not a comma, presumably because the requested * locale was not available, report locale information. */fprintf(stderr, "setlocale() returned '%s', decimal point is '%s'\n", locname ? locname : "NULL", lc->decimal_point); } FILE *file =fopen(filename, "r");if (! file) {fprintf(stderr, "Cannot open %s file.\n", filename);exit(1); }/* An attribute table is needed to read graph attributes. */igraph_set_attribute_table(&igraph_cattribute_table);/* At this point, the current locale may use decimal commas. * We temporarily set a C locale using enter_safelocale() to * allow the GML reader and writer to work correctly.*/igraph_enter_safelocale(&loc);if (igraph_read_graph_gml(&graph, file) != IGRAPH_SUCCESS) {fprintf(stderr, "Reading %s failed.\n", filename);abort(); }igraph_write_graph_gml(&graph, stdout, IGRAPH_WRITE_GML_DEFAULT_SW, NULL, "");igraph_exit_safelocale(&loc);igraph_destroy(&graph);return 0;}
void igraph_exit_safelocale(igraph_safelocale_t *loc);
This function is experimental and its signature is not considered final yet.We reserve the right to change the function signature without changing themajor version of igraph. Use it at your own risk.
Restores a locale saved byigraph_enter_safelocale() and deallocatesall associated data. This functionmust be paired with a call toigraph_enter_safelocale().
Arguments:
| A variable of type |
igraph_error_t igraph_read_graph_dimacs(igraph_t *graph, FILE *instream, igraph_strvector_t *problem, igraph_vector_int_t *label, igraph_integer_t *source, igraph_integer_t *target, igraph_vector_t *capacity, igraph_bool_t directed);
Deprecated since version 0.10.0. Please do not use this function in newcode; useigraph_read_graph_dimacs_flow()instead.
igraph_error_t igraph_write_graph_dimacs(const igraph_t *graph, FILE *outstream, igraph_integer_t source, igraph_integer_t target, const igraph_vector_t *capacity);
Deprecated since version 0.10.0. Please do not use this function in newcode; useigraph_write_graph_dimacs_flow()instead.
| ← Chapter 20. Generating layouts for graph drawing | Chapter 22. Maximum flows, minimum cuts and related measures → |