Don't import ogdf namespace
[TortoiseGit.git] / ext / OGDF / src / energybased / multilevelmixer / RandomMerger.cpp
blob5f7dbc567ff2efd8a93fb2594ea7a8581205fc7f
1 /*
2 * $Revision: 2523 $
4 * last checkin:
5 * $Author: gutwenger $
6 * $Date: 2012-07-02 20:59:27 +0200 (Mon, 02 Jul 2012) $
7 ***************************************************************/
9 /** \file
10 * \brief Merges nodes with neighbour to get a Multilevel Graph
12 * \author Gereon Bartel
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 ***************************************************************/
43 #include <ogdf/energybased/multilevelmixer/RandomMerger.h>
45 namespace ogdf {
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();
58 if (numNodes <= 3) {
59 return false;
62 node v;
63 int index = 0;
64 Array<node> candidates(numNodes);
65 forall_nodes(v, G) {
66 candidates[index] = v;
67 index++;
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];
76 candSize--;
77 node parent;
79 if (mergeNode->degree() > 0) {
80 int index = randomNumber(0, mergeNode->degree()-1);
81 int i = 0;
82 adjEntry adj;
83 forall_adj(adj, mergeNode) {
84 if (i == index) {
85 parent = adj->twinNode();
86 break;
87 } else {
88 i++;
91 } else {
92 do {
93 index = randomNumber(0, candSize-1);
94 parent = candidates[index];
95 } while (parent == mergeNode);
96 candidates[index] = candidates[candSize-1];
97 candSize--;
100 NodeMerge * NM = new NodeMerge(level);
101 bool ret = MLG.changeNode(NM, parent, MLG.radius(parent), mergeNode);
102 OGDF_ASSERT( ret );
103 MLG.moveEdgesToParent(NM, mergeNode, parent, true, m_adjustEdgeLengths);
104 ret = MLG.postMerge(NM, mergeNode);
105 if( !ret ) {
106 delete NM;
110 return true;
114 void RandomMerger::setFactor(double factor)
116 m_levelSizeFactor = factor;
119 } // namespace ogdf