Files
Abdelrahman Said a11edf0c53 Add graph references
2026-06-28 13:49:01 +01:00

3033 lines
142 KiB
HTML
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Chapter 31. Reading and writing graphs from and to files</title>
<meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
<link rel="home" href="index.html" title="igraph Reference Manual">
<link rel="up" href="index.html" title="igraph Reference Manual">
<link rel="prev" href="igraph-Processes.html" title="Chapter 30. Processes on graphs">
<link rel="next" href="igraph-Linalg.html" title="Chapter 32. Using BLAS, LAPACK and ARPACK for igraph matrices and graphs">
<script type="text/javascript" src="toggle.js"></script><link rel="stylesheet" href="style.css" type="text/css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" type="text/css">
<link rel="chapter" href="igraph-Introduction.html" title="Chapter 1. Introduction">
<link rel="chapter" href="igraph-Installation.html" title="Chapter 2. Installation">
<link rel="chapter" href="igraph-Tutorial.html" title="Chapter 3. Tutorial">
<link rel="chapter" href="igraph-Basic.html" title="Chapter 4. Basic data types and interface">
<link rel="chapter" href="igraph-Error.html" title="Chapter 5. Error handling">
<link rel="chapter" href="igraph-Memory.html" title="Chapter 6. Memory (de)allocation">
<link rel="chapter" href="igraph-Data-structures.html" title="Chapter 7. Data structure library: vector, matrix, other data types">
<link rel="chapter" href="igraph-Random.html" title="Chapter 8. Random numbers">
<link rel="chapter" href="igraph-Iterators.html" title="Chapter 9. Vertex and edge selectors and sequences, iterators">
<link rel="chapter" href="igraph-Attributes.html" title="Chapter 10. Graph, vertex and edge attributes">
<link rel="chapter" href="igraph-Generators.html" title="Chapter 11. Deterministic graph generators">
<link rel="chapter" href="igraph-Games.html" title='Chapter 12. Stochastic graph generators ("games")'>
<link rel="chapter" href="igraph-Bipartite.html" title="Chapter 13. Bipartite, i.e. two-mode graphs">
<link rel="chapter" href="igraph-Spatial.html" title="Chapter 14. Spatial graphs">
<link rel="chapter" href="igraph-Operators.html" title="Chapter 15. Graph operators">
<link rel="chapter" href="igraph-Visitors.html" title="Chapter 16. Graph visitors">
<link rel="chapter" href="igraph-Structural.html" title="Chapter 17. Structural properties of graphs">
<link rel="chapter" href="igraph-Cycles.html" title="Chapter 18. Graph cycles">
<link rel="chapter" href="igraph-Cliques.html" title="Chapter 19. Cliques and independent vertex sets">
<link rel="chapter" href="igraph-Motifs.html" title="Chapter 20. Graph motifs, dyad census and triad census">
<link rel="chapter" href="igraph-Isomorphism.html" title="Chapter 21. Graph isomorphism">
<link rel="chapter" href="igraph-Coloring.html" title="Chapter 22. Graph coloring">
<link rel="chapter" href="igraph-Flows.html" title="Chapter 23. Maximum flows, minimum cuts and related measures">
<link rel="chapter" href="igraph-Separators.html" title="Chapter 24. Vertex separators">
<link rel="chapter" href="igraph-Community.html" title="Chapter 25. Detecting community structure">
<link rel="chapter" href="igraph-Graphlets.html" title="Chapter 26. Graphlets">
<link rel="chapter" href="igraph-HRG.html" title="Chapter 27. Hierarchical random graphs">
<link rel="chapter" href="igraph-Embedding.html" title="Chapter 28. Embedding of graphs">
<link rel="chapter" href="igraph-Layout.html" title="Chapter 29. Generating layouts for graph drawing">
<link rel="chapter" href="igraph-Processes.html" title="Chapter 30. Processes on graphs">
<link rel="chapter" href="igraph-Foreign.html" title="Chapter 31. Reading and writing graphs from and to files">
<link rel="chapter" href="igraph-Linalg.html" title="Chapter 32. Using BLAS, LAPACK and ARPACK for igraph matrices and graphs">
<link rel="chapter" href="igraph-Nongraph.html" title="Chapter 33. Non-graph related functions">
<link rel="chapter" href="igraph-Advanced.html" title="Chapter 34. Advanced igraph programming">
<link rel="chapter" href="igraph-Glossary.html" title="Chapter 35. Glossary">
<link rel="chapter" href="igraph-Licenses.html" title="Chapter 36. Licenses for igraph and this manual">
<link rel="index" href="ix01.html" title="Index">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navigation-header mb-4" width="100%" summary="Navigation header"><div class="btn-group">
<a accesskey="p" class="btn btn-light" href="igraph-Processes.html"><i class="fa fa-chevron-left"></i>
Previous
</a><a accesskey="h" class="btn btn-light" href="index.html"><i class="fa fa-home"></i>
Home
</a><a accesskey="n" class="btn btn-light" href="igraph-Linalg.html"><i class="fa fa-chevron-right"></i>
Next
</a>
</div></div>
<div class="chapter">
<div class="titlepage"><div><div><h1 class="title">
<a name="igraph-Foreign"></a>Chapter 31. Reading and writing graphs from and to files</h1></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="igraph-Foreign.html#simple-edge-list-and-similar-formats">1. Simple edge list and similar formats</a></span></dt>
<dt><span class="section"><a href="igraph-Foreign.html#binary-formats">2. Binary formats</a></span></dt>
<dt><span class="section"><a href="igraph-Foreign.html#graphml-format">3. GraphML format</a></span></dt>
<dt><span class="section"><a href="igraph-Foreign.html#gml-format">4. GML format</a></span></dt>
<dt><span class="section"><a href="igraph-Foreign.html#pajek-format">5. Pajek format</a></span></dt>
<dt><span class="section"><a href="igraph-Foreign.html#ucinets-dl-file-format">6. UCINET's DL file format</a></span></dt>
<dt><span class="section"><a href="igraph-Foreign.html#graphviz-format">7. Graphviz format</a></span></dt>
<dt><span class="section"><a href="igraph-Foreign.html#leda-format">8. LEDA format</a></span></dt>
<dt><span class="section"><a href="igraph-Foreign.html#locale-helpers">9. Convenience functions for locale change</a></span></dt>
</dl></div>
<p>These functions can write a graph to a file, or read a graph
from a file.</p>
<p>They assume that the current locale uses a decimal point and not
a decimal comma. See <a class="link" href="igraph-Foreign.html#igraph_enter_safelocale" title="9.1. igraph_enter_safelocale — Temporarily set the C locale."><code class="function">igraph_enter_safelocale()</code></a> and
<a class="link" href="igraph-Foreign.html#igraph_exit_safelocale" title="9.2. igraph_exit_safelocale — Temporarily set the C locale."><code class="function">igraph_exit_safelocale()</code></a> for more information.</p>
<p>Note that as <span class="command"><strong>igraph</strong></span> uses the traditional C streams, it is
possible to read/write files from/to memory, at least on GNU
operating systems supporting <span class="quote"><span class="quote">non-standard</span></span> streams.</p>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="simple-edge-list-and-similar-formats"></a>1. Simple edge list and similar formats</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="igraph-Foreign.html#igraph_read_graph_edgelist">1.1. <code class="function">igraph_read_graph_edgelist</code> — Reads an edge list from a file and creates a graph.</a></span></dt>
<dt><span class="section"><a href="igraph-Foreign.html#igraph_write_graph_edgelist">1.2. <code class="function">igraph_write_graph_edgelist</code> — Writes the edge list of a graph to a file.</a></span></dt>
<dt><span class="section"><a href="igraph-Foreign.html#igraph_read_graph_ncol">1.3. <code class="function">igraph_read_graph_ncol</code> — Reads an <code class="literal">.ncol</code> file used by LGL.</a></span></dt>
<dt><span class="section"><a href="igraph-Foreign.html#igraph_write_graph_ncol">1.4. <code class="function">igraph_write_graph_ncol</code> — Writes the graph to a file in <code class="literal">.ncol</code> format.</a></span></dt>
<dt><span class="section"><a href="igraph-Foreign.html#igraph_read_graph_lgl">1.5. <code class="function">igraph_read_graph_lgl</code> — Reads a graph from an <code class="literal">.lgl</code> file.</a></span></dt>
<dt><span class="section"><a href="igraph-Foreign.html#igraph_write_graph_lgl">1.6. <code class="function">igraph_write_graph_lgl</code> — Writes the graph to a file in <code class="literal">.lgl</code> format.</a></span></dt>
<dt><span class="section"><a href="igraph-Foreign.html#igraph_read_graph_dimacs_flow">1.7. <code class="function">igraph_read_graph_dimacs_flow</code> — Read a graph in DIMACS format.</a></span></dt>
<dt><span class="section"><a href="igraph-Foreign.html#igraph_write_graph_dimacs_flow">1.8. <code class="function">igraph_write_graph_dimacs_flow</code> — Write a graph in DIMACS format.</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="igraph_read_graph_edgelist"></a>1.1. <code class="function">igraph_read_graph_edgelist</code> — Reads an edge list from a file and creates a graph.</h3></div></div></div>
<a class="indexterm" name="id-1.32.5.2.2"></a><p>
</p>
<div class="informalexample"><pre class="programlisting">
igraph_error_t igraph_read_graph_edgelist(igraph_t *graph, FILE *instream,
igraph_int_t n, igraph_bool_t directed);
</pre></div>
<p>
</p>
<p>
This format is simply a series of an even number of non-negative integers separated by
whitespace. 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.
</p>
<p>
The largest vertex ID plus one, or the parameter <em class="parameter"><code>n</code></em> determines the vertex count,
whichever is larger. See <a class="link" href="igraph-Foreign.html#igraph_read_graph_ncol" title="1.3. igraph_read_graph_ncol — Reads an .ncol file used by LGL."><code class="function">igraph_read_graph_ncol()</code></a> for reading files where
vertices are specified by name instead of by a numerical vertex ID.
</p>
<p><b>Arguments: </b>
</p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody>
<tr>
<td><p><span class="term"><em class="parameter"><code>graph</code></em>:</span></p></td>
<td><p>
Pointer to an uninitialized graph object.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>instream</code></em>:</span></p></td>
<td><p>
Pointer to a stream, it should be readable.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>n</code></em>:</span></p></td>
<td><p>
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.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>directed</code></em>:</span></p></td>
<td><p>
If true the graph is directed, if false it
will be undirected.
</p></td>
</tr>
</tbody>
</table></div>
<p>
</p>
<p><b>Returns: </b></p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody><tr>
<td><p><span class="term"><em class="parameter"><code></code></em></span></p></td>
<td><p>
Error code:
<code class="constant">IGRAPH_PARSEERROR</code>: if there is a
problem reading the file, or the file is syntactically
incorrect.
</p></td>
</tr></tbody>
</table></div>
<p>
Time complexity: O(|V|+|E|), the
number of vertices plus the number of edges. It is assumed that
reading an integer requires O(1) time.
</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="igraph_write_graph_edgelist"></a>1.2. <code class="function">igraph_write_graph_edgelist</code> — Writes the edge list of a graph to a file.</h3></div></div></div>
<a class="indexterm" name="id-1.32.5.3.2"></a><p>
</p>
<div class="informalexample"><pre class="programlisting">
igraph_error_t igraph_write_graph_edgelist(const igraph_t *graph, FILE *outstream);
</pre></div>
<p>
</p>
<p>
</p>
<p>
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.
</p>
<p><b>Arguments: </b>
</p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody>
<tr>
<td><p><span class="term"><em class="parameter"><code>graph</code></em>:</span></p></td>
<td><p>
The graph object to write.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>outstream</code></em>:</span></p></td>
<td><p>
Pointer to a stream, it should be writable.
</p></td>
</tr>
</tbody>
</table></div>
<p>
</p>
<p><b>Returns: </b></p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody><tr>
<td><p><span class="term"><em class="parameter"><code></code></em></span></p></td>
<td><p>
Error code:
<code class="constant">IGRAPH_EFILE</code> if there is an error writing the
file.
</p></td>
</tr></tbody>
</table></div>
<p>
Time complexity: O(|E|), the
number of edges in the graph. It is assumed that writing an
integer to the file requires O(1)
time.
</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="igraph_read_graph_ncol"></a>1.3. <code class="function">igraph_read_graph_ncol</code> — Reads an <code class="literal">.ncol</code> file used by LGL.</h3></div></div></div>
<a class="indexterm" name="id-1.32.5.4.2"></a><p>
</p>
<div class="informalexample"><pre class="programlisting">
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);
</pre></div>
<p>
</p>
<p>
Also useful for creating graphs from <span class="quote"><span class="quote">named</span></span> (and
optionally weighted) edge lists.
</p>
<p>
This format is used by the Large Graph Layout program
(<a class="ulink" href="https://lgl.sourceforge.net" target="_top">https://lgl.sourceforge.net</a>), and it is simply a
symbolic weighted edge list. It is a simple text file with one edge
per line. An edge is defined by two symbolic vertex names separated
by whitespace. The vertex names themselves cannot contain
whitespace. They may be followed by an optional number,
the weight of the edge; the number can be negative and can be in
scientific notation. If there is no weight specified to an edge it
is assumed to be zero.
</p>
<p>
The resulting graph is always undirected.
LGL cannot deal with files which contain multiple or loop edges,
this is however not checked here, as <span class="command"><strong>igraph</strong></span> is happy with
these.
</p>
<p><b>Arguments: </b>
</p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody>
<tr>
<td><p><span class="term"><em class="parameter"><code>graph</code></em>:</span></p></td>
<td><p>
Pointer to an uninitialized graph object.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>instream</code></em>:</span></p></td>
<td><p>
Pointer to a stream, it should be readable.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>predefnames</code></em>:</span></p></td>
<td><p>
Pointer to the symbolic names of the vertices in
the file. If <code class="constant">NULL</code> is given here then vertex IDs will be
assigned to vertex names in the order of their appearance in
the <code class="literal">.ncol</code> file. If it is not <code class="constant">NULL</code> and some unknown
vertex names are found in the <code class="literal">.ncol</code> file then new vertex
ids will be assigned to them.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>names</code></em>:</span></p></td>
<td><p>
Boolean value. If <code class="constant">true</code>, the symbolic names of the
vertices will be added to the graph as a vertex attribute
called <span class="quote"><span class="quote">name</span></span>.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>weights</code></em>:</span></p></td>
<td><p>
Whether to add the weights of the edges to the
graph as an edge attribute called <span class="quote"><span class="quote">weight</span></span>.
<code class="constant">IGRAPH_ADD_WEIGHTS_YES</code> adds the weights (even if they
are not present in the file, in this case they are assumed
to be 1). <code class="constant">IGRAPH_ADD_WEIGHTS_NO</code> does not add any
edge attribute. <code class="constant">IGRAPH_ADD_WEIGHTS_IF_PRESENT</code> adds the
attribute if and only if there is at least one explicit
edge weight in the input file, and edges without an explicit
weight are assumed to have a weight of 1.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>directed</code></em>:</span></p></td>
<td><p>
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 <code class="constant">IGRAPH_DIRECTED</code> or <code class="constant">IGRAPH_UNDIRECTED</code> to create a directed or undirected graph.
</p></td>
</tr>
</tbody>
</table></div>
<p>
</p>
<p><b>Returns: </b></p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody><tr>
<td><p><span class="term"><em class="parameter"><code></code></em></span></p></td>
<td><p>
Error code:
<code class="constant">IGRAPH_PARSEERROR</code>: if there is a
problem reading
the file, or the file is syntactically incorrect.
</p></td>
</tr></tbody>
</table></div>
<p>
Time complexity:
O(|V|+|E|log(|V|)) if we neglect
the time required by the parsing. As usual
|V| is the number of vertices,
while |E| is the number of edges.
</p>
<p><b>See also: </b></p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody><tr>
<td><p><span class="term"><em class="parameter"><code></code></em></span></p></td>
<td><p>
<a class="link" href="igraph-Foreign.html#igraph_read_graph_lgl" title="1.5. igraph_read_graph_lgl — Reads a graph from an .lgl file."><code class="function">igraph_read_graph_lgl()</code></a>, <a class="link" href="igraph-Foreign.html#igraph_write_graph_ncol" title="1.4. igraph_write_graph_ncol — Writes the graph to a file in .ncol format."><code class="function">igraph_write_graph_ncol()</code></a>
</p></td>
</tr></tbody>
</table></div>
<p>
</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="igraph_write_graph_ncol"></a>1.4. <code class="function">igraph_write_graph_ncol</code> — Writes the graph to a file in <code class="literal">.ncol</code> format.</h3></div></div></div>
<a class="indexterm" name="id-1.32.5.5.2"></a><p>
</p>
<div class="informalexample"><pre class="programlisting">
igraph_error_t igraph_write_graph_ncol(const igraph_t *graph, FILE *outstream,
const char *names, const char *weights);
</pre></div>
<p>
</p>
<p>
</p>
<p>
<code class="literal">.ncol</code> is a format used by LGL, see <a class="link" href="igraph-Foreign.html#igraph_read_graph_ncol" title="1.3. igraph_read_graph_ncol — Reads an .ncol file used by LGL."><code class="function">igraph_read_graph_ncol()</code></a> for details.
</p>
<p>
Note that having multiple or loop edges in an
<code class="literal">.ncol</code> file breaks the LGL software but
<span class="command"><strong>igraph</strong></span> does not check for this condition.
</p>
<p>
This format cannot represent zero-degree vertices.
</p>
<p><b>Arguments: </b>
</p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody>
<tr>
<td><p><span class="term"><em class="parameter"><code>graph</code></em>:</span></p></td>
<td><p>
The graph to write.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>outstream</code></em>:</span></p></td>
<td><p>
The stream object to write to, it should be
writable.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>names</code></em>:</span></p></td>
<td><p>
The name of a string vertex attribute, if symbolic names
are to be written to the file. Supply <code class="constant">NULL</code> to write vertex
ids instead.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>weights</code></em>:</span></p></td>
<td><p>
The name of a numerical edge attribute, which will be
written as weights to the file. Supply <code class="constant">NULL</code> to skip writing
edge weights.
</p></td>
</tr>
</tbody>
</table></div>
<p>
</p>
<p><b>Returns: </b></p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody><tr>
<td><p><span class="term"><em class="parameter"><code></code></em></span></p></td>
<td><p>
Error code:
<code class="constant">IGRAPH_EFILE</code> if there is an error writing the
file.
</p></td>
</tr></tbody>
</table></div>
<p>
Time complexity: O(|E|), the
number of edges. All file operations are expected to have time
complexity O(1).
</p>
<p><b>See also: </b></p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody><tr>
<td><p><span class="term"><em class="parameter"><code></code></em></span></p></td>
<td><p>
<a class="link" href="igraph-Foreign.html#igraph_read_graph_ncol" title="1.3. igraph_read_graph_ncol — Reads an .ncol file used by LGL."><code class="function">igraph_read_graph_ncol()</code></a>, <a class="link" href="igraph-Foreign.html#igraph_write_graph_lgl" title="1.6. igraph_write_graph_lgl — Writes the graph to a file in .lgl format."><code class="function">igraph_write_graph_lgl()</code></a>
</p></td>
</tr></tbody>
</table></div>
<p>
</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="igraph_read_graph_lgl"></a>1.5. <code class="function">igraph_read_graph_lgl</code> — Reads a graph from an <code class="literal">.lgl</code> file.</h3></div></div></div>
<a class="indexterm" name="id-1.32.5.6.2"></a><p>
</p>
<div class="informalexample"><pre class="programlisting">
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);
</pre></div>
<p>
</p>
<p>
The <code class="literal">.lgl</code> format is used by the Large Graph
Layout visualization software
(<a class="ulink" href="https://lgl.sourceforge.net" target="_top">https://lgl.sourceforge.net</a>), it can
describe undirected optionally weighted graphs. From the LGL
manual:
</p>
<div class="blockquote"><blockquote class="blockquote">
<p>The second format is the LGL file format
(<code class="literal">.lgl</code> file
suffix). This is yet another graph file format that tries to be as
stingy as possible with space, yet keeping the edge file in a human
readable (not binary) format. The format itself is like the
following:
</p>
<div class="informalexample"><pre class="programlisting"> # vertex1name
vertex2name [optionalWeight]
vertex3name [optionalWeight] </pre></div>
<p>
Here, the first vertex of an edge is preceded with a pound sign
'#'. Then each vertex that shares an edge with that vertex is
listed one per line on subsequent lines.</p>
</blockquote></div>
<p>
</p>
<p>
LGL cannot handle loop and multiple edges or directed graphs, but
in <span class="command"><strong>igraph</strong></span> it is not an error to have multiple and loop edges.
</p>
<p><b>Arguments: </b>
</p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody>
<tr>
<td><p><span class="term"><em class="parameter"><code>graph</code></em>:</span></p></td>
<td><p>
Pointer to an uninitialized graph object.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>instream</code></em>:</span></p></td>
<td><p>
A stream, it should be readable.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>names</code></em>:</span></p></td>
<td><p>
Boolean value, if <code class="constant">true</code> the symbolic names of the
vertices will be added to the graph as a vertex attribute
called <span class="quote"><span class="quote">name</span></span>.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>weights</code></em>:</span></p></td>
<td><p>
Whether to add the weights of the edges to the
graph as an edge attribute called <span class="quote"><span class="quote">weight</span></span>.
<code class="constant">IGRAPH_ADD_WEIGHTS_YES</code> adds the weights (even if they
are not present in the file, in this case they are assumed
to be 1). <code class="constant">IGRAPH_ADD_WEIGHTS_NO</code> does not add any
edge attribute. <code class="constant">IGRAPH_ADD_WEIGHTS_IF_PRESENT</code> adds the
attribute if and only if there is at least one explicit
edge weight in the input file, and edges without an explicit
weight are assumed to have a weight of 1.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>directed</code></em>:</span></p></td>
<td><p>
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 <code class="constant">IGRAPH_DIRECTED</code> or <code class="constant">IGRAPH_UNDIRECTED</code> to create a directed or undirected graph.
</p></td>
</tr>
</tbody>
</table></div>
<p>
</p>
<p><b>Returns: </b></p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody><tr>
<td><p><span class="term"><em class="parameter"><code></code></em></span></p></td>
<td><p>
Error code:
<code class="constant">IGRAPH_PARSEERROR</code>: if there is a
problem reading the file, or the file is syntactically
incorrect.
</p></td>
</tr></tbody>
</table></div>
<p>
Time complexity:
O(|V|+|E|log(|V|)) if we neglect
the time required by the parsing. As usual
|V| is the number of vertices,
while |E| is the number of edges.
</p>
<p><b>See also: </b></p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody><tr>
<td><p><span class="term"><em class="parameter"><code></code></em></span></p></td>
<td><p>
<a class="link" href="igraph-Foreign.html#igraph_read_graph_ncol" title="1.3. igraph_read_graph_ncol — Reads an .ncol file used by LGL."><code class="function">igraph_read_graph_ncol()</code></a>, <a class="link" href="igraph-Foreign.html#igraph_write_graph_lgl" title="1.6. igraph_write_graph_lgl — Writes the graph to a file in .lgl format."><code class="function">igraph_write_graph_lgl()</code></a>
</p></td>
</tr></tbody>
</table></div>
<p>
</p>
<div class="hideshow" onClick="toggle(this, event)">
<div class="example">
<a name="id-1.32.5.6.11.1"></a><p class="title"><b>Example 31.1.  File <code class="code">examples/simple/igraph_read_graph_lgl.c</code></b></p>
<div class="example-contents">
<pre class="programlisting"><span class="strong"><strong>#include</strong></span> &lt;igraph.h&gt;
int <span class="strong"><strong>main</strong></span>(void) {
igraph_t g;
FILE *input;
<span class="emphasis"><em>/* Initialize the library. */</em></span>
<span class="strong"><strong><a class="link" href="igraph-Basic.html#igraph_setup" title="4.1. igraph_setup — Initializes the igraph library.">igraph_setup</a></strong></span>();
<span class="emphasis"><em>/* Turn on attribute handling. */</em></span>
<span class="strong"><strong><a class="link" href="igraph-Attributes.html#igraph_set_attribute_table" title="1.2. igraph_set_attribute_table — Attach an attribute table.">igraph_set_attribute_table</a></strong></span>(&amp;igraph_cattribute_table);
<span class="emphasis"><em>/* Without names and weights */</em></span>
input = <span class="strong"><strong>fopen</strong></span>("igraph_read_graph_lgl-1.lgl", "r");
<span class="strong"><strong>if</strong></span> (!input) {
<span class="strong"><strong>return</strong></span> 1;
}
<span class="strong"><strong><a class="link" href="igraph-Foreign.html#igraph_read_graph_lgl" title="1.5. igraph_read_graph_lgl — Reads a graph from an .lgl file.">igraph_read_graph_lgl</a></strong></span>(&amp;g, input, 0, IGRAPH_ADD_WEIGHTS_NO, 1);
<span class="strong"><strong>fclose</strong></span>(input);
<span class="strong"><strong>if</strong></span> (!<span class="strong"><strong><a class="link" href="igraph-Basic.html#igraph_is_directed" title="5.2.3. igraph_is_directed — Is this a directed graph?">igraph_is_directed</a></strong></span>(&amp;g)) {
<span class="strong"><strong>return</strong></span> 2;
}
<span class="strong"><strong><a class="link" href="igraph-Foreign.html#igraph_write_graph_edgelist" title="1.2. igraph_write_graph_edgelist — Writes the edge list of a graph to a file.">igraph_write_graph_edgelist</a></strong></span>(&amp;g, stdout);
<span class="strong"><strong><a class="link" href="igraph-Basic.html#igraph_destroy" title="5.1.4. igraph_destroy — Frees the memory allocated for a graph object.">igraph_destroy</a></strong></span>(&amp;g);
<span class="emphasis"><em>/* With names and weights */</em></span>
input = <span class="strong"><strong>fopen</strong></span>("igraph_read_graph_lgl-2.lgl", "r");
<span class="strong"><strong>if</strong></span> (!input) {
<span class="strong"><strong>return</strong></span> 3;
}
<span class="strong"><strong><a class="link" href="igraph-Foreign.html#igraph_read_graph_lgl" title="1.5. igraph_read_graph_lgl — Reads a graph from an .lgl file.">igraph_read_graph_lgl</a></strong></span>(&amp;g, input, 0, IGRAPH_ADD_WEIGHTS_NO, 1);
<span class="strong"><strong>fclose</strong></span>(input);
<span class="strong"><strong>if</strong></span> (!<span class="strong"><strong><a class="link" href="igraph-Basic.html#igraph_is_directed" title="5.2.3. igraph_is_directed — Is this a directed graph?">igraph_is_directed</a></strong></span>(&amp;g)) {
<span class="strong"><strong>return</strong></span> 4;
}
<span class="strong"><strong><a class="link" href="igraph-Foreign.html#igraph_write_graph_ncol" title="1.4. igraph_write_graph_ncol — Writes the graph to a file in .ncol format.">igraph_write_graph_ncol</a></strong></span>(&amp;g, stdout, 0, 0);
<span class="strong"><strong><a class="link" href="igraph-Basic.html#igraph_destroy" title="5.1.4. igraph_destroy — Frees the memory allocated for a graph object.">igraph_destroy</a></strong></span>(&amp;g);
<span class="emphasis"><em>/* Same graph, but forcing undirected mode */</em></span>
input = <span class="strong"><strong>fopen</strong></span>("igraph_read_graph_lgl-2.lgl", "r");
<span class="strong"><strong><a class="link" href="igraph-Foreign.html#igraph_read_graph_lgl" title="1.5. igraph_read_graph_lgl — Reads a graph from an .lgl file.">igraph_read_graph_lgl</a></strong></span>(&amp;g, input, 0, IGRAPH_ADD_WEIGHTS_NO, 0);
<span class="strong"><strong>fclose</strong></span>(input);
<span class="strong"><strong>if</strong></span> (<span class="strong"><strong><a class="link" href="igraph-Basic.html#igraph_is_directed" title="5.2.3. igraph_is_directed — Is this a directed graph?">igraph_is_directed</a></strong></span>(&amp;g)) {
<span class="strong"><strong>return</strong></span> 5;
}
<span class="strong"><strong><a class="link" href="igraph-Foreign.html#igraph_write_graph_ncol" title="1.4. igraph_write_graph_ncol — Writes the graph to a file in .ncol format.">igraph_write_graph_ncol</a></strong></span>(&amp;g, stdout, 0, 0);
<span class="strong"><strong><a class="link" href="igraph-Basic.html#igraph_destroy" title="5.1.4. igraph_destroy — Frees the memory allocated for a graph object.">igraph_destroy</a></strong></span>(&amp;g);
<span class="emphasis"><em>/* Erroneous LGL file (empty vertex name) */</em></span>
input = <span class="strong"><strong>fopen</strong></span>("igraph_read_graph_lgl-3.lgl", "r");
<span class="strong"><strong>if</strong></span> (!input) {
<span class="strong"><strong>return</strong></span> 6;
}
<span class="strong"><strong><a class="link" href="igraph-Error.html#igraph_set_error_handler" title="5.1.1. igraph_set_error_handler — Sets a new error handler.">igraph_set_error_handler</a></strong></span>(<a class="link" href="igraph-Error.html#igraph_error_handler_ignore" title="2.3. igraph_error_handler_ignore — Ignore errors.">igraph_error_handler_ignore</a>);
<span class="strong"><strong>if</strong></span> (<span class="strong"><strong><a class="link" href="igraph-Foreign.html#igraph_read_graph_lgl" title="1.5. igraph_read_graph_lgl — Reads a graph from an .lgl file.">igraph_read_graph_lgl</a></strong></span>(&amp;g, input, 0, IGRAPH_ADD_WEIGHTS_NO, 1) !=
IGRAPH_PARSEERROR) {
<span class="strong"><strong>return</strong></span> 7;
}
<span class="strong"><strong>fclose</strong></span>(input);
<span class="strong"><strong>return</strong></span> 0;
}
</pre>
<p></p>
</div>
</div>
<br class="example-break">
</div>
<p>
</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="igraph_write_graph_lgl"></a>1.6. <code class="function">igraph_write_graph_lgl</code> — Writes the graph to a file in <code class="literal">.lgl</code> format.</h3></div></div></div>
<a class="indexterm" name="id-1.32.5.7.2"></a><p>
</p>
<div class="informalexample"><pre class="programlisting">
igraph_error_t igraph_write_graph_lgl(const igraph_t *graph, FILE *outstream,
const char *names, const char *weights,
igraph_bool_t isolates);
</pre></div>
<p>
</p>
<p>
<code class="literal">.lgl</code> is a format used by LGL, see <a class="link" href="igraph-Foreign.html#igraph_read_graph_lgl" title="1.5. igraph_read_graph_lgl — Reads a graph from an .lgl file."><code class="function">igraph_read_graph_lgl()</code></a> for details.
</p>
<p>
Note that having multiple or loop edges in an
<code class="literal">.lgl</code> file breaks the LGL software but <span class="command"><strong>igraph</strong></span>
does not check for this condition.
</p>
<p><b>Arguments: </b>
</p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody>
<tr>
<td><p><span class="term"><em class="parameter"><code>graph</code></em>:</span></p></td>
<td><p>
The graph to write.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>outstream</code></em>:</span></p></td>
<td><p>
The stream object to write to, it should be
writable.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>names</code></em>:</span></p></td>
<td><p>
The name of a string vertex attribute, if symbolic names
are to be written to the file. Supply <code class="constant">NULL</code> to write vertex
ids instead.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>weights</code></em>:</span></p></td>
<td><p>
The name of a numerical edge attribute, which will be
written as weights to the file. Supply <code class="constant">NULL</code> to skip writing
edge weights.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>isolates</code></em>:</span></p></td>
<td><p>
If <code class="constant">true</code>, isolated vertices are also written
to the file. If <code class="constant">false</code>, they will be omitted.
</p></td>
</tr>
</tbody>
</table></div>
<p>
</p>
<p><b>Returns: </b></p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody><tr>
<td><p><span class="term"><em class="parameter"><code></code></em></span></p></td>
<td><p>
Error code:
<code class="constant">IGRAPH_EFILE</code> if there is an error
writing the file.
</p></td>
</tr></tbody>
</table></div>
<p>
Time complexity: O(|E|), the number of edges if <em class="parameter"><code>isolates</code></em> is <code class="constant">false</code>,
O(|V|+|E|) otherwise. All file operations are expected to have
time complexity O(1).
</p>
<p><b>See also: </b></p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody><tr>
<td><p><span class="term"><em class="parameter"><code></code></em></span></p></td>
<td><p>
<a class="link" href="igraph-Foreign.html#igraph_read_graph_lgl" title="1.5. igraph_read_graph_lgl — Reads a graph from an .lgl file."><code class="function">igraph_read_graph_lgl()</code></a>, <a class="link" href="igraph-Foreign.html#igraph_write_graph_ncol" title="1.4. igraph_write_graph_ncol — Writes the graph to a file in .ncol format."><code class="function">igraph_write_graph_ncol()</code></a>
</p></td>
</tr></tbody>
</table></div>
<p>
</p>
<div class="hideshow" onClick="toggle(this, event)">
<div class="example">
<a name="id-1.32.5.7.11.1"></a><p class="title"><b>Example 31.2.  File <code class="code">examples/simple/igraph_write_graph_lgl.c</code></b></p>
<div class="example-contents">
<pre class="programlisting"><span class="strong"><strong>#include</strong></span> &lt;igraph.h&gt;
int <span class="strong"><strong>main</strong></span>(void) {
igraph_t graph;
igraph_strvector_t names;
<a class="link" href="igraph-Data-structures.html#igraph_vector_t" title="2.1.  About igraph_vector_t objects">igraph_vector_t</a> weights;
igraph_int_t i;
igraph_int_t vcount, ecount;
<span class="emphasis"><em>/* Initialize the library. */</em></span>
<span class="strong"><strong><a class="link" href="igraph-Basic.html#igraph_setup" title="4.1. igraph_setup — Initializes the igraph library.">igraph_setup</a></strong></span>();
<span class="strong"><strong><a class="link" href="igraph-Attributes.html#igraph_set_attribute_table" title="1.2. igraph_set_attribute_table — Attach an attribute table.">igraph_set_attribute_table</a></strong></span>(&amp;igraph_cattribute_table);
<span class="strong"><strong><a class="link" href="igraph-Generators.html#igraph_small" title="2.2. igraph_small — Shorthand to create a small graph, giving the edges as arguments.">igraph_small</a></strong></span>(&amp;graph, 7, IGRAPH_UNDIRECTED,
0,1, 1,3, 1,2, 2,0, 4,2, 3,4,
-1);
vcount = <span class="strong"><strong><a class="link" href="igraph-Basic.html#igraph_vcount" title="5.2.1. igraph_vcount — The number of vertices in a graph.">igraph_vcount</a></strong></span>(&amp;graph);
ecount = <span class="strong"><strong><a class="link" href="igraph-Basic.html#igraph_ecount" title="5.2.2. igraph_ecount — The number of edges in a graph.">igraph_ecount</a></strong></span>(&amp;graph);
<span class="strong"><strong>printf</strong></span>("Output without isolates:\n");
<span class="strong"><strong><a class="link" href="igraph-Foreign.html#igraph_write_graph_lgl" title="1.6. igraph_write_graph_lgl — Writes the graph to a file in .lgl format.">igraph_write_graph_lgl</a></strong></span>(&amp;graph, stdout, <span class="emphasis"><em>/*names*/</em></span> NULL, <span class="emphasis"><em>/*weights*/</em></span> NULL, <span class="emphasis"><em>/*isolates*/</em></span> 0);
<span class="strong"><strong>printf</strong></span>("\nOutput with isolates:\n");
<span class="strong"><strong><a class="link" href="igraph-Foreign.html#igraph_write_graph_lgl" title="1.6. igraph_write_graph_lgl — Writes the graph to a file in .lgl format.">igraph_write_graph_lgl</a></strong></span>(&amp;graph, stdout, <span class="emphasis"><em>/*names*/</em></span> NULL, <span class="emphasis"><em>/*weights*/</em></span> NULL, <span class="emphasis"><em>/*isolates*/</em></span> 1);
<span class="strong"><strong>printf</strong></span>("\nOutput vertex and edge labels:\n");
<span class="strong"><strong><a class="link" href="igraph-Data-structures.html#igraph_strvector_init" title="8.1. igraph_strvector_init — Initializes a string vector.">igraph_strvector_init</a></strong></span>(&amp;names, vcount);
<span class="strong"><strong>for</strong></span> (i = 0; i &lt; vcount; i++) {
char str[2] = " "; <span class="emphasis"><em>/* initialize to ensure presence of null terminator */</em></span>
str[0] = 'A' + i;
<span class="strong"><strong><a class="link" href="igraph-Data-structures.html#igraph_strvector_set" title="8.6. igraph_strvector_set — Sets an element of the string vector from a string.">igraph_strvector_set</a></strong></span>(&amp;names, i, str);
}
<span class="strong"><strong><a class="link" href="igraph-Attributes.html#SETVASV" title="4.2.24. SETVASV — Set a string vertex attribute for all vertices">SETVASV</a></strong></span>(&amp;graph, "names", &amp;names);
<span class="strong"><strong><a class="link" href="igraph-Data-structures.html#igraph_vector_init_range" title="2.2.4. igraph_vector_init_range — Initializes a vector with a range.">igraph_vector_init_range</a></strong></span>(&amp;weights, 1, ecount + 1);
<span class="strong"><strong><a class="link" href="igraph-Attributes.html#SETEANV" title="4.2.26. SETEANV — Set a numeric edge attribute for all edges">SETEANV</a></strong></span>(&amp;graph, "weights", &amp;weights);
<span class="strong"><strong><a class="link" href="igraph-Foreign.html#igraph_write_graph_lgl" title="1.6. igraph_write_graph_lgl — Writes the graph to a file in .lgl format.">igraph_write_graph_lgl</a></strong></span>(&amp;graph, stdout, "names", "weights", <span class="emphasis"><em>/*isolates*/</em></span> 0);
<span class="strong"><strong><a class="link" href="igraph-Data-structures.html#igraph_strvector_destroy" title="8.3. igraph_strvector_destroy — Frees the memory allocated for the string vector.">igraph_strvector_destroy</a></strong></span>(&amp;names);
<span class="strong"><strong><a class="link" href="igraph-Data-structures.html#igraph_vector_destroy" title="2.2.5. igraph_vector_destroy — Destroys a vector object.">igraph_vector_destroy</a></strong></span>(&amp;weights);
<span class="strong"><strong><a class="link" href="igraph-Basic.html#igraph_destroy" title="5.1.4. igraph_destroy — Frees the memory allocated for a graph object.">igraph_destroy</a></strong></span>(&amp;graph);
<span class="strong"><strong>return</strong></span> 0;
}
</pre>
<p></p>
</div>
</div>
<br class="example-break">
</div>
<p>
</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="igraph_read_graph_dimacs_flow"></a>1.7. <code class="function">igraph_read_graph_dimacs_flow</code> — Read a graph in DIMACS format.</h3></div></div></div>
<a class="indexterm" name="id-1.32.5.8.2"></a><p>
</p>
<div class="informalexample"><pre class="programlisting">
igraph_error_t igraph_read_graph_dimacs_flow(
igraph_t *graph, FILE *instream,
igraph_strvector_t *problem,
igraph_vector_int_t *label,
igraph_int_t *source,
igraph_int_t *target,
igraph_vector_t *capacity,
igraph_bool_t directed);
</pre></div>
<p>
</p>
<p>
This function reads the DIMACS file format, more specifically the
version for network flow problems, see the files at
<a class="ulink" href="http://archive.dimacs.rutgers.edu/pub/netflow/general-info/" target="_top">http://archive.dimacs.rutgers.edu/pub/netflow/general-info/</a>
</p>
<p>
This is a line-oriented text file (ASCII) format. The first
character of each line defines the type of the line. If the first
character is <code class="constant">c</code> the line is a comment line and it is
ignored. There is one problem line (<code class="constant">p</code> in the file), it
must appear before any node and arc descriptor lines. The problem
line has three fields separated by spaces: the problem type
(<code class="constant">max</code> or <code class="constant">edge</code>), the number of vertices,
and number of edges in the graph. In MAX problems,
exactly two node identification lines are expected
(<code class="constant">n</code>), one for the source, and one for the target vertex.
These have two fields: the ID of the vertex and the type of the
vertex, either <code class="constant">s</code> ( = source) or <code class="constant">t</code> ( = target).
Arc lines start with <code class="constant">a</code> and have three fields: the source vertex,
the target vertex and the edge capacity. In EDGE problems,
there may be a node line (<code class="constant">n</code>) for each node. It specifies the
node index and an integer node label. Nodes for which no explicit
label was specified will use their index as label. In EDGE problems,
each edge is specified as an edge line (<code class="constant">e</code>).
</p>
<p>
Within DIMACS files, vertex IDs are numbered from 1.
</p>
<p><b>Arguments: </b>
</p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody>
<tr>
<td><p><span class="term"><em class="parameter"><code>graph</code></em>:</span></p></td>
<td><p>
Pointer to an uninitialized graph object.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>instream</code></em>:</span></p></td>
<td><p>
The file to read from.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>problem</code></em>:</span></p></td>
<td><p>
If not <code class="constant">NULL</code>, it will contain the problem type.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>label</code></em>:</span></p></td>
<td><p>
If not <code class="constant">NULL</code>, node labels will be stored here for <code class="constant">edge</code>
problems. Ignored for <code class="constant">max</code> problems.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>source</code></em>:</span></p></td>
<td><p>
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 <code class="constant">NULL</code>.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>target</code></em>:</span></p></td>
<td><p>
Pointer to an integer, the (igraph) ID of the target
node will be stored here. It is ignored if <code class="constant">NULL</code>.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>capacity</code></em>:</span></p></td>
<td><p>
Pointer to an initialized vector, the capacity of
the edges will be stored here if not \ NULL.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>directed</code></em>:</span></p></td>
<td><p>
Boolean, whether to create a directed graph.
</p></td>
</tr>
</tbody>
</table></div>
<p>
</p>
<p><b>Returns: </b></p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody><tr>
<td><p><span class="term"><em class="parameter"><code></code></em></span></p></td>
<td><p>
Error code.
</p></td>
</tr></tbody>
</table></div>
<p>
Time complexity: O(|V|+|E|+c), the number of vertices plus the
number of edges, plus the size of the file in characters.
</p>
<p><b>See also: </b></p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody><tr>
<td><p><span class="term"><em class="parameter"><code></code></em></span></p></td>
<td><p>
<a class="link" href="igraph-Foreign.html#igraph_write_graph_dimacs_flow" title="1.8. igraph_write_graph_dimacs_flow — Write a graph in DIMACS format."><code class="function">igraph_write_graph_dimacs_flow()</code></a>
</p></td>
</tr></tbody>
</table></div>
<p>
</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="igraph_write_graph_dimacs_flow"></a>1.8. <code class="function">igraph_write_graph_dimacs_flow</code> — Write a graph in DIMACS format.</h3></div></div></div>
<a class="indexterm" name="id-1.32.5.9.2"></a><p>
</p>
<div class="informalexample"><pre class="programlisting">
igraph_error_t igraph_write_graph_dimacs_flow(const igraph_t *graph, FILE *outstream,
igraph_int_t source, igraph_int_t target,
const igraph_vector_t *capacity);
</pre></div>
<p>
</p>
<p>
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/
</p>
<p>
This file format is discussed in the documentation of <a class="link" href="igraph-Foreign.html#igraph_read_graph_dimacs_flow" title="1.7. igraph_read_graph_dimacs_flow — Read a graph in DIMACS format."><code class="function">igraph_read_graph_dimacs_flow()</code></a>, see that for more information.
</p>
<p><b>Arguments: </b>
</p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody>
<tr>
<td><p><span class="term"><em class="parameter"><code>graph</code></em>:</span></p></td>
<td><p>
The graph to write to the stream.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>outstream</code></em>:</span></p></td>
<td><p>
The stream.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>source</code></em>:</span></p></td>
<td><p>
Integer, the id of the source vertex for the maximum
flow.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>target</code></em>:</span></p></td>
<td><p>
Integer, the id of the target vertex.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>capacity</code></em>:</span></p></td>
<td><p>
Pointer to an initialized vector containing the
edge capacity values.
</p></td>
</tr>
</tbody>
</table></div>
<p>
</p>
<p><b>Returns: </b></p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody><tr>
<td><p><span class="term"><em class="parameter"><code></code></em></span></p></td>
<td><p>
Error code.
</p></td>
</tr></tbody>
</table></div>
<p>
Time complexity: O(|E|), the number of edges in the graph.
</p>
<p><b>See also: </b></p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody><tr>
<td><p><span class="term"><em class="parameter"><code></code></em></span></p></td>
<td><p>
<a class="link" href="igraph-Foreign.html#igraph_read_graph_dimacs_flow" title="1.7. igraph_read_graph_dimacs_flow — Read a graph in DIMACS format."><code class="function">igraph_read_graph_dimacs_flow()</code></a>
</p></td>
</tr></tbody>
</table></div>
<p>
</p>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="binary-formats"></a>2. Binary formats</h2></div></div></div>
<div class="toc"><dl class="toc"><dt><span class="section"><a href="igraph-Foreign.html#igraph_read_graph_graphdb">2.1. <code class="function">igraph_read_graph_graphdb</code> — Read a graph in the binary graph database format.</a></span></dt></dl></div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="igraph_read_graph_graphdb"></a>2.1. <code class="function">igraph_read_graph_graphdb</code> — Read a graph in the binary graph database format.</h3></div></div></div>
<a class="indexterm" name="id-1.32.6.2.2"></a><p>
</p>
<div class="informalexample"><pre class="programlisting">
igraph_error_t igraph_read_graph_graphdb(igraph_t *graph, FILE *instream,
igraph_bool_t directed);
</pre></div>
<p>
</p>
<p>
This is a binary format, used in the ARG Graph Database
for isomorphism testing. For more information, see
<a class="ulink" href="https://mivia.unisa.it/datasets/graph-database/arg-database/" target="_top">https://mivia.unisa.it/datasets/graph-database/arg-database/</a>
</p>
<p>
From the graph database homepage:
</p>
<div class="blockquote"><blockquote class="blockquote">
<p>
The graphs are stored in a compact binary format, one graph per
file. The file is composed of 16 bit words, which are represented
using the so-called little-endian convention, i.e. the least
significant byte of the word is stored first.</p>
<p>
Then, for each node, the file contains the list of edges coming
out of the node itself. The list is represented by a word encoding
its length, followed by a word for each edge, representing the
destination node of the edge. Node numeration is 0-based, so the
first node of the graph has index 0.</p>
</blockquote></div>
<p>
As of igraph 0.10, only unlabelled graphs are implemented.
</p>
<p>
References:
</p>
<p>
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).
<a class="ulink" href="https://doi.org/10.1016/S0167-8655(02)00253-2" target="_top">https://doi.org/10.1016/S0167-8655(02)00253-2</a>
</p>
<p>
MIVIA ARG Dataset,
<a class="ulink" href="https://zenodo.org/records/11204020" target="_top">https://zenodo.org/records/11204020</a>,
<a class="ulink" href="https://mivia.unisa.it/datasets/graph-database/arg-database/" target="_top">https://mivia.unisa.it/datasets/graph-database/arg-database/</a>
</p>
<p><b>Arguments: </b>
</p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody>
<tr>
<td><p><span class="term"><em class="parameter"><code>graph</code></em>:</span></p></td>
<td><p>
Pointer to an uninitialized graph object.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>instream</code></em>:</span></p></td>
<td><p>
The stream to read from. It should be opened
in binary mode.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>directed</code></em>:</span></p></td>
<td><p>
Whether to create a directed graph.
</p></td>
</tr>
</tbody>
</table></div>
<p>
</p>
<p><b>Returns: </b></p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody><tr>
<td><p><span class="term"><em class="parameter"><code></code></em></span></p></td>
<td><p>
Error code.
</p></td>
</tr></tbody>
</table></div>
<p>
Time complexity: O(|V|+|E|), the number of vertices plus the
number of edges.
</p>
<div class="hideshow" onClick="toggle(this, event)">
<div class="example">
<a name="id-1.32.6.2.14.1"></a><p class="title"><b>Example 31.3.  File <code class="code">examples/simple/igraph_read_graph_graphdb.c</code></b></p>
<div class="example-contents">
<pre class="programlisting"><span class="strong"><strong>#include</strong></span> &lt;igraph.h&gt;
int <span class="strong"><strong>main</strong></span>(void) {
igraph_t g;
FILE *input;
<span class="emphasis"><em>/* Initialize the library. */</em></span>
<span class="strong"><strong><a class="link" href="igraph-Basic.html#igraph_setup" title="4.1. igraph_setup — Initializes the igraph library.">igraph_setup</a></strong></span>();
input = <span class="strong"><strong>fopen</strong></span>("iso_b03_m1000.A00", "rb");
<span class="strong"><strong>if</strong></span> (!input) {
<span class="strong"><strong>return</strong></span> 1;
}
<span class="strong"><strong><a class="link" href="igraph-Foreign.html#igraph_read_graph_graphdb" title="2.1. igraph_read_graph_graphdb — Read a graph in the binary graph database format.">igraph_read_graph_graphdb</a></strong></span>(&amp;g, input, IGRAPH_DIRECTED);
<span class="strong"><strong>fclose</strong></span>(input);
<span class="strong"><strong><a class="link" href="igraph-Foreign.html#igraph_write_graph_edgelist" title="1.2. igraph_write_graph_edgelist — Writes the edge list of a graph to a file.">igraph_write_graph_edgelist</a></strong></span>(&amp;g, stdout);
<span class="strong"><strong><a class="link" href="igraph-Basic.html#igraph_destroy" title="5.1.4. igraph_destroy — Frees the memory allocated for a graph object.">igraph_destroy</a></strong></span>(&amp;g);
<span class="strong"><strong>return</strong></span> 0;
}
</pre>
<p></p>
</div>
</div>
<br class="example-break">
</div>
<p>
</p>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="graphml-format"></a>3. GraphML format</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="igraph-Foreign.html#igraph_read_graph_graphml">3.1. <code class="function">igraph_read_graph_graphml</code> — Reads a graph from a GraphML file.</a></span></dt>
<dt><span class="section"><a href="igraph-Foreign.html#igraph_write_graph_graphml">3.2. <code class="function">igraph_write_graph_graphml</code> — Writes the graph to a file in GraphML format.</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="igraph_read_graph_graphml"></a>3.1. <code class="function">igraph_read_graph_graphml</code> — Reads a graph from a GraphML file.</h3></div></div></div>
<a class="indexterm" name="id-1.32.7.2.2"></a><p>
</p>
<div class="informalexample"><pre class="programlisting">
igraph_error_t igraph_read_graph_graphml(igraph_t *graph, FILE *instream, igraph_int_t index);
</pre></div>
<p>
</p>
<p>
</p>
<p>
GraphML is an XML-based file format for representing various types of
graphs. Currently only the most basic import functionality is implemented
in igraph: it can read GraphML files without nested graphs and hyperedges.
Attributes of the graph are loaded only if an attribute interface
is attached, see <a class="link" href="igraph-Attributes.html#igraph_set_attribute_table" title="1.2. igraph_set_attribute_table — Attach an attribute table."><code class="function">igraph_set_attribute_table()</code></a>. String attrribute values
are returned in UTF-8 encoding.
</p>
<p>
Graph attribute names are taken from the <code class="literal">attr.name</code> attributes of the
<code class="constant">key</code> tags in the GraphML file. Since <code class="literal">attr.name</code> is not mandatory,
igraph will fall back to the <code class="constant">id</code> attribute of the <code class="constant">key</code> tag if
<code class="literal">attr.name</code> is missing.
</p>
<p><b>Arguments: </b>
</p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody>
<tr>
<td><p><span class="term"><em class="parameter"><code>graph</code></em>:</span></p></td>
<td><p>
Pointer to an uninitialized graph object.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>instream</code></em>:</span></p></td>
<td><p>
A stream, it should be readable.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>index</code></em>:</span></p></td>
<td><p>
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.</p></td>
</tr>
</tbody>
</table></div>
<p>
</p>
<p><b>Returns: </b></p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody><tr>
<td><p><span class="term"><em class="parameter"><code></code></em></span></p></td>
<td><p>
Error code:
<code class="constant">IGRAPH_PARSEERROR</code>: if there is a
problem reading the file, or the file is syntactically
incorrect.
<code class="constant">IGRAPH_UNIMPLEMENTED</code>: the GraphML functionality was disabled
at compile-time
</p></td>
</tr></tbody>
</table></div>
<p>
</p>
<div class="hideshow" onClick="toggle(this, event)">
<div class="example">
<a name="id-1.32.7.2.10.1"></a><p class="title"><b>Example 31.4.  File <code class="code">examples/simple/graphml.c</code></b></p>
<div class="example-contents">
<pre class="programlisting"><span class="strong"><strong>#include</strong></span> &lt;igraph.h&gt;
<span class="strong"><strong>#include</strong></span> &lt;stdio.h&gt;
<span class="strong"><strong>#include</strong></span> &lt;unistd.h&gt; <span class="emphasis"><em>/* unlink */</em></span>
int <span class="strong"><strong>main</strong></span>(void) {
igraph_t graph;
<span class="strong"><strong>const</strong></span> char *infilename = "test.graphml";
<span class="strong"><strong>const</strong></span> char *outfilename = "test2.graphml";
<span class="emphasis"><em>/* Initialize the library. */</em></span>
<span class="strong"><strong><a class="link" href="igraph-Basic.html#igraph_setup" title="4.1. igraph_setup — Initializes the igraph library.">igraph_setup</a></strong></span>();
<span class="emphasis"><em>/* Set up attribute handling, so graph attributes can be imported</em></span>
<span class="emphasis"><em> * from the GraphML file. */</em></span>
<span class="strong"><strong><a class="link" href="igraph-Attributes.html#igraph_set_attribute_table" title="1.2. igraph_set_attribute_table — Attach an attribute table.">igraph_set_attribute_table</a></strong></span>(&amp;igraph_cattribute_table);
<span class="emphasis"><em>/* Problems in the GraphML file may cause igraph to print warnings.</em></span>
<span class="emphasis"><em> * If this is not desired, set a silent warning handler: */</em></span>
<span class="strong"><strong><a class="link" href="igraph-Error.html#igraph_set_warning_handler" title="4.2. igraph_set_warning_handler — Installs a warning handler.">igraph_set_warning_handler</a></strong></span>(&amp;<a class="link" href="igraph-Error.html#igraph_warning_handler_ignore" title="4.7. igraph_warning_handler_ignore — Ignores all warnings.">igraph_warning_handler_ignore</a>);
<span class="emphasis"><em>/* Read the contents of a GraphML file. */</em></span>
<span class="emphasis"><em>/* GraphML */</em></span>
FILE *infile = <span class="strong"><strong>fopen</strong></span>("test.graphml", "r");
<span class="strong"><strong>if</strong></span> (! infile) {
<span class="strong"><strong>fprintf</strong></span>(stderr, "Could not open input file '%s'.", infilename);
<span class="strong"><strong>exit</strong></span>(1);
}
<span class="emphasis"><em>/* GraphML support is an optional feature in igraph. If igraph was compiled</em></span>
<span class="emphasis"><em> * without GraphML support, igraph_read_graph_graphml() returns IGRAPH_UNIMPLEMENTED.</em></span>
<span class="emphasis"><em> * We temporarily disable the default error handler so we can test for this condition. */</em></span>
<a class="link" href="igraph-Error.html#igraph_error_handler_t" title="2.1. igraph_error_handler_t — The type of error handler functions.">igraph_error_handler_t</a> *oldhandler = <span class="strong"><strong><a class="link" href="igraph-Error.html#igraph_set_error_handler" title="5.1.1. igraph_set_error_handler — Sets a new error handler.">igraph_set_error_handler</a></strong></span>(<a class="link" href="igraph-Error.html#igraph_error_handler_ignore" title="2.3. igraph_error_handler_ignore — Ignore errors.">igraph_error_handler_ignore</a>);
<a class="link" href="igraph-Error.html#igraph_error_t" title="3.1. igraph_error_t — Return type for functions returning an error code.">igraph_error_t</a> ret = <span class="strong"><strong><a class="link" href="igraph-Foreign.html#igraph_read_graph_graphml" title="3.1. igraph_read_graph_graphml — Reads a graph from a GraphML file.">igraph_read_graph_graphml</a></strong></span>(&amp;graph, infile, 0);
<span class="strong"><strong>if</strong></span> (ret == IGRAPH_UNIMPLEMENTED) {
<span class="strong"><strong>fprintf</strong></span>(stderr, "igraph was compiled without GraphML support.");
<span class="strong"><strong>exit</strong></span>(77);
}
<span class="strong"><strong>if</strong></span> (ret != IGRAPH_SUCCESS) {
<span class="strong"><strong>fprintf</strong></span>(stderr, "Unexpected error while reading GraphML.");
<span class="strong"><strong>exit</strong></span>(1);
}
<span class="strong"><strong><a class="link" href="igraph-Error.html#igraph_set_error_handler" title="5.1.1. igraph_set_error_handler — Sets a new error handler.">igraph_set_error_handler</a></strong></span>(oldhandler);
<span class="strong"><strong>fclose</strong></span>(infile);
<span class="emphasis"><em>/* Write it back into another file. */</em></span>
FILE *outfile = <span class="strong"><strong>fopen</strong></span>(outfilename, "w");
<span class="strong"><strong>if</strong></span> (outfile) {
<span class="strong"><strong><a class="link" href="igraph-Foreign.html#igraph_write_graph_graphml" title="3.2. igraph_write_graph_graphml — Writes the graph to a file in GraphML format.">igraph_write_graph_graphml</a></strong></span>(&amp;graph, outfile, true);
<span class="strong"><strong>fclose</strong></span>(outfile);
<span class="emphasis"><em>/* Clean up after ourselves */</em></span>
<span class="strong"><strong>unlink</strong></span>(outfilename);
} <span class="strong"><strong>else</strong></span> {
<span class="strong"><strong>fprintf</strong></span>(stderr, "Could not write output file '%s'.", outfilename);
}
<span class="emphasis"><em>/* Destroy the graph */</em></span>
<span class="strong"><strong><a class="link" href="igraph-Basic.html#igraph_destroy" title="5.1.4. igraph_destroy — Frees the memory allocated for a graph object.">igraph_destroy</a></strong></span>(&amp;graph);
<span class="strong"><strong>return</strong></span> 0;
}
</pre>
<p></p>
</div>
</div>
<br class="example-break">
</div>
<p>
</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="igraph_write_graph_graphml"></a>3.2. <code class="function">igraph_write_graph_graphml</code> — Writes the graph to a file in GraphML format.</h3></div></div></div>
<a class="indexterm" name="id-1.32.7.3.2"></a><p>
</p>
<div class="informalexample"><pre class="programlisting">
igraph_error_t igraph_write_graph_graphml(const igraph_t *graph, FILE *outstream,
igraph_bool_t prefixattr);
</pre></div>
<p>
</p>
<p>
GraphML is an XML-based file format for representing various types of
graphs. See the GraphML Primer (<a class="ulink" href="http://graphml.graphdrawing.org/primer/graphml-primer.html" target="_top">http://graphml.graphdrawing.org/primer/graphml-primer.html</a>)
for the detailed format description.
</p>
<p>
When a numerical attribute value is NaN, it will be omitted from the file.
</p>
<p>
This function assumes that non-ASCII characters in attribute names and string
attribute values are UTF-8 encoded. If this is not the case, the resulting
XML file will be invalid. Control characters, i.e. character codes up to and
including 31 (with the exception of tab, cr and lf), are not allowed.
</p>
<p><b>Arguments: </b>
</p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody>
<tr>
<td><p><span class="term"><em class="parameter"><code>graph</code></em>:</span></p></td>
<td><p>
The graph to write.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>outstream</code></em>:</span></p></td>
<td><p>
The stream object to write to, it should be
writable.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>prefixattr</code></em>:</span></p></td>
<td><p>
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.
</p></td>
</tr>
</tbody>
</table></div>
<p>
</p>
<p><b>Returns: </b></p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody><tr>
<td><p><span class="term"><em class="parameter"><code></code></em></span></p></td>
<td><p>
Error code:
<code class="constant">IGRAPH_EFILE</code> if there is an error
writing the file.
</p></td>
</tr></tbody>
</table></div>
<p>
Time complexity: O(|V|+|E|) otherwise. All
file operations are expected to have time complexity
O(1).
</p>
<div class="hideshow" onClick="toggle(this, event)">
<div class="example">
<a name="id-1.32.7.3.10.1"></a><p class="title"><b>Example 31.5.  File <code class="code">examples/simple/graphml.c</code></b></p>
<div class="example-contents">
<pre class="programlisting"><span class="strong"><strong>#include</strong></span> &lt;igraph.h&gt;
<span class="strong"><strong>#include</strong></span> &lt;stdio.h&gt;
<span class="strong"><strong>#include</strong></span> &lt;unistd.h&gt; <span class="emphasis"><em>/* unlink */</em></span>
int <span class="strong"><strong>main</strong></span>(void) {
igraph_t graph;
<span class="strong"><strong>const</strong></span> char *infilename = "test.graphml";
<span class="strong"><strong>const</strong></span> char *outfilename = "test2.graphml";
<span class="emphasis"><em>/* Initialize the library. */</em></span>
<span class="strong"><strong><a class="link" href="igraph-Basic.html#igraph_setup" title="4.1. igraph_setup — Initializes the igraph library.">igraph_setup</a></strong></span>();
<span class="emphasis"><em>/* Set up attribute handling, so graph attributes can be imported</em></span>
<span class="emphasis"><em> * from the GraphML file. */</em></span>
<span class="strong"><strong><a class="link" href="igraph-Attributes.html#igraph_set_attribute_table" title="1.2. igraph_set_attribute_table — Attach an attribute table.">igraph_set_attribute_table</a></strong></span>(&amp;igraph_cattribute_table);
<span class="emphasis"><em>/* Problems in the GraphML file may cause igraph to print warnings.</em></span>
<span class="emphasis"><em> * If this is not desired, set a silent warning handler: */</em></span>
<span class="strong"><strong><a class="link" href="igraph-Error.html#igraph_set_warning_handler" title="4.2. igraph_set_warning_handler — Installs a warning handler.">igraph_set_warning_handler</a></strong></span>(&amp;<a class="link" href="igraph-Error.html#igraph_warning_handler_ignore" title="4.7. igraph_warning_handler_ignore — Ignores all warnings.">igraph_warning_handler_ignore</a>);
<span class="emphasis"><em>/* Read the contents of a GraphML file. */</em></span>
<span class="emphasis"><em>/* GraphML */</em></span>
FILE *infile = <span class="strong"><strong>fopen</strong></span>("test.graphml", "r");
<span class="strong"><strong>if</strong></span> (! infile) {
<span class="strong"><strong>fprintf</strong></span>(stderr, "Could not open input file '%s'.", infilename);
<span class="strong"><strong>exit</strong></span>(1);
}
<span class="emphasis"><em>/* GraphML support is an optional feature in igraph. If igraph was compiled</em></span>
<span class="emphasis"><em> * without GraphML support, igraph_read_graph_graphml() returns IGRAPH_UNIMPLEMENTED.</em></span>
<span class="emphasis"><em> * We temporarily disable the default error handler so we can test for this condition. */</em></span>
<a class="link" href="igraph-Error.html#igraph_error_handler_t" title="2.1. igraph_error_handler_t — The type of error handler functions.">igraph_error_handler_t</a> *oldhandler = <span class="strong"><strong><a class="link" href="igraph-Error.html#igraph_set_error_handler" title="5.1.1. igraph_set_error_handler — Sets a new error handler.">igraph_set_error_handler</a></strong></span>(<a class="link" href="igraph-Error.html#igraph_error_handler_ignore" title="2.3. igraph_error_handler_ignore — Ignore errors.">igraph_error_handler_ignore</a>);
<a class="link" href="igraph-Error.html#igraph_error_t" title="3.1. igraph_error_t — Return type for functions returning an error code.">igraph_error_t</a> ret = <span class="strong"><strong><a class="link" href="igraph-Foreign.html#igraph_read_graph_graphml" title="3.1. igraph_read_graph_graphml — Reads a graph from a GraphML file.">igraph_read_graph_graphml</a></strong></span>(&amp;graph, infile, 0);
<span class="strong"><strong>if</strong></span> (ret == IGRAPH_UNIMPLEMENTED) {
<span class="strong"><strong>fprintf</strong></span>(stderr, "igraph was compiled without GraphML support.");
<span class="strong"><strong>exit</strong></span>(77);
}
<span class="strong"><strong>if</strong></span> (ret != IGRAPH_SUCCESS) {
<span class="strong"><strong>fprintf</strong></span>(stderr, "Unexpected error while reading GraphML.");
<span class="strong"><strong>exit</strong></span>(1);
}
<span class="strong"><strong><a class="link" href="igraph-Error.html#igraph_set_error_handler" title="5.1.1. igraph_set_error_handler — Sets a new error handler.">igraph_set_error_handler</a></strong></span>(oldhandler);
<span class="strong"><strong>fclose</strong></span>(infile);
<span class="emphasis"><em>/* Write it back into another file. */</em></span>
FILE *outfile = <span class="strong"><strong>fopen</strong></span>(outfilename, "w");
<span class="strong"><strong>if</strong></span> (outfile) {
<span class="strong"><strong><a class="link" href="igraph-Foreign.html#igraph_write_graph_graphml" title="3.2. igraph_write_graph_graphml — Writes the graph to a file in GraphML format.">igraph_write_graph_graphml</a></strong></span>(&amp;graph, outfile, true);
<span class="strong"><strong>fclose</strong></span>(outfile);
<span class="emphasis"><em>/* Clean up after ourselves */</em></span>
<span class="strong"><strong>unlink</strong></span>(outfilename);
} <span class="strong"><strong>else</strong></span> {
<span class="strong"><strong>fprintf</strong></span>(stderr, "Could not write output file '%s'.", outfilename);
}
<span class="emphasis"><em>/* Destroy the graph */</em></span>
<span class="strong"><strong><a class="link" href="igraph-Basic.html#igraph_destroy" title="5.1.4. igraph_destroy — Frees the memory allocated for a graph object.">igraph_destroy</a></strong></span>(&amp;graph);
<span class="strong"><strong>return</strong></span> 0;
}
</pre>
<p></p>
</div>
</div>
<br class="example-break">
</div>
<p>
</p>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="gml-format"></a>4. GML format</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="igraph-Foreign.html#igraph_read_graph_gml">4.1. <code class="function">igraph_read_graph_gml</code> — Read a graph in GML format.</a></span></dt>
<dt><span class="section"><a href="igraph-Foreign.html#igraph_write_graph_gml">4.2. <code class="function">igraph_write_graph_gml</code> — Write the graph to a stream in GML format.</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="igraph_read_graph_gml"></a>4.1. <code class="function">igraph_read_graph_gml</code> — Read a graph in GML format.</h3></div></div></div>
<a class="indexterm" name="id-1.32.8.2.2"></a><p>
</p>
<div class="informalexample"><pre class="programlisting">
igraph_error_t igraph_read_graph_gml(igraph_t *graph, FILE *instream);
</pre></div>
<p>
</p>
<p>
GML is a simple textual format, see
<a class="ulink" href="https://web.archive.org/web/20190207140002/http://www.fim.uni-passau.de/index.php?id=17297%26L=1" target="_top">https://web.archive.org/web/20190207140002/http://www.fim.uni-passau.de/index.php?id=17297%26L=1</a>
for details.
</p>
<p>
Although all syntactically correct GML can be parsed,
we implement only a subset of this format. Some attributes might be
ignored. Here is a list of all the differences:
</p>
<div class="orderedlist"><ol class="orderedlist" type="1">
<li class="listitem"><p>
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, <code class="literal">""</code> for string).
</p></li>
<li class="listitem"><p>
<code class="literal">comment</code> fields are not ignored. They are treated as any
other field and converted to attributes.
</p></li>
<li class="listitem"><p>
Top level attributes except for <code class="literal">Version</code> and the
first <code class="literal">graph</code> attribute are completely ignored.
</p></li>
<li class="listitem"><p>
There is no maximum line length or maximum keyword length.
</p></li>
<li class="listitem"><p>
Only the <code class="constant">quot</code>, <code class="constant">amp</code>, <code class="constant">apos</code>, <code class="constant">lt</code> and <code class="constant">gt</code> 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.
</p></li>
<li class="listitem"><p>
We allow <code class="literal">inf</code>, <code class="literal">-inf</code> and <code class="literal">nan</code>
(not a number) as a real number. This is case insensitive, so
<code class="literal">nan</code>, <code class="literal">NaN</code> and <code class="literal">NAN</code> are equivalent.
</p></li>
</ol></div>
<p>
</p>
<p> Please contact us if you cannot live with these
limitations of the GML parser.
</p>
<p><b>Arguments: </b>
</p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody>
<tr>
<td><p><span class="term"><em class="parameter"><code>graph</code></em>:</span></p></td>
<td><p>
Pointer to an uninitialized graph object.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>instream</code></em>:</span></p></td>
<td><p>
The stream to read the GML file from.
</p></td>
</tr>
</tbody>
</table></div>
<p>
</p>
<p><b>Returns: </b></p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody><tr>
<td><p><span class="term"><em class="parameter"><code></code></em></span></p></td>
<td><p>
Error code.
</p></td>
</tr></tbody>
</table></div>
<p>
Time complexity: should be proportional to the length of the file.
</p>
<p><b>See also: </b></p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody><tr>
<td><p><span class="term"><em class="parameter"><code></code></em></span></p></td>
<td><p>
<a class="link" href="igraph-Foreign.html#igraph_read_graph_graphml" title="3.1. igraph_read_graph_graphml — Reads a graph from a GraphML file."><code class="function">igraph_read_graph_graphml()</code></a> for a more modern format,
<a class="link" href="igraph-Foreign.html#igraph_write_graph_gml" title="4.2. igraph_write_graph_gml — Write the graph to a stream in GML format."><code class="function">igraph_write_graph_gml()</code></a> for writing GML files.
</p></td>
</tr></tbody>
</table></div>
<p>
</p>
<div class="hideshow" onClick="toggle(this, event)">
<div class="example">
<a name="id-1.32.8.2.12.1"></a><p class="title"><b>Example 31.6.  File <code class="code">examples/simple/gml.c</code></b></p>
<div class="example-contents">
<pre class="programlisting"><span class="strong"><strong>#include</strong></span> &lt;igraph.h&gt;
<span class="strong"><strong>#include</strong></span> &lt;stdio.h&gt;
int <span class="strong"><strong>main</strong></span>(void) {
igraph_t graph;
FILE *ifile;
<span class="emphasis"><em>/* Initialize the library. */</em></span>
<span class="strong"><strong><a class="link" href="igraph-Basic.html#igraph_setup" title="4.1. igraph_setup — Initializes the igraph library.">igraph_setup</a></strong></span>();
ifile = <span class="strong"><strong>fopen</strong></span>("karate.gml", "r");
<span class="strong"><strong>if</strong></span> (ifile == 0) {
<span class="strong"><strong>return</strong></span> 1;
}
<span class="strong"><strong><a class="link" href="igraph-Foreign.html#igraph_read_graph_gml" title="4.1. igraph_read_graph_gml — Read a graph in GML format.">igraph_read_graph_gml</a></strong></span>(&amp;graph, ifile);
<span class="strong"><strong>fclose</strong></span>(ifile);
<span class="strong"><strong>printf</strong></span>("The graph is %s.\n", <span class="strong"><strong><a class="link" href="igraph-Basic.html#igraph_is_directed" title="5.2.3. igraph_is_directed — Is this a directed graph?">igraph_is_directed</a></strong></span>(&amp;graph) ? "directed" : "undirected");
<span class="emphasis"><em>/* Output as edge list */</em></span>
<span class="strong"><strong>printf</strong></span>("\n-----------------\n");
<span class="strong"><strong><a class="link" href="igraph-Foreign.html#igraph_write_graph_edgelist" title="1.2. igraph_write_graph_edgelist — Writes the edge list of a graph to a file.">igraph_write_graph_edgelist</a></strong></span>(&amp;graph, stdout);
<span class="emphasis"><em>/* Output as GML */</em></span>
<span class="strong"><strong>printf</strong></span>("\n-----------------\n");
<span class="strong"><strong><a class="link" href="igraph-Foreign.html#igraph_write_graph_gml" title="4.2. igraph_write_graph_gml — Write the graph to a stream in GML format.">igraph_write_graph_gml</a></strong></span>(&amp;graph, stdout,IGRAPH_WRITE_GML_DEFAULT_SW, 0, "");
<span class="strong"><strong><a class="link" href="igraph-Basic.html#igraph_destroy" title="5.1.4. igraph_destroy — Frees the memory allocated for a graph object.">igraph_destroy</a></strong></span>(&amp;graph);
<span class="strong"><strong>return</strong></span> 0;
}
</pre>
<p></p>
</div>
</div>
<br class="example-break">
</div>
<p>
</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="igraph_write_graph_gml"></a>4.2. <code class="function">igraph_write_graph_gml</code> — Write the graph to a stream in GML format.</h3></div></div></div>
<a class="indexterm" name="id-1.32.8.3.2"></a><p>
</p>
<div class="informalexample"><pre class="programlisting">
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);
</pre></div>
<p>
</p>
<p>
GML is a quite general textual format, see
<a class="ulink" href="https://web.archive.org/web/20190207140002/http://www.fim.uni-passau.de/index.php?id=17297%26L=1" target="_top">https://web.archive.org/web/20190207140002/http://www.fim.uni-passau.de/index.php?id=17297%26L=1</a>
for details.
</p>
<p>
The graph, vertex and edges attributes are written to the
file as well, if they are numeric or string. Boolean attributes are converted
to 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 GML
specification and other software may not be able to read files containing them.
This is consistent with <a class="link" href="igraph-Foreign.html#igraph_read_graph_gml" title="4.1. igraph_read_graph_gml — Read a graph in GML format."><code class="function">igraph_read_graph_gml()</code></a>, which produces NaN
when an attribute value is missing. In contrast with NaN, infinite values
are retained. Ensure that none of the numeric attributes values are infinite
to produce a conformant GML file that can be read by other software.
</p>
<p>
As igraph is more forgiving about attribute names, it might
be necessary to simplify the them before writing to the GML file.
This way we'll have a syntactically correct GML file. The following
simple procedure is performed on each attribute name: first the alphanumeric
characters are extracted, the others are ignored. Then if the first character
is not a letter then the attribute name is prefixed with <span class="quote"><span class="quote">igraph</span></span>.
Note that this might result identical names for two attributes, igraph
does not check this.
</p>
<p>
The <span class="quote"><span class="quote">id</span></span> vertex attribute is treated specially.
If the <em class="parameter"><code>id</code></em> argument is not <code class="constant">NULL</code> then it should be a numeric
vector with the vertex IDs and the <span class="quote"><span class="quote">id</span></span> vertex attribute is
ignored (if there is one). If <em class="parameter"><code>id</code></em> is <code class="constant">NULL</code> and there is a
numeric <span class="quote"><span class="quote">id</span></span> vertex attribute, it will be used instead. If ids
are 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 supplied
id are ignored and igraph vertex IDs are used instead.
</p>
<p>
Note that whichever way vertex IDs are specified, their uniqueness is not checked.
</p>
<p>
If the graph has edge attributes that become <span class="quote"><span class="quote">source</span></span>
or <span class="quote"><span class="quote">target</span></span> after encoding, or the graph has an attribute that becomes
<span class="quote"><span class="quote">directed</span></span>, they will be ignored with a warning. GML uses these attributes
to specify the edge endpoints, and the graph directedness, so we cannot write them
to the file. Rename them before calling this function if you want to preserve them.
</p>
<p><b>Arguments: </b>
</p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody>
<tr>
<td><p><span class="term"><em class="parameter"><code>graph</code></em>:</span></p></td>
<td><p>
The graph to write to the stream.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>outstream</code></em>:</span></p></td>
<td><p>
The stream to write the file to.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>options</code></em>:</span></p></td>
<td>
<p>
Set of <code class="literal">|</code>-combinable boolean flags for writing the GML file.
</p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody>
<tr>
<td><p><span class="term"><code class="constant">0</code></span></p></td>
<td><p>
All options turned off.
</p></td>
</tr>
<tr>
<td><p><span class="term"><code class="constant">IGRAPH_WRITE_GML_DEFAULT_SW</code></span></p></td>
<td><p>
Default options, currently equivalent to 0. May change in future versions.
</p></td>
</tr>
<tr>
<td><p><span class="term"><code class="constant">IGRAPH_WRITE_GML_ENCODE_ONLY_QUOT_SW</code></span></p></td>
<td><p>
Do not encode any other characters than " as entities. Specifically, this
option prevents the encoding of &amp;. Useful when re-exporting a graph
that was read from a GML file in which igraph could not interpret all entities,
and thus passed them through without decoding.
</p></td>
</tr>
</tbody>
</table></div>
<p>
</p>
</td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>id</code></em>:</span></p></td>
<td><p>
Either <code class="literal">NULL</code> or a numeric vector with the vertex IDs.
See details above.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>creator</code></em>:</span></p></td>
<td><p>
An optional string to write to the stream in the creator line.
If <code class="constant">NULL</code>, the igraph version with the current date and time is added.
If <code class="literal">""</code>, the creator line is omitted. Otherwise, the
supplied string is used verbatim.
</p></td>
</tr>
</tbody>
</table></div>
<p>
</p>
<p><b>Returns: </b></p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody><tr>
<td><p><span class="term"><em class="parameter"><code></code></em></span></p></td>
<td><p>
Error code.
</p></td>
</tr></tbody>
</table></div>
<p>
Time complexity: should be proportional to the number of characters written
to the file.
</p>
<p><b>See also: </b></p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody><tr>
<td><p><span class="term"><em class="parameter"><code></code></em></span></p></td>
<td><p>
<a class="link" href="igraph-Foreign.html#igraph_read_graph_gml" title="4.1. igraph_read_graph_gml — Read a graph in GML format."><code class="function">igraph_read_graph_gml()</code></a> for reading GML files,
<a class="link" href="igraph-Foreign.html#igraph_read_graph_graphml" title="3.1. igraph_read_graph_graphml — Reads a graph from a GraphML file."><code class="function">igraph_read_graph_graphml()</code></a> for a more modern format.
</p></td>
</tr></tbody>
</table></div>
<p>
</p>
<div class="hideshow" onClick="toggle(this, event)">
<div class="example">
<a name="id-1.32.8.3.15.1"></a><p class="title"><b>Example 31.7.  File <code class="code">examples/simple/gml.c</code></b></p>
<div class="example-contents">
<pre class="programlisting"><span class="strong"><strong>#include</strong></span> &lt;igraph.h&gt;
<span class="strong"><strong>#include</strong></span> &lt;stdio.h&gt;
int <span class="strong"><strong>main</strong></span>(void) {
igraph_t graph;
FILE *ifile;
<span class="emphasis"><em>/* Initialize the library. */</em></span>
<span class="strong"><strong><a class="link" href="igraph-Basic.html#igraph_setup" title="4.1. igraph_setup — Initializes the igraph library.">igraph_setup</a></strong></span>();
ifile = <span class="strong"><strong>fopen</strong></span>("karate.gml", "r");
<span class="strong"><strong>if</strong></span> (ifile == 0) {
<span class="strong"><strong>return</strong></span> 1;
}
<span class="strong"><strong><a class="link" href="igraph-Foreign.html#igraph_read_graph_gml" title="4.1. igraph_read_graph_gml — Read a graph in GML format.">igraph_read_graph_gml</a></strong></span>(&amp;graph, ifile);
<span class="strong"><strong>fclose</strong></span>(ifile);
<span class="strong"><strong>printf</strong></span>("The graph is %s.\n", <span class="strong"><strong><a class="link" href="igraph-Basic.html#igraph_is_directed" title="5.2.3. igraph_is_directed — Is this a directed graph?">igraph_is_directed</a></strong></span>(&amp;graph) ? "directed" : "undirected");
<span class="emphasis"><em>/* Output as edge list */</em></span>
<span class="strong"><strong>printf</strong></span>("\n-----------------\n");
<span class="strong"><strong><a class="link" href="igraph-Foreign.html#igraph_write_graph_edgelist" title="1.2. igraph_write_graph_edgelist — Writes the edge list of a graph to a file.">igraph_write_graph_edgelist</a></strong></span>(&amp;graph, stdout);
<span class="emphasis"><em>/* Output as GML */</em></span>
<span class="strong"><strong>printf</strong></span>("\n-----------------\n");
<span class="strong"><strong><a class="link" href="igraph-Foreign.html#igraph_write_graph_gml" title="4.2. igraph_write_graph_gml — Write the graph to a stream in GML format.">igraph_write_graph_gml</a></strong></span>(&amp;graph, stdout,IGRAPH_WRITE_GML_DEFAULT_SW, 0, "");
<span class="strong"><strong><a class="link" href="igraph-Basic.html#igraph_destroy" title="5.1.4. igraph_destroy — Frees the memory allocated for a graph object.">igraph_destroy</a></strong></span>(&amp;graph);
<span class="strong"><strong>return</strong></span> 0;
}
</pre>
<p></p>
</div>
</div>
<br class="example-break">
</div>
<p>
</p>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="pajek-format"></a>5. Pajek format</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="igraph-Foreign.html#igraph_read_graph_pajek">5.1. <code class="function">igraph_read_graph_pajek</code> — Reads a file in Pajek format.</a></span></dt>
<dt><span class="section"><a href="igraph-Foreign.html#igraph_write_graph_pajek">5.2. <code class="function">igraph_write_graph_pajek</code> — Writes a graph to a file in Pajek format.</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="igraph_read_graph_pajek"></a>5.1. <code class="function">igraph_read_graph_pajek</code> — Reads a file in Pajek format.</h3></div></div></div>
<a class="indexterm" name="id-1.32.9.2.2"></a><p>
</p>
<div class="informalexample"><pre class="programlisting">
igraph_error_t igraph_read_graph_pajek(igraph_t *graph, FILE *instream);
</pre></div>
<p>
</p>
<p>
Only a subset of the Pajek format is implemented. This is partially
because there is no formal specification for this format, but also because
<span class="command"><strong>igraph</strong></span> does not support some Pajek features, like
mixed graphs.
</p>
<p>
Starting from version 0.6.1 igraph reads bipartite (two-mode)
graphs from Pajek files and adds the <code class="constant">type</code> Boolean vertex attribute for
them. Warnings are given for invalid edges, i.e. edges connecting
vertices of the same type.
</p>
<p>
The list of the current limitations:
</p>
<div class="orderedlist"><ol class="orderedlist" type="1">
<li class="listitem"><p>
Only <code class="filename">.net</code> files are supported, Pajek
project files (<code class="filename">.paj</code>) are not.
</p></li>
<li class="listitem"><p>
Temporal networks (i.e. with time events) are not supported.
</p></li>
<li class="listitem"><p>
Graphs with both directed and non-directed edges are not
supported, as they cannot be represented in <span class="command"><strong>igraph</strong></span>.
</p></li>
<li class="listitem"><p>
Only Pajek networks are supported; permutations, hierarchies,
clusters and vectors are not.
</p></li>
<li class="listitem"><p>
Multi-relational networks (i.e. networks with multiple edge
types) are not supported.
</p></li>
<li class="listitem"><p>
Unicode characters encoded as <code class="literal">&amp;#dddd;</code>, or newlines
encoded as <code class="literal">\n</code> will not be decoded.
</p></li>
</ol></div>
<p>
</p>
<p>
If an attribute handler is installed,
<span class="command"><strong>igraph</strong></span> also reads the vertex and edge attributes
from the file. Most attributes are renamed to be more informative:
<code class="constant">color</code> instead of <code class="constant">c</code>, <code class="constant">xfact</code> instead of <code class="constant">x_fact</code>,
<code class="constant">yfact</code> instead of y_fact, <code class="constant">labeldist</code> instead of <code class="constant">lr</code>,
<code class="constant">labeldegree2</code> instead of <code class="constant">lphi</code>, <code class="constant">framewidth</code> instead of <code class="constant">bw</code>,
<code class="constant">fontsize</code> instead of <code class="constant">fos</code>, <code class="constant">rotation</code> instead of <code class="constant">phi</code>,
<code class="constant">radius</code> instead of <code class="constant">r</code>, <code class="constant">diamondratio</code> instead of <code class="constant">q</code>,
<code class="constant">labeldegree</code> instead of <code class="constant">la</code>,
<code class="constant">color</code> instead of <code class="constant">ic</code>, <code class="constant">framecolor</code> instead of <code class="constant">bc</code>,
<code class="constant">labelcolor</code> instead of <code class="constant">lc</code>; these belong to vertices.
</p>
<p>
Edge attributes are also renamed, <code class="constant">s</code> to <code class="constant">arrowsize</code>,
<code class="constant">w</code> to <code class="constant">edgewidth</code>, <code class="constant">h1</code> to <code class="constant">hook1</code>, <code class="constant">h2</code> to <code class="constant">hook2</code>,
<code class="constant">a1</code> to <code class="constant">angle1</code>, <code class="constant">a2</code> to <code class="constant">angle2</code>, <code class="constant">k1</code> to
<code class="constant">velocity1</code>, <code class="constant">k2</code> to <code class="constant">velocity2</code>, <code class="constant">ap</code> to <code class="constant">arrowpos</code>,
<code class="constant">lp</code> to <code class="constant">labelpos</code>, <code class="constant">lr</code> to <code class="constant">labelangle</code>,
<code class="constant">lphi</code> to <code class="constant">labelangle2</code>, <code class="constant">la</code> to <code class="constant">labeldegree</code>,
<code class="constant">fos</code> to <code class="constant">fontsize</code>, <code class="constant">a</code> to <code class="constant">arrowtype</code>, <code class="constant">p</code> to <code class="constant">linepattern</code>,
<code class="constant">l</code> to <code class="constant">label</code>, <code class="constant">lc</code> to <code class="constant">labelcolor</code>, <code class="constant">c</code> to <code class="constant">color</code>.
</p>
<p>
Unknown vertex or edge parameters are read as string vertex
or edge attributes. If the parameter name conflicts with one
the standard attribute names mentioned above, a <code class="literal">_</code>
character is appended to it to avoid conflict.
</p>
<p>
In addition the following vertex attributes might be added: <code class="constant">name</code> is added
(with the same value) if there are vertex IDs in the file.
<code class="constant">x</code> and <code class="constant">y</code>, and potentially <code class="constant">z</code> are also added if there are vertex
coordinates in the file.
</p>
<p>
The <code class="constant">weight</code> edge attribute will be added if there are edge weights present.
</p>
<p>
See the Pajek homepage:
<a class="ulink" href="http://vlado.fmf.uni-lj.si/pub/networks/pajek/" target="_top">http://vlado.fmf.uni-lj.si/pub/networks/pajek/</a> for more info on
Pajek. The Pajek manual,
<a class="ulink" href="http://vlado.fmf.uni-lj.si/pub/networks/pajek/doc/pajekman.pdf" target="_top">http://vlado.fmf.uni-lj.si/pub/networks/pajek/doc/pajekman.pdf</a>,
and <a class="ulink" href="http://mrvar.fdv.uni-lj.si/pajek/DrawEPS.htm" target="_top">http://mrvar.fdv.uni-lj.si/pajek/DrawEPS.htm</a>
have information on the Pajek file format. There is additional
useful information and sample files at
<a class="ulink" href="http://mrvar.fdv.uni-lj.si/pajek/history.htm" target="_top">http://mrvar.fdv.uni-lj.si/pajek/history.htm</a>
</p>
<p><b>Arguments: </b>
</p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody>
<tr>
<td><p><span class="term"><em class="parameter"><code>graph</code></em>:</span></p></td>
<td><p>
Pointer to an uninitialized graph object.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>instream</code></em>:</span></p></td>
<td><p>
An already opened file handler.
</p></td>
</tr>
</tbody>
</table></div>
<p>
</p>
<p><b>Returns: </b></p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody><tr>
<td><p><span class="term"><em class="parameter"><code></code></em></span></p></td>
<td><p>
Error code.
</p></td>
</tr></tbody>
</table></div>
<p>
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.
</p>
<p><b>See also: </b></p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody><tr>
<td><p><span class="term"><em class="parameter"><code></code></em></span></p></td>
<td><p>
<a class="link" href="igraph-Foreign.html#igraph_write_graph_pajek" title="5.2. igraph_write_graph_pajek — Writes a graph to a file in Pajek format."><code class="function">igraph_write_graph_pajek()</code></a> for writing Pajek files, <a class="link" href="igraph-Foreign.html#igraph_read_graph_graphml" title="3.1. igraph_read_graph_graphml — Reads a graph from a GraphML file."><code class="function">igraph_read_graph_graphml()</code></a> for reading GraphML files.
</p></td>
</tr></tbody>
</table></div>
<p>
</p>
<div class="hideshow" onClick="toggle(this, event)">
<div class="example">
<a name="id-1.32.9.2.18.1"></a><p class="title"><b>Example 31.8.  File <code class="code">examples/simple/foreign.c</code></b></p>
<div class="example-contents">
<pre class="programlisting"><span class="strong"><strong>#include</strong></span> &lt;igraph.h&gt;
<span class="strong"><strong>#include</strong></span> &lt;stdio.h&gt;
int <span class="strong"><strong>main</strong></span>(void) {
igraph_t g;
FILE *ifile;
<span class="emphasis"><em>/* Initialize the library. */</em></span>
<span class="strong"><strong><a class="link" href="igraph-Basic.html#igraph_setup" title="4.1. igraph_setup — Initializes the igraph library.">igraph_setup</a></strong></span>();
<span class="emphasis"><em>/* Turn on attribute handling. */</em></span>
<span class="strong"><strong><a class="link" href="igraph-Attributes.html#igraph_set_attribute_table" title="1.2. igraph_set_attribute_table — Attach an attribute table.">igraph_set_attribute_table</a></strong></span>(&amp;igraph_cattribute_table);
<span class="emphasis"><em>/* Read a Pajek file. */</em></span>
ifile = <span class="strong"><strong>fopen</strong></span>("links.net", "r");
<span class="strong"><strong>if</strong></span> (ifile == 0) {
<span class="strong"><strong>return</strong></span> 10;
}
<span class="strong"><strong><a class="link" href="igraph-Foreign.html#igraph_read_graph_pajek" title="5.1. igraph_read_graph_pajek — Reads a file in Pajek format.">igraph_read_graph_pajek</a></strong></span>(&amp;g, ifile);
<span class="strong"><strong>fclose</strong></span>(ifile);
<span class="emphasis"><em>/* Write it in edgelist format. */</em></span>
<span class="strong"><strong>printf</strong></span>("The graph:\n");
<span class="strong"><strong>printf</strong></span>("Vertices: %" IGRAPH_PRId "\n", <span class="strong"><strong><a class="link" href="igraph-Basic.html#igraph_vcount" title="5.2.1. igraph_vcount — The number of vertices in a graph.">igraph_vcount</a></strong></span>(&amp;g));
<span class="strong"><strong>printf</strong></span>("Edges: %" IGRAPH_PRId "\n", <span class="strong"><strong><a class="link" href="igraph-Basic.html#igraph_ecount" title="5.2.2. igraph_ecount — The number of edges in a graph.">igraph_ecount</a></strong></span>(&amp;g));
<span class="strong"><strong>printf</strong></span>("Directed: %i\n", <span class="strong"><strong><a class="link" href="igraph-Basic.html#igraph_is_directed" title="5.2.3. igraph_is_directed — Is this a directed graph?">igraph_is_directed</a></strong></span>(&amp;g) ? 1 : 0);
<span class="strong"><strong><a class="link" href="igraph-Foreign.html#igraph_write_graph_edgelist" title="1.2. igraph_write_graph_edgelist — Writes the edge list of a graph to a file.">igraph_write_graph_edgelist</a></strong></span>(&amp;g, stdout);
<span class="strong"><strong><a class="link" href="igraph-Basic.html#igraph_destroy" title="5.1.4. igraph_destroy — Frees the memory allocated for a graph object.">igraph_destroy</a></strong></span>(&amp;g);
<span class="strong"><strong>return</strong></span> 0;
}
</pre>
<p></p>
</div>
</div>
<br class="example-break">
</div>
<p>
</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="igraph_write_graph_pajek"></a>5.2. <code class="function">igraph_write_graph_pajek</code> — Writes a graph to a file in Pajek format.</h3></div></div></div>
<a class="indexterm" name="id-1.32.9.3.2"></a><p>
</p>
<div class="informalexample"><pre class="programlisting">
igraph_error_t igraph_write_graph_pajek(const igraph_t *graph, FILE *outstream);
</pre></div>
<p>
</p>
<p>
Writes files in the native format of the Pajek software. This format
is not recommended for data exchange or archival. It is meant solely
for interoperability with Pajek.
</p>
<p>
The Pajek vertex and edge parameters (like color) are determined by
the attributes of the vertices and edges. Of course this requires
an attribute handler to be installed. The names of the
corresponding vertex and edge attributes are listed at <a class="link" href="igraph-Foreign.html#igraph_read_graph_pajek" title="5.1. igraph_read_graph_pajek — Reads a file in Pajek format."><code class="function">igraph_read_graph_pajek()</code></a>, e.g. the <code class="constant">color</code> vertex attributes
determines the color (<code class="constant">c</code> in Pajek) parameter.
</p>
<p>
Vertex and edge attributes that do not correspond to any documented
Pajek parameter are discarded.
</p>
<p>
As of version 0.6.1 igraph writes bipartite graphs into Pajek files
correctly, i.e. they will be also bipartite when read into Pajek.
As Pajek is less flexible for bipartite graphs (the numeric IDs of
the vertices must be sorted according to vertex type), igraph might
need to reorder the vertices when writing a bipartite Pajek file.
This effectively means that numeric vertex IDs usually change when
a bipartite graph is written to a Pajek file, and then read back
into igraph.
</p>
<p>
Early versions of Pajek supported only Windows-style line endings
in Pajek files, but recent versions support both Windows and Unix
line endings. igraph therefore uses the platform-native line endings
when the input file is opened in text mode, and uses Unix-style
line endings when the input file is opened in binary mode. If you
are using an old version of Pajek, you are on Unix and you are having
problems reading files written by igraph on a Windows machine, convert the
line endings manually with a text editor or with <code class="constant">unix2dos</code> or <code class="constant">iconv</code>
from the command line).
</p>
<p>
Pajek will only interpret UTF-8 encoded files if they contain a byte-order
mark (BOM) at the beginning. igraph is agnostic of string attribute encodings
and therefore it will never write a BOM. You need to add this manually
if/when necessary.
</p>
<p><b>Arguments: </b>
</p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody>
<tr>
<td><p><span class="term"><em class="parameter"><code>graph</code></em>:</span></p></td>
<td><p>
The graph object to write.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>outstream</code></em>:</span></p></td>
<td><p>
The file to write to. It should be opened and writable.
</p></td>
</tr>
</tbody>
</table></div>
<p>
</p>
<p><b>Returns: </b></p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody><tr>
<td><p><span class="term"><em class="parameter"><code></code></em></span></p></td>
<td><p>
Error code.
</p></td>
</tr></tbody>
</table></div>
<p>
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.
</p>
<p><b>See also: </b></p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody><tr>
<td><p><span class="term"><em class="parameter"><code></code></em></span></p></td>
<td><p>
<a class="link" href="igraph-Foreign.html#igraph_read_graph_pajek" title="5.1. igraph_read_graph_pajek — Reads a file in Pajek format."><code class="function">igraph_read_graph_pajek()</code></a> for reading Pajek graphs, <a class="link" href="igraph-Foreign.html#igraph_write_graph_graphml" title="3.2. igraph_write_graph_graphml — Writes the graph to a file in GraphML format."><code class="function">igraph_write_graph_graphml()</code></a> for writing a graph in GraphML format,
this suites <span class="command"><strong>igraph</strong></span> graphs better.
</p></td>
</tr></tbody>
</table></div>
<p>
</p>
<div class="hideshow" onClick="toggle(this, event)">
<div class="example">
<a name="id-1.32.9.3.15.1"></a><p class="title"><b>Example 31.9.  File <code class="code">examples/simple/igraph_write_graph_pajek.c</code></b></p>
<div class="example-contents">
<pre class="programlisting"><span class="strong"><strong>#include</strong></span> &lt;igraph.h&gt;
int <span class="strong"><strong>main</strong></span>(void) {
igraph_t g;
igraph_strvector_t names;
<span class="emphasis"><em>/* Initialize the library. */</em></span>
<span class="strong"><strong><a class="link" href="igraph-Basic.html#igraph_setup" title="4.1. igraph_setup — Initializes the igraph library.">igraph_setup</a></strong></span>();
<span class="strong"><strong><a class="link" href="igraph-Attributes.html#igraph_set_attribute_table" title="1.2. igraph_set_attribute_table — Attach an attribute table.">igraph_set_attribute_table</a></strong></span>(&amp;igraph_cattribute_table);
<span class="emphasis"><em>/* save a simple ring graph */</em></span>
<span class="strong"><strong><a class="link" href="igraph-Generators.html#igraph_ring" title="4.7. igraph_ring — Creates a cycle graph or a path graph.">igraph_ring</a></strong></span>(&amp;g, 10, IGRAPH_DIRECTED, false <span class="emphasis"><em>/* mutual */</em></span>, true <span class="emphasis"><em>/* circular */</em></span>);
<span class="strong"><strong><a class="link" href="igraph-Foreign.html#igraph_write_graph_pajek" title="5.2. igraph_write_graph_pajek — Writes a graph to a file in Pajek format.">igraph_write_graph_pajek</a></strong></span>(&amp;g, stdout);
<span class="emphasis"><em>/* add some vertex attributes */</em></span>
<span class="strong"><strong><a class="link" href="igraph-Data-structures.html#igraph_strvector_init" title="8.1. igraph_strvector_init — Initializes a string vector.">igraph_strvector_init</a></strong></span>(&amp;names, 0);
<span class="strong"><strong><a class="link" href="igraph-Data-structures.html#igraph_strvector_push_back" title="8.8. igraph_strvector_push_back — Adds an element to the back of a string vector.">igraph_strvector_push_back</a></strong></span>(&amp;names, "A");
<span class="strong"><strong><a class="link" href="igraph-Data-structures.html#igraph_strvector_push_back" title="8.8. igraph_strvector_push_back — Adds an element to the back of a string vector.">igraph_strvector_push_back</a></strong></span>(&amp;names, "B");
<span class="strong"><strong><a class="link" href="igraph-Data-structures.html#igraph_strvector_push_back" title="8.8. igraph_strvector_push_back — Adds an element to the back of a string vector.">igraph_strvector_push_back</a></strong></span>(&amp;names, "C");
<span class="strong"><strong><a class="link" href="igraph-Data-structures.html#igraph_strvector_push_back" title="8.8. igraph_strvector_push_back — Adds an element to the back of a string vector.">igraph_strvector_push_back</a></strong></span>(&amp;names, "D");
<span class="strong"><strong><a class="link" href="igraph-Data-structures.html#igraph_strvector_push_back" title="8.8. igraph_strvector_push_back — Adds an element to the back of a string vector.">igraph_strvector_push_back</a></strong></span>(&amp;names, "E");
<span class="strong"><strong><a class="link" href="igraph-Data-structures.html#igraph_strvector_push_back" title="8.8. igraph_strvector_push_back — Adds an element to the back of a string vector.">igraph_strvector_push_back</a></strong></span>(&amp;names, "F");
<span class="strong"><strong><a class="link" href="igraph-Data-structures.html#igraph_strvector_push_back" title="8.8. igraph_strvector_push_back — Adds an element to the back of a string vector.">igraph_strvector_push_back</a></strong></span>(&amp;names, "G");
<span class="strong"><strong><a class="link" href="igraph-Data-structures.html#igraph_strvector_push_back" title="8.8. igraph_strvector_push_back — Adds an element to the back of a string vector.">igraph_strvector_push_back</a></strong></span>(&amp;names, "H");
<span class="strong"><strong><a class="link" href="igraph-Data-structures.html#igraph_strvector_push_back" title="8.8. igraph_strvector_push_back — Adds an element to the back of a string vector.">igraph_strvector_push_back</a></strong></span>(&amp;names, "I");
<span class="strong"><strong><a class="link" href="igraph-Data-structures.html#igraph_strvector_push_back" title="8.8. igraph_strvector_push_back — Adds an element to the back of a string vector.">igraph_strvector_push_back</a></strong></span>(&amp;names, "J");
<span class="strong"><strong><a class="link" href="igraph-Attributes.html#SETVASV" title="4.2.24. SETVASV — Set a string vertex attribute for all vertices">SETVASV</a></strong></span>(&amp;g, "name", &amp;names);
<span class="strong"><strong><a class="link" href="igraph-Data-structures.html#igraph_strvector_destroy" title="8.3. igraph_strvector_destroy — Frees the memory allocated for the string vector.">igraph_strvector_destroy</a></strong></span>(&amp;names);
<span class="emphasis"><em>/* save the graph with vertex names */</em></span>
<span class="strong"><strong><a class="link" href="igraph-Foreign.html#igraph_write_graph_pajek" title="5.2. igraph_write_graph_pajek — Writes a graph to a file in Pajek format.">igraph_write_graph_pajek</a></strong></span>(&amp;g, stdout);
<span class="strong"><strong><a class="link" href="igraph-Data-structures.html#igraph_strvector_init" title="8.1. igraph_strvector_init — Initializes a string vector.">igraph_strvector_init</a></strong></span>(&amp;names, 0);
<span class="strong"><strong><a class="link" href="igraph-Data-structures.html#igraph_strvector_push_back" title="8.8. igraph_strvector_push_back — Adds an element to the back of a string vector.">igraph_strvector_push_back</a></strong></span>(&amp;names, "square");
<span class="strong"><strong><a class="link" href="igraph-Data-structures.html#igraph_strvector_push_back" title="8.8. igraph_strvector_push_back — Adds an element to the back of a string vector.">igraph_strvector_push_back</a></strong></span>(&amp;names, "square");
<span class="strong"><strong><a class="link" href="igraph-Data-structures.html#igraph_strvector_push_back" title="8.8. igraph_strvector_push_back — Adds an element to the back of a string vector.">igraph_strvector_push_back</a></strong></span>(&amp;names, "square");
<span class="strong"><strong><a class="link" href="igraph-Data-structures.html#igraph_strvector_push_back" title="8.8. igraph_strvector_push_back — Adds an element to the back of a string vector.">igraph_strvector_push_back</a></strong></span>(&amp;names, "square");
<span class="strong"><strong><a class="link" href="igraph-Data-structures.html#igraph_strvector_push_back" title="8.8. igraph_strvector_push_back — Adds an element to the back of a string vector.">igraph_strvector_push_back</a></strong></span>(&amp;names, "escaping spaces");
<span class="strong"><strong><a class="link" href="igraph-Data-structures.html#igraph_strvector_push_back" title="8.8. igraph_strvector_push_back — Adds an element to the back of a string vector.">igraph_strvector_push_back</a></strong></span>(&amp;names, "square");
<span class="strong"><strong><a class="link" href="igraph-Data-structures.html#igraph_strvector_push_back" title="8.8. igraph_strvector_push_back — Adds an element to the back of a string vector.">igraph_strvector_push_back</a></strong></span>(&amp;names, "square");
<span class="strong"><strong><a class="link" href="igraph-Data-structures.html#igraph_strvector_push_back" title="8.8. igraph_strvector_push_back — Adds an element to the back of a string vector.">igraph_strvector_push_back</a></strong></span>(&amp;names, "escaping\nnewline");
<span class="strong"><strong><a class="link" href="igraph-Data-structures.html#igraph_strvector_push_back" title="8.8. igraph_strvector_push_back — Adds an element to the back of a string vector.">igraph_strvector_push_back</a></strong></span>(&amp;names, "square");
<span class="strong"><strong><a class="link" href="igraph-Data-structures.html#igraph_strvector_push_back" title="8.8. igraph_strvector_push_back — Adds an element to the back of a string vector.">igraph_strvector_push_back</a></strong></span>(&amp;names, "encoding \"quotes\"");
<span class="strong"><strong><a class="link" href="igraph-Attributes.html#SETVASV" title="4.2.24. SETVASV — Set a string vertex attribute for all vertices">SETVASV</a></strong></span>(&amp;g, "shape", &amp;names);
<span class="strong"><strong><a class="link" href="igraph-Data-structures.html#igraph_strvector_destroy" title="8.3. igraph_strvector_destroy — Frees the memory allocated for the string vector.">igraph_strvector_destroy</a></strong></span>(&amp;names);
<span class="emphasis"><em>/* save the graph with escaped shapes */</em></span>
<span class="strong"><strong><a class="link" href="igraph-Foreign.html#igraph_write_graph_pajek" title="5.2. igraph_write_graph_pajek — Writes a graph to a file in Pajek format.">igraph_write_graph_pajek</a></strong></span>(&amp;g, stdout);
<span class="emphasis"><em>/* destroy the graph */</em></span>
<span class="strong"><strong><a class="link" href="igraph-Basic.html#igraph_destroy" title="5.1.4. igraph_destroy — Frees the memory allocated for a graph object.">igraph_destroy</a></strong></span>(&amp;g);
<span class="strong"><strong>return</strong></span> 0;
}
</pre>
<p></p>
</div>
</div>
<br class="example-break">
</div>
<p>
</p>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="ucinets-dl-file-format"></a>6. UCINET's DL file format</h2></div></div></div>
<div class="toc"><dl class="toc"><dt><span class="section"><a href="igraph-Foreign.html#igraph_read_graph_dl">6.1. <code class="function">igraph_read_graph_dl</code> — Reads a file in the DL format of UCINET.</a></span></dt></dl></div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="igraph_read_graph_dl"></a>6.1. <code class="function">igraph_read_graph_dl</code> — Reads a file in the DL format of UCINET.</h3></div></div></div>
<a class="indexterm" name="id-1.32.10.2.2"></a><p>
</p>
<div class="informalexample"><pre class="programlisting">
igraph_error_t igraph_read_graph_dl(igraph_t *graph, FILE *instream,
igraph_bool_t directed);
</pre></div>
<p>
</p>
<p>
This is a simple textual file format used by UCINET. See
<a class="ulink" href="http://www.analytictech.com/networks/dataentry.htm" target="_top">http://www.analytictech.com/networks/dataentry.htm</a> for
examples. All the forms described here are supported by
igraph. Vertex names and edge weights are also supported and they
are added as attributes. (If an attribute handler is attached.)
</p>
<p> Note the specification does not mention whether the
format is case sensitive or not. For igraph DL files are case
sensitive, i.e. <code class="constant">Larry</code> and <code class="constant">larry</code> are not the same.
</p>
<p><b>Arguments: </b>
</p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody>
<tr>
<td><p><span class="term"><em class="parameter"><code>graph</code></em>:</span></p></td>
<td><p>
Pointer to an uninitialized graph object.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>instream</code></em>:</span></p></td>
<td><p>
The stream to read the DL file from.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>directed</code></em>:</span></p></td>
<td><p>
Boolean, whether to create a directed file.
</p></td>
</tr>
</tbody>
</table></div>
<p>
</p>
<p><b>Returns: </b></p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody><tr>
<td><p><span class="term"><em class="parameter"><code></code></em></span></p></td>
<td><p>
Error code.
</p></td>
</tr></tbody>
</table></div>
<p>
Time complexity: linear in terms of the number of edges and
vertices, except for the matrix format, which is quadratic in the
number of vertices.
</p>
<div class="hideshow" onClick="toggle(this, event)">
<div class="example">
<a name="id-1.32.10.2.9.1"></a><p class="title"><b>Example 31.10.  File <code class="code">examples/simple/igraph_read_graph_dl.c</code></b></p>
<div class="example-contents">
<pre class="programlisting"><span class="strong"><strong>#include</strong></span> &lt;igraph.h&gt;
<span class="strong"><strong>#include</strong></span> &lt;stdio.h&gt;
<span class="strong"><strong>#include</strong></span> &lt;stdlib.h&gt;
int <span class="strong"><strong>main</strong></span>(void) {
<span class="strong"><strong>const</strong></span> 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;
<span class="emphasis"><em>/* Initialize the library. */</em></span>
<span class="strong"><strong><a class="link" href="igraph-Basic.html#igraph_setup" title="4.1. igraph_setup — Initializes the igraph library.">igraph_setup</a></strong></span>();
<span class="emphasis"><em>/* Turn on attribute handling. */</em></span>
<span class="strong"><strong><a class="link" href="igraph-Attributes.html#igraph_set_attribute_table" title="1.2. igraph_set_attribute_table — Attach an attribute table.">igraph_set_attribute_table</a></strong></span>(&amp;igraph_cattribute_table);
<span class="strong"><strong>for</strong></span> (size_t i = 0; i &lt; <span class="strong"><strong>sizeof</strong></span>(files) / <span class="strong"><strong>sizeof</strong></span>(files[0]); i++) {
<span class="strong"><strong>printf</strong></span>("Doing %s\n", files[i]);
infile = <span class="strong"><strong>fopen</strong></span>(files[i], "r");
<span class="strong"><strong>if</strong></span> (!infile) {
<span class="strong"><strong>printf</strong></span>("Cannot open file: %s\n", files[i]);
<span class="strong"><strong>abort</strong></span>();
}
<span class="strong"><strong><a class="link" href="igraph-Foreign.html#igraph_read_graph_dl" title="6.1. igraph_read_graph_dl — Reads a file in the DL format of UCINET.">igraph_read_graph_dl</a></strong></span>(&amp;graph, infile, IGRAPH_DIRECTED);
<span class="strong"><strong>fclose</strong></span>(infile);
<span class="strong"><strong><a class="link" href="igraph-Foreign.html#igraph_write_graph_edgelist" title="1.2. igraph_write_graph_edgelist — Writes the edge list of a graph to a file.">igraph_write_graph_edgelist</a></strong></span>(&amp;graph, stdout);
<span class="strong"><strong><a class="link" href="igraph-Basic.html#igraph_destroy" title="5.1.4. igraph_destroy — Frees the memory allocated for a graph object.">igraph_destroy</a></strong></span>(&amp;graph);
}
<span class="strong"><strong>if</strong></span> (<span class="strong"><strong>IGRAPH_FINALLY_STACK_SIZE</strong></span>() != 0) {
<span class="strong"><strong>return</strong></span> 1;
}
<span class="strong"><strong>return</strong></span> 0;
}
</pre>
<p></p>
</div>
</div>
<br class="example-break">
</div>
<p>
</p>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="graphviz-format"></a>7. Graphviz format</h2></div></div></div>
<div class="toc"><dl class="toc"><dt><span class="section"><a href="igraph-Foreign.html#igraph_write_graph_dot">7.1. <code class="function">igraph_write_graph_dot</code> — Write the graph to a stream in DOT format.</a></span></dt></dl></div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="igraph_write_graph_dot"></a>7.1. <code class="function">igraph_write_graph_dot</code> — Write the graph to a stream in DOT format.</h3></div></div></div>
<a class="indexterm" name="id-1.32.11.2.2"></a><p>
</p>
<div class="informalexample"><pre class="programlisting">
igraph_error_t igraph_write_graph_dot(const igraph_t *graph, FILE* outstream);
</pre></div>
<p>
</p>
<p>
</p>
<p>
DOT is the format used by the widely known GraphViz software, see
<a class="ulink" href="http://www.graphviz.org" target="_top">http://www.graphviz.org</a> for details. The grammar of the DOT format
can be found here: <a class="ulink" href="http://www.graphviz.org/doc/info/lang.html" target="_top">http://www.graphviz.org/doc/info/lang.html</a>
</p>
<p>
This is only a preliminary implementation, no visualization
information is written.
</p>
<p>
This format is meant solely for interoperability with Graphviz.
It is not recommended for data exchange or archival.
</p>
<p><b>Arguments: </b>
</p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody>
<tr>
<td><p><span class="term"><em class="parameter"><code>graph</code></em>:</span></p></td>
<td><p>
The graph to write to the stream.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>outstream</code></em>:</span></p></td>
<td><p>
The stream to write the file to.
</p></td>
</tr>
</tbody>
</table></div>
<p>
</p>
<p><b>Returns: </b></p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody><tr>
<td><p><span class="term"><em class="parameter"><code></code></em></span></p></td>
<td><p>
Error code.
</p></td>
</tr></tbody>
</table></div>
<p>
Time complexity: should be proportional to the number of characters written
to the file.
</p>
<p><b>See also: </b></p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody><tr>
<td><p><span class="term"><em class="parameter"><code></code></em></span></p></td>
<td><p>
<a class="link" href="igraph-Foreign.html#igraph_write_graph_graphml" title="3.2. igraph_write_graph_graphml — Writes the graph to a file in GraphML format."><code class="function">igraph_write_graph_graphml()</code></a> for a more modern format.
</p></td>
</tr></tbody>
</table></div>
<p>
</p>
<div class="hideshow" onClick="toggle(this, event)">
<div class="example">
<a name="id-1.32.11.2.13.1"></a><p class="title"><b>Example 31.11.  File <code class="code">examples/simple/dot.c</code></b></p>
<div class="example-contents">
<pre class="programlisting"><span class="strong"><strong>#include</strong></span> &lt;igraph.h&gt;
<span class="strong"><strong>#include</strong></span> &lt;stdio.h&gt;
int <span class="strong"><strong>main</strong></span>(void) {
igraph_t g;
FILE *ifile;
<span class="emphasis"><em>/* Initialize the library. */</em></span>
<span class="strong"><strong><a class="link" href="igraph-Basic.html#igraph_setup" title="4.1. igraph_setup — Initializes the igraph library.">igraph_setup</a></strong></span>();
ifile = <span class="strong"><strong>fopen</strong></span>("karate.gml", "r");
<span class="strong"><strong>if</strong></span> (ifile == 0) {
<span class="strong"><strong>return</strong></span> 10;
}
<span class="strong"><strong><a class="link" href="igraph-Foreign.html#igraph_read_graph_gml" title="4.1. igraph_read_graph_gml — Read a graph in GML format.">igraph_read_graph_gml</a></strong></span>(&amp;g, ifile);
<span class="strong"><strong>fclose</strong></span>(ifile);
<span class="strong"><strong>if</strong></span> (<span class="strong"><strong><a class="link" href="igraph-Basic.html#igraph_is_directed" title="5.2.3. igraph_is_directed — Is this a directed graph?">igraph_is_directed</a></strong></span>(&amp;g)) {
<span class="strong"><strong>printf</strong></span>("directed\n");
} <span class="strong"><strong>else</strong></span> {
<span class="strong"><strong>printf</strong></span>("undirected\n");
}
<span class="strong"><strong><a class="link" href="igraph-Foreign.html#igraph_write_graph_edgelist" title="1.2. igraph_write_graph_edgelist — Writes the edge list of a graph to a file.">igraph_write_graph_edgelist</a></strong></span>(&amp;g, stdout);
<span class="strong"><strong>printf</strong></span>("-----------------\n");
<span class="strong"><strong><a class="link" href="igraph-Foreign.html#igraph_write_graph_dot" title="7.1. igraph_write_graph_dot — Write the graph to a stream in DOT format.">igraph_write_graph_dot</a></strong></span>(&amp;g, stdout);
<span class="strong"><strong><a class="link" href="igraph-Basic.html#igraph_destroy" title="5.1.4. igraph_destroy — Frees the memory allocated for a graph object.">igraph_destroy</a></strong></span>(&amp;g);
<span class="strong"><strong>return</strong></span> 0;
}
</pre>
<p></p>
</div>
</div>
<br class="example-break">
</div>
<p>
</p>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="leda-format"></a>8. LEDA format</h2></div></div></div>
<div class="toc"><dl class="toc"><dt><span class="section"><a href="igraph-Foreign.html#igraph_write_graph_leda">8.1. <code class="function">igraph_write_graph_leda</code> — Write a graph in LEDA native graph format.</a></span></dt></dl></div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="igraph_write_graph_leda"></a>8.1. <code class="function">igraph_write_graph_leda</code> — Write a graph in LEDA native graph format.</h3></div></div></div>
<a class="indexterm" name="id-1.32.12.2.2"></a><p>
</p>
<div class="informalexample"><pre class="programlisting">
igraph_error_t igraph_write_graph_leda(const igraph_t *graph, FILE *outstream,
const char *vertex_attr_name,
const char *edge_attr_name);
</pre></div>
<p>
</p>
<p>
This function writes a graph to an output stream in LEDA format.
See <a class="ulink" href="http://www.algorithmic-solutions.info/leda_guide/graphs/leda_native_graph_fileformat.html" target="_top">http://www.algorithmic-solutions.info/leda_guide/graphs/leda_native_graph_fileformat.html</a>
</p>
<p>
The support for the LEDA format is very basic at the moment; igraph
writes only the LEDA graph section which supports one selected vertex
and edge attribute and no layout information or visual attributes.
</p>
<p><b>Arguments: </b>
</p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody>
<tr>
<td><p><span class="term"><em class="parameter"><code>graph</code></em>:</span></p></td>
<td><p>
The graph to write to the stream.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>outstream</code></em>:</span></p></td>
<td><p>
The stream.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>vertex_attr_name</code></em>:</span></p></td>
<td><p>
The name of the vertex attribute whose values
are to be stored in the output, or <code class="constant">NULL</code> if no
vertex attribute should be stored.
</p></td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>edge_attr_name</code></em>:</span></p></td>
<td><p>
The name of the edge attribute whose values
are to be stored in the output, or <code class="constant">NULL</code> if no
edge attribute should be stored.
</p></td>
</tr>
</tbody>
</table></div>
<p>
</p>
<p><b>Returns: </b></p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody><tr>
<td><p><span class="term"><em class="parameter"><code></code></em></span></p></td>
<td><p>
Error code.
</p></td>
</tr></tbody>
</table></div>
<p>
Time complexity: O(|V|+|E|), the number of vertices and edges in the
graph.
</p>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="locale-helpers"></a>9. Convenience functions for locale change</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="igraph-Foreign.html#igraph_enter_safelocale">9.1. <code class="function">igraph_enter_safelocale</code> — Temporarily set the C locale.</a></span></dt>
<dt><span class="section"><a href="igraph-Foreign.html#igraph_exit_safelocale">9.2. <code class="function">igraph_exit_safelocale</code> — Temporarily set the C locale.</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="igraph_enter_safelocale"></a>9.1. <code class="function">igraph_enter_safelocale</code> — Temporarily set the C locale.</h3></div></div></div>
<a class="indexterm" name="id-1.32.13.2.2"></a><p>
</p>
<div class="informalexample"><pre class="programlisting">
igraph_error_t igraph_enter_safelocale(igraph_safelocale_t *loc);
</pre></div>
<p>
</p>
<p>
igraph's foreign format readers and writers require a locale that uses a
decimal point instead of a decimal comma. This is a convenience function
that temporarily sets the C locale so that readers and writers would work
correctly. It <span class="emphasis"><em>must</em></span> be paired with a call to <a class="link" href="igraph-Foreign.html#igraph_exit_safelocale" title="9.2. igraph_exit_safelocale — Temporarily set the C locale."><code class="function">igraph_exit_safelocale()</code></a>,
otherwise a memory leak will occur.
</p>
<p>
This function tries to set the locale for the current thread only on a
best-effort basis. Restricting the locale change to a single thread is not
supported on all platforms. In these cases, this function falls back to using
the standard <code class="literal">setlocale()</code> function, which affects the entire process
and is not safe to use from concurrent threads.
</p>
<p>
It is generally recommended to run igraph within a thread that has been
permanently set to the C locale using system-specific means. This is a convenience
function for situations when this is not easily possible because the programmer
is 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 to
be done unless the locale has been changed away from the default.
</p>
<p><b>Arguments: </b>
</p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody><tr>
<td><p><span class="term"><em class="parameter"><code>loc</code></em>:</span></p></td>
<td><p>
Pointer to a variable of type <code class="constant">igraph_safelocale_t</code>. The current
locale will be stored here, so that it can be restored using
<a class="link" href="igraph-Foreign.html#igraph_exit_safelocale" title="9.2. igraph_exit_safelocale — Temporarily set the C locale."><code class="function">igraph_exit_safelocale()</code></a>.
</p></td>
</tr></tbody>
</table></div>
<p>
</p>
<p><b>Returns: </b></p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody><tr>
<td><p><span class="term"><em class="parameter"><code></code></em></span></p></td>
<td><p>
Error code.
</p></td>
</tr></tbody>
</table></div>
<p>
</p>
<div class="hideshow" onClick="toggle(this, event)">
<div class="example">
<a name="id-1.32.13.2.10.1"></a><p class="title"><b>Example 31.12.  File <code class="code">examples/simple/safelocale.c</code></b></p>
<div class="example-contents">
<pre class="programlisting"><span class="strong"><strong>#include</strong></span> &lt;igraph.h&gt;
<span class="strong"><strong>#include</strong></span> &lt;locale.h&gt;
<span class="strong"><strong>#include</strong></span> &lt;stdio.h&gt;
<span class="strong"><strong>#include</strong></span> &lt;string.h&gt;
int <span class="strong"><strong>main</strong></span>(void) {
<span class="strong"><strong>const</strong></span> char *filename = "weighted.gml";
igraph_t graph;
igraph_safelocale_t loc;
<span class="emphasis"><em>/* Initialize the library. */</em></span>
<span class="strong"><strong><a class="link" href="igraph-Basic.html#igraph_setup" title="4.1. igraph_setup — Initializes the igraph library.">igraph_setup</a></strong></span>();
<span class="emphasis"><em>/* Attempt to set a locale that uses a decimal comma. Locale names</em></span>
<span class="emphasis"><em> * differ between platforms, and not all locales are available,</em></span>
<span class="emphasis"><em> * so the locale change may not be successful. */</em></span>
<span class="strong"><strong>const</strong></span> char *locname = <span class="strong"><strong>setlocale</strong></span>(LC_ALL, "de_DE");
<span class="strong"><strong>struct</strong></span> lconv *lc = <span class="strong"><strong>localeconv</strong></span>();
<span class="strong"><strong>if</strong></span> (<span class="strong"><strong>strcmp</strong></span>(lc-&gt;decimal_point, ",")) {
<span class="emphasis"><em>/* If decimal point is not a comma, presumably because the requested</em></span>
<span class="emphasis"><em> * locale was not available, report locale information. */</em></span>
<span class="strong"><strong>fprintf</strong></span>(stderr, "setlocale() returned '%s', decimal point is '%s'\n",
locname ? locname : "NULL",
lc-&gt;decimal_point);
}
FILE *file = <span class="strong"><strong>fopen</strong></span>(filename, "r");
<span class="strong"><strong>if</strong></span> (! file) {
<span class="strong"><strong>fprintf</strong></span>(stderr, "Cannot open %s file.\n", filename);
<span class="strong"><strong>exit</strong></span>(1);
}
<span class="emphasis"><em>/* An attribute table is needed to read graph attributes. */</em></span>
<span class="strong"><strong><a class="link" href="igraph-Attributes.html#igraph_set_attribute_table" title="1.2. igraph_set_attribute_table — Attach an attribute table.">igraph_set_attribute_table</a></strong></span>(&amp;igraph_cattribute_table);
<span class="emphasis"><em>/* At this point, the current locale may use decimal commas.</em></span>
<span class="emphasis"><em> * We temporarily set a C locale using enter_safelocale() to</em></span>
<span class="emphasis"><em> * allow the GML reader and writer to work correctly.*/</em></span>
<span class="strong"><strong><a class="link" href="igraph-Foreign.html#igraph_enter_safelocale" title="9.1. igraph_enter_safelocale — Temporarily set the C locale.">igraph_enter_safelocale</a></strong></span>(&amp;loc);
<span class="strong"><strong>if</strong></span> (<span class="strong"><strong><a class="link" href="igraph-Foreign.html#igraph_read_graph_gml" title="4.1. igraph_read_graph_gml — Read a graph in GML format.">igraph_read_graph_gml</a></strong></span>(&amp;graph, file) != IGRAPH_SUCCESS) {
<span class="strong"><strong>fprintf</strong></span>(stderr, "Reading %s failed.\n", filename);
<span class="strong"><strong>abort</strong></span>();
}
<span class="strong"><strong><a class="link" href="igraph-Foreign.html#igraph_write_graph_gml" title="4.2. igraph_write_graph_gml — Write the graph to a stream in GML format.">igraph_write_graph_gml</a></strong></span>(&amp;graph, stdout, IGRAPH_WRITE_GML_DEFAULT_SW, NULL, "");
<span class="strong"><strong><a class="link" href="igraph-Foreign.html#igraph_exit_safelocale" title="9.2. igraph_exit_safelocale — Temporarily set the C locale.">igraph_exit_safelocale</a></strong></span>(&amp;loc);
<span class="strong"><strong><a class="link" href="igraph-Basic.html#igraph_destroy" title="5.1.4. igraph_destroy — Frees the memory allocated for a graph object.">igraph_destroy</a></strong></span>(&amp;graph);
<span class="strong"><strong>return</strong></span> 0;
}
</pre>
<p></p>
</div>
</div>
<br class="example-break">
</div>
<p>
</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="igraph_exit_safelocale"></a>9.2. <code class="function">igraph_exit_safelocale</code> — Temporarily set the C locale.</h3></div></div></div>
<a class="indexterm" name="id-1.32.13.3.2"></a><p>
</p>
<div class="informalexample"><pre class="programlisting">
void igraph_exit_safelocale(igraph_safelocale_t *loc);
</pre></div>
<p>
</p>
<p>
Restores a locale saved by <a class="link" href="igraph-Foreign.html#igraph_enter_safelocale" title="9.1. igraph_enter_safelocale — Temporarily set the C locale."><code class="function">igraph_enter_safelocale()</code></a> and deallocates
all associated data. This function <span class="emphasis"><em>must</em></span> be paired with a call to
<a class="link" href="igraph-Foreign.html#igraph_enter_safelocale" title="9.1. igraph_enter_safelocale — Temporarily set the C locale."><code class="function">igraph_enter_safelocale()</code></a>.
</p>
<p><b>Arguments: </b>
</p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody><tr>
<td><p><span class="term"><em class="parameter"><code>loc</code></em>:</span></p></td>
<td><p>
A variable of type <code class="constant">igraph_safelocale_t</code>, originally set
by <a class="link" href="igraph-Foreign.html#igraph_enter_safelocale" title="9.1. igraph_enter_safelocale — Temporarily set the C locale."><code class="function">igraph_enter_safelocale()</code></a>.</p></td>
</tr></tbody>
</table></div>
<p>
</p>
</div>
</div>
</div>
<table class="navigation-footer" width="100%" summary="Navigation footer" cellpadding="2" cellspacing="0"><tr valign="middle">
<td align="left"><a accesskey="p" href="igraph-Processes.html"><b>← Chapter 30. Processes on graphs</b></a></td>
<td align="right"><a accesskey="n" href="igraph-Linalg.html"><b>Chapter 32. Using BLAS, LAPACK and ARPACK for igraph matrices and graphs →</b></a></td>
</tr></table>
</body>
</html>