COIN-OR::LEMON - Graph Library

source: lemon-0.x/doc/graph_io.dox @ 1660:93792a112fd5

Last change on this file since 1660:93792a112fd5 was 1631:e15162d8eca1, checked in by Alpar Juttner, 19 years ago

Fixed most (but not all) of Doxygen warnings

File size: 14.8 KB
RevLine 
[1118]1namespace lemon {
[1114]2/*!
3
4
5\page graph-io-page Graph Input-Output
6
[1540]7The 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.
9Before you read this page you should be familiar with LEMON
10\ref graphs "graphs" and \ref maps-page "maps".
[1114]11
12\section format The general file format
13
[1532]14The file contains sections in the following order:
[1114]15
16\li nodeset
17\li edgeset
18\li nodes
19\li edges
[1532]20\li attributes
[1114]21
[1540]22Some of these sections can be omitted, but you will basicly need the nodeset
23section (unless your graph has no nodes at all) and the edgeset section
24(unless your graph has no edges at all).
25
26The nodeset section describes the nodes of your graph: it identifies the nodes
27and gives the maps defined on them, if any. It starts with the
28following line:
[1522]29
30<tt>\@nodeset</tt>
31
32The next line contains the names of the nodemaps, separated by whitespaces.  Each
33following line describes a node in the graph: it contains the values of the
34maps in the right order. The map named "id" should contain unique values
[1540]35because it is regarded as an ID-map. These ids need not be numbers but they
36must identify the nodes uniquely for later reference. For example:
[1114]37
38\code
39@nodeset
40id  x-coord  y-coord  color
413   1.0      4.0      blue
425   2.3      5.7      red
4312  7.8      2.3      green
44\endcode
45
46The edgeset section is very similar to the nodeset section, it has
[1522]47the same coloumn oriented structure. It starts with the line
48
49<tt>\@edgeset</tt>
50
[1540]51The next line contains the whitespace separated list of names of the edge
52maps.  Each of the next lines describes one edge. The first two elements in
53the line are the IDs of the source and target (or tail and head) nodes of the
54edge as they occur in the ID node map of the nodeset section. You can also
55have an optional ID map on the edges for later reference (which has to be
56unique in this case).
[1114]57
58\code
59@edgeset
60             id    weight   label
613   5        a     4.3      a-edge
625   12       c     2.6      c-edge
633   12       g     3.4      g-edge
64\endcode
65
[1540]66The \e nodes section contains <em>labeled (distinguished) nodes</em>
67(i.e. nodes having a special
[1118]68label on them). The section starts with
[1522]69
70<tt> \@nodes </tt>
71
72Each of the next lines contains a label for a node in the graph
[1540]73and then the ID as described in the \e nodeset section.
[1114]74
75\code
76@nodes
77source 3
78target 12
79\endcode
80
[1540]81The last section describes the <em>labeled (distinguished) edges</em>
[1333]82(i.e. edges having a special label on them). It starts with \c \@edges
[1114]83and then each line contains the name of the edge and the ID.
84
85\code
[1540]86@edges
[1114]87observed c
88\endcode
89
90
91The file may contain empty lines and comment lines. The comment lines
92start with an \c # character.
93
[1532]94The attributes section can handle some information about the graph. It
[1540]95contains key-value pairs in each line (a key and the mapped value to key). The
96key should be a string without whitespaces, the value can be of various types.
[1532]97
98\code
99@attributes
100title "Four colored plan graph"
101author "Balazs DEZSO"
102copyright "Lemon Library"
103version 12
104\endcode
105
[1522]106<tt> \@end </tt>
107
108line.
109
[1114]110
111\section use Using graph input-output
[1540]112
113The 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.
117
118The graph input and output is based on <em> reading and writing
119commands</em>. The user gives reading and writing commands to the reader or
120writer class, then he calls the \c run() method that executes all the given
121commands.
[1114]122
123\subsection write Writing a graph
124
[1631]125The \ref lemon::GraphWriter "GraphWriter" template class
126provides the graph output. To write a graph
[1526]127you should first give writing commands to the writer. You can declare
[1540]128writing command as \c NodeMap or \c EdgeMap writing and labeled Node and
[1114]129Edge writing.
130
131\code
[1333]132GraphWriter<ListGraph> writer(std::cout, graph);
[1114]133\endcode
134
[1631]135The \ref lemon::GraphWriter::writeNodeMap() "writeNodeMap()"
136function declares a \c NodeMap writing command in the
137\ref lemon::GraphWriter "GraphWriter".
138You should give a name to the map and the map
[1522]139object as parameters. The NodeMap writing command with name "id" should write a
[1540]140unique map because it will be regarded as an ID map.
[1114]141
142\see IdMap, DescriptorMap 
143
144\code
145IdMap<ListGraph, Node> nodeIdMap;
[1394]146writer.writeNodeMap("id", nodeIdMap);
[1114]147
[1394]148writer.writeNodeMap("x-coord", xCoordMap);
149writer.writeNodeMap("y-coord", yCoordMap);
150writer.writeNodeMap("color", colorMap);
[1114]151\endcode
152
[1631]153With the \ref lemon::GraphWriter::writeEdgeMap() "writeEdgeMap()"
154member function you can give an edge map
[1333]155writing command similar to the NodeMaps.
[1114]156
157\see IdMap, DescriptorMap 
[1522]158
[1114]159\code
160DescriptorMap<ListGraph, Edge, ListGraph::EdgeMap<int> > edgeDescMap(graph);
[1394]161writer.writeEdgeMap("descriptor", edgeDescMap);
[1114]162
[1394]163writer.writeEdgeMap("weight", weightMap);
164writer.writeEdgeMap("label", labelMap);
[1114]165\endcode
166
[1631]167With \ref lemon::GraphWriter::writeNode() "writeNode()"
168and \ref lemon::GraphWriter::writeEdge() "writeEdge()"
169functions you can designate Nodes and
[1522]170Edges in the graph. For example, you can write out the source and target node
171of a maximum flow instance.
[1114]172
173\code
[1394]174writer.writeNode("source", sourceNode);
175writer.writeNode("target", targetNode);
[1114]176
[1394]177writer.writeEdge("observed", edge);
[1114]178\endcode
179
[1631]180With \ref lemon::GraphWriter::writeAttribute() "writeAttribute()"
181function you can write an attribute to the file.
[1532]182
183\code
184writer.writeAttribute("author", "Balazs DEZSO");
185writer.writeAttribute("version", 12);
186\endcode
187
[1631]188After you give all write commands you must call the
189\ref lemon::GraphWriter::run() "run()" member
[1522]190function, which executes all the writing commands.
[1114]191
192\code
193writer.run();
194\endcode
195
196\subsection reading Reading a graph
197
[1540]198The file to be read may contain several maps and labeled nodes or edges.
[1114]199If you read a graph you need not read all the maps and items just those
[1631]200that you need. The interface of the \ref lemon::GraphReader "GraphReader"
201is very similar to
202the \ref lemon::GraphWriter "GraphWriter"
203but the reading method does not depend on the order of the
[1114]204given commands.
205
[1522]206The reader object assumes that each not readed value does not contain
[1118]207whitespaces, therefore it has some extra possibilities to control how
208it should skip the values when the string representation contains spaces.
[1114]209
210\code
[1333]211GraphReader<ListGraph> reader(std::cin, graph);
[1114]212\endcode
213
[1631]214The \ref lemon::GraphReader::readNodeMap() "readNodeMap()"
215function reads a map from the \c nodeset section.
[1522]216If there is a map that you do not want to read from the file and there are
217whitespaces in the string represenation of the values then you should
[1631]218call the \ref lemon::GraphReader::skipNodeMap() "skipNodeMap()"
219template member function with proper parameters.
[1114]220
221\see QuotedStringReader
[1522]222
[1114]223\code
[1394]224reader.readNodeMap("x-coord", xCoordMap);
225reader.readNodeMap("y-coord", yCoordMap);
[1114]226
[1394]227reader.readNodeMap<QuotedStringReader>("label", labelMap);
[1114]228reader.skipNodeMap<QuotedStringReader>("description");
229
[1394]230reader.readNodeMap("color", colorMap);
[1114]231\endcode
232
[1631]233With the \ref lemon::GraphReader::readEdgeMap() "readEdgeMap()"
234member function you can give an edge map
[1114]235reading command similar to the NodeMaps.
236
237\code
[1394]238reader.readEdgeMap("weight", weightMap);
239reader.readEdgeMap("label", labelMap);
[1114]240\endcode
241
[1631]242With \ref lemon::GraphReader::readNode() "readNode()"
243and \ref lemon::GraphReader::readEdge() "readEdge()"
244functions you can read labeled Nodes and
[1114]245Edges.
246
247\code
[1394]248reader.readNode("source", sourceNode);
249reader.readNode("target", targetNode);
[1114]250
[1394]251reader.readEdge("observed", edge);
[1114]252\endcode
253
[1631]254With \ref lemon::GraphReader::readAttribute() "readAttribute()"
255function you can read an attribute from the file.
[1532]256
257\code
258std::string author;
259writer.readAttribute("author", author);
260int version;
261writer.writeAttribute("version", version);
262\endcode
263
[1631]264After you give all read commands you must call the
265\ref lemon::GraphReader::run() "run()" member
[1522]266function, which executes all the commands.
[1114]267
268\code
269reader.run();
270\endcode
271
[1540]272\anchor rwbackground
[1527]273\section types Background of Reading and Writing
[1540]274
275
[1527]276To read a map (on the nodes or edges)
[1631]277the \ref lemon::GraphReader "GraphReader"
278should know how to read a Value from the given map.
[1114]279By the default implementation the input operator reads a value from
280the stream and the type of the readed value is the value type of the given map.
281When the reader should skip a value in the stream, because you do not
[1527]282want to store it in a map, the reader skips a character sequence without
[1540]283whitespaces.
[1114]284
285If you want to change the functionality of the reader, you can use
286template parameters to specialize it. When you give a reading
287command for a map you can give a Reader type as template parameter.
[1333]288With this template parameter you can control how the Reader reads
[1114]289a value from the stream.
290
291The reader has the next structure:
292\code
293struct TypeReader {
294  typedef TypeName Value;
295
296  void read(std::istream& is, Value& value);
297};
298\endcode
299
[1527]300For example, the \c "strings" nodemap contains strings and you do not need
[1540]301the value of the string just the length. Then you can implement an own Reader
[1114]302struct.
303
304\code
305struct LengthReader {
306  typedef int Value;
307
308  void read(std::istream& is, Value& value) {
309    std::string tmp;
310    is >> tmp;
311    value = tmp.length();
312  }
313};
314...
[1394]315reader.readNodeMap<LengthReader>("strings", lengthMap);
[1114]316\endcode 
317
318The global functionality of the reader class can be changed by giving a
[1526]319special template parameter to the GraphReader class. By default, the
[1118]320template parameter is \c DefaultReaderTraits. A reader traits class
[1540]321should provide an inner template class Reader for each type, and a
[1114]322DefaultReader for skipping a value.
323
[1540]324The specialization of  writing is very similar to that of reading.
[1114]325
[1540]326\section undir Undirected graphs
[1532]327
[1540]328In a file describing an undirected graph (undir graph, for short) you find an
329\c undiredgeset section instead of the \c edgeset section. The first line of
330the section describes the names of the maps on the undirected egdes and all
331next lines describe one undirected edge with the the incident nodes and the
332values of the map.
[1532]333
[1540]334The format handles directed edge maps as a syntactical sugar???, if there
335are two maps with names being the same with a \c '+' and a \c '-' prefix
336then this will be read as a directed map.
[1532]337
338\code
339@undiredgeset
340             id    capacity +flow -flow
34132   2       1     4.3      2.0   0.0
34221   21      5     2.6      0.0   2.6
34321   12      8     3.4      0.0   0.0
344\endcode
345
[1540]346The \c edges section is changed to \c undiredges section. This section
[1532]347describes labeled edges and undirected edges. The directed edge label
[1540]348should start with a \c '+' or a \c '-' prefix to decide the direction
[1532]349of the edge.
350
351\code
352@undiredges
353undiredge 1
354+edge 5
355-back 5
356\endcode
357
[1631]358There are similar classes to the \ref lemon::GraphReader "GraphReader" and
359\ref lemon::GraphWriter "GraphWriter" which
360handle the undirected graphs. These classes are
361the \ref lemon::UndirGraphReader "UndirGraphReader"
362and \ref lemon::UndirGraphWriter "UndirGraphWriter".
[1532]363
[1631]364The \ref lemon::UndirGraphReader::readUndirMap() "readUndirMap()"
365function reads an undirected map and the
366\ref lemon::UndirGraphReader::readUndirEdge() "readUndirEdge()"
367reads an undirected edge from the file,
[1532]368
369\code
370reader.readUndirEdgeMap("capacity", capacityMap);
371reader.readEdgeMap("flow", flowMap);
372...
373reader.readUndirEdge("undir_edge", undir_edge);
374reader.readEdge("edge", edge);
375\endcode
376
377\section advanced Advanced features
378
[1540]379The graph reader and writer classes give an easy way to read and write
380graphs. But sometimes we want more advanced features. In this case we can
381use the more general <tt>lemon reader and writer</tt> interface.
[1532]382
[1540]383The LEMON file format is a section oriented file format. It contains one or
384more sections, each starting with a line identifying its type
385(the word starting with the \c \@  character).
[1532]386The content of the section this way cannot contain line with \c \@ first
387character. The file may contains comment lines with \c # first character.
388
[1631]389The \ref lemon::LemonReader "LemonReader"
390and \ref lemon::LemonWriter "LemonWriter"
391gives a framework to read and
[1532]392write sections. There are various section reader and section writer
[1631]393classes which can be attached to a \ref lemon::LemonReader "LemonReader"
394or a \ref lemon::LemonWriter "LemonWriter".
[1532]395
396There are default section readers and writers for reading and writing
[1540]397item sets, and labeled items in the graph. These read and write
[1532]398the format described above. Other type of data can be handled with own
399section reader and writer classes which are inherited from the
[1631]400\c LemonReader::SectionReader or the
401\ref lemon::LemonWriter::SectionWriter "LemonWriter::SectionWriter"
402classes.
[1532]403
404The next example defines a special section reader which reads the
405\c \@description sections into a string:
406
407\code
408class DescriptionReader : LemonReader::SectionReader {
409protected:
410  virtual bool header(const std::string& line) {
411    std::istringstream ls(line);
412    std::string head;
413    ls >> head;
414    return head == "@description";
415  }
416
417  virtual void read(std::istream& is) {
418    std::string line;
419    while (getline(is, line)) {
420      desc += line;
421    }
422  }
423public:
424
425  typedef LemonReader::SectionReader Parent;
426 
427  DescriptionReader(LemonReader& reader) : Parent(reader) {}
428
429  const std::string& description() const {
430    return description;
431  }
432
433private:
434  std::string desc;
435};
436\endcode
437
438The other advanced stuff of the generalized file format is that
439multiple edgesets can be stored to the same nodeset. It can be used
[1540]440for example as a network traffic matrix.
[1532]441
[1540]442In our example there is a network with symmetric links and there are assymetric
[1532]443traffic request on the network. This construction can be stored in an
[1631]444undirected graph and in a directed \c NewEdgeSetAdaptor class. The example
445shows the input with the \ref lemon::LemonReader "LemonReader" class:
[1532]446
447\code
448UndirListGraph network;
449UndirListGraph::UndirEdgeSet<double> capacity;
450NewEdgeSetAdaptor<UndirListGraph> traffic(network);
451NewEdgeSetAdaptor<UndirListGraph>::EdgeSet<double> request(network);
452
453LemonReader reader(std::cin);
454NodeSetReader nodesetReader(reader, network);
455UndirEdgeSetReader undirEdgesetReader(reader, network, nodesetReader);
456undirEdgesetReader.readEdgeMap("capacity", capacity);
457EdgeSetReader edgesetReader(reader, traffic, nodesetReader);
458edgesetReader.readEdgeMap("request", request);
459
460reader.run();
461\endcode
462
[1631]463Because both the \ref lemon::GraphReader "GraphReader"
464and the \ref lemon::UndirGraphReader "UndirGraphReader" can be converted
465to \ref lemon::LemonReader "LemonReader"
466and it can resolve the ID's of the items, the previous
467result can be achived with the \ref lemon::UndirGraphReader "UndirGraphReader"
468class, too.
[1532]469
470
471\code
472UndirListGraph network;
473UndirListGraph::UndirEdgeSet<double> capacity;
474NewEdgeSetAdaptor<UndirListGraph> traffic(network);
475NewEdgeSetAdaptor<UndirListGraph>::EdgeSet<double> request(network);
476
477UndirGraphReader reader(std::cin, network);
478reader.readEdgeMap("capacity", capacity);
479EdgeSetReader edgesetReader(reader, traffic, reader);
480edgesetReader.readEdgeMap("request", request);
481
482reader.run();
483\endcode
484
[1333]485\author Balazs Dezso
[1114]486*/
[1631]487}
Note: See TracBrowser for help on using the repository browser.