Don't import ogdf namespace
[TortoiseGit.git] / ext / OGDF / src / layered / CoffmanGrahamRanking.cpp
blob1e79d72ca339e5b1b8a6ae7c0d40deb3b30698e6
1 /*
2 * $Revision: 2552 $
4 * last checkin:
5 * $Author: gutwenger $
6 * $Date: 2012-07-05 16:45:20 +0200 (Do, 05. Jul 2012) $
7 ***************************************************************/
9 /** \file
10 * \brief Definition of coffman graham ranking algorithm for Sugiyama
12 * \author Till Schäfer
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/layered/CoffmanGrahamRanking.h>
44 #include <ogdf/basic/ModuleOption.h>
45 #include <ogdf/layered/DfsAcyclicSubgraph.h>
46 #include <ogdf/basic/GraphCopy.h>
48 namespace ogdf {
50 CoffmanGrahamRanking::CoffmanGrahamRanking() : m_w(3)
52 m_subgraph.set(new DfsAcyclicSubgraph());
56 void CoffmanGrahamRanking::call (const Graph& G, NodeArray<int>& rank)
58 rank.init(G);
59 GraphCopy gc(G);
61 m_subgraph.get().callAndReverse(gc);
62 removeTransitiveEdges(gc);
64 List<Tuple2<node, int> > ready_nodes;
65 NodeArray<int> deg(gc);
66 NodeArray<int> pi(gc);
67 m_s.init(gc);
69 node v;
70 List<edge> edges;
72 forall_nodes(v,gc) {
73 edges.clear();
74 gc.inEdges(v, edges);
75 deg[v] = edges.size();
76 if (deg[v] == 0) {
77 ready_nodes.pushBack(Tuple2<node,int>(v,0));
79 m_s[v].init(deg[v]);
82 int i = 1;
83 while(!ready_nodes.empty()) {
84 v = ready_nodes.popFrontRet().x1();
85 pi[v] = i++;
87 adjEntry adj;
88 forall_adj(adj,v) {
89 if ((adj->theEdge()->source()) == v) {
90 node u = adj->twinNode();
91 m_s[u].insert(pi[v]);
92 if (--deg[u] == 0) {
93 insert(u,ready_nodes);
100 List<node> ready, waiting;
102 forall_nodes(v,gc) {
103 edges.clear();
104 gc.outEdges(v, edges);
105 deg[v] = edges.size();
106 if (deg[v] == 0) {
107 insert(v,ready,pi); // ready.append(v);
111 int k;
112 // for all ranks
113 for (k = 1; !ready.empty(); k++) {
115 for (i = 1; i <= m_w && !ready.empty(); i++) {
116 node u = ready.popFrontRet();
117 rank[gc.original(u)] = k;
119 gc.inEdges<List<edge> >(u, edges);
120 for (ListIterator<edge> it = edges.begin(); it.valid() ; ++it){
121 if (--deg[(*it)->source()] == 0){
122 waiting.pushBack((*it)->source());
127 while (!waiting.empty()) {
128 insert(waiting.popFrontRet(), ready, pi);
132 k--;
133 forall_nodes(v,G){
134 rank[v] = k - rank[v];
137 m_s.init();
141 void CoffmanGrahamRanking::insert (node u, List<Tuple2<node,int> > &ready_nodes)
143 int j = 0;
145 for( ListIterator<Tuple2<node,int> > it = ready_nodes.rbegin(); it.valid(); --it) {
146 node v = (*it).x1();
147 int sigma = (*it).x2();
149 if (sigma < j) {
150 ready_nodes.insertAfter(Tuple2<node,int>(u,j), it);
151 return;
154 if (sigma > j) continue;
156 const _int_set &x = m_s[u], &y = m_s[v];
157 int k = min(x.length(), y.length());
159 while (j < k && x[j] == y[j]) {
160 j++;
163 if (j == k) {
164 if (x.length() < y.length()) continue;
166 (*it).x2() = k;
167 ready_nodes.insertAfter(Tuple2<node,int>(u,sigma), it);
168 return;
171 if (x[j] < y[j]) continue;
173 (*it).x2() = j;
174 ready_nodes.insert(Tuple2<node,int>(u,sigma), it);
175 return;
178 ready_nodes.pushFront(Tuple2<node,int>(u,j));
182 void CoffmanGrahamRanking::insert (node v, List<node> &ready, const NodeArray<int> &pi)
184 for( ListIterator<node> it = ready.rbegin(); it.valid(); --it) {
185 if (pi[v] <= pi[*it]) {
186 ready.insertAfter(v, it);
187 return;
191 ready.pushFront(v);
195 void CoffmanGrahamRanking::dfs(node v)
197 visited->push(v);
198 mark[v] |= 1;
200 node w;
201 adjEntry adj;
202 forall_adj(adj,v) {
203 if ((adj->theEdge()->source()) == v) {
204 w = adj->twinNode();
205 if (mark[w] & 2) {
206 mark[w] |= 4;
209 if ((mark[w] & 1) == 0) {
210 dfs(w);
216 void CoffmanGrahamRanking::removeTransitiveEdges (Graph& G)
218 node v, w;
219 List<edge> vout;
221 mark.init(G,0);
222 visited = new StackPure<node>();
224 forall_nodes(v,G) {
225 G.outEdges<List<edge> >(v, vout);
226 /* alternative: iterate over all adjELements (only out Edges)
228 * forall_adj(adj,v) {
229 * if ((adj->theEdge()->source()) == v) ...
231 * In this solution a List is generated, because we iterate three times
232 * over this subset of adjElements
234 for (ListIterator<edge> it = vout.begin(); it.valid() ; ++it){
235 w = (*it)-> target();
236 mark[w] = 2;
239 // forall out edges
240 for (ListIterator<edge> it = vout.begin(); it.valid() ; ++it){
241 w = (*it)-> target();
243 // if (w != 1)
244 if ((mark[w] & 1) == 0) {
245 dfs(w);
249 // forall out edges
250 for (ListIterator<edge> it = vout.begin(); it.valid() ; ++it){
251 node u = (*it)->target();
252 if (mark[u] & 4) {
253 G.delEdge(*it);
256 while (!visited->empty())
257 mark[visited->pop()] = 0;
260 mark.init();
261 delete visited;
264 } //namespace ogdf