@Test public void test(){ AllPathsGraph<String> graph = new AllPathsGraph<>(); graph.addEdge("0","X"); graph.addEdge("X","1"); graph.addEdge("X","Y"); graph.addEdge("1","2"); graph.addEdge("2","Y"); graph.addEdge("1","3"); graph.addEdge("3","4"); graph.addEdge("3","5"); graph.addEdge("4","5"); List<List<String>> paths = graph.findAllPaths("X","5"); assertEquals(4, paths.size()); assertTrue(paths.stream().anyMatch(p -> p.size() == 4)); assertTrue(paths.stream().anyMatch(p -> p.size() == 5)); assertTrue(paths.stream().anyMatch(p -> p.size() == 6)); assertTrue(paths.stream().anyMatch(p -> p.size() == 7)); }
public List<List<V>> findAllPaths(V start, V end) { if (!graph.containsKey(start) && !graph.containsKey(end)) { return Collections.emptyList(); } if (start.equals(end)) { throw new IllegalArgumentException(); } Deque<V> stack = new ArrayDeque<>(); List<List<V>> paths = new ArrayList<>(); dfs(paths, stack, start, end); return paths; }
AllPathsGraph extends UndirectedGraph<V> { public List<List<V>> findAllPaths(V start, V end) { if (!graph.containsKey(start) && !graph.containsKey(end)) { return Collections.emptyList(); } if (start.equals(end)) { throw new IllegalArgumentException(); } Deque<V> stack = new ArrayDeque<>(); List<List<V>> paths = new ArrayList<>(); dfs(paths, stack, start, end); return paths; } }
AllPathsGraph extends UndirectedGraph<V> { public List<List<V>> findAllPaths(V start, V end) { if (!graph.containsKey(start) && !graph.containsKey(end)) { return Collections.emptyList(); } if (start.equals(end)) { throw new IllegalArgumentException(); } Deque<V> stack = new ArrayDeque<>(); List<List<V>> paths = new ArrayList<>(); dfs(paths, stack, start, end); return paths; }  }
AllPathsGraph extends UndirectedGraph<V> { public List<List<V>> findAllPaths(V start, V end) { if (!graph.containsKey(start) && !graph.containsKey(end)) { return Collections.emptyList(); } if (start.equals(end)) { throw new IllegalArgumentException(); } Deque<V> stack = new ArrayDeque<>(); List<List<V>> paths = new ArrayList<>(); dfs(paths, stack, start, end); return paths; }  List<List<V>> findAllPaths(V start, V end); }
AllPathsGraph extends UndirectedGraph<V> { public List<List<V>> findAllPaths(V start, V end) { if (!graph.containsKey(start) && !graph.containsKey(end)) { return Collections.emptyList(); } if (start.equals(end)) { throw new IllegalArgumentException(); } Deque<V> stack = new ArrayDeque<>(); List<List<V>> paths = new ArrayList<>(); dfs(paths, stack, start, end); return paths; }  List<List<V>> findAllPaths(V start, V end);  }