Bug fix.
2 * lemon/list_graph.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Research Group on Combinatorial Optimization, EGRES).
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
17 #ifndef LEMON_LIST_GRAPH_H
18 #define LEMON_LIST_GRAPH_H
22 ///\brief ListGraph, UndirListGraph classes.
24 #include <lemon/bits/erasable_graph_extender.h>
25 #include <lemon/bits/clearable_graph_extender.h>
26 #include <lemon/bits/extendable_graph_extender.h>
27 #include <lemon/bits/iterable_graph_extender.h>
28 #include <lemon/bits/alteration_notifier.h>
29 #include <lemon/bits/default_map.h>
31 #include <lemon/bits/undir_graph_extender.h>
33 #include <lemon/error.h>
43 int first_in, first_out;
49 int prev_in, prev_out;
50 int next_in, next_out;
53 std::vector<NodeT> nodes;
59 std::vector<EdgeT> edges;
65 typedef ListGraphBase Graph;
68 friend class ListGraphBase;
72 Node(int pid) { id = pid;}
76 Node (Invalid) { id = -1; }
77 bool operator==(const Node& node) const {return id == node.id;}
78 bool operator!=(const Node& node) const {return id != node.id;}
79 bool operator<(const Node& node) const {return id < node.id;}
83 friend class ListGraphBase;
87 Edge(int pid) { id = pid;}
91 Edge (Invalid) { id = -1; }
92 bool operator==(const Edge& edge) const {return id == edge.id;}
93 bool operator!=(const Edge& edge) const {return id != edge.id;}
94 bool operator<(const Edge& edge) const {return id < edge.id;}
100 : nodes(), first_node(-1),
101 first_free_node(-1), edges(), first_free_edge(-1) {}
108 int maxId(Node = INVALID) const { return nodes.size()-1; }
114 int maxId(Edge = INVALID) const { return edges.size()-1; }
116 Node source(Edge e) const { return edges[e.id].source; }
117 Node target(Edge e) const { return edges[e.id].target; }
120 void first(Node& node) const {
121 node.id = first_node;
124 void next(Node& node) const {
125 node.id = nodes[node.id].next;
129 void first(Edge& e) const {
132 n!=-1 && nodes[n].first_in == -1;
134 e.id = (n == -1) ? -1 : nodes[n].first_in;
137 void next(Edge& edge) const {
138 if (edges[edge.id].next_in != -1) {
139 edge.id = edges[edge.id].next_in;
142 for(n = nodes[edges[edge.id].target].next;
143 n!=-1 && nodes[n].first_in == -1;
145 edge.id = (n == -1) ? -1 : nodes[n].first_in;
149 void firstOut(Edge &e, const Node& v) const {
150 e.id = nodes[v.id].first_out;
152 void nextOut(Edge &e) const {
153 e.id=edges[e.id].next_out;
156 void firstIn(Edge &e, const Node& v) const {
157 e.id = nodes[v.id].first_in;
159 void nextIn(Edge &e) const {
160 e.id=edges[e.id].next_in;
164 static int id(Node v) { return v.id; }
165 static int id(Edge e) { return e.id; }
167 static Node fromId(int id, Node) { return Node(id);}
168 static Edge fromId(int id, Edge) { return Edge(id);}
170 /// Adds a new node to the graph.
172 /// \warning It adds the new node to the front of the list.
173 /// (i.e. the lastly added node becomes the first.)
177 if(first_free_node==-1) {
179 nodes.push_back(NodeT());
182 first_free_node = nodes[n].next;
185 nodes[n].next = first_node;
186 if(first_node != -1) nodes[first_node].prev = n;
190 nodes[n].first_in = nodes[n].first_out = -1;
195 Edge addEdge(Node u, Node v) {
198 if (first_free_edge == -1) {
200 edges.push_back(EdgeT());
203 first_free_edge = edges[n].next_in;
206 edges[n].source = u.id;
207 edges[n].target = v.id;
209 edges[n].next_out = nodes[u.id].first_out;
210 if(nodes[u.id].first_out != -1) {
211 edges[nodes[u.id].first_out].prev_out = n;
214 edges[n].next_in = nodes[v.id].first_in;
215 if(nodes[v.id].first_in != -1) {
216 edges[nodes[v.id].first_in].prev_in = n;
219 edges[n].prev_in = edges[n].prev_out = -1;
221 nodes[u.id].first_out = nodes[v.id].first_in = n;
226 void erase(const Node& node) {
229 if(nodes[n].next != -1) {
230 nodes[nodes[n].next].prev = nodes[n].prev;
233 if(nodes[n].prev != -1) {
234 nodes[nodes[n].prev].next = nodes[n].next;
236 first_node = nodes[n].next;
239 nodes[n].next = first_free_node;
244 void erase(const Edge& edge) {
247 if(edges[n].next_in!=-1) {
248 edges[edges[n].next_in].prev_in = edges[n].prev_in;
251 if(edges[n].prev_in!=-1) {
252 edges[edges[n].prev_in].next_in = edges[n].next_in;
254 nodes[edges[n].target].first_in = edges[n].next_in;
258 if(edges[n].next_out!=-1) {
259 edges[edges[n].next_out].prev_out = edges[n].prev_out;
262 if(edges[n].prev_out!=-1) {
263 edges[edges[n].prev_out].next_out = edges[n].next_out;
265 nodes[edges[n].source].first_out = edges[n].next_out;
268 edges[n].next_in = first_free_edge;
276 first_node = first_free_node = first_free_edge = -1;
280 void _changeTarget(Edge e, Node n)
282 if(edges[e.id].next_in != -1)
283 edges[edges[e.id].next_in].prev_in = edges[e.id].prev_in;
284 if(edges[e.id].prev_in != -1)
285 edges[edges[e.id].prev_in].next_in = edges[e.id].next_in;
286 else nodes[edges[e.id].target].first_in = edges[e.id].next_in;
287 if (nodes[n.id].first_in != -1) {
288 edges[nodes[n.id].first_in].prev_in = e.id;
290 edges[e.id].target = n.id;
291 edges[e.id].prev_in = -1;
292 edges[e.id].next_in = nodes[n.id].first_in;
293 nodes[n.id].first_in = e.id;
295 void _changeSource(Edge e, Node n)
297 if(edges[e.id].next_out != -1)
298 edges[edges[e.id].next_out].prev_out = edges[e.id].prev_out;
299 if(edges[e.id].prev_out != -1)
300 edges[edges[e.id].prev_out].next_out = edges[e.id].next_out;
301 else nodes[edges[e.id].source].first_out = edges[e.id].next_out;
302 if (nodes[n.id].first_out != -1) {
303 edges[nodes[n.id].first_out].prev_out = e.id;
305 edges[e.id].source = n.id;
306 edges[e.id].prev_out = -1;
307 edges[e.id].next_out = nodes[n.id].first_out;
308 nodes[n.id].first_out = e.id;
313 typedef ErasableGraphExtender<
314 ClearableGraphExtender<
315 ExtendableGraphExtender<
316 MappableGraphExtender<
317 IterableGraphExtender<
318 AlterableGraphExtender<ListGraphBase> > > > > > ExtendedListGraphBase;
320 /// \addtogroup graphs
323 ///A list graph class.
325 ///This is a simple and fast erasable graph implementation.
327 ///It addition that it conforms to the
328 ///\ref concept::ErasableGraph "ErasableGraph" concept,
329 ///it also provides several additional useful extra functionalities.
330 ///\sa concept::ErasableGraph.
332 class ListGraph : public ExtendedListGraphBase
335 /// Changes the target of \c e to \c n
337 /// Changes the target of \c e to \c n
339 ///\note The <tt>Edge</tt>'s and <tt>OutEdge</tt>'s
340 ///referencing the changed edge remain
341 ///valid. However <tt>InEdge</tt>'s are invalidated.
342 void changeTarget(Edge e, Node n) {
345 /// Changes the source of \c e to \c n
347 /// Changes the source of \c e to \c n
349 ///\note The <tt>Edge</tt>'s and <tt>InEdge</tt>'s
350 ///referencing the changed edge remain
351 ///valid. However <tt>OutEdge</tt>'s are invalidated.
352 void changeSource(Edge e, Node n) {
356 /// Invert the direction of an edge.
358 ///\note The <tt>Edge</tt>'s
359 ///referencing the changed edge remain
360 ///valid. However <tt>OutEdge</tt>'s and <tt>InEdge</tt>'s are invalidated.
361 void reverseEdge(Edge e) {
363 _changeTarget(e,source(e));
367 ///Using this it possible to avoid the superfluous memory allocation.
369 ///Using this it possible to avoid the superfluous memory allocation.
370 ///\todo more docs...
371 void reserveEdge(int n) { edges.reserve(n); };
373 ///Contract two nodes.
375 ///This function contracts two nodes.
377 ///Node \p b will be removed but instead of deleting
378 ///its neighboring edges, they will be joined to \p a.
379 ///The last parameter \p r controls whether to remove loops. \c true
380 ///means that loops will be removed.
382 ///\note The <tt>Edge</tt>s
383 ///referencing a moved edge remain
384 ///valid. However <tt>InEdge</tt>'s and <tt>OutEdge</tt>'s
385 ///may be invalidated.
386 void contract(Node a, Node b, bool r = true)
388 for(OutEdgeIt e(*this,b);e!=INVALID;) {
391 if(r && target(e)==a) erase(e);
392 else changeSource(e,a);
395 for(InEdgeIt e(*this,b);e!=INVALID;) {
398 if(r && source(e)==a) erase(e);
399 else changeTarget(e,a);
407 ///This function splits a node. First a new node is added to the graph,
408 ///then the source of each outgoing edge of \c n is moved to this new node.
409 ///If \c connect is \c true (this is the default value), then a new edge
410 ///from \c n to the newly created node is also added.
411 ///\return The newly created node.
413 ///\note The <tt>Edge</tt>s
414 ///referencing a moved edge remain
415 ///valid. However <tt>InEdge</tt>'s and <tt>OutEdge</tt>'s
416 ///may be invalidated.
417 ///\warning This functionality cannot be used together with the Snapshot
419 ///\todo It could be implemented in a bit faster way.
420 Node split(Node n, bool connect = true)
423 for(OutEdgeIt e(*this,n);e!=INVALID;) {
429 if(connect) addEdge(n,b);
433 ///Class to make a snapshot of the graph and to restrore to it later.
435 ///Class to make a snapshot of the graph and to restrore to it later.
437 ///The newly added nodes and edges can be removed using the
438 ///restore() function.
440 ///\warning Edge and node deletions cannot be restored.
441 ///\warning Snapshots cannot be nested.
442 class Snapshot : protected AlterationNotifier<Node>::ObserverBase,
443 protected AlterationNotifier<Edge>::ObserverBase
447 class UnsupportedOperation : public LogicError {
449 virtual const char* exceptionName() const {
450 return "lemon::ListGraph::Snapshot::UnsupportedOperation";
458 std::list<Node> added_nodes;
459 std::list<Edge> added_edges;
462 virtual void add(const Node& n) {
463 added_nodes.push_back(n);
465 virtual void erase(const Node&)
467 throw UnsupportedOperation();
469 virtual void add(const Edge& n) {
470 added_edges.push_back(n);
472 virtual void erase(const Edge&)
474 throw UnsupportedOperation();
477 ///\bug What is this used for?
479 virtual void build() {}
480 ///\bug What is this used for?
482 virtual void clear() {}
484 void regist(ListGraph &_g) {
486 AlterationNotifier<Node>::ObserverBase::
487 attach(g->getNotifier(Node()));
488 AlterationNotifier<Edge>::ObserverBase::
489 attach(g->getNotifier(Edge()));
493 AlterationNotifier<Node>::ObserverBase::
495 AlterationNotifier<Edge>::ObserverBase::
501 ///Default constructur.
503 ///Default constructur.
504 ///To actually make a snapshot you must call save().
507 ///Constructor that immediately makes a snapshot.
509 ///This constructor immediately makes a snapshot of the graph.
510 ///\param _g The graph we make a snapshot of.
511 Snapshot(ListGraph &_g) {
514 ///\bug Is it necessary?
523 ///Make a snapshot of the graph.
525 ///This function can be called more than once. In case of a repeated
526 ///call, the previous snapshot gets lost.
527 ///\param _g The graph we make the snapshot of.
528 void save(ListGraph &_g)
538 ///Undo the changes until the last snapshot.
540 ///Undo the changes until last snapshot created by save().
542 ///\todo This function might be called undo().
546 while(!added_edges.empty()) {
547 old_g.erase(added_edges.front());
548 added_edges.pop_front();
550 while(!added_nodes.empty()) {
551 old_g.erase(added_nodes.front());
552 added_nodes.pop_front();
561 /**************** Undirected List Graph ****************/
563 typedef ErasableUndirGraphExtender<
564 ClearableUndirGraphExtender<
565 ExtendableUndirGraphExtender<
566 MappableUndirGraphExtender<
567 IterableUndirGraphExtender<
568 AlterableUndirGraphExtender<
569 UndirGraphExtender<ListGraphBase> > > > > > > ExtendedUndirListGraphBase;
571 /// \addtogroup graphs
574 ///An undirected list graph class.
576 ///This is a simple and fast erasable undirected graph implementation.
578 ///It conforms to the
579 ///\ref concept::UndirGraph "UndirGraph" concept.
581 ///\sa concept::UndirGraph.
583 ///\todo Snapshot, reverseEdge(), changeTarget(), changeSource(), contract()
584 ///haven't been implemented yet.
586 class UndirListGraph : public ExtendedUndirListGraphBase {
588 typedef ExtendedUndirListGraphBase Parent;
589 /// \brief Changes the target of \c e to \c n
591 /// Changes the target of \c e to \c n
593 /// \note The <tt>Edge</tt>'s and <tt>OutEdge</tt>'s
594 /// referencing the changed edge remain
595 /// valid. However <tt>InEdge</tt>'s are invalidated.
596 void changeTarget(UndirEdge e, Node n) {
599 /// Changes the source of \c e to \c n
601 /// Changes the source of \c e to \c n
603 ///\note The <tt>Edge</tt>'s and <tt>InEdge</tt>'s
604 ///referencing the changed edge remain
605 ///valid. However <tt>OutEdge</tt>'s are invalidated.
606 void changeSource(UndirEdge e, Node n) {
609 /// \brief Contract two nodes.
611 /// This function contracts two nodes.
613 /// Node \p b will be removed but instead of deleting
614 /// its neighboring edges, they will be joined to \p a.
615 /// The last parameter \p r controls whether to remove loops. \c true
616 /// means that loops will be removed.
618 /// \note The <tt>Edge</tt>s
619 /// referencing a moved edge remain
621 void contract(Node a, Node b, bool r = true) {
622 for(IncEdgeIt e(*this, b); e!=INVALID;) {
623 IncEdgeIt f = e; ++f;
624 if (r && runningNode(e) == a) {
626 } else if (source(e) == b) {