6 * $Date: 2012-07-07 00:03:48 +0200 (Sa, 07. Jul 2012) $
7 ***************************************************************/
10 * \brief Declaration of Sugiyama algorithm.
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_SUGIYAMA_LAYOUT_H
49 #define OGDF_SUGIYAMA_LAYOUT_H
53 #include <ogdf/module/LayoutModule.h>
54 #include <ogdf/module/RankingModule.h>
55 #include <ogdf/simultaneous/TwoLayerCrossMinSimDraw.h>
56 #include <ogdf/module/HierarchyLayoutModule.h>
57 #include <ogdf/module/HierarchyClusterLayoutModule.h>
58 #include <ogdf/module/CCLayoutPackModule.h>
59 #include <ogdf/basic/ModuleOption.h>
60 #include <ogdf/cluster/ClusterGraphAttributes.h>
65 class ExtendedNestingGraph
;
68 * \brief Sugiyama's layout algorithm.
70 * The class SugiyamaLayout represents a customizable implementation
71 * of Sugiyama's layout algorithm. The class provides three different
73 * - Calling the algorithm for a usual graph; this is the well-known
75 * - Calling the algorithm for a cluster graph.
76 * - Calling the algorithm for mixed-upward graphs (e.g., UML class
77 * diagrams), where only some edges are directed and shall point
78 * in the same direction.
80 * If the Sugiyama algorithm shall be used for simultaneous drawing,
81 * you need to define the different subgraphs by setting the <i>subgraphs</i>
84 * The implementation used in SugiyamaLayout is based on the following
87 * Emden R. Gansner, Eleftherios Koutsofios, Stephen C. North,
88 * Kiem-Phong Vo: <i>A technique for drawing directed graphs</i>.
89 * IEEE Trans. Software Eng. 19(3), pp. 214-230, 1993.
91 * Georg Sander: <i>%Layout of compound directed graphs</i>.
92 * Technical Report, Universität des Saarlandes, 1996.
94 * <H3>Optional parameters</H3>
95 * The following options affect the crossing minimization step
100 * <th><i>Option</i><th><i>Type</i><th><i>Default</i><th><i>Description</i>
102 * <td><i>runs</i><td>int<td>15
103 * <td>Determines, how many times the crossing minimization is repeated.
104 * Each repetition (except for the first) starts with randomly
105 * permuted nodes on each layer. Deterministic behaviour can be achieved
106 * by setting runs to 1.
108 * <td><i>transpose</i><td>bool<td>true
109 * <td>Determines whether the transpose step is
110 * performed after each 2-layer crossing minimization; this step
111 * tries to reduce the number of crossings by switching neighbored
114 * <td><i>fails</i><td>int<td>4
115 * <td>The number of times that the number of crossings
116 * may not decrease after a complete top-down bottom-up traversal,
117 * before a run is terminated.
119 * <td><i>arrangeCCs</i><td>bool<td>true
120 * <td>If set to true connected components are
121 * laid out separately and the resulting layouts are arranged afterwards
122 * using the packer module.
124 * <td><i>minDistCC</i><td>double<td>20.0
125 * <td>Specifies the spacing between connected components of the graph.
126 * Other spacing parameters have to be set in the used hierarchy layout
129 * <td><i>alignBaseClasses</i><td>bool<td>false
130 * <td>Determines if base classes of inheritance hierarchies shall be
131 * aligned (only callUML()).
133 * <td><i>alignSiblings</i><td>bool<td>false
134 * <td>Determines if siblings in an inheritance tree shall be aligned
139 * The crossing minimization step of the algorithm is affected by the
140 * options <i>runs</i>, <i>transpose</i>, and <i>fails</i>. The options
141 * <i>alignBaseClasses</i> and <i>alignSiblings</i> are only relevant for
142 * laying out mixed-upward graphs, where directed edges are interpreted
143 * as <i>generlizations</i> and undirected egdes as <i>associations</i>
144 * in a UML class diagram.
146 * <H3>%Module options</H3>
147 * The various phases of the algorithm can be exchanged by setting
148 * module options allowing flexible customization. The algorithm provides
149 * the following module options:
153 * <th><i>Option</i><th><i>Type</i><th><i>Default</i><th><i>Description</i>
155 * <td><i>ranking</i><td>RankingModule<td>LongestPathRanking
156 * <td>The ranking module determines the layering of the graph.
158 * <td><i>crossMin</i><td>TwoLayerCrossMin<td>BarycenterHeuristic
159 * <td>The crossMin module performs two-layer crossing
160 * minimization and is applied during the top-down bottom-up traversals.
162 * <td><i>crossMinSimDraw</i><td>TwoLayerCrossMinSimDraw<td>SplitHeuristic
163 * <td>The crossMin module used with simultaneous drawing.
165 * <td><i>layout</i><td>HierarchyLayoutModule<td>FastHierarchyLayout
166 * <td>The hierarchy layout module that computes the final layout
169 * <td><i>clusterLayout</i><td>HierarchyClusterLayoutModule<td>OptimalHierarchyClusterLayout
170 * <td>The hierarchy layout module that computes the final
171 * layout (call for cluster graph).
173 * <td><i>packer</i><td>CCLayoutPackModule<td>TileToRowsCCPacker
174 * <td>The packer module used for arranging connected components.
178 class OGDF_EXPORT SugiyamaLayout
: public LayoutModule
{
182 //! the ranking module (level assignment)
183 ModuleOption
<RankingModule
> m_ranking
;
185 //! the module for two-layer crossing minimization
186 ModuleOption
<TwoLayerCrossMin
> m_crossMin
;
188 ModuleOption
<TwoLayerCrossMinSimDraw
> m_crossMinSimDraw
;
190 //! the hierarchy layout module (final coordinate assignment)
191 ModuleOption
<HierarchyLayoutModule
> m_layout
;
193 //! the hierarchy cluster layout module (final coordinate assignment for clustered graphs)
194 ModuleOption
<HierarchyClusterLayoutModule
> m_clusterLayout
;
196 //! The module for arranging connected components.
197 ModuleOption
<CCLayoutPackModule
> m_packer
;
199 int m_fails
; //!< Option for maximal number of fails.
200 int m_runs
; //!< Option for number of runs.
201 bool m_transpose
; //!< Option for switching on transposal heuristic.
202 bool m_arrangeCCs
; //!< Option for laying out components separately.
203 double m_minDistCC
; //!< Option for distance between connected components.
204 double m_pageRatio
; //!< Option for desired page ratio.
207 int m_nCrossings
; //!< Number of crossings in computed layout.
208 RCCrossings m_nCrossingsCluster
;
209 Array
<bool> m_levelChanged
;
211 bool m_alignBaseClasses
; //!< Option for aligning base classes.
212 bool m_alignSiblings
; //!< Option for aligning siblings in inheritance trees.
214 EdgeArray
<unsigned int> *m_subgraphs
; //!< Defines the subgraphs for simultaneous drawing.
218 //! Creates an instance of SugiyamaLayout and sets options to default values.
222 ~SugiyamaLayout() { }
226 * @name Algorithm call
231 * \brief Calls the layout algorithm for graph \a AG.
233 * Returns the computed layout in \a AG.
235 void call(GraphAttributes
&AG
);
238 * \brief Calls the layout algorithm for clustered graph \a AG.
240 * Returns the computed layout in \a AG.
242 void call(ClusterGraphAttributes
&AG
);
245 * \brief Calls the layout algorithm for graph \a AG with a given level assignment.
247 * Returns the computed layout in \a AG.
248 * @param AG is the input graph (with node size information) and is assigned
249 * the computed layout.
250 * @param rank defines the level of each node.
252 void call(GraphAttributes
&AG
, NodeArray
<int> &rank
);
254 // special call for UML graphs
255 void callUML(GraphAttributes
&AG
);
259 * @name Optional parameters
264 * \brief Returns the current setting of option fails.
266 * This option determines, how many times the total number of crossings
267 * after a complete top down or bottom up traversal may not decrease before
268 * one repetition is stopped.
270 int fails() const { return m_fails
; }
272 //! Sets the option fails to \a nFails.
273 void fails(int nFails
) { m_fails
= nFails
; }
276 * \brief Returns the current setting of option runs.
278 * This option determines, how many times the crossing minimization is
279 * repeated. Each repetition (except for the first) starts with randomly
280 * permuted nodes on each layer. Deterministic behaviour can be achieved
281 * by setting runs to 1.
283 int runs() const { return m_runs
; }
285 //! Sets the option runs to \a nRuns.
286 void runs(int nRuns
) { m_runs
= nRuns
; }
289 * \brief Returns the current setting of option transpose.
291 * If this option is set to true an additional fine tuning step is
292 * performed after each traversal, which tries to reduce the total number
293 * of crossings by switching adjacent vertices on the same layer.
295 bool transpose() const { return m_transpose
; }
297 //! Sets the option transpose to \a bTranspose.
298 void transpose(bool bTranspose
) { m_transpose
= bTranspose
; }
301 * \brief Returns the current setting of option arrangeCCs.
303 * If this option is set to true, connected components are laid out
304 * separately and arranged using a packing module.
306 bool arrangeCCs() const { return m_arrangeCCs
; }
308 //! Sets the options arrangeCCs to \a bArrange.
309 void arrangeCCs(bool bArrange
) { m_arrangeCCs
= bArrange
; }
312 * \brief Returns the current setting of option minDistCC (distance between components).
314 * This options defines the minimum distance between connected
315 * components of the graph.
317 double minDistCC() const { return m_minDistCC
; }
319 //! Sets the option minDistCC to \a x.
320 void minDistCC(double x
) { m_minDistCC
= x
; }
323 * \brief Returns the current setting of option pageRation.
325 * This option defines the desired page ratio of the layout and
326 * is used by the packing algorithms used for laying out
327 * connected components.
329 double pageRatio() const { return m_pageRatio
; }
331 //! Sets the option pageRatio to \a x.
332 void pageRatio(double x
) { m_pageRatio
= x
; }
335 * \brief Returns the current setting of option alignBaseClasses.
337 * This option defines whether base classes in inheritance hierarchies
340 bool alignBaseClasses() const { return m_alignBaseClasses
; }
342 //! Sets the option alignBaseClasses to \a b.
343 void alignBaseClasses(bool b
) { m_alignBaseClasses
= b
; }
346 * \brief Returns the current setting of option alignSiblings.
348 * This option defines whether siblings in inheritance trees
351 bool alignSiblings() const { return m_alignSiblings
; }
353 //! Sets the option alignSiblings to \a b.
354 void alignSiblings(bool b
) { m_alignSiblings
= b
; }
356 //! Sets the subgraphs for simultaneous drawing.
357 void setSubgraphs(EdgeArray
<unsigned int> *esg
) { m_subgraphs
= esg
; }
359 //! Returns true iff subgraphs for simultaneous drawing are set.
360 bool useSubgraphs() const { return (m_subgraphs
== 0) ? 0 : 1; }
363 bool permuteFirst() const { return m_permuteFirst
; }
364 void permuteFirst(bool b
) { m_permuteFirst
= b
; }
368 * @name Module options
373 * \brief Sets the module option for the node ranking (layer assignment).
375 * The layer assignment is the first step of the Sugiyama algorithm
376 * and distributes the nodes onto layers. The layer assignment usually
377 * respects edge directions; if the graph is not acyclic, the ranking
378 * module computes an acyclic subgraph. The ranking module specifies
379 * which method is used and usually provides a module option for
380 * the acyclic subgraph.
382 void setRanking(RankingModule
*pRanking
) {
383 m_ranking
.set(pRanking
);
387 * \brief Sets the module option for the two-layer crossing minimization.
389 * This module is called within the top-down and bottom-up traversal
390 * of the Sugiyama crossing minimization procedure.
392 void setCrossMin(TwoLayerCrossMin
*pCrossMin
) {
393 m_crossMin
.set(pCrossMin
);
397 * \brief Sets the module option for the computation of the final layout.
399 * This module receives as input the computed layer assignment and
400 * and order of nodes on each layer, and computes the final coordinates
401 * of nodes and bend points.
403 void setLayout(HierarchyLayoutModule
*pLayout
) {
404 m_layout
.set(pLayout
);
408 * \brief Sets the module option for the computation of the final layout for clustered graphs.
410 * This module receives as input the computed layer assignment and
411 * and order of nodes on each layer, and computes the final coordinates
412 * of nodes and bend points.
414 void setClusterLayout(HierarchyClusterLayoutModule
*pLayout
) {
415 m_clusterLayout
.set(pLayout
);
419 * \brief Sets the module option for the arrangement of connected components.
421 * If arrangeCCs is set to true, the Sugiyama layout algorithm draws each
422 * connected component of the input graph seperately, and then arranges the
423 * resulting drawings using this packing module.
425 void setPacker(CCLayoutPackModule
*pPacker
) {
426 m_packer
.set(pPacker
);
430 * @name Information after call
431 * The following information can be accessed after calling the algorithm.
435 //! Returns the number of crossings in the computed layout (usual graph).
436 int numberOfCrossings() const { return m_nCrossings
; }
438 //! Returns the number of crossings in the computed layout (cluster graph).
439 RCCrossings
numberOfCrossingsCluster() const { return m_nCrossingsCluster
; }
441 //! Return the number of layers/levels}
442 int numberOfLevels() { return m_numLevels
; }
444 //! Return the max. number of elements on a layer
445 int maxLevelSize() { return m_maxLevelSize
; }
447 double timeReduceCrossings() { return m_timeReduceCrossings
; }
451 void reduceCrossings(Hierarchy
&H
);
452 //void reduceCrossings2(Hierarchy &H);
453 void reduceCrossings(ExtendedNestingGraph
&H
);
457 NodeArray
<int> m_compGC
;
459 void doCall(GraphAttributes
&AG
, bool umlCall
);
460 void doCall(GraphAttributes
&AG
, bool umlCall
, NodeArray
<int> &rank
);
462 int traverseTopDown (Hierarchy
&H
);
463 int traverseBottomUp(Hierarchy
&H
);
464 //int traverseTopDown2 (Hierarchy &H);
465 //int traverseBottomUp2(Hierarchy &H);
467 bool transposeLevel(int i
, Hierarchy
&H
);
468 void doTranspose(Hierarchy
&H
);
469 void doTransposeRev(Hierarchy
&H
);
473 double m_timeReduceCrossings
;
475 RCCrossings
traverseTopDown (ExtendedNestingGraph
&H
);
476 RCCrossings
traverseBottomUp(ExtendedNestingGraph
&H
);
478 //NodeArray<double> m_weight;