/** * Add an edge if both vertices are non-NULL and distinct */ bool addEdge(Vertex *vertA, Vertex *vertB) { if (vertA == NULL || vertB == NULL || vertA == vertB) return false; // no side effects on failure addVertex(vertA); addVertex(vertB); if (mEdges.find(vertA) == mEdges.end()) { mEdges.insert(EdgeMap::value_type(vertA, new VertSet())); } VertSet *nexts = mEdges.at(vertA); return nexts->insert(vertB).second; }
bool findDfsVertexPathRec(vector<Vertex*> &path, const Vertex *end) const { Vertex *lastVert = path.back(); if (lastVert == end) return true; if (mEdges.find(lastVert) != mEdges.end()) { VertSet *nexts = mEdges.at(lastVert); for (auto it = nexts->begin(); it != nexts->end(); ++it) { path.push_back(*it); bool found = findDfsVertexPathRec(path, end); if ( found ) return found; else path.pop_back(); } } return false; }
bool findDfsVertexPathMarkingRec(vector<Vertex*> &path, const Vertex *end) { Vertex *lastVert = path.back(); if (lastVert == end) return true; if (mEdges.find(lastVert) != mEdges.end()) { VertSet *nexts = mEdges.at(lastVert); for (VertSet::iterator it = nexts->begin(); it != nexts->end(); ++it) { Vertex *vert = *it; if (mMarks.find(vert) == mMarks.end()) { mMarks.insert(vert); path.push_back(vert); bool found = findDfsVertexPathMarkingRec(path, end); if ( found ) return found; else path.pop_back(); } } } return false; }