## Check if there is a route between two nodes in a Diagraph

Given a directed graph, design an algorithm to find out whether there is a route between two nodes.

My initial thoughts:
Starting from the source, using DFS to traverse the graph to check if we arrive at the target.

My initial codes:

```
public static boolean isRouteBetween(Digraph g, Integer start, Integer end) {
Stack<Integer> stack = new Stack<Integer>();
Set<Integer> visited = new HashSet<Integer>();
if (start != null) {
stack.push(start);

while (!stack.isEmpty()) {
Integer curr = stack.pop();
if (!visited.contains(curr)) {
if (curr.equals(end))
return true;
else {
stack.push(child);
}
}
}
}
return false;
}

Time complexity: $O(|V|+|E|)$. Space complexity: $O(|V|).$
Use Stack for DFS. Use Queue for BFS.

Solution:
public enum State {
Unvisited, Visited, Visiting;
}

public static boolean search(Graph g, Node start, Node end) {
for (Node u : g.getNodes()) {
u.state = State.Unvisited;
}
start.state = State.Visiting;
Node u;
while (!q.isEmpty()) {
u = q.removeFirst(); // i.e., pop()
if (u != null) {
for (Node v : u.getAdjacent()) {
if (v.state == State.Unvisited) {
if (v == end) {
return true;
} else {
v.state = State.Visiting;
}
}
}
u.state = State.Visited;
}
}
return false;
}

__ATA.cmd.push(function() {
__ATA.initVideoSlot('atatags-370373-5b2de8ea7a3a3', {
sectionId: '370373',
});
});

__ATA.cmd.push(function() {
__ATA.initSlot('atatags-26942-5b2de8ea7a3ef',  {
collapseEmpty: 'before',
sectionId: '26942',
width: 300,
height: 250
});
});

__ATA.cmd.push(function() {
__ATA.initSlot('atatags-114160-5b2de8ea7a3f2',  {
collapseEmpty: 'before',
sectionId: '114160',
width: 300,
height: 250
});
});

Related
```