Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- HashSet<Integer> points = graph.values().stream().flatMap(List::stream).collect(Collectors.toCollection(HashSet::new));
- points.addAll(graph.keySet());
- if (start.equals(goal)) return new SimpleEntry<>(start, null);
- HashMap<Integer, Boolean> visited = new HashMap<>();
- LinkedList<Integer> queue = new LinkedList<>();
- for (int i = 0; i < points.size(); i++) {
- visited.put(new ArrayList<>(points).get(i), false);
- }
- queue.push(start);
- visited.put(start, true);
- while (!queue.isEmpty()) {
- start = queue.pop();
- System.out.print(start + " ");
- List<Integer> neighbours = graph.get(start);
- if (neighbours == null) {
- continue;
- }
- for (Integer neighbour : neighbours) {
- if (!visited.get(neighbour)) {
- visited.put(neighbour, true);
- queue.push(neighbour);
- }
- }
- }
- return new SimpleEntry<>(start, new ArrayList<>(visited.keySet()));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement