public UndirectedGraphNode searchNode(ArrayList<UndirectedGraphNode> graph,
Map<UndirectedGraphNode, Integer> map,
UndirectedGraphNode start,
int target) {
// init queue and set
Queue<UndirectedGraphNode> queue = new LinkedList<>();
Set<UndirectedGraphNode> visited = new HashSet<>();
queue.offer(start);
// breadth first search
UndirectedGraphNode current;
while (!queue.isEmpty()) {
current = queue.poll();
if (!visited.contains(current)) {
visited.add(current);
if (map.get(current) == target) {
return current;
}
for (UndirectedGraphNode neighbor : current.neighbors) {
queue.offer(neighbor);
}
}
}
return null;
}