COIN-OR::LEMON - Graph Library

source: lemon-0.x/doc/graph_io.dox @ 1522:321661278137

Last change on this file since 1522:321661278137 was 1522:321661278137, checked in by athos, 19 years ago

Some corrections to graph_io.dox (mainly language corrections). Improvements on quicktour.

File size: 7.3 KB
Line 
1namespace lemon {
2/*!
3
4
5\page graph-io-page Graph Input-Output
6
7The standard graph IO enables to store graphs and additional maps
8in a flexible and efficient way.
9
10\section format The general file format
11
12The file contains at most four sections in the following order:
13
14\li nodeset
15\li edgeset
16\li nodes
17\li edges
18
19The nodeset section starts with the following line:
20
21<tt>\@nodeset</tt>
22
23The next line contains the names of the nodemaps, separated by whitespaces.  Each
24following line describes a node in the graph: it contains the values of the
25maps in the right order. The map named "id" should contain unique values
26because it is regarded as an ID-map. For example:
27
28\code
29@nodeset
30id  x-coord  y-coord  color
313   1.0      4.0      blue
325   2.3      5.7      red
3312  7.8      2.3      green
34\endcode
35
36The edgeset section is very similar to the nodeset section, it has
37the same coloumn oriented structure. It starts with the line
38
39<tt>\@edgeset</tt>
40
41The next line contains the whitespace separated list of names of the maps.
42Each of the next lines describes one edge. The first two elements in the line
43are the IDs of the source and target (or tail and head) node of the edge as they occur in the ID node
44map. You can also have an optional ID map on the edges for later reference.
45
46\code
47@edgeset
48             id    weight   label
493   5        a     4.3      a-edge
505   12       c     2.6      c-edge
513   12       g     3.4      g-edge
52\endcode
53
54The next section contains <em>labeled nodes</em> (i.e. nodes having a special
55label on them). The section starts with
56
57<tt> \@nodes </tt>
58
59Each of the next lines contains a label for a node in the graph
60and then the ID described in the nodeset section.
61
62\code
63@nodes
64source 3
65target 12
66\endcode
67
68The last section describes the <em>labeled edges</em>
69(i.e. edges having a special label on them). It starts with \c \@edges
70and then each line contains the name of the edge and the ID.
71
72\code
73@nodes
74observed c
75\endcode
76
77
78The file may contain empty lines and comment lines. The comment lines
79start with an \c # character.
80
81The file ends with the
82
83<tt> \@end </tt>
84
85line.
86
87
88\section use Using graph input-output
89The graph input and output is based on reading and writing  commands. The user
90adds reading and writing commands to the reader or writer class, then he
91calls the \c run() method that executes all the given commands.
92
93\subsection write Writing a graph
94
95The \c GraphWriter class provides the graph output. To write a graph
96you should first give writing commands for the writer. You can declare
97write command as \c NodeMap or \c EdgeMap writing and labeled Node and
98Edge writing.
99
100\code
101GraphWriter<ListGraph> writer(std::cout, graph);
102\endcode
103
104The \c writeNodeMap() function declares a \c NodeMap writing command in the
105\c GraphWriter. You should give a name of the map and the map
106object as parameters. The NodeMap writing command with name "id" should write a
107unique map because it is regarded as ID map.
108
109\see IdMap, DescriptorMap 
110
111\code
112IdMap<ListGraph, Node> nodeIdMap;
113writer.writeNodeMap("id", nodeIdMap);
114
115writer.writeNodeMap("x-coord", xCoordMap);
116writer.writeNodeMap("y-coord", yCoordMap);
117writer.writeNodeMap("color", colorMap);
118\endcode
119
120With the \c writeEdgeMap() member function you can give an edge map
121writing command similar to the NodeMaps.
122
123\see IdMap, DescriptorMap 
124
125\code
126DescriptorMap<ListGraph, Edge, ListGraph::EdgeMap<int> > edgeDescMap(graph);
127writer.writeEdgeMap("descriptor", edgeDescMap);
128
129writer.writeEdgeMap("weight", weightMap);
130writer.writeEdgeMap("label", labelMap);
131\endcode
132
133With \c writeNode() and \c writeEdge() functions you can designate Nodes and
134Edges in the graph. For example, you can write out the source and target node
135of a maximum flow instance.
136
137\code
138writer.writeNode("source", sourceNode);
139writer.writeNode("target", targetNode);
140
141writer.writeEdge("observed", edge);
142\endcode
143
144After you give all write commands you must call the \c run() member
145function, which executes all the writing commands.
146
147\code
148writer.run();
149\endcode
150
151\subsection reading Reading a graph
152
153The given file format may contain several maps and labeled nodes or edges.
154If you read a graph you need not read all the maps and items just those
155that you need. The interface of the \c GraphReader is very similar to
156the GraphWriter but the reading method does not depend on the order of the
157given commands.
158
159The reader object assumes that each not readed value does not contain
160whitespaces, therefore it has some extra possibilities to control how
161it should skip the values when the string representation contains spaces.
162
163\code
164GraphReader<ListGraph> reader(std::cin, graph);
165\endcode
166
167The \c readNodeMap() function reads a map from the \c \@nodeset section.
168If there is a map that you do not want to read from the file and there are
169whitespaces in the string represenation of the values then you should
170call the \c skipNodeMap() template member function with proper parameters.
171
172\see QuotedStringReader
173
174\code
175reader.readNodeMap("x-coord", xCoordMap);
176reader.readNodeMap("y-coord", yCoordMap);
177
178reader.readNodeMap<QuotedStringReader>("label", labelMap);
179reader.skipNodeMap<QuotedStringReader>("description");
180
181reader.readNodeMap("color", colorMap);
182\endcode
183
184With the \c readEdgeMap() member function you can give an edge map
185reading command similar to the NodeMaps.
186
187\code
188reader.readEdgeMap("weight", weightMap);
189reader.readEdgeMap("label", labelMap);
190\endcode
191
192With \c readNode() and \c readEdge() functions you can read labeled Nodes and
193Edges.
194
195\code
196reader.readNode("source", sourceNode);
197reader.readNode("target", targetNode);
198
199reader.readEdge("observed", edge);
200\endcode
201
202After you give all read commands you must call the \c run() member
203function, which executes all the commands.
204
205\code
206reader.run();
207\endcode
208
209\section types The background of Reading and Writing
210The \c GraphReader should know how to read a Value from the given map.
211By the default implementation the input operator reads a value from
212the stream and the type of the readed value is the value type of the given map.
213When the reader should skip a value in the stream, because you do not
214want to store it in map, the reader skips a character sequence without
215whitespace.
216
217If you want to change the functionality of the reader, you can use
218template parameters to specialize it. When you give a reading
219command for a map you can give a Reader type as template parameter.
220With this template parameter you can control how the Reader reads
221a value from the stream.
222
223The reader has the next structure:
224\code
225struct TypeReader {
226  typedef TypeName Value;
227
228  void read(std::istream& is, Value& value);
229};
230\endcode
231
232By example, the \c "strings" nodemap contains strings and you do not need
233the value of the string just the length. Then you can implement own Reader
234struct.
235
236\code
237struct LengthReader {
238  typedef int Value;
239
240  void read(std::istream& is, Value& value) {
241    std::string tmp;
242    is >> tmp;
243    value = tmp.length();
244  }
245};
246...
247reader.readNodeMap<LengthReader>("strings", lengthMap);
248\endcode 
249
250The global functionality of the reader class can be changed by giving a
251special template parameter for the GraphReader class. By default, the
252template parameter is \c DefaultReaderTraits. A reader traits class
253should provide an inner template class Reader for each type, and an
254DefaultReader for skipping a value.
255
256The specialization of the writing should be very similar to the reading.
257
258\author Balazs Dezso
259*/
260}
Note: See TracBrowser for help on using the repository browser.