COIN-OR::LEMON - Graph Library

source: lemon-0.x/doc/graph_io.dox @ 1255:6455f64a85dc

Last change on this file since 1255:6455f64a85dc was 1118:62296604afb4, checked in by Alpar Juttner, 20 years ago

Minor changes.

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