2 * src/lemon/list_graph.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, 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, SymListGraph, NodeSet and EdgeSet classes.
24 #include <lemon/erasable_graph_extender.h>
25 #include <lemon/clearable_graph_extender.h>
26 #include <lemon/extendable_graph_extender.h>
28 #include <lemon/iterable_graph_extender.h>
30 #include <lemon/alteration_observer_registry.h>
32 #include <lemon/default_map.h>
42 int first_in,first_out;
48 int prev_in, prev_out;
49 int next_in, next_out;
52 std::vector<NodeT> nodes;
58 std::vector<EdgeT> edges;
64 typedef ListGraphBase Graph;
67 friend class ListGraphBase;
71 Node(int pid) { id = pid;}
75 Node (Invalid) { id = -1; }
76 bool operator==(const Node& node) const {return id == node.id;}
77 bool operator!=(const Node& node) const {return id != node.id;}
78 bool operator<(const Node& node) const {return id < node.id;}
82 friend class ListGraphBase;
86 Edge(int pid) { id = pid;}
90 Edge (Invalid) { id = -1; }
91 bool operator==(const Edge& edge) const {return id == edge.id;}
92 bool operator!=(const Edge& edge) const {return id != edge.id;}
93 bool operator<(const Edge& edge) const {return id < edge.id;}
99 : nodes(), first_node(-1),
100 first_free_node(-1), edges(), first_free_edge(-1) {}
107 int maxId(Node = INVALID) const { return nodes.size()-1; }
113 int maxId(Edge = INVALID) const { return edges.size()-1; }
115 Node source(Edge e) const { return edges[e.id].source; }
116 Node target(Edge e) const { return edges[e.id].target; }
119 void first(Node& node) const {
120 node.id = first_node;
123 void next(Node& node) const {
124 node.id = nodes[node.id].next;
128 void first(Edge& e) const {
131 n!=-1 && nodes[n].first_in == -1;
133 e.id = (n == -1) ? -1 : nodes[n].first_in;
136 void next(Edge& edge) const {
137 if (edges[edge.id].next_in != -1) {
138 edge.id = edges[edge.id].next_in;
141 for(n = nodes[edges[edge.id].target].next;
142 n!=-1 && nodes[n].first_in == -1;
144 edge.id = (n == -1) ? -1 : nodes[n].first_in;
148 void firstOut(Edge &e, const Node& v) const {
149 e.id = nodes[v.id].first_out;
151 void nextOut(Edge &e) const {
152 e.id=edges[e.id].next_out;
155 void firstIn(Edge &e, const Node& v) const {
156 e.id = nodes[v.id].first_in;
158 void nextIn(Edge &e) const {
159 e.id=edges[e.id].next_in;
163 static int id(Node v) { return v.id; }
164 static int id(Edge e) { return e.id; }
166 /// Adds a new node to the graph.
168 /// \warning It adds the new node to the front of the list.
169 /// (i.e. the lastly added node becomes the first.)
173 if(first_free_node==-1) {
175 nodes.push_back(NodeT());
178 first_free_node = nodes[n].next;
181 nodes[n].next = first_node;
182 if(first_node != -1) nodes[first_node].prev = n;
186 nodes[n].first_in = nodes[n].first_out = -1;
191 Edge addEdge(Node u, Node v) {
194 if (first_free_edge == -1) {
196 edges.push_back(EdgeT());
199 first_free_edge = edges[n].next_in;
202 edges[n].source = u.id;
203 edges[n].target = v.id;
205 edges[n].next_out = nodes[u.id].first_out;
206 if(nodes[u.id].first_out != -1) {
207 edges[nodes[u.id].first_out].prev_out = n;
210 edges[n].next_in = nodes[v.id].first_in;
211 if(nodes[v.id].first_in != -1) {
212 edges[nodes[v.id].first_in].prev_in = n;
215 edges[n].prev_in = edges[n].prev_out = -1;
217 nodes[u.id].first_out = nodes[v.id].first_in = n;
222 void erase(const Node& node) {
225 if(nodes[n].next != -1) {
226 nodes[nodes[n].next].prev = nodes[n].prev;
229 if(nodes[n].prev != -1) {
230 nodes[nodes[n].prev].next = nodes[n].next;
232 first_node = nodes[n].next;
235 nodes[n].next = first_free_node;
240 void erase(const Edge& edge) {
243 if(edges[n].next_in!=-1) {
244 edges[edges[n].next_in].prev_in = edges[n].prev_in;
247 if(edges[n].prev_in!=-1) {
248 edges[edges[n].prev_in].next_in = edges[n].next_in;
250 nodes[edges[n].target].first_in = edges[n].next_in;
254 if(edges[n].next_out!=-1) {
255 edges[edges[n].next_out].prev_out = edges[n].prev_out;
258 if(edges[n].prev_out!=-1) {
259 edges[edges[n].prev_out].next_out = edges[n].next_out;
261 nodes[edges[n].source].first_out = edges[n].next_out;
264 edges[n].next_in = first_free_edge;
272 first_node = first_free_node = first_free_edge = -1;
276 void _moveTarget(Edge e, Node n)
278 if(edges[e.id].next_in != -1)
279 edges[edges[e.id].next_in].prev_in = edges[e.id].prev_in;
280 if(edges[e.id].prev_in != -1)
281 edges[edges[e.id].prev_in].next_in = edges[e.id].next_in;
282 else nodes[edges[e.id].target].first_in = edges[e.id].next_in;
283 edges[e.id].target = n.id;
284 edges[e.id].prev_in = -1;
285 edges[e.id].next_in = nodes[n.id].first_in;
286 nodes[n.id].first_in = e.id;
288 void _moveSource(Edge e, Node n)
290 if(edges[e.id].next_out != -1)
291 edges[edges[e.id].next_out].prev_out = edges[e.id].prev_out;
292 if(edges[e.id].prev_out != -1)
293 edges[edges[e.id].prev_out].next_out = edges[e.id].next_out;
294 else nodes[edges[e.id].source].first_out = edges[e.id].next_out;
295 edges[e.id].source = n.id;
296 edges[e.id].prev_out = -1;
297 edges[e.id].next_out = nodes[n.id].first_out;
298 nodes[n.id].first_out = e.id;
303 typedef AlterableGraphExtender<ListGraphBase> AlterableListGraphBase;
304 typedef IterableGraphExtender<AlterableListGraphBase> IterableListGraphBase;
305 typedef DefaultMappableGraphExtender<IterableListGraphBase> MappableListGraphBase;
306 typedef ExtendableGraphExtender<MappableListGraphBase> ExtendableListGraphBase;
307 typedef ClearableGraphExtender<ExtendableListGraphBase> ClearableListGraphBase;
308 typedef ErasableGraphExtender<ClearableListGraphBase> ErasableListGraphBase;
310 /// \addtogroup graphs
313 ///A list graph class.
315 ///This is a simple and fast erasable graph implementation.
317 ///It addition that it conforms to the
318 ///\ref concept::ErasableGraph "ErasableGraph" concept,
319 ///it also provides several additional useful extra functionalities.
320 ///\sa concept::ErasableGraph.
322 class ListGraph : public ErasableListGraphBase
325 /// Moves the target of \c e to \c n
327 /// Moves the target of \c e to \c n
329 ///\note The <tt>Edge</tt>'s and <tt>OutEdge</tt>'s
330 ///referencing the moved edge remain
331 ///valid. However <tt>InEdge</tt>'s are invalidated.
332 void moveTarget(Edge e, Node n) { _moveTarget(e,n); }
333 /// Moves the source of \c e to \c n
335 /// Moves the source of \c e to \c n
337 ///\note The <tt>Edge</tt>'s and <tt>InEdge</tt>'s
338 ///referencing the moved edge remain
339 ///valid. However <tt>OutEdge</tt>'s are invalidated.
340 void moveSource(Edge e, Node n) { _moveSource(e,n); }
342 /// Invert the direction of an edge.
344 ///\note The <tt>Edge</tt>'s
345 ///referencing the moved edge remain
346 ///valid. However <tt>OutEdge</tt>'s and <tt>InEdge</tt>'s are invalidated.
347 void reverseEdge(Edge e) {
349 _moveTarget(e,source(e));
353 ///Using this it possible to avoid the superfluous memory allocation.
355 ///Using this it possible to avoid the superfluous memory allocation.
356 ///\todo more docs...
357 void reserveEdge(int n) { edges.reserve(n); };
359 ///Contract two nodes.
361 ///This function contracts two nodes.
363 ///Node \p b will be removed but instead of deleting
364 ///its neighboring edges, they will be joined to \p a.
365 ///The last parameter \p r controls whether to remove loops. \c true
366 ///means that loops will be removed.
368 ///\note The <tt>Edge</tt>s
369 ///referencing the moved edge remain
370 ///valid. However <tt>InEdge</tt>'s and <tt>OutEdge</tt>'s
371 ///may be invalidated.
372 void contract(Node a,Node b,bool r=true)
374 for(OutEdgeIt e(*this,b);e!=INVALID;) {
377 if(r && target(e)==a) erase(e);
378 else moveSource(e,b);
381 for(InEdgeIt e(*this,b);e!=INVALID;) {
384 if(r && source(e)==a) erase(e);
385 else moveTarget(e,b);
392 ///Class to make a snapshot of the graph and to restrore to it later.
394 ///Class to make a snapshot of the graph and to restrore to it later.
396 ///The newly added nodes and edges can be removed using the
397 ///restore() function.
399 ///\warning Edge and node deletions cannot be restored.
400 ///\warning SnapShots cannot be nested.
402 class SnapShot : protected AlterationObserverRegistry<Node>::ObserverBase,
403 protected AlterationObserverRegistry<Edge>::ObserverBase
408 std::list<Node> added_nodes;
409 std::list<Edge> added_edges;
412 virtual void add(const Node& n) {
413 added_nodes.push_back(n);
417 virtual void erase(const Node&)
421 virtual void add(const Edge& n) {
422 added_edges.push_back(n);
426 virtual void erase(const Edge&)
431 void regist(ListGraph &_g) {
433 AlterationObserverRegistry<Node>::ObserverBase::
434 attach(g->node_observers);
435 AlterationObserverRegistry<Edge>::ObserverBase::
436 attach(g->edge_observers);
440 AlterationObserverRegistry<Node>::ObserverBase::
442 AlterationObserverRegistry<Edge>::ObserverBase::
448 ///Default constructur.
450 ///Default constructur.
451 ///To actually make a snapshot you must call save().
454 ///Constructor that immediately makes a snapshot.
456 ///This constructor immediately makes a snapshot of the graph.
457 ///\param _g The graph we make a snapshot of.
458 SnapShot(ListGraph &_g) {
461 ///\bug Is it necessary?
470 ///Make a snapshot of the graph.
472 ///This function can be called more than once. In case of a repeated
473 ///call, the previous snapshot gets lost.
474 ///\param _g The graph we make the snapshot of.
475 void save(ListGraph &_g)
485 ///Undo the changes until the last snapshot.
487 ///Undo the changes until last snapshot created by save().
489 ///\todo This function might be called undo().
492 while(!added_edges.empty()) {
493 g->erase(added_edges.front());
494 added_edges.pop_front();
496 while(!added_nodes.empty()) {
497 g->erase(added_nodes.front());
498 added_nodes.pop_front();