Java's
double-ended queue interface
(
java.util.Deque)
can be used to exploit BFS/DFS similarity:
- During both BFS and DFS, discovered vertices are removed from the head
(also called first).
- During BFS, a discovered vertex's neighbors are added at the tail
(also called last).
- During DFS, a discovered vertex's neighbors are added at the head
The
Deque interface is implemented by Java's
LinkedList class
(
java.util.LinkedList).