File: EqualValueClustering.cpp

package info (click to toggle)
tulip 4.8.0dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 179,264 kB
  • ctags: 64,517
  • sloc: cpp: 600,444; ansic: 36,311; makefile: 22,136; python: 1,304; sh: 946; yacc: 522; xml: 337; pascal: 157; php: 66; lex: 55
file content (520 lines) | stat: -rw-r--r-- 16,186 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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/**
 *
 * 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 "EqualValueClustering.h"

#include <tulip/StringCollection.h>

//================================================================================
using namespace std;
using namespace tlp;

PLUGIN(EqualValueClustering)

namespace {
const char * paramHelp[] = {
  // selectedNodes
  HTML_HELP_OPEN() \
  HTML_HELP_DEF( "type", "PropertyInterface*" ) \
  HTML_HELP_BODY() \
  "Property used to partition the graph" \
  HTML_HELP_CLOSE(),
  HTML_HELP_OPEN()         \
  HTML_HELP_DEF( "type", "String Collection" ) \
  HTML_HELP_DEF("values", "nodes <BR> edges") \
  HTML_HELP_DEF( "default", "nodes" )  \
  HTML_HELP_BODY() \
  "Graph elements to partition"  \
  HTML_HELP_CLOSE(),
  HTML_HELP_OPEN()         \
  HTML_HELP_DEF( "type", "bool" ) \
  HTML_HELP_DEF( "values", "[true, false]" ) \
  HTML_HELP_DEF( "default", "false" ) \
  HTML_HELP_BODY() \
  "If true, the resulting subgraphs are guaranted to be connected." \
  HTML_HELP_CLOSE()
};
}
#define ELT_TYPE "Type"
#define ELT_TYPES "nodes;edges;"
#define NODE_ELT 0
#define EDGE_ELT 1
//================================================================================
EqualValueClustering::EqualValueClustering(tlp::PluginContext* context):Algorithm(context) {
  addInParameter<PropertyInterface*>("Property", paramHelp[0], "viewMetric");
  addInParameter<StringCollection>(ELT_TYPE, paramHelp[1], ELT_TYPES);
  addInParameter<bool>("Connected", paramHelp[2], "false");
}
//===============================================================================
bool EqualValueClustering::run() {
  PropertyInterface *property=NULL;
  StringCollection eltTypes(ELT_TYPES);
  bool connected = false;
  eltTypes.setCurrent(0);

  if (dataSet!=NULL) {
    dataSet->get("Property", property);
    dataSet->get(ELT_TYPE, eltTypes);
    dataSet->get("Connected", connected);
  }

  if (property == NULL)
    property = graph->getProperty("viewMetric");

  const bool onNodes = eltTypes.getCurrent() == NODE_ELT;

  // try to work with NumericProperty
  if (dynamic_cast<NumericProperty*>(property))
    return computeClusters((NumericProperty *) property,
                           onNodes, connected);

  return computeClusters(property, onNodes, connected);
}

bool EqualValueClustering::computeClusters(NumericProperty* prop,
    bool onNodes, bool connected) {
  unsigned int step = 0;
  unsigned int maxSteps;

  TLP_HASH_MAP<double, Graph *> clusters;
  TLP_HASH_MAP<std::string, unsigned int> valuesCount;
  MutableContainer<bool> visited;
  visited.setAll(false);

  if (onNodes) {
    maxSteps = graph->numberOfNodes();

    if (pluginProgress)
      pluginProgress->setComment("Partitioning nodes...");

    // do a bfs traversal for each node
    node curNode;
    forEach(curNode, graph->getNodes()) {
      // check if curNode has been already visited
      if (!visited.get(curNode.id)) {
        // get the value of the node
        double curValue = prop->getNodeDoubleValue(curNode);
        Graph* sg;

        if (connected || (clusters.find(curValue) == clusters.end())) {
          // add a new cluster
          sg = graph->addSubGraph();
          // set its name
          string strVal = prop->getNodeStringValue(curNode);
          stringstream sstr;
          sstr << prop->getName().c_str() << ": ";
          sstr.width(8);
          sstr << curValue;

          if (connected) {
            TLP_HASH_MAP<std::string, unsigned int>::iterator itv =
              valuesCount.find(strVal);

            if (itv != valuesCount.end()) {
              itv->second += 1;
              sstr << " [" << itv->second << ']';
            }
            else
              valuesCount[strVal] = 0;
          }
          else
            clusters[curValue] = sg;

          sg->setName(sstr.str());
        }
        else
          sg = clusters[curValue];

        // add curNode in the cluster
        sg->addNode(curNode);

        if (pluginProgress && (++step % 50 == 1)) {
          pluginProgress->progress(step, maxSteps);

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

        // do a bfs traversal for this node
        list<node> nodesToVisit;
        visited.set(curNode.id, true);
        nodesToVisit.push_front(curNode);

        while(!nodesToVisit.empty()) {
          curNode = nodesToVisit.front();
          nodesToVisit.pop_front();
          edge curEdge;
          forEach(curEdge, graph->getInOutEdges(curNode)) {
            node neighbour = graph->opposite(curEdge, curNode);

            if (neighbour == curNode) {
              // add loop
              sg->addEdge(curEdge);
              continue;
            }

            //
            // check if neighbour has the same value
            if (curValue == prop->getNodeDoubleValue(neighbour)) {
              // check if neighbour has not been visited
              if (!visited.get(neighbour.id)) {
                // add neighbour and edge in cluster
                sg->addNode(neighbour);
                sg->addEdge(curEdge);
                // push it for further deeper exploration
                visited.set(neighbour.id, true);
                nodesToVisit.push_back(neighbour);

                if (pluginProgress && (++step % 50 == 1)) {
                  pluginProgress->progress(step, maxSteps);

                  if (pluginProgress->state() !=TLP_CONTINUE)
                    return pluginProgress->state()!= TLP_CANCEL;
                }
              }
              else {
                // check if curEdge already exist in cluster
                if (!sg->isElement(curEdge))
                  sg->addEdge(curEdge);
              }
            }
          }
        }
      }
    }
  }
  else {
    maxSteps = graph->numberOfEdges();

    if (pluginProgress)
      pluginProgress->setComment("Partitioning edges...");

    // do a bfs traversal for each edge
    edge curEdge;
    forEach(curEdge, graph->getEdges()) {
      // check if curEdge has been already visited
      if (!visited.get(curEdge.id)) {
        // get the value of the edge
        double curValue = prop->getEdgeDoubleValue(curEdge);
        Graph* sg;

        if (connected || (clusters.find(curValue) == clusters.end())) {
          // add a new cluster
          sg = graph->addSubGraph();
          // set its name
          string strVal = prop->getEdgeStringValue(curEdge);
          stringstream sstr;
          sstr << prop->getName().c_str() << ": ";
          sstr.width(8);
          sstr << curValue;

          if (connected) {
            TLP_HASH_MAP<std::string, unsigned int>::iterator itv =
              valuesCount.find(strVal);

            if (itv != valuesCount.end()) {
              itv->second += 1;
              sstr << " [" << itv->second << ']';
            }
            else
              valuesCount[strVal] = 0;
          }
          else
            clusters[curValue] = sg;

          sg->setName(sstr.str());
        }
        else
          sg = clusters[curValue];

        // add curEdge in cluster
        std::pair<node, node> ends = graph->ends(curEdge);
        sg->addNode(ends.first);
        sg->addNode(ends.second);
        sg->addEdge(curEdge);

        if (pluginProgress && (++step % 50 == 1)) {
          pluginProgress->progress(step, maxSteps);

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

        // do a bfs traversal for this edge
        list<node> nodesToVisit;
        nodesToVisit.push_front(ends.first);
        nodesToVisit.push_front(ends.second);
        visited.set(curEdge.id, true);

        while(!nodesToVisit.empty()) {
          node curNode = nodesToVisit.front();
          nodesToVisit.pop_front();
          forEach(curEdge, graph->getInOutEdges(curNode)) {
            // check if the edge has not been visited AND
            // if it has the same value
            if (!visited.get(curEdge.id) &&
                curValue == prop->getEdgeDoubleValue(curEdge)) {
              node neighbour = graph->opposite(curEdge, curNode);

              if (neighbour != curNode) {
                // add neighbour in cluster
                sg->addNode(neighbour);
                // and push it for further deeper exploration
                nodesToVisit.push_back(neighbour);
              }

              // add edge in cluster
              sg->addEdge(curEdge);
              visited.set(curEdge.id, true);

              if (pluginProgress && (++step % 50 == 1)) {
                pluginProgress->progress(step, maxSteps);

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

  return true;
}

bool EqualValueClustering::computeClusters(PropertyInterface* prop,
    bool onNodes, bool connected) {
  unsigned int step = 0;
  unsigned int maxSteps;

  TLP_HASH_MAP<std::string, Graph *> clusters;
  TLP_HASH_MAP<std::string, unsigned int> valuesCount;
  MutableContainer<bool> visited;
  visited.setAll(false);

  if (onNodes) {
    maxSteps = graph->numberOfNodes();

    if (pluginProgress)
      pluginProgress->setComment("Partitioning nodes...");

    // do a bfs traversal for each node
    StableIterator<node> itN(graph->getNodes());

    while (itN.hasNext()) {
      node curNode = itN.next();

      // check if curNode has been already visited
      if (!visited.get(curNode.id)) {
        // get the value of the node
        string curValue = prop->getNodeStringValue(curNode);
        Graph* sg;

        if (connected || (clusters.find(curValue) == clusters.end())) {
          // add a new cluster
          sg = graph->addSubGraph();
          // set its name
          stringstream sstr;
          sstr << prop->getName().c_str() << ": " << curValue.c_str();

          if (connected) {
            TLP_HASH_MAP<std::string, unsigned int>::iterator itv =
              valuesCount.find(curValue);

            if (itv != valuesCount.end()) {
              itv->second += 1;
              sstr << " [" << itv->second << ']';
            }
            else
              valuesCount[curValue] = 0;
          }
          else
            clusters[curValue] = sg;

          sg->setName(sstr.str());
        }
        else
          sg = clusters[curValue];

        // add curNode in cluster
        sg->addNode(curNode);

        if (pluginProgress && (++step % 50 == 1)) {
          pluginProgress->progress(step, maxSteps);

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

        // do a bfs traversal for this node
        list<node> nodesToVisit;
        visited.set(curNode.id, true);
        nodesToVisit.push_front(curNode);

        while(!nodesToVisit.empty()) {
          curNode = nodesToVisit.front();
          nodesToVisit.pop_front();
          edge curEdge;
          forEach(curEdge, graph->getInOutEdges(curNode)) {
            node neighbour = graph->opposite(curEdge, curNode);

            if (neighbour == curNode) {
              // add loop in cluster
              sg->addEdge(curEdge);
              continue;
            }

            // check if neighbour has the same value
            if (curValue == prop->getNodeStringValue(neighbour)) {
              // check if neighbour has not been visited
              if (!visited.get(neighbour.id)) {
                // add neighbour and edge in cluster
                sg->addNode(neighbour);
                sg->addEdge(curEdge);
                // push it for further deeper exploration
                visited.set(neighbour.id, true);
                nodesToVisit.push_back(neighbour);

                if (pluginProgress && (++step % 50 == 1)) {
                  pluginProgress->progress(step, maxSteps);

                  if (pluginProgress->state() !=TLP_CONTINUE)
                    return pluginProgress->state()!= TLP_CANCEL;
                }
              }
              else {
                // check if curEdge already exist in cluster
                if (!sg->isElement(curEdge))
                  sg->addEdge(curEdge);
              }
            }
          }
        }
      }
    }
  }
  else {
    maxSteps = graph->numberOfEdges();

    if (pluginProgress)
      pluginProgress->setComment("Partitioning edges...");

    // do a bfs traversal for each edge
    StableIterator<edge> itE(graph->getEdges());

    // do a bfs traversal for each node
    while (itE.hasNext()) {
      edge curEdge = itE.next();

      // check if curEdge has been already visited
      if (!visited.get(curEdge.id)) {
        // get the value of the edge
        string curValue = prop->getEdgeStringValue(curEdge);
        Graph* sg;

        if (connected || (clusters.find(curValue) == clusters.end())) {
          // add a new cluster
          sg = graph->addSubGraph();
          // set its name
          string strVal = prop->getEdgeStringValue(curEdge);
          stringstream sstr;
          sstr << prop->getName().c_str() << ": " << curValue.c_str();

          if (connected) {
            TLP_HASH_MAP<std::string, unsigned int>::iterator itv =
              valuesCount.find(curValue);

            if (itv != valuesCount.end()) {
              itv->second += 1;
              sstr << " [" << itv->second << ']';
            }
            else
              valuesCount[strVal] = 0;
          }
          else
            clusters[curValue] = sg;

          sg->setName(sstr.str());
        }
        else
          sg = clusters[curValue];

        // add curEdge in cluster
        std::pair<node, node> ends = graph->ends(curEdge);
        sg->addNode(ends.first);
        sg->addNode(ends.second);
        sg->addEdge(curEdge);

        if (pluginProgress && (++step % 50 == 1)) {
          pluginProgress->progress(step, maxSteps);

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

        // do a bfs traversal for this edge
        list<node> nodesToVisit;
        nodesToVisit.push_front(ends.first);
        nodesToVisit.push_front(ends.second);
        visited.set(curEdge.id, true);

        while(!nodesToVisit.empty()) {
          node curNode = nodesToVisit.front();
          nodesToVisit.pop_front();
          forEach(curEdge, graph->getInOutEdges(curNode)) {
            // check if the edge has not been visited AND
            // if it has the same value
            if (!visited.get(curEdge.id) &&
                curValue == prop->getEdgeStringValue(curEdge)) {
              node neighbour = graph->opposite(curEdge, curNode);

              if (neighbour != curNode) {
                // add neighbour in cluster
                sg->addNode(neighbour);
                // and push it for further deeper exploration
                nodesToVisit.push_back(neighbour);
              }

              // add edge in cluster
              sg->addEdge(curEdge);
              visited.set(curEdge.id, true);

              if (pluginProgress && (++step % 50 == 1)) {
                pluginProgress->progress(step, maxSteps);

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

  return true;
}






//================================================================================