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