6 * $Date: 2012-07-02 20:59:27 +0200 (Mon, 02 Jul 2012) $
7 ***************************************************************/
10 * \brief Declaration of Hierarchy class.
12 * \author Carsten Gutwenger
15 * This file is part of the Open Graph Drawing Framework (OGDF).
19 * See README.txt in the root directory of the OGDF installation for details.
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
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.
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 ***************************************************************/
48 #ifndef OGDF_HIERARCHY_H
49 #define OGDF_HIERARCHY_H
52 #include <ogdf/basic/EdgeArray.h>
53 #include <ogdf/layered/Level.h>
54 #include <ogdf/basic/GraphCopy.h>
60 //! Representation of proper hierarchies used by Sugiyama-layout.
62 * \see Level, SugiyamaLayout
64 class OGDF_EXPORT Hierarchy
{
68 friend class LayerBasedUPRLayout
;
69 friend class HierarchyLayoutModule
;
71 //! The traversing direction of the layer-by-layer sweep.
72 enum TraversingDir
{ downward
, upward
};
75 Array
<Level
*> m_pLevel
; //!< The array of all levels.
76 GraphCopy m_GC
; //!< The graph copy representing the topology of the proper hierarchy.
77 NodeArray
<int> m_pos
; //!< The position of a node on its level.
78 NodeArray
<int> m_rank
; //!< The rank (level) of a node.
79 //! (Sorted) adjacent nodes on lower level.
80 NodeArray
<Array
<node
> > m_lowerAdjNodes
;
81 //! (Sorted) adjacent nodes on upper level.
82 NodeArray
<Array
<node
> > m_upperAdjNodes
;
84 NodeArray
<int> m_nSet
; //!< (Only used by buildAdjNodes().)
86 //! (Only used by calculateCrossingsPlaneSweep().)
87 NodeArray
<ListIterator
<node
> > m_lastOcc
;
89 TraversingDir m_direction
; //!< The current direction of layer-by-layer sweep.
91 bool getMarkedNodes(NodeArray
<bool> &markedNodes
, int r
, List
<node
> &result
, List
<int> &positions
) {
94 bool allDummies
= true;
96 if (r
> high() || r
< 0)
99 Level
&lvl
= *m_pLevel
[r
];
101 for(int i
= 0; i
<= lvl
.high(); i
++) {
103 if (markedNodes
[v
]) {
105 positions
.pushBack(pos(v
));
106 if (!isLongEdgeDummy(v
))
110 positions
.quicksort();
116 //! Creates an empty hierarchy.
118 //! Creates an hierarchy of graph \a G with node ranks \a rank.
119 Hierarchy(const Graph
&G
, const NodeArray
<int> &rank
);
124 void createEmpty(const Graph
&G
);
125 void initByNodes(const List
<node
> &nodes
,
126 EdgeArray
<edge
> &eCopy
,
127 const NodeArray
<int> &rank
);
129 //! Conversion to const GraphCopy reference.
130 operator const GraphCopy
&() const { return m_GC
; }
132 //! Returns the current direction of layer-by-layer sweep.
133 TraversingDir
direction() const {
137 //! Sets the current direction of layer-by-layer sweep.
138 void direction (TraversingDir dir
) {
142 //! Returns the number of levels.
143 int size() const { return m_pLevel
.size(); }
145 //! Returns the maximal array index of a level (= size()-1).
146 int high() const { return m_pLevel
.high(); }
148 //! Returns the rank (level) of node \a v.
149 int rank(node v
) const { return m_rank
[v
]; }
151 //! Returns the position of node \a v on its level.
152 int pos(node v
) const { return m_pos
[v
]; }
154 //! Returns the adjacent nodes of \a v (according to direction()).
155 const Array
<node
> &adjNodes(node v
) {
156 return (m_direction
== downward
) ? m_lowerAdjNodes
[v
] :
160 //! Returns the adjacent nodes of \a v.
161 const Array
<node
> &adjNodes(node v
, TraversingDir dir
) const {
162 return (dir
== downward
) ? m_lowerAdjNodes
[v
] :
166 //! Returns the adjacent level of level \a i (according to direction()).
167 const Level
&adjLevel(int i
) const {
168 return (m_direction
== downward
) ? *m_pLevel
[i
-1] : *m_pLevel
[i
+1];
172 bool isLongEdgeDummy(node v
) const {
173 return (m_GC
.isDummy(v
) && v
->outdeg() == 1);
176 //! Returns the <i>i</i>-th level.
177 const Level
&operator[](int i
) const { return *m_pLevel
[i
]; }
179 //! Returns the <i>i</i>-th level.
180 Level
&operator[](int i
) { return *m_pLevel
[i
]; }
183 //! Computes the number of crossings between level \a i and \a i+1.
184 int calculateCrossings(int i
);
185 //! Computes the total number of crossings.
186 int calculateCrossings();
188 //! Old version of counting crossings using plane-seep algorithm (only for testing purposes).
189 int calculateCrossingsPlaneSweep(int i
);
190 //! Old version of counting crossings using plane-seep algorithm (only for testing purposes).
191 int calculateCrossingsPlaneSweep();
193 //! Computes the number of crossings between level \a i and \a i+1 (for simultaneous drawing).
194 int calculateCrossingsSimDraw(int i
, const EdgeArray
<unsigned int> *edgeSubGraph
);
195 //! Computes the total number of crossings (for simultaneous drawing).
196 int calculateCrossingsSimDraw(const EdgeArray
<unsigned int> *edgeSubGraph
);
199 //! Stores the position of nodes in \a oldPos.
200 void storePos (NodeArray
<int> &oldPos
);
201 //! Restores the position of nodes from \a newPos.
202 void restorePos (const NodeArray
<int> &newPos
);
204 //! Permutes the order of nodes on each level.
207 //! Adjusts node positions such that nodes are ordered according to components numbers.
208 void separateCCs(int numCC
, NodeArray
<int> &component
);
210 bool transpose(node v
);
212 void print(ostream
&os
);
214 void buildAdjNodes(int i
);
215 void buildAdjNodes();
220 void doInit(const NodeArray
<int> &rank
);
222 int transposePart(const Array
<node
> &adjV
, const Array
<node
> &adjW
);
224 OGDF_MALLOC_NEW_DELETE
228 } // end namespace ogdf