File: StrahlerMetric.cpp

package info (click to toggle)
tulip 4.6.0dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 139,284 kB
  • ctags: 35,942
  • sloc: cpp: 289,758; ansic: 27,264; python: 1,256; sh: 923; yacc: 522; xml: 337; makefile: 258; php: 66; lex: 55
file content (304 lines) | stat: -rw-r--r-- 8,526 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/**
 *
 * This file is part of Tulip (www.tulip-software.org)
 *
 * Authors: David Auber and the Tulip development Team
 * from LaBRI, University of Bordeaux
 *
 * Tulip is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation, either version 3
 * of the License, or (at your option) any later version.
 *
 * Tulip is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 */

#include <cmath>
#include <cstdio>

#include <tulip/StringProperty.h>
#include <tulip/StringCollection.h>

#include "StrahlerMetric.h"

PLUGIN(StrahlerMetric)

using namespace std;
using namespace tlp;

namespace std {
struct couple {
  int p,r;
  bool operator ==(const couple&d) {
    if ((p==d.p) && (r==d.r)) return true;

    return false;
  }
};

template<>
struct equal_to<couple> {
  bool operator()(const couple &c,const couple &d) {
    if ((c.r==d.r) && (c.p==d.p)) return true;

    return false;
  }
};

template<>
struct less<couple> {
  bool operator()(const couple &c,const couple &d) {
    return (c.r < d.r) ||
           ((c.r == d.r) && (c.p < d.p));
  }
};
}

struct StackEval {
  StackEval(int f,int u):freeS(f),usedS(u) {}
  int freeS,usedS;
};

struct GreaterStackEval {
  bool operator()(const StackEval& e1,const StackEval& e2) {
    return (e1.freeS>e2.freeS);
  }
};

Strahler StrahlerMetric::topSortStrahler(tlp::node n, int &curPref,
    TLP_HASH_MAP<node,int> &tofree,
    TLP_HASH_MAP<node,int> &prefix,
    TLP_HASH_MAP<node,bool> &visited,
    TLP_HASH_MAP<node,bool> &finished,
    TLP_HASH_MAP<node,Strahler> &cachedValues) {
  visited[n]=true;
  Strahler result;
  prefix[n]=curPref;
  curPref++;

  if (graph->outdeg(n)==0) {
    finished[n]=true;
    return(result);
  }

  list<int> strahlerResult;
  list<StackEval> tmpEval;
  //Construction des ensembles pour evaluer le strahler
  Iterator<node> *itN=graph->getOutNodes(n);

  for (; itN->hasNext();) {
    node tmpN=itN->next();

    if (!visited[tmpN]) {
      //Arc Normal
      tofree[n]=0;
      Strahler tmpValue=topSortStrahler(tmpN,curPref,tofree,prefix,visited,finished,cachedValues);
      //Data for strahler evaluation on the spanning Dag.
      strahlerResult.push_front(tmpValue.strahler);
      //Counting current used stacks.
      tmpEval.push_back(StackEval(tmpValue.stacks-tmpValue.usedStack+tofree[n],tmpValue.usedStack-tofree[n]));
      //Looking if we need more stacks to evaluate this node.
      //old freeStacks=max(freeStacks,tmpValue.stacks-tmpValue.usedStack+tofree[n]);
    }
    else {
      if (finished[tmpN]) {
        if(prefix[tmpN]<prefix[n]) {
          //Cross Edge
          Strahler tmpValue=cachedValues[tmpN];
          //Data for strahler evaluation on the spanning Dag.
          strahlerResult.push_front(tmpValue.strahler);
          //Looking if we need more stacks to evaluate this node.
          tmpEval.push_back(StackEval(tmpValue.stacks,0));
        }
        else {
          //Arc descent
          Strahler tmpValue=cachedValues[tmpN];
          //Data for strahler evaluation on the spanning Dag.
          strahlerResult.push_front(tmpValue.strahler);
        }
      }
      else {
        if(tmpN==n) {
          tmpEval.push_back(StackEval(1,0));
        }
        else {
          //New nested cycle.
          tofree[tmpN]++;
          tmpEval.push_back(StackEval(0,1));
          //result.usedStack++;
        }

        //Return edge
        //Register needed tp store the result of the recursive call
        strahlerResult.push_front(1);
      }
    }
  }

  delete itN;

  //compute the minimal nested cycles
  GreaterStackEval gSE;
  tmpEval.sort(gSE);
  result.stacks=0;
  result.usedStack=0;

  for (list<StackEval>::iterator it=tmpEval.begin(); it!=tmpEval.end(); ++it) {
    result.usedStack+=it->usedS;
    result.stacks=std::max(result.stacks,it->freeS+it->usedS);
    result.stacks-=it->usedS;
  }

  result.stacks=result.stacks+result.usedStack;
  //evaluation of tree strahler
  int tmpDbl;
  int additional=0;
  int available=0;
  strahlerResult.sort();

  while (!strahlerResult.empty()) {
    tmpDbl=strahlerResult.back();
    strahlerResult.pop_back();

    if (tmpDbl>available) {
      additional+=tmpDbl - available;
      available=tmpDbl - 1;
    }
    else
      available -=1;
  }

  result.strahler=additional;
  strahlerResult.clear();
  finished [n]=true;
  cachedValues[n]=result;
  return result;
}

namespace {
const char * paramHelp[] = {
  // property
  HTML_HELP_OPEN() \
  HTML_HELP_DEF( "type", "bool" ) \
  HTML_HELP_DEF( "values", "true, false" ) \
  HTML_HELP_DEF( "default", "false" ) \
  HTML_HELP_BODY() \
  "If true, for each node the Strahler number is computed from a spanning tree having that node as root: complexity o(n^2). If false the Strahler number is computed from a spanning tree having the heuristicly estimated graph center as root." \
  HTML_HELP_CLOSE(),
  //Orientation
  HTML_HELP_OPEN()         \
  HTML_HELP_DEF( "type", "String Collection" ) \
  HTML_HELP_DEF("Values", "all <BR> ramification<BR> nested cycles") \
  HTML_HELP_DEF( "default", "all" )  \
  HTML_HELP_BODY() \
  "Type of computation"  \
  HTML_HELP_CLOSE()
};
}
#define COMPUTATION_TYPE "Type"
#define COMPUTATION_TYPES "all;ramification;nested cycles;"
#define ALL 0
#define REGISTERS 1
#define STACKS 2
//==============================================================================
StrahlerMetric::StrahlerMetric(const tlp::PluginContext* context):DoubleAlgorithm(context), allNodes(false) {
  addInParameter<bool>("All nodes", paramHelp[0], "false");
  addInParameter<StringCollection>(COMPUTATION_TYPE, paramHelp[1], COMPUTATION_TYPES);
}
//==============================================================================
bool StrahlerMetric::run() {
  allNodes = false;
  StringCollection computationTypes(COMPUTATION_TYPES);
  computationTypes.setCurrent(0);

  if (dataSet!=NULL) {
    dataSet->get("All nodes", allNodes);
    dataSet->get(COMPUTATION_TYPE, computationTypes);
  }

  TLP_HASH_MAP<node,bool> visited;
  TLP_HASH_MAP<node,bool> finished;
  TLP_HASH_MAP<node,int> prefix;
  TLP_HASH_MAP<node,int> tofree;
  TLP_HASH_MAP<node,Strahler> cachedValues;
  int curPref=0;
  /*
    Selection *parameter=graph->getProperty<BooleanProperty>("viewSelection");
    Iterator<node> *it=graph->getNodes();
    for (;it->hasNext();) {
    node curNode=it->next();
    if ((!visited[curNode]) && (parameter->getNodeValue(curNode)) ) {
    tofree[curNode]=0;
    topSortStrahler(curNode,curPref,tofree,prefix,visited,finished,cachedValues);
    }
    } delete it;
  */
  Iterator<node> *itN=graph->getNodes();
  unsigned int i = 0;

  while (itN->hasNext()) {
    node itn = itN->next();
    tofree[itn] = 0;

    if (!finished[itn]) {
      topSortStrahler(itn,curPref,tofree,prefix,visited,finished,cachedValues);
    }

    if (allNodes) {
      if (pluginProgress->progress(i++, graph->numberOfNodes())!=TLP_CONTINUE) break;

      switch(computationTypes.getCurrent()) {
      case ALL:
        result->setNodeValue(itn,sqrt((double)cachedValues[itn].strahler*(double)cachedValues[itn].strahler
                                      +(double)cachedValues[itn].stacks*(double)cachedValues[itn].stacks));
        break;

      case REGISTERS:
        result->setNodeValue(itn, (double)cachedValues[itn].strahler);
        break;

      case STACKS:
        result->setNodeValue(itn, (double)cachedValues[itn].stacks);
      }

      visited.clear();
      finished.clear();
      prefix.clear();
      tofree.clear();
      cachedValues.clear();
      curPref=0;
    }
  }

  delete itN;

  if (!allNodes) {
    itN = graph->getNodes();

    while (itN->hasNext()) {
      node itn=itN->next();

      switch(computationTypes.getCurrent()) {
      case ALL:
        result->setNodeValue(itn,sqrt((double)cachedValues[itn].strahler*(double)cachedValues[itn].strahler
                                      +(double)cachedValues[itn].stacks*(double)cachedValues[itn].stacks));
        break;

      case REGISTERS:
        result->setNodeValue(itn, (double)cachedValues[itn].strahler);
        break;

      case STACKS:
        result->setNodeValue(itn, (double)cachedValues[itn].stacks);
      }
    }

    delete itN;
  }

  return pluginProgress->state()!=TLP_CANCEL;
}