gravatar
alpar (Alpar Juttner)
alpar@cs.elte.hu
Merge
0 1 0
merge default
1 file changed with 7 insertions and 7 deletions:
↑ Collapse diff ↑
Show white space 768 line context
1 1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library.
4 4
 *
5 5
 * Copyright (C) 2003-2009
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#ifndef LEMON_LIST_GRAPH_H
20 20
#define LEMON_LIST_GRAPH_H
21 21

	
22 22
///\ingroup graphs
23 23
///\file
24 24
///\brief ListDigraph and ListGraph classes.
25 25

	
26 26
#include <lemon/core.h>
27 27
#include <lemon/error.h>
28 28
#include <lemon/bits/graph_extender.h>
29 29

	
30 30
#include <vector>
31 31
#include <list>
32 32

	
33 33
namespace lemon {
34 34

	
35 35
  class ListDigraph;
36 36

	
37 37
  class ListDigraphBase {
38 38

	
39 39
  protected:
40 40
    struct NodeT {
41 41
      int first_in, first_out;
42 42
      int prev, next;
43 43
    };
44 44

	
45 45
    struct ArcT {
46 46
      int target, source;
47 47
      int prev_in, prev_out;
48 48
      int next_in, next_out;
49 49
    };
50 50

	
51 51
    std::vector<NodeT> nodes;
52 52

	
53 53
    int first_node;
54 54

	
55 55
    int first_free_node;
56 56

	
57 57
    std::vector<ArcT> arcs;
58 58

	
59 59
    int first_free_arc;
60 60

	
61 61
  public:
62 62

	
63 63
    typedef ListDigraphBase Digraph;
64 64

	
65 65
    class Node {
66 66
      friend class ListDigraphBase;
67 67
      friend class ListDigraph;
68 68
    protected:
69 69

	
70 70
      int id;
71 71
      explicit Node(int pid) { id = pid;}
72 72

	
73 73
    public:
74 74
      Node() {}
75 75
      Node (Invalid) { id = -1; }
76 76
      bool operator==(const Node& node) const {return id == node.id;}
77 77
      bool operator!=(const Node& node) const {return id != node.id;}
78 78
      bool operator<(const Node& node) const {return id < node.id;}
79 79
    };
80 80

	
81 81
    class Arc {
82 82
      friend class ListDigraphBase;
83 83
      friend class ListDigraph;
84 84
    protected:
85 85

	
86 86
      int id;
87 87
      explicit Arc(int pid) { id = pid;}
88 88

	
89 89
    public:
90 90
      Arc() {}
91 91
      Arc (Invalid) { id = -1; }
92 92
      bool operator==(const Arc& arc) const {return id == arc.id;}
93 93
      bool operator!=(const Arc& arc) const {return id != arc.id;}
94 94
      bool operator<(const Arc& arc) const {return id < arc.id;}
95 95
    };
96 96

	
97 97

	
98 98

	
99 99
    ListDigraphBase()
100 100
      : nodes(), first_node(-1),
101 101
        first_free_node(-1), arcs(), first_free_arc(-1) {}
102 102

	
103 103

	
104 104
    int maxNodeId() const { return nodes.size()-1; }
105 105
    int maxArcId() const { return arcs.size()-1; }
106 106

	
107 107
    Node source(Arc e) const { return Node(arcs[e.id].source); }
108 108
    Node target(Arc e) const { return Node(arcs[e.id].target); }
109 109

	
110 110

	
111 111
    void first(Node& node) const {
112 112
      node.id = first_node;
113 113
    }
114 114

	
115 115
    void next(Node& node) const {
116 116
      node.id = nodes[node.id].next;
117 117
    }
118 118

	
119 119

	
120 120
    void first(Arc& arc) const {
121 121
      int n;
122 122
      for(n = first_node;
123
          n!=-1 && nodes[n].first_in == -1;
123
          n != -1 && nodes[n].first_out == -1;
124 124
          n = nodes[n].next) {}
125
      arc.id = (n == -1) ? -1 : nodes[n].first_in;
125
      arc.id = (n == -1) ? -1 : nodes[n].first_out;
126 126
    }
127 127

	
128 128
    void next(Arc& arc) const {
129
      if (arcs[arc.id].next_in != -1) {
130
        arc.id = arcs[arc.id].next_in;
129
      if (arcs[arc.id].next_out != -1) {
130
        arc.id = arcs[arc.id].next_out;
131 131
      } else {
132 132
        int n;
133
        for(n = nodes[arcs[arc.id].target].next;
134
            n!=-1 && nodes[n].first_in == -1;
133
        for(n = nodes[arcs[arc.id].source].next;
134
            n != -1 && nodes[n].first_out == -1;
135 135
            n = nodes[n].next) {}
136
        arc.id = (n == -1) ? -1 : nodes[n].first_in;
136
        arc.id = (n == -1) ? -1 : nodes[n].first_out;
137 137
      }
138 138
    }
139 139

	
140 140
    void firstOut(Arc &e, const Node& v) const {
141 141
      e.id = nodes[v.id].first_out;
142 142
    }
143 143
    void nextOut(Arc &e) const {
144 144
      e.id=arcs[e.id].next_out;
145 145
    }
146 146

	
147 147
    void firstIn(Arc &e, const Node& v) const {
148 148
      e.id = nodes[v.id].first_in;
149 149
    }
150 150
    void nextIn(Arc &e) const {
151 151
      e.id=arcs[e.id].next_in;
152 152
    }
153 153

	
154 154

	
155 155
    static int id(Node v) { return v.id; }
156 156
    static int id(Arc e) { return e.id; }
157 157

	
158 158
    static Node nodeFromId(int id) { return Node(id);}
159 159
    static Arc arcFromId(int id) { return Arc(id);}
160 160

	
161 161
    bool valid(Node n) const {
162 162
      return n.id >= 0 && n.id < static_cast<int>(nodes.size()) &&
163 163
        nodes[n.id].prev != -2;
164 164
    }
165 165

	
166 166
    bool valid(Arc a) const {
167 167
      return a.id >= 0 && a.id < static_cast<int>(arcs.size()) &&
168 168
        arcs[a.id].prev_in != -2;
169 169
    }
170 170

	
171 171
    Node addNode() {
172 172
      int n;
173 173

	
174 174
      if(first_free_node==-1) {
175 175
        n = nodes.size();
176 176
        nodes.push_back(NodeT());
177 177
      } else {
178 178
        n = first_free_node;
179 179
        first_free_node = nodes[n].next;
180 180
      }
181 181

	
182 182
      nodes[n].next = first_node;
183 183
      if(first_node != -1) nodes[first_node].prev = n;
184 184
      first_node = n;
185 185
      nodes[n].prev = -1;
186 186

	
187 187
      nodes[n].first_in = nodes[n].first_out = -1;
188 188

	
189 189
      return Node(n);
190 190
    }
191 191

	
192 192
    Arc addArc(Node u, Node v) {
193 193
      int n;
194 194

	
195 195
      if (first_free_arc == -1) {
196 196
        n = arcs.size();
197 197
        arcs.push_back(ArcT());
198 198
      } else {
199 199
        n = first_free_arc;
200 200
        first_free_arc = arcs[n].next_in;
201 201
      }
202 202

	
203 203
      arcs[n].source = u.id;
204 204
      arcs[n].target = v.id;
205 205

	
206 206
      arcs[n].next_out = nodes[u.id].first_out;
207 207
      if(nodes[u.id].first_out != -1) {
208 208
        arcs[nodes[u.id].first_out].prev_out = n;
209 209
      }
210 210

	
211 211
      arcs[n].next_in = nodes[v.id].first_in;
212 212
      if(nodes[v.id].first_in != -1) {
213 213
        arcs[nodes[v.id].first_in].prev_in = n;
214 214
      }
215 215

	
216 216
      arcs[n].prev_in = arcs[n].prev_out = -1;
217 217

	
218 218
      nodes[u.id].first_out = nodes[v.id].first_in = n;
219 219

	
220 220
      return Arc(n);
221 221
    }
222 222

	
223 223
    void erase(const Node& node) {
224 224
      int n = node.id;
225 225

	
226 226
      if(nodes[n].next != -1) {
227 227
        nodes[nodes[n].next].prev = nodes[n].prev;
228 228
      }
229 229

	
230 230
      if(nodes[n].prev != -1) {
231 231
        nodes[nodes[n].prev].next = nodes[n].next;
232 232
      } else {
233 233
        first_node = nodes[n].next;
234 234
      }
235 235

	
236 236
      nodes[n].next = first_free_node;
237 237
      first_free_node = n;
238 238
      nodes[n].prev = -2;
239 239

	
240 240
    }
241 241

	
242 242
    void erase(const Arc& arc) {
243 243
      int n = arc.id;
244 244

	
245 245
      if(arcs[n].next_in!=-1) {
246 246
        arcs[arcs[n].next_in].prev_in = arcs[n].prev_in;
247 247
      }
248 248

	
249 249
      if(arcs[n].prev_in!=-1) {
250 250
        arcs[arcs[n].prev_in].next_in = arcs[n].next_in;
251 251
      } else {
252 252
        nodes[arcs[n].target].first_in = arcs[n].next_in;
253 253
      }
254 254

	
255 255

	
256 256
      if(arcs[n].next_out!=-1) {
257 257
        arcs[arcs[n].next_out].prev_out = arcs[n].prev_out;
258 258
      }
259 259

	
260 260
      if(arcs[n].prev_out!=-1) {
261 261
        arcs[arcs[n].prev_out].next_out = arcs[n].next_out;
262 262
      } else {
263 263
        nodes[arcs[n].source].first_out = arcs[n].next_out;
264 264
      }
265 265

	
266 266
      arcs[n].next_in = first_free_arc;
267 267
      first_free_arc = n;
268 268
      arcs[n].prev_in = -2;
269 269
    }
270 270

	
271 271
    void clear() {
272 272
      arcs.clear();
273 273
      nodes.clear();
274 274
      first_node = first_free_node = first_free_arc = -1;
275 275
    }
276 276

	
277 277
  protected:
278 278
    void changeTarget(Arc e, Node n)
279 279
    {
280 280
      if(arcs[e.id].next_in != -1)
281 281
        arcs[arcs[e.id].next_in].prev_in = arcs[e.id].prev_in;
282 282
      if(arcs[e.id].prev_in != -1)
283 283
        arcs[arcs[e.id].prev_in].next_in = arcs[e.id].next_in;
284 284
      else nodes[arcs[e.id].target].first_in = arcs[e.id].next_in;
285 285
      if (nodes[n.id].first_in != -1) {
286 286
        arcs[nodes[n.id].first_in].prev_in = e.id;
287 287
      }
288 288
      arcs[e.id].target = n.id;
289 289
      arcs[e.id].prev_in = -1;
290 290
      arcs[e.id].next_in = nodes[n.id].first_in;
291 291
      nodes[n.id].first_in = e.id;
292 292
    }
293 293
    void changeSource(Arc e, Node n)
294 294
    {
295 295
      if(arcs[e.id].next_out != -1)
296 296
        arcs[arcs[e.id].next_out].prev_out = arcs[e.id].prev_out;
297 297
      if(arcs[e.id].prev_out != -1)
298 298
        arcs[arcs[e.id].prev_out].next_out = arcs[e.id].next_out;
299 299
      else nodes[arcs[e.id].source].first_out = arcs[e.id].next_out;
300 300
      if (nodes[n.id].first_out != -1) {
301 301
        arcs[nodes[n.id].first_out].prev_out = e.id;
302 302
      }
303 303
      arcs[e.id].source = n.id;
304 304
      arcs[e.id].prev_out = -1;
305 305
      arcs[e.id].next_out = nodes[n.id].first_out;
306 306
      nodes[n.id].first_out = e.id;
307 307
    }
308 308

	
309 309
  };
310 310

	
311 311
  typedef DigraphExtender<ListDigraphBase> ExtendedListDigraphBase;
312 312

	
313 313
  /// \addtogroup graphs
314 314
  /// @{
315 315

	
316 316
  ///A general directed graph structure.
317 317

	
318 318
  ///\ref ListDigraph is a versatile and fast directed graph
319 319
  ///implementation based on linked lists that are stored in
320 320
  ///\c std::vector structures.
321 321
  ///
322 322
  ///This type fully conforms to the \ref concepts::Digraph "Digraph concept"
323 323
  ///and it also provides several useful additional functionalities.
324 324
  ///Most of its member functions and nested classes are documented
325 325
  ///only in the concept class.
326 326
  ///
327 327
  ///\sa concepts::Digraph
328 328
  ///\sa ListGraph
329 329
  class ListDigraph : public ExtendedListDigraphBase {
330 330
    typedef ExtendedListDigraphBase Parent;
331 331

	
332 332
  private:
333 333
    /// Digraphs are \e not copy constructible. Use DigraphCopy instead.
334 334
    ListDigraph(const ListDigraph &) :ExtendedListDigraphBase() {};
335 335
    /// \brief Assignment of a digraph to another one is \e not allowed.
336 336
    /// Use DigraphCopy instead.
337 337
    void operator=(const ListDigraph &) {}
338 338
  public:
339 339

	
340 340
    /// Constructor
341 341

	
342 342
    /// Constructor.
343 343
    ///
344 344
    ListDigraph() {}
345 345

	
346 346
    ///Add a new node to the digraph.
347 347

	
348 348
    ///This function adds a new node to the digraph.
349 349
    ///\return The new node.
350 350
    Node addNode() { return Parent::addNode(); }
351 351

	
352 352
    ///Add a new arc to the digraph.
353 353

	
354 354
    ///This function adds a new arc to the digraph with source node \c s
355 355
    ///and target node \c t.
356 356
    ///\return The new arc.
357 357
    Arc addArc(Node s, Node t) {
358 358
      return Parent::addArc(s, t);
359 359
    }
360 360

	
361 361
    ///\brief Erase a node from the digraph.
362 362
    ///
363 363
    ///This function erases the given node from the digraph.
364 364
    void erase(Node n) { Parent::erase(n); }
365 365

	
366 366
    ///\brief Erase an arc from the digraph.
367 367
    ///
368 368
    ///This function erases the given arc from the digraph.
369 369
    void erase(Arc a) { Parent::erase(a); }
370 370

	
371 371
    /// Node validity check
372 372

	
373 373
    /// This function gives back \c true if the given node is valid,
374 374
    /// i.e. it is a real node of the digraph.
375 375
    ///
376 376
    /// \warning A removed node could become valid again if new nodes are
377 377
    /// added to the digraph.
378 378
    bool valid(Node n) const { return Parent::valid(n); }
379 379

	
380 380
    /// Arc validity check
381 381

	
382 382
    /// This function gives back \c true if the given arc is valid,
383 383
    /// i.e. it is a real arc of the digraph.
384 384
    ///
385 385
    /// \warning A removed arc could become valid again if new arcs are
386 386
    /// added to the digraph.
387 387
    bool valid(Arc a) const { return Parent::valid(a); }
388 388

	
389 389
    /// Change the target node of an arc
390 390

	
391 391
    /// This function changes the target node of the given arc \c a to \c n.
392 392
    ///
393 393
    ///\note \c ArcIt and \c OutArcIt iterators referencing the changed
394 394
    ///arc remain valid, however \c InArcIt iterators are invalidated.
395 395
    ///
396 396
    ///\warning This functionality cannot be used together with the Snapshot
397 397
    ///feature.
398 398
    void changeTarget(Arc a, Node n) {
399 399
      Parent::changeTarget(a,n);
400 400
    }
401 401
    /// Change the source node of an arc
402 402

	
403 403
    /// This function changes the source node of the given arc \c a to \c n.
404 404
    ///
405 405
    ///\note \c InArcIt iterators referencing the changed arc remain
406 406
    ///valid, however \c ArcIt and \c OutArcIt iterators are invalidated.
407 407
    ///
408 408
    ///\warning This functionality cannot be used together with the Snapshot
409 409
    ///feature.
410 410
    void changeSource(Arc a, Node n) {
411 411
      Parent::changeSource(a,n);
412 412
    }
413 413

	
414 414
    /// Reverse the direction of an arc.
415 415

	
416 416
    /// This function reverses the direction of the given arc.
417 417
    ///\note \c ArcIt, \c OutArcIt and \c InArcIt iterators referencing
418 418
    ///the changed arc are invalidated.
419 419
    ///
420 420
    ///\warning This functionality cannot be used together with the Snapshot
421 421
    ///feature.
422 422
    void reverseArc(Arc a) {
423 423
      Node t=target(a);
424 424
      changeTarget(a,source(a));
425 425
      changeSource(a,t);
426 426
    }
427 427

	
428 428
    ///Contract two nodes.
429 429

	
430 430
    ///This function contracts the given two nodes.
431 431
    ///Node \c v is removed, but instead of deleting its
432 432
    ///incident arcs, they are joined to node \c u.
433 433
    ///If the last parameter \c r is \c true (this is the default value),
434 434
    ///then the newly created loops are removed.
435 435
    ///
436 436
    ///\note The moved arcs are joined to node \c u using changeSource()
437 437
    ///or changeTarget(), thus \c ArcIt and \c OutArcIt iterators are
438 438
    ///invalidated for the outgoing arcs of node \c v and \c InArcIt
439 439
    ///iterators are invalidated for the incomming arcs of \c v.
440 440
    ///Moreover all iterators referencing node \c v or the removed 
441 441
    ///loops are also invalidated. Other iterators remain valid.
442 442
    ///
443 443
    ///\warning This functionality cannot be used together with the Snapshot
444 444
    ///feature.
445 445
    void contract(Node u, Node v, bool r = true)
446 446
    {
447 447
      for(OutArcIt e(*this,v);e!=INVALID;) {
448 448
        OutArcIt f=e;
449 449
        ++f;
450 450
        if(r && target(e)==u) erase(e);
451 451
        else changeSource(e,u);
452 452
        e=f;
453 453
      }
454 454
      for(InArcIt e(*this,v);e!=INVALID;) {
455 455
        InArcIt f=e;
456 456
        ++f;
457 457
        if(r && source(e)==u) erase(e);
458 458
        else changeTarget(e,u);
459 459
        e=f;
460 460
      }
461 461
      erase(v);
462 462
    }
463 463

	
464 464
    ///Split a node.
465 465

	
466 466
    ///This function splits the given node. First, a new node is added
467 467
    ///to the digraph, then the source of each outgoing arc of node \c n
468 468
    ///is moved to this new node.
469 469
    ///If the second parameter \c connect is \c true (this is the default
470 470
    ///value), then a new arc from node \c n to the newly created node
471 471
    ///is also added.
472 472
    ///\return The newly created node.
473 473
    ///
474 474
    ///\note All iterators remain valid.
475 475
    ///
476 476
    ///\warning This functionality cannot be used together with the
477 477
    ///Snapshot feature.
478 478
    Node split(Node n, bool connect = true) {
479 479
      Node b = addNode();
480 480
      nodes[b.id].first_out=nodes[n.id].first_out;
481 481
      nodes[n.id].first_out=-1;
482 482
      for(int i=nodes[b.id].first_out; i!=-1; i=arcs[i].next_out) {
483 483
        arcs[i].source=b.id;
484 484
      }
485 485
      if (connect) addArc(n,b);
486 486
      return b;
487 487
    }
488 488

	
489 489
    ///Split an arc.
490 490

	
491 491
    ///This function splits the given arc. First, a new node \c v is
492 492
    ///added to the digraph, then the target node of the original arc
493 493
    ///is set to \c v. Finally, an arc from \c v to the original target
494 494
    ///is added.
495 495
    ///\return The newly created node.
496 496
    ///
497 497
    ///\note \c InArcIt iterators referencing the original arc are
498 498
    ///invalidated. Other iterators remain valid.
499 499
    ///
500 500
    ///\warning This functionality cannot be used together with the
501 501
    ///Snapshot feature.
502 502
    Node split(Arc a) {
503 503
      Node v = addNode();
504 504
      addArc(v,target(a));
505 505
      changeTarget(a,v);
506 506
      return v;
507 507
    }
508 508

	
509 509
    ///Clear the digraph.
510 510

	
511 511
    ///This function erases all nodes and arcs from the digraph.
512 512
    ///
513 513
    void clear() {
514 514
      Parent::clear();
515 515
    }
516 516

	
517 517
    /// Reserve memory for nodes.
518 518

	
519 519
    /// Using this function, it is possible to avoid superfluous memory
520 520
    /// allocation: if you know that the digraph you want to build will
0 comments (0 inline)