Route Between Nodes
// great for lightweight labeling
enum State { Unvisited, Visiting, Visited };
bool isConnected(Graph g, Node *one, Node *two) {
// same node or no links
if (one == two || one.getAdjacent() == nullptr || two.getAdjacent() == nullptr)
return true;
// mark each node
for (Node i : g.getNodes()) {
i.state = State.Unvisited;
}
// base case
one.state = State.Visiting;
queue<Node*> explore;
explore.push(one);
Node *temp = new Node();
// explore adjacent node starting from arbitrary node
while (!explore.isEmpty()) {
// explore one node at a time, and its adjacent pairs
temp = explore.pop();
if (temp != nullptr) {
for (Node i : temp.getAdjacent()) {
if (i.state == State.Unvisted) {
if (i == two) return true;
i.state == State.Visiting;
explore.push(i);
}
}
}
temp.state = State.Visited;
}
return false;
}Last updated