File demoprograms.dox thrown away: demos module used instead.
5 \page graph-io-page Graph Input-Output
7 The standard graph IO enables one to store graphs and additional maps
8 (i.e. functions on the nodes or edges) in a flexible and efficient way.
9 Before you read this page you should be familiar with LEMON
10 \ref graphs "graphs" and \ref maps-page "maps".
12 \section format The general file format
14 The file contains sections in the following order:
22 Some of these sections can be omitted, but you will basicly need the nodeset
23 section (unless your graph has no nodes at all) and the edgeset section
24 (unless your graph has no edges at all).
26 The nodeset section describes the nodes of your graph: it identifies the nodes
27 and gives the maps defined on them, if any. It starts with the
32 The next line contains the names of the nodemaps, separated by whitespaces. Each
33 following line describes a node in the graph: it contains the values of the
34 maps in the right order. The map named "id" should contain unique values
35 because it is regarded as an ID-map. These ids need not be numbers but they
36 must identify the nodes uniquely for later reference. For example:
40 id x-coord y-coord color
46 The edgeset section is very similar to the nodeset section, it has
47 the same coloumn oriented structure. It starts with the line
51 The next line contains the whitespace separated list of names of the edge
52 maps. Each of the next lines describes one edge. The first two elements in
53 the line are the IDs of the source and target (or tail and head) nodes of the
54 edge as they occur in the ID node map of the nodeset section. You can also
55 have an optional ID map on the edges for later reference (which has to be
66 The \e nodes section contains <em>labeled (distinguished) nodes</em>
67 (i.e. nodes having a special
68 label on them). The section starts with
72 Each of the next lines contains a label for a node in the graph
73 and then the ID as described in the \e nodeset section.
81 The last section describes the <em>labeled (distinguished) edges</em>
82 (i.e. edges having a special label on them). It starts with \c \@edges
83 and then each line contains the name of the edge and the ID.
91 The file may contain empty lines and comment lines. The comment lines
92 start with an \c # character.
94 The attributes section can handle some information about the graph. It
95 contains key-value pairs in each line (a key and the mapped value to key). The
96 key should be a string without whitespaces, the value can be of various types.
100 title "Four colored plan graph"
101 author "Balazs DEZSO"
102 copyright "Lemon Library"
111 \section use Using graph input-output
113 The easiest way of using graph input and output is using the versions of the
114 public \ref readGraph() and \ref writeGraph() functions; if you don't need
115 very sophisticated behaviour then you might be satisfied with
116 those. Otherwise go on reading this page.
118 The graph input and output is based on <em> reading and writing
119 commands</em>. The user gives reading and writing commands to the reader or
120 writer class, then he calls the \c run() method that executes all the given
123 \subsection write Writing a graph
125 The \c GraphWriter class provides the graph output. To write a graph
126 you should first give writing commands to the writer. You can declare
127 writing command as \c NodeMap or \c EdgeMap writing and labeled Node and
131 GraphWriter<ListGraph> writer(std::cout, graph);
134 The \c writeNodeMap() function declares a \c NodeMap writing command in the
135 \c GraphWriter. You should give a name to the map and the map
136 object as parameters. The NodeMap writing command with name "id" should write a
137 unique map because it will be regarded as an ID map.
139 \see IdMap, DescriptorMap
142 IdMap<ListGraph, Node> nodeIdMap;
143 writer.writeNodeMap("id", nodeIdMap);
145 writer.writeNodeMap("x-coord", xCoordMap);
146 writer.writeNodeMap("y-coord", yCoordMap);
147 writer.writeNodeMap("color", colorMap);
150 With the \c writeEdgeMap() member function you can give an edge map
151 writing command similar to the NodeMaps.
153 \see IdMap, DescriptorMap
156 DescriptorMap<ListGraph, Edge, ListGraph::EdgeMap<int> > edgeDescMap(graph);
157 writer.writeEdgeMap("descriptor", edgeDescMap);
159 writer.writeEdgeMap("weight", weightMap);
160 writer.writeEdgeMap("label", labelMap);
163 With \c writeNode() and \c writeEdge() functions you can designate Nodes and
164 Edges in the graph. For example, you can write out the source and target node
165 of a maximum flow instance.
168 writer.writeNode("source", sourceNode);
169 writer.writeNode("target", targetNode);
171 writer.writeEdge("observed", edge);
174 With \c writeAttribute() function you can write an attribute to the file.
177 writer.writeAttribute("author", "Balazs DEZSO");
178 writer.writeAttribute("version", 12);
181 After you give all write commands you must call the \c run() member
182 function, which executes all the writing commands.
188 \subsection reading Reading a graph
190 The file to be read may contain several maps and labeled nodes or edges.
191 If you read a graph you need not read all the maps and items just those
192 that you need. The interface of the \c GraphReader is very similar to
193 the GraphWriter but the reading method does not depend on the order of the
196 The reader object assumes that each not readed value does not contain
197 whitespaces, therefore it has some extra possibilities to control how
198 it should skip the values when the string representation contains spaces.
201 GraphReader<ListGraph> reader(std::cin, graph);
204 The \c readNodeMap() function reads a map from the \c nodeset section.
205 If there is a map that you do not want to read from the file and there are
206 whitespaces in the string represenation of the values then you should
207 call the \c skipNodeMap() template member function with proper parameters.
209 \see QuotedStringReader
212 reader.readNodeMap("x-coord", xCoordMap);
213 reader.readNodeMap("y-coord", yCoordMap);
215 reader.readNodeMap<QuotedStringReader>("label", labelMap);
216 reader.skipNodeMap<QuotedStringReader>("description");
218 reader.readNodeMap("color", colorMap);
221 With the \c readEdgeMap() member function you can give an edge map
222 reading command similar to the NodeMaps.
225 reader.readEdgeMap("weight", weightMap);
226 reader.readEdgeMap("label", labelMap);
229 With \c readNode() and \c readEdge() functions you can read labeled Nodes and
233 reader.readNode("source", sourceNode);
234 reader.readNode("target", targetNode);
236 reader.readEdge("observed", edge);
239 With \c readAttribute() function you can read an attribute from the file.
243 writer.readAttribute("author", author);
245 writer.writeAttribute("version", version);
248 After you give all read commands you must call the \c run() member
249 function, which executes all the commands.
256 \section types Background of Reading and Writing
259 To read a map (on the nodes or edges)
260 the \c GraphReader should know how to read a Value from the given map.
261 By the default implementation the input operator reads a value from
262 the stream and the type of the readed value is the value type of the given map.
263 When the reader should skip a value in the stream, because you do not
264 want to store it in a map, the reader skips a character sequence without
267 If you want to change the functionality of the reader, you can use
268 template parameters to specialize it. When you give a reading
269 command for a map you can give a Reader type as template parameter.
270 With this template parameter you can control how the Reader reads
271 a value from the stream.
273 The reader has the next structure:
276 typedef TypeName Value;
278 void read(std::istream& is, Value& value);
282 For example, the \c "strings" nodemap contains strings and you do not need
283 the value of the string just the length. Then you can implement an own Reader
287 struct LengthReader {
290 void read(std::istream& is, Value& value) {
293 value = tmp.length();
297 reader.readNodeMap<LengthReader>("strings", lengthMap);
300 The global functionality of the reader class can be changed by giving a
301 special template parameter to the GraphReader class. By default, the
302 template parameter is \c DefaultReaderTraits. A reader traits class
303 should provide an inner template class Reader for each type, and a
304 DefaultReader for skipping a value.
306 The specialization of writing is very similar to that of reading.
308 \section undir Undirected graphs
310 In a file describing an undirected graph (undir graph, for short) you find an
311 \c undiredgeset section instead of the \c edgeset section. The first line of
312 the section describes the names of the maps on the undirected egdes and all
313 next lines describe one undirected edge with the the incident nodes and the
316 The format handles directed edge maps as a syntactical sugar???, if there
317 are two maps with names being the same with a \c '+' and a \c '-' prefix
318 then this will be read as a directed map.
322 id capacity +flow -flow
328 The \c edges section is changed to \c undiredges section. This section
329 describes labeled edges and undirected edges. The directed edge label
330 should start with a \c '+' or a \c '-' prefix to decide the direction
340 There are similar classes to the \c GraphReader ans \c GraphWriter
341 which handle the undirected graphs. These classes are the
342 \c UndirGraphReader and \UndirGraphWriter.
344 The \c readUndirMap() function reads an undirected map and the
345 \c readUndirEdge() reads an undirected edge from the file,
348 reader.readUndirEdgeMap("capacity", capacityMap);
349 reader.readEdgeMap("flow", flowMap);
351 reader.readUndirEdge("undir_edge", undir_edge);
352 reader.readEdge("edge", edge);
355 \section advanced Advanced features
357 The graph reader and writer classes give an easy way to read and write
358 graphs. But sometimes we want more advanced features. In this case we can
359 use the more general <tt>lemon reader and writer</tt> interface.
361 The LEMON file format is a section oriented file format. It contains one or
362 more sections, each starting with a line identifying its type
363 (the word starting with the \c \@ character).
364 The content of the section this way cannot contain line with \c \@ first
365 character. The file may contains comment lines with \c # first character.
367 The \c LemonReader and \c LemonWriter gives a framework to read and
368 write sections. There are various section reader and section writer
369 classes which can be attached to a \c LemonReader or a \c LemonWriter.
371 There are default section readers and writers for reading and writing
372 item sets, and labeled items in the graph. These read and write
373 the format described above. Other type of data can be handled with own
374 section reader and writer classes which are inherited from the
375 \c LemonReader::SectionReader or the \c LemonWriter::SectionWriter classes.
377 The next example defines a special section reader which reads the
378 \c \@description sections into a string:
381 class DescriptionReader : LemonReader::SectionReader {
383 virtual bool header(const std::string& line) {
384 std::istringstream ls(line);
387 return head == "@description";
390 virtual void read(std::istream& is) {
392 while (getline(is, line)) {
398 typedef LemonReader::SectionReader Parent;
400 DescriptionReader(LemonReader& reader) : Parent(reader) {}
402 const std::string& description() const {
411 The other advanced stuff of the generalized file format is that
412 multiple edgesets can be stored to the same nodeset. It can be used
413 for example as a network traffic matrix.
415 In our example there is a network with symmetric links and there are assymetric
416 traffic request on the network. This construction can be stored in an
417 undirected graph and in a directed NewEdgeSetAdaptor class. The example
418 shows the input with the LemonReader class:
421 UndirListGraph network;
422 UndirListGraph::UndirEdgeSet<double> capacity;
423 NewEdgeSetAdaptor<UndirListGraph> traffic(network);
424 NewEdgeSetAdaptor<UndirListGraph>::EdgeSet<double> request(network);
426 LemonReader reader(std::cin);
427 NodeSetReader nodesetReader(reader, network);
428 UndirEdgeSetReader undirEdgesetReader(reader, network, nodesetReader);
429 undirEdgesetReader.readEdgeMap("capacity", capacity);
430 EdgeSetReader edgesetReader(reader, traffic, nodesetReader);
431 edgesetReader.readEdgeMap("request", request);
436 Because the GraphReader and the UndirGraphReader can be converted
437 to LemonReader and it can resolve the ID's of the items, the previous
438 result can be achived with the UndirGraphReader class, too.
442 UndirListGraph network;
443 UndirListGraph::UndirEdgeSet<double> capacity;
444 NewEdgeSetAdaptor<UndirListGraph> traffic(network);
445 NewEdgeSetAdaptor<UndirListGraph>::EdgeSet<double> request(network);
447 UndirGraphReader reader(std::cin, network);
448 reader.readEdgeMap("capacity", capacity);
449 EdgeSetReader edgesetReader(reader, traffic, reader);
450 edgesetReader.readEdgeMap("request", request);