Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. HashSet<Integer> points = graph.values().stream().flatMap(List::stream).collect(Collectors.toCollection(HashSet::new));
  2. points.addAll(graph.keySet());
  3. if (start.equals(goal)) return new SimpleEntry<>(start, null);
  4.  
  5. HashMap<Integer, Boolean> visited = new HashMap<>();
  6. LinkedList<Integer> queue = new LinkedList<>();
  7.  
  8. for (int i = 0; i < points.size(); i++) {
  9. visited.put(new ArrayList<>(points).get(i), false);
  10. }
  11.  
  12. queue.push(start);
  13. visited.put(start, true);
  14. while (!queue.isEmpty()) {
  15. start = queue.pop();
  16. System.out.print(start + " ");
  17.  
  18. List<Integer> neighbours = graph.get(start);
  19. if (neighbours == null) {
  20. continue;
  21. }
  22.  
  23. for (Integer neighbour : neighbours) {
  24. if (!visited.get(neighbour)) {
  25. visited.put(neighbour, true);
  26. queue.push(neighbour);
  27. }
  28. }
  29. }
  30. return new SimpleEntry<>(start, new ArrayList<>(visited.keySet()));
  31.  
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement