}
-void Graph::clearMarks()
+void Graph::clearPaths()
{
- Arrows::iterator it = arrows_.begin();
- Arrows::iterator const en = arrows_.end();
+ vector<Vertex>::iterator it = vertices_.begin();
+ vector<Vertex>::iterator en = vertices_.end();
for (; it != en; ++it)
- it->marked = false;
+ it->path.clear();
}
Graph::EdgePath const Graph::getPath(int from, int to)
{
- EdgePath path;
+ static const EdgePath path;
if (from == to)
return path;
if (to < 0 || !bfs_init(from))
return path;
- // In effect, the way this works is that we construct a sub-graph
- // by starting at "from" and following the arrows outward. Instead
- // of actually constructing a sub-graph, though, we "mark" the
- // arrows we traverse as we go. Once we hit "to", we abort the
- // marking process and then call getMarkedPath() to reconstruct
- // the marked path.
- bool found = false;
- clearMarks();
+ clearPaths();
while (!Q_.empty()) {
int const current = Q_.front();
Q_.pop();
- vector<Arrow *>::const_iterator const beg =
+ vector<Arrow *>::const_iterator cit =
vertices_[current].out_arrows.begin();
- vector<Arrow *>::const_iterator cit = beg;
vector<Arrow *>::const_iterator end =
vertices_[current].out_arrows.end();
for (; cit != end; ++cit) {
if (!vertices_[cv].visited) {
vertices_[cv].visited = true;
Q_.push(cv);
- (*cit)->marked = true;
+ // NOTE If we wanted to collect all the paths, then
+ // we just need to collect them here and not worry
+ // about "visited".
+ EdgePath lastpath = vertices_[(*cit)->from].path;
+ lastpath.push_back((*cit)->id);
+ vertices_[cv].path = lastpath;
}
if (cv == to) {
- found = true;
- break;
+ return vertices_[cv].path;
}
}
}
- if (!found)
- return path;
-
- getMarkedPath(from, to, path);
+ // failure
return path;
}
-// We assume we have marked the graph, as in getPath(). We also
-// assume that we have done so in such a way as to guarantee a
-// marked path from "from" to "to".
-// We then start at "to" and find the arrow leading to it that
-// has been marked. We add that to the path we are constructing,
-// step back on that arrow, and continue the process (i.e., recurse).
-void Graph::getMarkedPath(int from, int to, EdgePath & path) {
- if (from == to) {
- reverse(path.begin(), path.end());
- return;
- }
- // find marked in_arrow
- vector<Arrow *>::const_iterator it = vertices_[to].in_arrows.begin();
- vector<Arrow *>::const_iterator en = vertices_[to].in_arrows.end();
- for (; it != en; ++it)
- if ((*it)->marked)
- break;
- if (it == en) {
- LASSERT(false, /* */);
- return;
- }
- int const newnode = (*it)->from;
- path.push_back(newnode);
- getMarkedPath(from, newnode, path);
-}
-
-
void Graph::init(int size)
{
vertices_ = vector<Vertex>(size);
}
+// At present, we do not need this debugging code, but
+// I am going to leave it here in case we need it again.
+#if 0
+void Graph::dumpGraph() const
+{
+ vector<Vertex>::const_iterator it = vertices_.begin();
+ vector<Vertex>::const_iterator en = vertices_.end();
+ for (; it != en; ++it) {
+ LYXERR0("Next vertex...");
+ LYXERR0("In arrows...");
+ std::vector<Arrow *>::const_iterator iit = it->in_arrows.begin();
+ std::vector<Arrow *>::const_iterator ien = it->in_arrows.end();
+ for (; iit != ien; ++iit)
+ LYXERR0("From " << (*iit)->from << " to " << (*iit)->to);
+ LYXERR0("Out arrows...");
+ iit = it->out_arrows.begin();
+ ien = it->out_arrows.end();
+ for (; iit != ien; ++iit)
+ LYXERR0("From " << (*iit)->from << " to " << (*iit)->to);
+ }
+}
+#endif
+
+
} // namespace lyx