Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static boolean test(HashMap<Integer, List<Integer>> graph, int start, int finish) {
- HashSet<Integer> points = graph.values().stream().flatMap(List::stream).collect(Collectors.toCollection(HashSet::new));
- points.addAll(graph.keySet());
- LinkedList<Integer> queue = new LinkedList<>();
- HashMap<Integer, Boolean> visited = new HashMap<>();
- for (int i = 1; i <= points.size(); i++) {
- visited.put(i, false);
- }
- visited.put(start, true);
- queue.push(start);
- while (!queue.isEmpty()) {
- int current = queue.pop();
- System.out.print(current + " ");
- List<Integer> neighbours = graph.get(current);
- System.out.println(visited);
- if (neighbours == null) {
- continue;
- }
- for (Integer neighbor : neighbours) {
- if (!visited.get(neighbor)) {
- queue.add(neighbor);
- visited.put(neighbor, true);
- }
- }
- }
- return false;
- }
- public static void main(String[] args) {
- HashMap<Integer, List<Integer>> graph = new HashMap<Integer, List<Integer>>();
- graph.put(1, List.of(2, 3, 4));
- graph.put(2, List.of(3, 4, 5));
- graph.put(3, List.of(6));
- test(graph, 2, 1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement