6 * $Date: 2012-07-02 20:59:27 +0200 (Mon, 02 Jul 2012) $
7 ***************************************************************/
10 * \brief Merges nodes with neighbour to get a Multilevel Graph
12 * \author Gereon Bartel
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 ***************************************************************/
43 #include <ogdf/energybased/multilevelmixer/RandomMerger.h>
47 RandomMerger::RandomMerger()
48 :m_levelSizeFactor(2.0)
52 bool RandomMerger::buildOneLevel(MultilevelGraph
&MLG
)
54 Graph
&G
= MLG
.getGraph();
55 int level
= MLG
.getLevel() + 1;
56 int numNodes
= G
.numberOfNodes();
64 Array
<node
> candidates(numNodes
);
66 candidates
[index
] = v
;
70 int candSize
= candidates
.size();
71 while (candSize
> numNodes
/ m_levelSizeFactor
)
73 index
= randomNumber(0, candSize
-1);
74 node mergeNode
= candidates
[index
];
75 candidates
[index
] = candidates
[candSize
-1];
79 if (mergeNode
->degree() > 0) {
80 int index
= randomNumber(0, mergeNode
->degree()-1);
83 forall_adj(adj
, mergeNode
) {
85 parent
= adj
->twinNode();
93 index
= randomNumber(0, candSize
-1);
94 parent
= candidates
[index
];
95 } while (parent
== mergeNode
);
96 candidates
[index
] = candidates
[candSize
-1];
100 NodeMerge
* NM
= new NodeMerge(level
);
101 bool ret
= MLG
.changeNode(NM
, parent
, MLG
.radius(parent
), mergeNode
);
103 MLG
.moveEdgesToParent(NM
, mergeNode
, parent
, true, m_adjustEdgeLengths
);
104 ret
= MLG
.postMerge(NM
, mergeNode
);
114 void RandomMerger::setFactor(double factor
)
116 m_levelSizeFactor
= factor
;