Don't import ogdf namespace
[TortoiseGit.git] / ext / OGDF / ogdf / basic / GraphCopy.h
blobc5c0a42eed3fe358236d5aa6413f6d30df6c7390
1 /*
2 * $Revision: 2583 $
4 * last checkin:
5 * $Author: gutwenger $
6 * $Date: 2012-07-12 01:02:21 +0200 (Do, 12. Jul 2012) $
7 ***************************************************************/
9 /** \file
10 * \brief Declaration of graph copy classes.
12 * \author Carsten Gutwenger
14 * \par License:
15 * This file is part of the Open Graph Drawing Framework (OGDF).
17 * \par
18 * Copyright (C)<br>
19 * See README.txt in the root directory of the OGDF installation for details.
21 * \par
22 * This program is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU General Public License
24 * Version 2 or 3 as published by the Free Software Foundation;
25 * see the file LICENSE.txt included in the packaging of this file
26 * for details.
28 * \par
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * \par
35 * You should have received a copy of the GNU General Public
36 * License along with this program; if not, write to the Free
37 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
38 * Boston, MA 02110-1301, USA.
40 * \see http://www.gnu.org/copyleft/gpl.html
41 ***************************************************************/
44 #ifdef _MSC_VER
45 #pragma once
46 #endif
48 #ifndef OGDF_GRAPH_COPY_H
49 #define OGDF_GRAPH_COPY_H
52 #include <ogdf/basic/NodeArray.h>
53 #include <ogdf/basic/EdgeArray.h>
54 #include <ogdf/basic/SList.h>
55 #include <ogdf/basic/CombinatorialEmbedding.h>
58 namespace ogdf {
60 class FaceSetPure;
63 //---------------------------------------------------------
64 // GraphCopySimple
65 // simple graph copies (no support for edge splitting)
66 //---------------------------------------------------------
67 /**
68 * \brief Copies of graphs with mapping between nodes and edges
70 * The class GraphCopySimple represents a copy of a graph and
71 * maintains a mapping between the nodes and edges of the original
72 * graph to the copy and vice versa.
74 * New nodes and edges can be added to the copy; the counterpart
75 * of those nodes and edges is 0 indicating that there is no counterpart.
76 * This class <b>does not</b> support splitting of edges in such a way
77 * that both edges resulting from the split are mapped to the same
78 * original edge; this feature is provided by GraphCopy.
80 class OGDF_EXPORT GraphCopySimple : public Graph
82 const Graph *m_pGraph; //!< The original graph.
83 NodeArray<node> m_vOrig; //!< The corresponding node in the original graph.
84 NodeArray<node> m_vCopy; //!< The corresponding node in the graph copy.
85 EdgeArray<edge> m_eOrig; //!< The corresponding edge in the original graph.
86 EdgeArray<edge> m_eCopy; //!< The corresponding edge in the graph copy.
88 public:
89 // construction
91 //! Constructs a copy of graph \a G.
92 GraphCopySimple(const Graph &G);
94 //! Copy constructor.
95 GraphCopySimple(const GraphCopySimple &GC);
97 virtual ~GraphCopySimple() { }
99 //! Returns a reference to the original graph.
100 const Graph &original() const { return *m_pGraph; }
103 * \brief Returns the node in the original graph corresponding to \a v.
104 * @param v is a node in the graph copy.
105 * \return the corresponding node in the original graph, or 0 if no
106 * such node exists.
108 node original(node v) const { return m_vOrig[v]; }
111 * \brief Returns the edge in the original graph corresponding to \a e.
112 * @param e is an edge in the graph copy.
113 * \return the corresponding edge in the original graph, or 0 if no
114 * such edge exists.
116 edge original(edge e) const { return m_eOrig[e]; }
119 * \brief Returns the node in the graph copy corresponding to \a v.
120 * @param v is a node in the original graph.
121 * \return the corresponding node in the graph copy.
123 node copy(node v) const { return m_vCopy[v]; }
126 * \brief Returns the edge in the graph copy corresponding to \a e.
127 * @param e is an edge in the original graph.
128 * \return the corresponding edge in the graph copy.
130 edge copy(edge e) const { return m_eCopy[e]; }
133 * \brief Returns true iff \a v has no corresponding node in the original graph.
134 * @param v is a node in the graph copy.
136 bool isDummy(node v) const { return (m_vOrig[v] == 0); }
139 * \brief Returns true iff \a e has no corresponding edge in the original graph.
140 * @param e is an edge in the graph copy.
142 bool isDummy(edge e) const { return (m_eOrig[e] == 0); }
144 //! Assignment operator.
145 GraphCopySimple &operator=(const GraphCopySimple &GC);
148 //! Creates a new node in the graph copy.
149 node newNode() {
150 return Graph::newNode();
154 * \brief Creates a new node in the graph copy with original node \a vOrig.
155 * \warning You have to make sure that the original node makes sense, in
156 * particular that \a vOrig is not the original node of another node in the copy.
158 node newNode(node vOrig) {
159 OGDF_ASSERT(vOrig != 0 && vOrig->graphOf() == m_pGraph);
160 node v = Graph::newNode();
161 m_vCopy[m_vOrig[v] = vOrig] = v;
162 return v;
165 //! Creates a new edge from \a v to \a w in the graph copy.
166 edge newEdge(node v, node w) {
167 return Graph::newEdge(v,w);
171 * \brief Creates a new edge in the graph copy with original edge \a eOrig.
172 * \warning You have to make sure that the original edge makes sense, in
173 * particular that \a eOrig is not the original edge of another edge in the copy.
175 edge newEdge(edge eOrig) {
176 OGDF_ASSERT(eOrig != 0 && eOrig->graphOf() == m_pGraph);
177 edge e = Graph::newEdge(m_vCopy[eOrig->source()], m_vCopy[eOrig->target()]);
178 m_eCopy[m_eOrig[e] = eOrig] = e;
179 return e;
182 private:
183 void initGC(const GraphCopySimple &GC, NodeArray<node> &vCopy,
184 EdgeArray<edge> &eCopy);
185 }; // class GraphCopySimple
188 //---------------------------------------------------------
189 // GraphCopy
190 // graph copies (support for edge splitting)
191 //---------------------------------------------------------
193 * \brief Copies of graphs supporting edge splitting
195 * The class GraphCopy represents a copy of a graph and
196 * maintains a mapping between the nodes and edges of the original
197 * graph to the copy and vice versa.
199 * New nodes and edges can be added to the copy; the counterpart
200 * of those nodes and edges is 0 indicating that there is no counterpart.
201 * GraphCopy also support splitting of edges in such a way
202 * that both edges resulting from the split are mapped to the same
203 * original edge, and each edge of the original graph is mapped to a list
204 * of edges. Furthermore, it is allowed to reverse edges in the graph copy.
206 * <h3>Do's and Dont's</h3>
207 * Here is a short summary, what can be done with GraphCopy, and what should not
208 * be done. The following operations are safely supported:
209 * - Splitting of edges such that an original edge is represented by a path
210 * in the copy (split(), unsplit()).
211 * - Reversing edges in the copy (Graph::reverseEdge(), Graph::reverseAllEdges()).
212 * - Reinsertion of original edges such that they are represented by a path
213 * in the copy (insertEdgePath(), insertEdgePathEmbedded(), removeEdgePath(),
214 * removeEdgePathEmbedded()).
215 * - Inserting and removing dummy edges in the copy which are not associated
216 * with edges in the original graph.
218 * The following operations are <b>not supported</b> and are thus dangerous:
219 * - Any modifications on the original graph, since the copy will not be
220 * notified.
221 * - Moving the source or target node of an edge in the copy to a different node.
222 * - Removing edges in the graph copy that belong to a path representing an
223 * original edge.
224 * - ... (better think first!)
226 class OGDF_EXPORT GraphCopy : public Graph {
227 protected:
229 const Graph *m_pGraph; //!< The original graph.
230 NodeArray<node> m_vOrig; //!< The corresponding node in the original graph.
231 EdgeArray<edge> m_eOrig; //!< The corresponding edge in the original graph.
232 EdgeArray<ListIterator<edge> > m_eIterator; //!< The position of copy edge in the list.
234 NodeArray<node> m_vCopy; //!< The corresponding node in the graph copy.
235 EdgeArray<List<edge> > m_eCopy; //!< The corresponding list of edges in the graph copy.
237 public:
238 //! Creates a graph copy of \a G.
240 * The constructor assures that the adjacency lists of nodes in the
241 * constructed copy are in the same order as the adjacency lists in \a G.
242 * This is in particular important when dealing with embedded graphs.
244 GraphCopy(const Graph &G);
246 //! Default constructor (does nothing!).
247 GraphCopy() : Graph() { }
249 //! Copy constructor.
251 * Creates a graph copy that is a copy of \a GC and represents a graph
252 * copy of the original graph of \a GC.
254 GraphCopy(const GraphCopy &GC);
256 virtual ~GraphCopy() { }
260 * @name Mapping between original graph and copy
262 //@{
264 //! Returns a reference to the original graph.
265 const Graph &original() const { return *m_pGraph; }
268 * \brief Returns the node in the original graph corresponding to \a v.
269 * @param v is a node in the graph copy.
270 * \return the corresponding node in the original graph, or 0 if no
271 * such node exists.
273 node original(node v) const { return m_vOrig[v]; }
276 * \brief Returns the edge in the original graph corresponding to \a e.
277 * @param e is an edge in the graph copy.
278 * \return the corresponding edge in the original graph, or 0 if no
279 * such edge exists.
281 edge original(edge e) const { return m_eOrig[e]; }
284 * \brief Returns the node in the graph copy corresponding to \a v.
285 * @param v is a node in the original graph.
286 * \return the corresponding node in the graph copy.
288 node copy(node v) const { return m_vCopy[v]; }
291 * \brief Returns the list of edges coresponding to edge \a e.
292 * \param e is an edge in the original graph.
293 * \return the corresponding list of edges in the graph copy.
295 const List<edge> &chain(edge e) const { return m_eCopy[e]; }
297 // returns first edge in chain(e)
299 * \brief Returns the first edge in the list of edges coresponding to edge \a e.
300 * @param e is an edge in the original graph.
301 * \return the first edge in the corresponding list of edges in
302 * the graph copy.
304 edge copy(edge e) const { return m_eCopy[e].front(); }
307 * \brief Returns true iff \a v has no corresponding node in the original graph.
308 * @param v is a node in the graph copy.
310 bool isDummy(node v) const { return (m_vOrig[v] == 0); }
313 * \brief Returns true iff \a e has no corresponding edge in the original graph.
314 * @param e is an edge in the graph copy.
316 bool isDummy(edge e) const { return (m_eOrig[e] == 0); }
319 * \brief Returns true iff edge \a e has been reversed.
320 * @param e is an edge in the original graph.
322 bool isReversed (edge e) const {
323 return e->source() != original(copy(e)->source());
328 * @name Creation and deletion of nodes and edges
330 //@{
332 //! Creates a new node in the graph copy.
333 node newNode() {
334 return Graph::newNode();
338 * \brief Creates a new node in the graph copy with original node \a vOrig.
339 * \warning You have to make sure that the original node makes sense, in
340 * particular that \a vOrig is not the original node of another node in the copy.
342 node newNode(node vOrig) {
343 OGDF_ASSERT(vOrig != 0 && vOrig->graphOf() == m_pGraph);
344 node v = Graph::newNode();
345 m_vCopy[m_vOrig[v] = vOrig] = v;
346 return v;
350 * \brief Removes node \a v and all its adjacent edges cleaning-up their corresponding lists of original edges.
352 * \pre The corresponding lists oforiginal edges contain each only one edge.
353 * \param v is a node in the graph copy.
355 void delCopy(node v);
358 * \brief Removes edge e and clears the list of edges corresponding to \a e's original edge.
360 * \pre The list of edges corresponding to \a e's original edge contains only \a e.
361 * \param e is an edge in the graph copy.
363 void delCopy(edge e);
367 * \brief Splits edge \a e.
368 * @param e is an edge in the graph copy.
370 virtual edge split(edge e);
374 * \brief Undoes a previous split operation.
375 * The two edges \a eIn and \a eOut are merged to a single edge \a eIn.
376 * \pre The vertex \a u that was created by the previous split operation has
377 * exactly one incoming edge \a eIn and one outgoing edge \a eOut.
378 * @param eIn is an edge (*,\a u) in the graph copy.
379 * @param eOut is an edge (\a u,*) in the graph copy.
381 void unsplit(edge eIn, edge eOut);
383 //! Creates a new edge (\a v,\a w) with original edge \a eOrig.
384 edge newEdge(edge eOrig);
386 //! Creates a new edge with original edge \a eOrig at predefined positions in the adjacency lists.
388 * Let \a v be the node whose adjacency list contains \a adjSrc. Then,
389 * the created edge is (\a v,\a w).
390 * @param eOrig is the original edge.
391 * @param adjSrc is the adjacency entry after which the new edge is inserted
392 * in the adjacency list of \a v.
393 * @param w is the source node of the new edge; the edge is added at the end
394 * of the adjacency list of \a w.
395 * @return the created edge.
397 edge newEdge(edge eOrig, adjEntry adjSrc, node w);
399 //! Creates a new edge with original edge \a eOrig at predefined positions in the adjacency lists.
401 * Let \a w be the node whose adjacency list contains \a adjTgt. Then,
402 * the created edge is (\a v,\a w).
403 * @param eOrig is the original edge.
404 * @param v is the source node of the new edge; the edge is added at the end
405 * of the adjacency list of \a v.
406 * @param adjTgt is the adjacency entry after which the new edge is inserted
407 * in the adjacency list of \a w.
408 * @return the created edge.
410 edge newEdge(edge eOrig, node v, adjEntry adjTgt);
412 edge newEdge(node v, node w) { return Graph::newEdge(v, w); }
413 edge newEdge(adjEntry adjSrc, adjEntry adjTgt) { return Graph::newEdge(adjSrc, adjTgt); }
414 edge newEdge(node v, adjEntry adjTgt) { return Graph::newEdge(v, adjTgt); }
415 edge newEdge(adjEntry adjSrc, node w) { return Graph::newEdge(adjSrc, w); }
417 //! sets eOrig to be the corresponding original edge of eCopy and vice versa
419 * @param eOrig is the original edge
420 * @param eCopy is the edge copy
422 void setEdge(edge eOrig, edge eCopy);
424 //! Re-inserts edge \a eOrig by "crossing" the edges in \a crossedEdges.
426 * Let \a v and \a w be the copies of the source and target nodes of \a eOrig.
427 * Each edge in \a crossedEdges is split creating a sequence
428 * \f$u_1,\ldots,u_k\f$ of new nodes, and additional edges are inserted creating
429 * a path \f$v,u_1,\ldots,u_k,w\f$.
430 * @param eOrig is an edge in the original graph and becomes the original edge of
431 * all edges in the path \f$v,u_1,\ldots,u_k,w\f$.
432 * @param crossedEdges are edges in the graph copy.
434 void insertEdgePath(edge eOrig, const SList<adjEntry> &crossedEdges);
436 //for FixedEmbeddingUpwardEdgeInserter only
437 void insertEdgePath(node srcOrig, node tgtOrig, const SList<adjEntry> &crossedEdges);
440 //! Removes the complete edge path for edge \a eOrig.
442 * \@param eOrig is an edge in the original graph.
444 void removeEdgePath(edge eOrig);
446 //! Inserts crossings between two copy edges.
448 * This method is used in TopologyModule.
450 * Let \a crossingEdge = (\a a, \a b) and \a crossedEdge = (\a v, \a w).
451 * Then \a crossedEdge is split creating two edges \a crossedEdge = (\a v, \a u)
452 * and (\a u, \a w), \a crossingEdge is removed and replaced by two new edges
453 * \a e1 = (\a a, \a u) and \a e1 = (\a u, \a b).
454 * Finally it sets \a crossingEdge to \a e2 and returns (\a u, \a w).
456 * @param crossingEdge is the edge that gets split.
457 * @param crossedEdge is the edge that is replaced by two new edges.
458 * @param topDown is used as follows: If set to true, \a crossingEdge will cross
459 * \a crossedEdge from right to left, otherwise from left to right.
461 edge insertCrossing(
462 edge& crossingEdge,
463 edge crossedEdge,
464 bool topDown);
468 * @name Combinatorial Embeddings
470 //@{
472 //! Creates a new edge with original edge \a eOrig in an embedding \a E.
474 * Let \a w be the node whose adjacency list contains \a adjTgt. The original
475 * edge \a eOrig must connect the original nodes of \a v and \a w. If \a eOrig =
476 * (original(\a v),original(\a w)), then the created edge is (\a v,\a w), otherwise
477 * it is (\a w,\a v). The new edge \a e must split a face in \a E, such that \a e
478 * comes after \a adj in the adjacency list of \a v and at the end of the adjacency
479 * list of \a v.
481 * @param v is a node in the graph copy.
482 * @param adj is an adjacency entry in the graph copy.
483 * @param eOrig is an edge in the original graph.
484 * @param E is an embedding of the graph copy.
485 * @return the created edge.
487 edge newEdge(node v, adjEntry adj, edge eOrig, CombinatorialEmbedding &E);
490 * \brief Sets the embedding of the graph copy to the embedding of the original graph.
491 * \pre The graph copy has not been changed after construction, i.e., no new nodes
492 * or edges have been added and no edges have been split.
494 void setOriginalEmbedding();
496 //! Re-inserts edge \a eOrig by "crossing" the edges in \a crossedEdges in embedding \a E.
498 * Let \a v and \a w be the copies of the source and target nodes of \a eOrig,
499 * and let \f$e_0,e_1,\ldots,e_k,e_{k+1}\f$ be the sequence of edges corresponding
500 * to the adjacency entries in \a crossedEdges. The first edge needs to be incident
501 * to \a v and the last to \a w; the edges \f$e_1,\ldots,e_k\f$ are each split
502 * creating a sequence \f$u_1,\ldots,u_k\f$ of new nodes, and additional edges
503 * are inserted creating a path \f$v,u_1,\ldots,u_k,w\f$.
505 * The following figure illustrates, which adjacency entries need to be in the list
506 * \a crossedEdges. It inserts an edge connecting \a v and \a w by passing through
507 * the faces \f$f_0,f_1,f_2\f$; in this case, the list \a crossedEdges must contain
508 * the adjacency entries \f$adj_0,\ldots,adj_3\f$ (in this order).
509 * \image html insertEdgePathEmbedded.png
511 * @param eOrig is an edge in the original graph and becomes the original edge of
512 * all edges in the path \f$v,u_1,\ldots,u_k,w\f$.
513 * @param E is an embedding of the graph copy.
514 * @param crossedEdges are a list of adjacency entries in the graph copy.
516 void insertEdgePathEmbedded(
517 edge eOrig,
518 CombinatorialEmbedding &E,
519 const SList<adjEntry> &crossedEdges);
522 * Removes the complete edge path for edge \a eOrig while preserving the embedding.
523 * @param E is an embedding of the graph copy.
524 * @param eOrig is an edge in the original graph.
525 * @param newFaces is assigned the set of new faces resulting from joining faces
526 * when removing edges.
528 void removeEdgePathEmbedded(
529 CombinatorialEmbedding &E,
530 edge eOrig,
531 FaceSetPure &newFaces);
534 //@}
536 * @name Miscellaneous
538 //@{
540 //! Checks the consistency of the data structure (for debugging only).
541 bool consistencyCheck() const;
543 //! Associates the graph copy with \a G, but does not create any nodes or edges.
545 * This method is used for a special creation of the graph copy.
546 * The graph copy needs to be constructed with the default constructor,
547 * gets associated with \a G using this method, and then is initialized
548 * using either initByNodes() or initByActiveNodes().
550 * The following code snippet shows a typical application of this functionality:
551 * \code
552 * GraphCopy GC;
553 * GC.createEmpty(G);
555 * // compute connected components of G
556 * NodeArray<int> component(G);
557 * int numCC = connectedComponents(G,component);
559 * // intialize the array of lists of nodes contained in a CC
560 * Array<List<node> > nodesInCC(numCC);
562 * node v;
563 * forall_nodes(v,G)
564 * nodesInCC[component[v]].pushBack(v);
566 * EdgeArray<edge> auxCopy(G);
567 * Array<DPoint> boundingBox(numCC);
569 * for(int i = 0; i < numCC; ++i) {
570 * GC.initByNodes(nodesInCC[i],auxCopy);
571 * ...
573 * \endcode
574 * @param G is the graph of which this graph copy shall be a copy.
576 void createEmpty(const Graph &G);
578 //! Initializes the graph copy for the nodes in a component.
580 * Creates copies of all nodes in \a nodes and their incident edges.
581 * Any nodes and edges allocated before are removed.
583 * The order of entries in the adjacency lists is preserved, i.e., if
584 * the original graph is embedded, its embedding induces the embedding
585 * of the created copy.
587 * It is important that \a nodes is the complete list of nodes in
588 * a connected component. If you wish to initialize the graph copy for an
589 * arbitrary set of nodes, use the method initByActiveNodes().
590 * \see createEmpty() for an example.
591 * @param nodes is the list of nodes in the original graph for which
592 * copies are created in the graph copy.
593 * @param eCopy is assigned the copy of each original edge.
595 void initByNodes(const List<node> &nodes, EdgeArray<edge> &eCopy);
597 //! Initializes the graph copy for the nodes in \a nodes.
599 * Creates copies of all nodes in \a nodes and edges between two nodes
600 * which are both contained in \a nodes.
601 * Any nodes and edges allocated before are destroyed.
603 * \see createEmpty()
604 * @param nodes is the list of nodes in the original graph for which
605 * copies are created in the graph copy.
606 * @param activeNodes must be true for every node in \a nodes, false
607 * otherwise.
608 * @param eCopy is assigned the copy of each original edge.
610 void initByActiveNodes(const List<node> &nodes,
611 const NodeArray<bool> &activeNodes, EdgeArray<edge> &eCopy);
613 //@}
615 * @name Operators
617 //@{
619 //! Assignment operator.
621 * Creates a graph copy that is a copy of \a GC and represents a graph
622 * copy of the original graph of \a GC.
624 * The constructor assures that the adjacency lists of nodes in the
625 * constructed graph are in the same order as the adjacency lists in \a G.
626 * This is in particular important when dealing with embedded graphs.
628 GraphCopy &operator=(const GraphCopy &GC);
631 //@}
633 private:
634 void initGC(const GraphCopy &GC,
635 NodeArray<node> &vCopy, EdgeArray<edge> &eCopy);
637 }; // class GraphCopy
640 } // end namespace ogdf
642 #endif