File: DFS.cpp

package info (click to toggle)
tulip 6.0.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 196,224 kB
  • sloc: cpp: 571,851; ansic: 13,983; python: 4,105; sh: 1,555; yacc: 522; xml: 484; makefile: 168; pascal: 148; lex: 55
file content (188 lines) | stat: -rw-r--r-- 5,250 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
/**
 *
 * This file is part of Tulip (https://tulip.labri.fr)
 *
 * 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 "DFS.h"

#include <stack>
#include <tulip/BooleanProperty.h>
#include <tulip/DoubleProperty.h>
#include <tulip/PluginProgress.h>
#include <tulip/Perspective.h>

using namespace tlp;
using namespace std;

#define PROGRESS_TIME_STEP 1000
DFS::DFS(Graph *graph, BooleanProperty *result, node tgt,
         const EdgeStaticProperty<double> &eWeights, EdgeOrientation edgesOrientation,
         double maxDist)
    : graph(graph), result(result), tgt(tgt), weights(eWeights), currentDist(0),
      edgesOrientation(edgesOrientation), maxDist(maxDist), progress(nullptr), nbPaths(0) {
#ifndef NDEBUG
  assert(graph->getRoot() == result->getGraph()->getRoot());
#endif /* NDEBUG */
}

bool DFS::searchPaths(node n) {
  DoubleProperty dists(result->getGraph());
  dists.setAllNodeValue(DBL_MAX);

  BooleanProperty visitable(graph);
  visitable.setAllNodeValue(true);

  // keep paths origin
  src = n;
  // initialize progress timer
  // progress initialization is delayed until the first progress step
  // has elapsed
  lastProgressTime = QTime::currentTime();
  auto result = computeSearchPaths(n, &visitable, &dists);
  delete progress;
  return result;
}

bool DFS::computeSearchPaths(node n, BooleanProperty *visitable, DoubleProperty *dists) {
  if (!visitable->getNodeValue(n))
    return false;

  if ((maxDist != DBL_MAX) &&
      (((dists->getNodeValue(n) != DBL_MAX) && (currentDist + dists->getNodeValue(n) > maxDist)) ||
       (currentDist > maxDist)))
    return false;

  if (n == tgt || result->getNodeValue(n)) {
    double distLeft(0);

    if ((maxDist != DBL_MAX) && result->getNodeValue(n))
      distLeft = dists->getNodeValue(n);

    for (vector<edge>::reverse_iterator it = path.rbegin(); it != path.rend(); ++it) {
      edge e(*it);
      node opposite(graph->opposite(e, n));
      result->setEdgeValue(e, true);
      result->setNodeValue(opposite, true);
      result->setNodeValue(n, true);
      if (maxDist != DBL_MAX) {
        dists->setNodeValue(n, min<double>(distLeft, dists->getNodeValue(n)));
        distLeft += weights.getEdgeValue(e);
      }
      n = opposite;
    }

    if (maxDist != DBL_MAX)
      dists->setNodeValue(n, min<double>(distLeft, dists->getNodeValue(n)));

    // update the number of paths found
    ++nbPaths;
    updateProgressComment();
    return true;
  }

  bool res = false;
  visitable->setNodeValue(n, false);

  Iterator<edge> *edgeIt = nullptr;

  switch (edgesOrientation) {
  case Undirected:
    edgeIt = graph->getInOutEdges(n);
    break;

  case Directed:
    edgeIt = graph->getOutEdges(n);
    break;

  case Reversed:
    edgeIt = graph->getInEdges(n);
    break;
  }

  for (auto e : edgeIt) {
    // check for progress interruption
    if (lastProgressTime.msecsTo(QTime::currentTime()) >= PROGRESS_TIME_STEP) {
      if (progress == nullptr) {
        // initialize plugin progress
        progress = Perspective::instance()->progress();
        std::ostringstream oss;
        oss << "Searching all ";
        switch (edgesOrientation) {
        case Undirected:
          oss << "undirected ";
          break;

        case Directed:
          oss << "directed ";
          break;

        case Reversed:
          oss << "reverse ";
          break;
        }
        oss << "paths from node #" << src.id << " to node #" << tgt.id;
        progress->setTitle(oss.str());
        updateProgressComment();
        progress->showPreview(false);
        progress->showText(false);
      }
      // (0, 0) means "show a busy progress bar"
      if (progress->progress(0, 0) != TLP_CONTINUE)
        break;
      lastProgressTime = QTime::currentTime();
    }
    if (maxDist != DBL_MAX)
      currentDist += weights.getEdgeValue(e);
    path.push_back(e);
    res |= computeSearchPaths(graph->opposite(e, n), visitable, dists);
    path.pop_back();
    if (progress && (progress->state() != TLP_CONTINUE))
      break;
    if (maxDist != DBL_MAX)
      currentDist -= weights.getEdgeValue(e);
  }

  if (progress) {
    switch (progress->state()) {
    case TLP_CANCEL:
      return false;
    case TLP_STOP:
      return result->getNodeValue(tgt);
    default:
      break;
    }
  }
  visitable->setNodeValue(n, true);
  return res;
}

void DFS::updateProgressComment() {
  if (progress) {
    switch (nbPaths) {
    case 0:
      progress->setComment("No path found...");
      break;
    case 1:
      progress->setComment("One path found...");
      break;
    default:
      std::ostringstream oss;
      oss << nbPaths << " paths found...";
      progress->setComment(oss.str());
    }
  }
}